描述
从键盘上输入一个后缀表达式,试编写算法计算表达式的值。规定:后缀表达式的长度不超过一行,以“=”作为输入结束,操作数之间用空格分隔,操作符只可能有+、−、*、/四种运算。
输入
多组数据,每组数据一行,对应一个后缀算术表达式,每个表达式均以“=”结尾。当表达式只有一个“=”时,输入结束。
输出
对于每组数据输出一行,为表达式的运算结果。
输入样例 1
1 2+8 2-7 4-/*=
1 2+=
1 2/=
=
输出样例 1
6.00
3.00
0.50
#include <iostream>
#include<cmath>
#include <iomanip>
#define MAXSIZE 1000
using namespace std;
char op[7] = { '+', '-', '*', '/', '(', ')', '=' };
typedef struct
{
char *base;
char *top;
int stacksize;
}SqStackOPTR;
typedef struct
{
double *base;
double *top;
int stacksize;
}SqStackOPND;
int InitStack(SqStackOPTR &S)
{
S.base=new char [MAXSIZE];
if(!S.base)
{
cout<<"失败!" <<endl;return 0;
}//应该在new后
S.top=S.base;
S.stacksize=MAXSIZE;
return 1;
}
int InitStack(SqStackOPND &S)
{
S.base=new double [MAXSIZE];
if(!S.base)
{
cout<<"失败!" <<endl;return 0;
}//应该在new后
S.top=S.base;
S.stacksize=MAXSIZE;
return 1;
}
int Push(SqStackOPTR &S,char e)
{
if(S.top-S.base==S.stacksize)
{
cout<<"PUSH ERROR"<<endl;return 0;
}
*S.top=e;
S.top++;
//cout<<"PUSH ing"<<*(S.top-1)<<e<<endl;
return 1;
}
int Push(SqStackOPND &S,double e)
{
if(S.top-S.base==S.stacksize)
{
cout<<"PUSH ERROR"<<endl;return 0;
}
*S.top=e;
S.top++;
//cout<<"PUSH ing"<<*(S.top-1)<<e<<endl;
return 1;
}
double GetTop(SqStackOPND S)
{
if(S.top!=S.base)
return *(S.top-1);
}
char GetTop(SqStackOPTR S)
{
if(S.top!=S.base)
return *(S.top-1);
}
int Pop(SqStackOPTR &S,char &e)
{
if(S.top==S.base)
{
cout<<"POP ERROR"<<endl;
return 0;
}
S.top--;
e=*S.top;
return 1;
}
int Pop(SqStackOPND &S,double &e)
{
if(S.top==S.base)
{
cout<<"POP ERROR"<<endl;
return 0;
}
S.top--;
e=*S.top;
return 1;
}
int In(char ch) {//判断ch是否为运算符
for (int i = 0; i < 7; i++) {
if (ch == op[i]) {
return 1;
}
}
return 0;
}
char Precede(char theta1, char theta2) {//判断运算符优先级
if ((theta1 == '(' && theta2 == ')') || (theta1 == '=' && theta2 == '=')) {
return '=';
} else if (theta1 == '(' || theta1 == '=' || theta2 == '(' || (theta1
== '+' || theta1 == '-') && (theta2 == '*' || theta2 == '/')) {
return '<';
} else
return '>';
}
double Operate(double first, char theta, double second) {//计算两数运算结果
switch (theta) {
case '+':
return first + second;
case '-':
return first - second;
case '*':
return first * second;
case '/':
return first / second;
}
return 0;
}
double EvaluateExpression(char ch) {//算术表达式求值的算符优先算法,设OPTR和OPND分别为运算符栈和操作数栈
SqStackOPND OPND;
char theta;double a, b;char x, top;
InitStack(OPND);
while (ch != '=') //表达式没有扫描完毕或OPTR的栈顶元素不为“=”
{
char sign='+';
if(ch=='-')
{
sign=ch;
}
if(!In(ch))//不是运算符则解析数字字符串然后进操作数栈
{
if(sign=='-')
Push(OPND,-(ch-48));
else
{
Push(OPND,ch-48);
//cout<<ch-48<<"进"<<endl;
cin>>ch;
}
}//while 解析第一个数,整合整数部分和小数部分并将其压入栈
else
{
Pop(OPND, b);
Pop(OPND, a);
// cout<<"出栈:" <<b<<theta<<a<<endl;
theta=ch;
// cout<<"theta:"<<theta<<endl;
Push(OPND, Operate(a, theta, b)); //将运算结果压入OPND栈
cin>>ch;
}
} //while
return GetTop(OPND); //OPND栈顶元素即为表达式求值结果
}
int main()
{
while (1)
{
char ch;
cin >> ch;
//cout << "请输入要计算的表达式" << endl;
if(ch=='=')break;
double res = EvaluateExpression(ch);
cout <<setiosflags(ios::fixed)<<setprecision(2)<< res <<endl;
}
return 0;
}
原博地址
https://blog.csdn.net/weixin_43673589