【题目】
任何一个表达式都由操作数、运算符和界限符组成的,为了叙述的简洁,在此仅讨论简单算数表达式的求值问题,这种表达式只包含加、减、乘、除4种运算符。 例如: 控制台输入 2*(2+3)-5+4*5 控制台输出 25
我们知道,算术四则运算遵循以下3条规则: 1、先乘除后加减 2、从左到右运算 3、先括号内后括号外
【思路分析】
1、通过for循环索引来遍历我们输入的表达式 2、如果索引处的值是数字就直接压入数字栈 3、如果索引处的值是运算符分两种情况 3.1、如果符号栈为空就直接入栈 3.2、如果符号栈中有运算符,将当前运算符优先级与栈顶运算符的优先级比较。如果当前运算符优先级小于或等于栈顶运算符的优先级,就需要从数字栈中弹出两个数并从符号栈中弹出一个运算符。进行运算后将结果入数字栈,循环直至当前运算符优先级大于栈顶运算符的优先级将,然后当前运算符入符号栈。如果当前运算符优先级大于栈顶运算符的优先级,就直接入符号栈。如果是"(“号就直接入符号栈,如果是”)"就从数字栈中弹出两个数并从符号栈中弹出一个运算符。进行运算后将结果入数字栈,循环这个操作直至遇到")“符号,将”)"符号弹出。 4、当表达式扫面完毕,就按顺序从数字栈中弹出两个数字从符号栈中弹出一个运算符,将运行结果压入数字栈 5、重复第4不操作直至数字栈中只剩一个数字,这个数字就是表达式的结果
【完整代码】
#include<iostream>
#include <string>
#define OK 1
#define ERROR 0
#define MAXSIZE 100
using namespace std;
typedef int Status;
typedef int SElemType;
typedef struct {
SElemType* base;
SElemType* top;
int stackSize;
}SqStack;
Status InitStack(SqStack &s){
s.base = new SElemType[MAXSIZE];
if(!s.base) exit(ERROR);
s.top = s.base;
s.stackSize = MAXSIZE;
return OK;
}
Status Push(SqStack &s,SElemType e){
if(s.top-s.base == s.stackSize)
return ERROR;
*s.top=e;
s.top ++;
return OK;
}
/**
*弹栈
* */
Status Pop(SqStack &s,SElemType &e){
if (s.top == s.base)
return ERROR;
s.top--;
e = *s.top;
return OK;
}
SElemType GetTop(SqStack &s){
if(s.top == s.base) return ERROR;
return *(s.top-1);
}
int priority(int oper){
if (oper == '*' || oper == '/') return 1;
else if (oper == '+' || oper == '-') return 0;
else if (oper == '(' || oper == ')') return 2;
else return -1;
}
int operatorNum(SqStack &numStack,SqStack &operatorStack){
int right,left,oper1;
Pop(numStack,left);
Pop(numStack,right);
Pop(operatorStack,oper1);
switch (oper1){
case 42 :return right*left;
case 43:return right+left;
case 45:return right-left;
case 47:return right/left;
}
}
void noSingle(string num,SqStack &numStack){
for (int j = 0; j < num.length(); ++j) {
int i;
Pop(numStack, i);
}
Push(numStack, atoi(num.c_str()));
}
int main(){
string input;
cin>>input;
SqStack numStack;
SqStack operatorStack;
InitStack(numStack);
InitStack(operatorStack);
string num;
for (int i = 0; i <input.length(); i++) {
if (48<=input[i] && input[i]<=57){
Push(numStack,input[i]-48);
string str(1,input[i]);
num.append(str);
if(i == input.length()-1 && num.length()>=2)
noSingle(num,numStack);
}else{
if (num.length()>=2)
noSingle(num,numStack);
num = "";
if (operatorStack.top == operatorStack.base)
Push(operatorStack,input[i]);
else{
SElemType oper = GetTop(operatorStack);
if(input[i] == ')'){
while(oper != '('){
int result = operatorNum(numStack,operatorStack);
Push(numStack,result);
oper = GetTop(operatorStack);
}
Pop(operatorStack,oper);
}else if (priority(input[i]) <= priority(oper) && priority(oper) != 2){
while (priority(input[i]) <= priority(oper)){
int result = operatorNum(numStack,operatorStack);
Push(numStack,result);
oper = GetTop(operatorStack);
}
Push(operatorStack, input[i]);
}
else{
Push(operatorStack, input[i]);
}
}
}
}
while (numStack.top != numStack.base+1){
int result = operatorNum(numStack,operatorStack);
Push(numStack,result);
}
int result = GetTop(numStack);
cout<<"结果="<<result;
return 0;
}
【运行结果】
2*(2+3)-5+4*5
结果=25
Process finished with exit code 0
|