写作原因:
今天写了一天这个题,,也陆陆续续看了一些文章,真的栓Q。不记录一下都对不起这一天(抹眼泪........),欢迎留言评论!
下面就进入正题吧!
Problem Description
算数四则运算的规则是1)先乘除,后加减;2)从左算到右;3)先括号内,后括号外。
由此,算式4+2*3-10/5的计算顺序为4+2*3-10/5=4+6-10/5=4+6-2=8。
给定一个以“#”作为结束符的算式,求出算式的结果。
Input Description
以“#”结尾的表达式,运算数为正整数。每个表达式占一行。
Output Description
输出表达式运算的结果。
Sample Input
4+2*3-10/5# 3*(7-2)# 2*3/2#
Sample Output
8 15 3
解答:
#include <iostream>
#include <string>
//#include <stdio.h>
#define STACKSIZE 200
using namespace std;
template <class T1>//封装了一个类模板,操作数栈、运算符栈通用
class Stack{
public:
T1 *top,*base;
int stacksize;
Stack()//初始化
{
base=new T1[STACKSIZE];
if(!base)
return ;
top=base;
stacksize=STACKSIZE;
}
int isFull()//判满
{
if(top-base==stacksize)
{
return 1;
}
return 0;
}
int isEmpty() //判空
{
if(top==base)
{
return 1;
}
return 0;
}
void push(T1 e)//入栈
{
if(isFull())
return ;
*top=e;
top++;
}
void pop(T1 &temp)//出栈
{
if(isEmpty())
return ;
--top;
temp=*top;
}
T1 GetTop()//取栈顶元素
{
if(!isEmpty())
return *(top-1);
}
void destroystack()//销毁栈
{
if(base!=NULL)
{
delete base;
top=base=NULL;
stacksize=0;
}
}
void clearstack()//清空栈
{
if(base!=NULL)
{
top=base;
}
}
};
int Isnumber(char ch)//判断是否为数
{
if(ch>='0'&&ch<='9')
return 1;
return 0;
}
int In(char ch)//判断是否为运算符
{
switch(ch)
{
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#': return 1;break;
default: return 0; break;
}
}
char precede(char a,char b)//算符优先级比较,看的是Attract1206博主的文章
{
int i, j;
char Prior[7][7]={{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}
};
switch(b)
{
case '+':j=0;break;
case '-':j=1;break;
case '*':j=2;break;
case '/':j=3;break;
case '(':j=4;break;
case ')':j=5;break;
case '#':j=6;break;
}
switch(a)
{
case '+':i=0;break;
case '-':i=1;break;
case '*':i=2;break;
case '/':i=3;break;
case '(':i=4;break;
case ')':i=5;break;
case '#':i=6;break;
}
return Prior[i][j];
}
double Operate(double a,char z,double b)
{
switch(z)
{//计算表达式的值
case '+': { return (double)a+b; }break;
case '-': { return (double)a-b; }break;
case '*': { return (double)a*b; }break;
case '/': {return (double)a/b; }break;
}
}
int main()
{
Stack <double>s1;//操作数栈 ,double
Stack <char>s2;//算符栈
// cout<<s2.GetTop();
char arr[100];
char *ch;
while(scanf("%s", arr) != EOF)
{
s2.push('#');
ch = arr;
// // cout<<"首元素为"<<*ch<<endl;
while(*ch!='#'||s2.GetTop()!='#')
{
if(Isnumber(*ch))
{
double t=*ch-'0';//字符转数字
ch++;
double s=0;
s=s*10+t;
while(Isnumber(*ch))//这个也是看博主的文章
{
t=*ch-'0';
s=s*10+t;
ch++;
}
s1.push(s);
//cout<<s<<"入操作数栈"<<endl;
//cout<<s<<"此时栈顶元素为"<<s1.GetTop()<<endl;
}
else if(In(*ch))
{
switch (precede(s2.GetTop(),*ch))
{
case '>':
{
double e1,e2;
char e3;
s1.pop(e1);
// cout<<"右操作数"<<e1<<"出栈"<<endl;
s1.pop(e2);
// cout<<"左操作数"<<e2<<"出栈"<<endl;
s2.pop(e3);
// cout<<e3<<"运算符出栈"<<endl;
s1.push(Operate(e2,e3,e1));
//cout<<Operate(e2,e3,e1)<<"求值入栈"<<endl;
}
break;
case '<':
{
s2.push(*ch);
// cout<<*ch<<"入字符栈"<<endl;
ch++;
break;
}
break;
case '=':
{
char e;
s2.pop(e);
ch++;
// cout<<e<<"出栈"<<endl;
}
break;
}
}
}
//cout<<"表达式值为"
cout<<s1.GetTop()<<endl;
s1.clearstack();
s2.clearstack();
}
s1.destroystack();
s2.destroystack();
return 0;
system("pause");
}