表达式求值
来源:互联网 发布:华策影视 知乎 编辑:程序博客网 时间:2024/06/05 18:52
#include <iostream>
using namespace std;
struct //设定运算符等级
{
char cp;//运算符
int weight;//优先级
} lweight[]={{'=',0},{'(',1},{'*',5},{'/',5},{'+',3},{'-',3},{')',6}},
rweight[]={{'=',0},{'(',6},{'*',4},{'/',4},{'+',2},{'-',2},{')',1}};
int leftweight(char op)//求左运算符OP的等级
{
for(int i=0;i<7;++i)
{
if(lweight[i].cp==op)
{
return lweight[i].weight;
}
}
}
int rightweight(char op)//求右运算符OP的等级
{
for(int i=0;i<7;++i)
{
if(rweight[i].cp==op)
{
return rweight[i].weight;
}
}
}
bool Inop(char ch)//判断是否运算符
{
if(ch=='('||ch==')'||ch=='+'||ch=='+'||ch=='-'||ch=='*'||ch=='/')
{
return true;
}
else
{
return false ;
}
}
int Compare(char op1,char op2)//Op1和Op2比较结果
{
if(leftweight(op1)==rightweight(op2))
{
return 0;
}
else if(leftweight(op1)<rightweight(op2))
{
return -1;
}
else
{
return 1;
}
}
void transexp(char *exp,char postexp[])//将算术式转换为后缀表达式
{
struct
{
char data[100];//存放运算符
int top;//栈指针
}op;//定义一个运算符栈
int i=0;//i作为postexp的下标
op.top=-1;
op.top++;
op.data[op.top]='=';//将’=‘进栈
while(*exp!='\0')//exp表达式没有扫描完的时候循环
{
if(!Inop(*exp))//为数字字符的情况
{
while(*exp>='0'&&*exp<='9')//判定是数字
{
postexp[i++]=*exp;
exp++;
}
postexp[i++]='#';//用’#‘标识一个数值串结束
}
else //位运算符的情况
{
switch(Compare(op.data[op.top],*exp))
{
case -1:
op.top++;//栈顶的运算符优先级低
op.data[op.top]=*exp;
exp++;//继续扫描其他字符
break;
case 0://只有括号满足情况
op.top--;//将’(‘退栈
exp++;//继续扫描其他字符
break;
case 1://退栈并且输出到postexp中
postexp[i++]=op.data[op.top];
op.top--;
break;
}
}
}
while (op.data[op.top]!='=')//此时EXp扫描完毕,退栈到'='为止
{
postexp[i++]=op.data[op.top];
op.top--;
}
postexp[i]='\0';//给postexp添加结束标识
}
float compvalue(char *postexp)//计算后缀表达式postexp的值
{
struct
{
float data[100];//存放数值
int top;//栈指针
}st;//定义一个运算数栈
float a,b,c,d;
st.top=-1;
while(*postexp!='\0')//postexp字符串未扫描完时循环
{
switch (*postexp)
{
case '+'://判定运算符是加号
a=st.data[st.top];
st.top--;//退栈取数值a
b=st.data[st.top];
st.top--;//退栈取数值b
c=a+b;//计算c
st.top++;
st.data[st.top]=c;//结果重新入栈
break;
case '-':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=b-a;
st.top++;
st.data[st.top]=c;
break;
case '*':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=a*b;
st.top++;
st.data[st.top]=c;
break;
case '/'://进行除法运算的时候,要注意除数是否为0
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
if(a!=0)
{
c=b/a;
st.top++;
st.data[st.top]=c;
}
else
{
cout<<"除零错误,除数不能为零"<<endl;
}
break;
default ://处理数字字符
d=0;//将连续的数字字符转换成对应的数值存放到d中
while(*postexp>='0'&&*postexp<='9')//判断是数字字符时
{
d=10*d+*postexp-'0';
postexp++;
}
st.top++;
st.data[st.top]=d;
break;
}
postexp++;//继续处理其他字符
}
return (st.data[st.top]);
}
int main()
{ char temp[100];
cin>>temp;
char postexp[100];
transexp(temp,postexp);
cout<<"中缀表达式"<<temp<<endl;
cout<<"后缀表达式"<<postexp<<endl;
cout<<"表达式求的值"<<compvalue(postexp)<<endl;
return 0;
}
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 宁静难得放假年德国基尔年度奖金
- C#使用TCP/IP与ModBus进行通讯
- JavaEE技术路线图
- Vmware What is the location of the directory of C header files that match your running问题的解决
- Proguard android代码混淆 防止反编译
- 表达式求值
- 基于tiny6410 的led驱动
- mysql导入sql文件
- 一些比较好的知识点学习网址
- Java获取当前的日期和时间
- 《征服C指针》第一章重点小结
- 数字统计---解题思路
- HDU 1251 统计难题
- Android回调机制(全面深入学习一)