📄 计算器.cpp
字号:
#include<iostream>
#include<conio.h>
using namespace std;
const int maxlen=200;
int &operation(const int &,const int &,const char &);
char expstr[maxlen];
enum Error_code{success=0,downflow,stackempty,overflow}code;
template<class Stack_entry>
class Stack{
public:
Stack();
bool isempty() const;
bool isfull() const;
void Get_count(int &)const;
Error_code pop();
Error_code get(Stack_entry &item)const;
Error_code push(const Stack_entry &item);
private:
int count;
Stack_entry data[maxlen];
};
template<class Stack_entry>
Stack<Stack_entry>::Stack()
{
count=0;
}
template<class Stack_entry>
bool Stack<Stack_entry>::isempty()const
{
if(count==0) return true;
else return false;
}
template<class Stack_entry>
bool Stack<Stack_entry>::isfull() const
{
if(count==maxlen) return true;
else return false;
}
template<class Stack_entry>
void Stack<Stack_entry>::Get_count(int &n)const
{
n=count;
}
template<class Stack_entry>
Error_code Stack<Stack_entry>::pop()
{
if(isempty()) return downflow;
else count--;
return success;
}
template<class Stack_entry>
Error_code Stack<Stack_entry>::get(Stack_entry &item)const
{
if(isempty()) return stackempty;
else
item=data[count-1];
return success;
}
template<class Stack_entry>
Error_code Stack<Stack_entry>::push(const Stack_entry &item)
{
if(isfull()) return overflow;
else
data[count]=item;
count++;
return success;
}
void Do_command()
{
Stack<int>S_number;
Stack<char>S_symbol;
int getnb1,getnb2,result,i=0,n;
char getch;
while(expstr[i]!='\0')
{
switch(expstr[i])
{
case '+':
case '-':
S_symbol.get(getch);
if(S_symbol.isempty()||getch=='(')
S_symbol.push(expstr[i]);
else
{
S_symbol.get(getch);
S_symbol.pop();
S_number.get(getnb1);
S_number.pop();
S_number.get(getnb2);
S_number.pop();
result=operation(getnb2,getnb1,getch);
S_number.push(result); ;
S_symbol.push(expstr[i]);
}
i++;
break;
case '*':
case '/':
S_symbol.get(getch);
if(S_symbol.isempty()||getch=='('||getch=='+'||getch=='-')
{
S_symbol.push(expstr[i]);
}
else
{
S_symbol.get(getch);
S_symbol.pop();
S_number.get(getnb1);
S_number.pop();
S_number.get(getnb2);
S_number.pop();
result=operation(getnb2,getnb1,getch);
S_number.push(result);
S_symbol.push(expstr[i]);
}
i++;
break;
case '(':
S_symbol.push(expstr[i]);
i++;
break;
case ')':
S_symbol.get(getch);
while(getch!='(')
{
S_symbol.pop();
S_number.get(getnb1);
S_number.pop();
S_number.get(getnb2);
S_number.pop();
result=operation(getnb2,getnb1,getch);
S_number.push(result);
S_symbol.get(getch);
}
S_symbol.pop();
i++;
break;
default:
int res=0;
while(expstr[i]>='0'&&expstr[i]<='9')
{
res=res*10+expstr[i]-'0';
i++;
}
S_number.push(res);
}
}
S_number.Get_count(n);
while(!S_symbol.isempty()&&n>=2)
{
S_symbol.get(getch);
S_symbol.pop();
S_number.get(getnb1);
S_number.pop();
S_number.get(getnb2);
S_number.pop();
result=operation(getnb2,getnb1,getch);
S_number.push(result);
S_number.Get_count(n);
}
S_number.get(result);
cout<<result;
}
int &operation(const int &n1,const int &n2,const char &ch)
{
static int n;
switch(ch)
{
case '+': n=n1+n2; break;
case '-': n=n1-n2; break;
case '*': n=n1*n2; break;
case '/': n=n1/n2; break;
}
return n;
}
void main()
{
char *introduce="The expression can only contain the number:0~9 and symbol:'+'、'-'、'*'、'/'、'('、')' \n for example: 2+3*(9/3-2)";
cout<<introduce<<'\n'<<"Enter the expression: ";
gets(expstr);
cout<<"expstr="<<expstr<<"="<<flush;
Do_command();getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -