利用表达式树,计算一个加减乘除(可带括号)的表达式
2017-07-29 11:03
483 查看
#include <iostream>
#include <iterator>
#include <vector>
#include <cstdio>
#include <map>
#include <cstring>
using namespace std;
const int maxn = 255;
struct node
{
node* left;
node* right;
char op[100];
node():left(NULL),right(NULL){}
};
node* dfs(char* s,int x,int y)
{
int c1 = -1,c2 = -1,d = 0;
if(y-x==1)
{
node* p = new node;
(p->op)[0] = s[x];
return p;
}
int flag = 0;
for(int i = x;i<y;i++)
{
switch(s[i])
{
case '(':
d++;
flag = 1;
break;
case ')':
d--;
flag = 1;
break;
case '+':
case '-':
if(!d)
c1 = i;
break;
case '*':
case '/':
if(!d)
c2 = i;
break;
}
}
if(c1<0)
c1 = c2;
if(c1<0)
{
if(flag)
return dfs(s,x+1,y-1);
else
{
node* p1 = new node;
for(int j = x;j<y;j++)
{
(p1->op)[j-x] = s[j];
}
return p1;
}
}
node* p = new node;
(p->op)[0] = s[c1];
p->left = dfs(s,x,c1);
p->right = dfs(s,c1+1,y);
return p;
}
int cal(node* p)
{
char c = (p->op)[0];
if(c>47&&c<58)
{
int num = 0;
if(strlen(p->op)==1)
num = 0+c-'0';
else
{
for(int i = 0;i<strlen(p->op);i++)
{
num *= 10;
num += 0+(p->op)[i]-'0';
}
}
return num;
}
if(c=='+')
return cal(p->left)+cal(p->right);
if(c=='-')
return cal(p->left)-cal(p->right);
if(c=='*')
return cal(p->left)*cal(p->right);
if(c=='/')
return cal(p->left)/cal(p->right);
}
int main()
{
char str[maxn];
while(~scanf("%s",str))
{
int len = strlen(str);
node* root = dfs(str,0,len);
int sum = cal(root);
cout<<sum<<endl;
}
return 0;
}
#include <iterator>
#include <vector>
#include <cstdio>
#include <map>
#include <cstring>
using namespace std;
const int maxn = 255;
struct node
{
node* left;
node* right;
char op[100];
node():left(NULL),right(NULL){}
};
node* dfs(char* s,int x,int y)
{
int c1 = -1,c2 = -1,d = 0;
if(y-x==1)
{
node* p = new node;
(p->op)[0] = s[x];
return p;
}
int flag = 0;
for(int i = x;i<y;i++)
{
switch(s[i])
{
case '(':
d++;
flag = 1;
break;
case ')':
d--;
flag = 1;
break;
case '+':
case '-':
if(!d)
c1 = i;
break;
case '*':
case '/':
if(!d)
c2 = i;
break;
}
}
if(c1<0)
c1 = c2;
if(c1<0)
{
if(flag)
return dfs(s,x+1,y-1);
else
{
node* p1 = new node;
for(int j = x;j<y;j++)
{
(p1->op)[j-x] = s[j];
}
return p1;
}
}
node* p = new node;
(p->op)[0] = s[c1];
p->left = dfs(s,x,c1);
p->right = dfs(s,c1+1,y);
return p;
}
int cal(node* p)
{
char c = (p->op)[0];
if(c>47&&c<58)
{
int num = 0;
if(strlen(p->op)==1)
num = 0+c-'0';
else
{
for(int i = 0;i<strlen(p->op);i++)
{
num *= 10;
num += 0+(p->op)[i]-'0';
}
}
return num;
}
if(c=='+')
return cal(p->left)+cal(p->right);
if(c=='-')
return cal(p->left)-cal(p->right);
if(c=='*')
return cal(p->left)*cal(p->right);
if(c=='/')
return cal(p->left)/cal(p->right);
}
int main()
{
char str[maxn];
while(~scanf("%s",str))
{
int len = strlen(str);
node* root = dfs(str,0,len);
int sum = cal(root);
cout<<sum<<endl;
}
return 0;
}
相关文章推荐
- 利用栈的原理计算一个表达式的值
- 11.表达式计算对一串加减乘除带括号进行计算
- 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2
- 简单的JAVA计算表达式,支持加减乘除括号
- 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2
- 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2
- 模拟计算器:一个带加减乘除和括号的表达式 求值
- USTC机试—根据四则不含括号的四则表达式计算其值输出到一个文件中
- 利用栈和队列计算带有括号的表达式(纯理论)
- 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
- 华为机试 计算加减乘除数学表达式的结果
- 一个基于特征向量的近似网页去重算法——term用SVM人工提取训练,基于term的特征向量,倒排索引查询相似文档,同时利用cos计算相似度
- 计算一个表达式的值
- 一个计算四则运算表达式文本的方法
- 我就给一个PHP逆波兰表达式的算法吧---工资计算专用
- 对一个可进行带括号、加减乘除运算类的分析
- 利用正则式计算表达式的值
- 计算一个表达式的值
- 1.创建一个Rectangle类,添加width和height两个成员变量。 2.在Rectangle中添加两种方法分别计算矩形的周长和面积 3.编程利用Rectangle输出一个矩形的周
- JS中利用正则表达式提取一个字符串中的子字符串的方法