📄 main.cpp
字号:
#include <iostream.h>
#include <math.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
const int maxstacksize = 50;
template <class t>
class stack
{ private:
t stacklist[maxstacksize];int top;
public:
stack (void):top(-1){}
void push (const t& item);
t pop (void);
void clearstack(void){ top = -1;}
t peek (void) const;
int stackempty(void) const{ return top == -1;}
int stackfull(void) const{ return top == maxstacksize-1; }
};
template <class t>
void stack<t>::push (const t& item)
{ if (top == maxstacksize-1){cerr << "stack overflow!" << endl;exit(1); }
top++; stacklist[top] = item; }
template <class t>
t stack<t>::pop (void)
{ t temp;
if(top == -1){cerr << "attempt to pop an empty stack!" << endl;exit(1); }
temp = stacklist[top]; top--; return temp;}
template <class t>
t stack<t>::peek (void) const
{if(top == -1){cerr << "attempt to peek at an empty stack!"<<endl;exit(1);}
return stacklist[top]; }
class calculator
{private:
stack<float> s;
stack<char> optr;
int youxian(char a,char b);
public:
bool gettwooperands(float& opnd1, float& opnd2);
void compute(char op);
float run(char *a);
};
int calculator:: youxian(char a,char b)
{if(b=='#'||b=='(')return 1;
if (a=='+'||a=='-')return -1;
if ((a=='*'||a=='/')&&(b=='-'||b=='+'))return 1;
if ((a=='*'||a=='/')&&(b=='*'||b=='/'||b=='^'))return -1;
if (a=='^'&&(b=='-'||b=='+'||b=='*'||b=='/'))return 1;
if (a=='^'&&b=='^')return -1;
return 0;
}
bool calculator:: gettwooperands(float& opnd1, float& opnd2)
{if(s.stackempty()) { cerr << "missing operand!" << endl;return false;}
opnd1 = s.pop();
if(s.stackempty()){ cerr << "missing operand!" << endl;return false;}
opnd2 = s.pop();
return true;
}
void calculator::compute(char op)
{ bool result; float operand1, operand2;
result = gettwooperands(operand1, operand2);
if (result == true)
{ switch(op)
{case '+': s.push(operand2+operand1);break;
case '-': s.push(operand2-operand1);break;
case '*': s.push(operand2*operand1);break;
case '/': if (operand1 == 0)
{ cerr << "divide by 0!" << endl;s.clearstack();}
else s.push(operand2/operand1);break;
case '^': s.push(pow(operand2,operand1));break;
}
}
else s.clearstack();
}
float calculator::run(char *a)
{
s.clearstack();
optr.clearstack();
optr.push('#');
int i=0;
char c,op;
float x;
while (a[i]=='('){optr.push('(');i++;}
for(;i<strlen(a);)
{ if((a[i]>='0'&&a[i]<='9') ||a[i]=='-'||a[i]=='+')
{x=atof(a+i);i++;
while((a[i]>='0'&&a[i]<='9')||a[i]=='.')i++;
s.push(x);
c=a[i];
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^')
{i++; while (youxian(c,optr.peek())<0) {op=optr.pop(); compute(op);}
if (youxian(c,optr.peek())>0){optr.push(c);}
while (a[i]=='('){optr.push('(');i++;}
if (!((a[i]>='0'&&a[i]<='9')||a[i]=='-'||a[i]=='+'))
{cout<<"error\n;" ;exit(0);}
}
}
else if (a[i]==')')
{while((op=optr.pop())!='('){compute(op);}
i++; c=a[i];
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^')
{i++; while (youxian(c,optr.peek())<0) {op=optr.pop(); compute(op);}
if (youxian(c,optr.peek())>0){optr.push(c);}
if (!((a[i]>='0'&&a[i]<='9')||a[i]=='-'||a[i]=='+'))
{cout<<"error shizi\n;" ;exit(0);}
}
}
else {cout<<a[i]<<"error shizi\n";exit(0);}
}
while(optr.peek()!='#'){compute(optr.pop());}
return s.peek();
}
void main()
{
char ch;
char a[100];
do
{cout<<"放入一个只能含有小括号的算术表达式+-*/^:\n";
cin.getline(a,100,'\n');
calculator cal;
cout<<cal.run(a)<<'\0';
}while(ch);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -