📄 calculator1.h
字号:
#include"stack.h"
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
enum Boolean{False,True};
class Calculator
{
private:
Stack <double> Num; //操作数栈
Stack <char> Op;
void EnterNum(double num); //将操作数num压入数栈
void EnterOp(char op); //将操作符op压入符号栈
Boolean GetTwoOperands(double &oper1,double &oper2); //从数栈中提取两个操作数。
int Judge(char op); //判断优先级
Boolean Compare(char &op1,char &op2) ; //比较优先级
void Compute(char c); //执行运算
public:
Calculator(void);
void Run(void); //运行
void Clear(void); //清理栈
};
Calculator::Calculator()
{}
void Calculator::EnterNum(double num)
{ Num.Push(num);}
void Calculator::EnterOp(char op)
{ Op.Push(op); }
Boolean Calculator::GetTwoOperands(double &oper1,double &oper2)
{
if(Num.StackEmpty())
{
cerr<<"error:stack empty!"<<endl;
Clear();
}
else
{oper1=Num.Pop();}
if(Num.StackEmpty())
{
cerr<<"error:stack empty!"<<endl;
Clear();
return False;
}
else
{
oper2=Num.Pop();
return True;
}
}
int Calculator::Judge(char op) //优先级越高,返回值越大
{
if(op!='('&&op!=')'&&op!='^'&&op!='*'&&op!='/'&&op!='+'&&op!='-')
{
cerr<<"error: Judge input error!"<<endl;
Clear();
exit(1);
}
else
{
int m;
switch(op)
{
case '(': m=5;break;
case '^': m=4;break;
case '*': m=3;break;
case '/': m=3;break;
case '+': m=2;break;
case '-': m=2;break;
case ')': m=1;break;
}
return m;
}
}
Boolean Calculator::Compare(char &op1,char &op2)
{
if( Judge(op1)>=Judge(op2)) //当op1的优先级比op2的优先级高或相等时返回true
return True;
else
return False;
}
void Calculator::Compute(char c)
{
Boolean result;
double operand1,operand2 ;
result=GetTwoOperands(operand1,operand2);
if(result==True)
{
switch(c)
{
case '^': Num.Push(pow(operand2,operand1));break;
case '*': Num.Push(operand2*operand1);break;
case '/': if(operand1==0)
{
cerr<<"error: divide by 0 !"<<endl;
Clear();
exit(1);
}
else
{Num.Push(operand2/operand1);break;}
case '+': Num.Push(operand2+operand1);break;
case '-': Num.Push(operand2-operand1);break;
}
}
else
Clear();
}
void Calculator::Run(void)
{
Clear();
char b;
double data;
while(cin>>b, b!='=')
{
if(b >= '0' && b <= '9')
{ // ch为数值
cin.putback(b);// 将ch放回输入流中
cin>>data;// 按double型方式,重新读入操作数
EnterNum(data);// 将操作数放入栈中
}
else if (b=='^'||b == '+' || b == '-' || b == '*' || b == '/'||b=='('||b==')') // b是运算符
{
if(Op.StackFull())
{
cerr<<"error: stack full!"<<endl;
exit(1);
}
if(Op.StackEmpty())
{ Op.Push(b);}
else
{
char Top;
if(b==')')
{
while(Op.peek()!='(' )
{
char ch;
ch=Op.Pop();
Compute(ch);
if(Op.StackEmpty())
{
cerr<<"error: missing char '(' "<<endl;
exit(1);
}
}
if(Op.peek()=='(')
{
Op.Pop();
}
}
else //b!=')'
{
Top=Op.peek();
while( True==Compare(Top,b)&& Top!='(' ) //如果站定元素的优先级比读进操作高或相等
{
Compute(Top);
Op.Pop(); //运算符被执行,不能再应用
//cout<<Num.peek()<<endl;
if(Op.StackEmpty())
break;
Top=Op.peek();
}
Op.Push(b); //
}
}
}
}
while(1)
{
if(Op.StackEmpty())
break;
Compute(Op.Pop());
}
cout<<Num.peek()<<endl;
}
void Calculator::Clear()
{
Num.ClearStack();
Op .ClearStack();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -