无法进行负数和多位数运算
#include <iostream>
#include <Stack>
#include <string>
#include <cctype>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;
string inTopostExp(string exp); //
int prior(char c); //judge the priority of the operator
int computepostExp(string postExp);
int Auxcal(int a,int b,char ch);
int main()
{
string inexp,posexp;
for(;;)
{
cout<<"输入中缀表达式:(输入exit退出)";
getline(cin,inexp);
if(inexp=="exit")
break;
posexp=inTopostExp(inexp);
cout<<"后缀表达式为:"<<posexp<<endl;
cout<<"计算结果为:"<<computepostExp(posexp)<<endl;
}
return 1;
}
int Auxcal(int a,int b,char ch)
{
switch(ch)
{
case'+':return a+b;
case'-':return a-b;
case'*':return a*b;
case'/':return a/b;
case'%':return a%b;
case'^':return pow(a,b);
}
}
int computepostExp(string postExp)
{
char curch;
int num1,num2;
stack<int> numStack;
for(int i=0;i<postExp.length();i++)
{
curch=postExp[i];
if(postExp[i]==32)
{
continue;
}
else if(postExp[i]<'9'&&postExp[i]>'0')
{
int tmpnum=int(postExp[i])-'0';
while(postExp[i+1]<'9'&& postExp[i+1]>'0')
{
tmpnum=tmpnum*10+int(postExp[i+1])-'0';
i++;
}
numStack.push(tmpnum);
}
else
{
num2=numStack.top();
numStack.pop();
num1=numStack.top();
numStack.pop();
numStack.push(Auxcal(num1,num2,curch));
}
}
return numStack.top();
}
int prior(char c)
{
switch(c)
{
case'+':case'-':return 1;
case'*':case'/':case'%':return 2;
case'^':return 3;
}
}
string inTopostExp(string exp)
{
string postExp;
char curtoken,toptoken;
stack<char> opStack;
for(int i=0;i<exp.length();i++)
{
curtoken=exp[i];
if(isalnum(curtoken))
{
postExp.append(1,' ');
postExp.append(1,curtoken);
for(;isalnum(exp[i+1]);i++)
{
postExp.append(1,exp[i+1]);
}
continue;
}
else
{
switch(curtoken)
{
case '(':
opStack.push(curtoken);
break;
case')':
while(opStack.top()!='(')
{
toptoken=opStack.top();
postExp.append(1,toptoken);
opStack.pop();
}
opStack.pop();
break;
case'+':case'-':case'*':case'/':case'%':case'^':
for(;;)
{
if(opStack.empty())
{
opStack.push(curtoken);
break;
}
else if(opStack.top()=='(')
{
opStack.push(curtoken);
break;
}
else if(prior(opStack.top())<prior(curtoken))
{
opStack.push(curtoken);
break;
}
else
//若栈顶符号优先级大于当前符号优先级,则该符号出栈,但此时当前符号不入栈,因为还需继续判断下一个优先级,也就是说这一步只是将操作符栈栈顶的符号出栈进入后缀表达式中。
{
toptoken=opStack.top();
postExp.append(1,' ');
postExp.append(1,toptoken);
opStack.pop();
}
}
break;
}
}
}
//最后一个字符如果是数字,那么栈内还有没出栈的符号
while(!opStack.empty())
{
toptoken=opStack.top();
postExp.append(1,' ');
postExp.append(1,toptoken);
opStack.pop();
}
return postExp;
}
for(i = 0; str[i] != '\0'; i ++)
{
if(i == 0 && str[i] == '-')
{
v[t++] = str[i];
}
else if(str[i] == '(' && str[i+1] == '-')
{
i ++;
v[t++] = str[i++];
while(str[i] >= '0' && str[i] <= '9')
{
v[t] = str[i];
t ++;
i ++;
}
Inshuju(&data, atoi(v));
while(t > 0)
{
v[t] = 0;
t --;
}
if(str[i] != ')')
{
i --;
Infuhao(&symbol, '(');
}
}
else if(str[i] >= '0' && str[i] <= '9')
{
while(str[i] >= '0' && str[i] <= '9')
{
v[t] = str[i];
t ++;
i ++;
}
Inshuju(&data, atoi(v));
while(t > 0)
{
v[t] = 0;
t --;
}
i --;
}
可以进行负数和两位数的运算
如果可以编程代码的话十分感谢呦