SDUT2484算术表达式的转换
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2484&cid=1182
题目描述
输入
输出
示例输入
a*b+(c-d/e)*f#
示例输出
+*ab*-c/def
a*b+c-d/e*f
ab*cde/-f*+ 这个题的话,看了白皮书上的那个建树,然后再写上前后中序遍历就可以了,其余的话,注意一下输入就可以,代码中两种输入都可以。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
using namespace std;
const int maxn = ;
int lch[maxn],rch[maxn];
char op[maxn];
int nc=;
int build_tree(char *s,int x,int y)
{
int i,c1=-,c2=-,p=;
int u ;
if(y-x==)
{
u=++nc;
lch[u]=rch[u] = ;
op[u] = s[x] ;
return u;
} for(i = x ; i < y ; i++)
{
switch(s[i])
{
case '(':
p++;
break;
case ')':
p--;
break;
case '+':
case '-':
if(!p) c1=i;
break;
case '*':
case '/':
if(!p) c2=i;
break;
}
}
if(c1<) c1 = c2 ;
if(c1 < ) return build_tree(s,x+,y-);
u=++nc;
lch[u] = build_tree(s,x,c1);
rch[u] = build_tree(s,c1+,y);
op[u] = s[c1];
return u ;
}
void preorder(int u)
{
if(u)
{
printf("%c", op[u]);
preorder(lch[u]);
preorder(rch[u]);
}
} void inorder(int u)
{
if(u)
{
inorder(lch[u]);
printf("%c", op[u]);
inorder(rch[u]);
}
} void postorder(int u)
{
if(u)
{
postorder(lch[u]);
postorder(rch[u]);
printf("%c",op[u]);
}
}
int main()
{
char s[];
//int count ;
int i=;
for(i = ; ; i ++)
{
scanf("%c",&s[i]);
if(s[i]=='#')
break;
}
s[++i]='\0';
/*scanf("%c",&s[i++]);
while(s[i-1]!='#')
{
scanf("%c",&s[i++]);
}
s[i]='\0';*/
//scanf("%s",s);
int len = strlen(s);
int u = build_tree(s,,len-);
preorder(u);
printf("\n");
inorder(u);
printf("\n");
postorder(u);
printf("\n");
return ;
}
这个题还有很多别的做法,可以借鉴一下大神们的博客
http://blog.csdn.net/lin375691011/article/details/9372245
这个的话贵在也简单在用了栈和队列,
http://www.cnblogs.com/kongkaikai/archive/2013/07/30/3224683.html
SDUT2484算术表达式的转换的更多相关文章
- SDUT2484 算术表达式的转换(表达式树)
题目链接. 分析: 转换成表达式树,然后先序.中序.后序遍历. AC代码如下: #include <stdio.h> #include <string.h> #define m ...
- SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式
数据结构实验之栈与队列二:一般算术表达式转换成后缀式 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运 ...
- Project Euler 93:Arithmetic expressions 算术表达式
Arithmetic expressions By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and mak ...
- C++算术运算符与算术表达式
基本的算术运算符 在本章中主要介绍算术运算符与算术表达式,赋值运算符与赋值表达式,逗号运算符与逗号表达式,其他运算符将在以后各章中陆续介绍. 常见算数运算符 运算符 说明 举例 + 加法运算符,或正值 ...
- java实现算术表达式求值
需要根据配置的表达式(例如:5+12*(3+5)/7.0)计算出相应的结果,因此使用java中的栈利用后缀表达式的方式实现该工具类. 后缀表达式就是将操作符放在操作数的后面展示的方式,例如:3+2 后 ...
- [Java]算术表达式求值之二(中序表达式转后序表达式方案,支持小数)
Inlet类,入口类,这个类的主要用途是验证用户输入的算术表达式: package com.hy; import java.io.BufferedReader; import java.io.IOEx ...
- [Java]算术表达式求值之一(中序表达式转后序表达式方案)
第二版请见:https://www.cnblogs.com/xiandedanteng/p/11451359.html 入口类,这个类的主要用途是粗筛用户输入的算术表达式: package com.h ...
- 利用栈实现算术表达式求值(Java语言描述)
利用栈实现算术表达式求值(Java语言描述) 算术表达式求值是栈的典型应用,自己写栈,实现Java栈算术表达式求值,涉及栈,编译原理方面的知识.声明:部分代码参考自茫茫大海的专栏. 链栈的实现: pa ...
- OpenJudge计算概论-简单算术表达式求值
/*===================================== 简单算术表达式求值 总时间限制: 1000ms 内存限制: 65536kB 描述 2位正整数的简单算术运算(只考虑整数运 ...
随机推荐
- C++ 实现不能被继承的类
方法一: #include <iostream> using namespace std; class A { public: static A* getInstance(); stati ...
- zedboard U盘挂载+交叉编译helloworld
交叉编译环境见http://blog.csdn.net/xiabodan/article/details/22717175 1:编写hello.c文件 #include<stdio.h> ...
- DIV_ROUND_UP(x,y)实现x/y向上取整
#define DIV_ROUND_UP(x,y) (((x) + ((y) - 1)) / (y)) 1.问题 x.y都是整数,且x > 1, y > 1,求 x / y的向上取整,即: ...
- 浅析php fwrite写入txt文件的时候用 \r\n不能换行的问题
以下是对php中fwrite写入txt文件的时候用 \r\n不能换行的问题进行了介绍,需要的朋友可以过来参考下今天遇到了一个问题就是用fwrite写入txt文件的时候用 rn不能换行试了很久都没找到办 ...
- cadence16.6 如何对齐元件
1.选中Setup-->Application Mode-->Placement Edit mode2.按"CTRL"键,选中需要对齐的所有对象.3.点击右键右,或者在 ...
- GGS: Sybase to Oracle
Step 1: Start the GGSCI on Source and Target Source Target Oracle GoldenGate Command Interpreter for ...
- google api , the problem of null refresh token
http://stackoverflow.com/questions/10827920/google-oauth-refresh-token-is-not-being-received The ref ...
- sql Mirroring
http://www.codeproject.com/Articles/109236/Mirroring-a-SQL-Server-Database-is-not-as-hard-as http:// ...
- NodeJS学习笔记(转载)
前言 让nodeJS跑起来 文件结构 node_modules/ejs app.js 路由 路由规则 添加路由规则 注册功能 MongoDB 安装MongoDB 链接MongoDB 结语 前言 最近同 ...
- 为什么V8引擎这么快?(转载)
转载请注明出处:http://blog.csdn.net/horkychen Google研发的V8 JavaScript引擎性能优异.我们请熟悉内部程序实现的作者依源代码来看看V8是如何加速的. 作 ...