返回信息流贴一下代码:
#include<iostream>
#include<string>
using namespace std;
const int StackSize=1024; //实现栈的最大高度
class SeqStack //定义一个顺序栈模板类
{
public:
void Seqstack(){top=-1;};
void push(char x);
char pop();
char GetTop();
int Empty(){
if(top=-1)
return 1;
else
return 0;
};
private:
char data[StackSize];
int top;
};
void SeqStack::push(char x)
{
if(top>=StackSize-1)
{
throw"上溢";
}
top++;
data[top]=x;
}
char SeqStack::pop()
{
top--;
return data[top+1];
}
char SeqStack::GetTop()
{
return data[top];
}
int priority(char A)
{
if(A=='+'||A=='-')
return 0;
else if(A=='*'||A=='/')
return 1;
}
void exchange(char infix[])
{
SeqStack calculate;
int j=strlen(infix);
for(int i=0;i<j;i++)
{
char p=infix[i];
if(p>=65&&p<=90)
{
cout<<p;
}
if(p=='+'||p=='-'||p=='*'||p=='/')
{
if(calculate.Empty())
calculate.push(p);
else if(priority(p)>calculate.GetTop())
calculate.push(p);
else if(priority(p)<=calculate.GetTop())
{
while(!((calculate.GetTop()=='(')||(calculate.Empty())))
{
cout<<calculate.pop();
}
calculate.push(p);
}
}
if(p=='(')
{
calculate.push(p);
}
if(p==')')
{
while(calculate.GetTop()=='(')
{
cout<<calculate.pop();
}
cout<<calculate.pop();
}
}
if(!calculate.Empty())
{
while(calculate.Empty())
cout<<calculate.pop();
}
}
void main()
{
char infix[100];
cout<<"******简易计算器******"<<endl;
cout<<"请输入中缀表达式:"<<endl;
cin>>infix;
exchange(infix);
}
这是一条镜像帖。来源:北邮人论坛 / cpp / #75166同步于 2013/11/12
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
求助在线等大神!!!简易计算器 为什么编译没错误 运行时运算
cheer23
2013/11/12镜像同步1 回复
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复