📄 calculator.h
字号:
enum boolean{ false,true };
#include<math.h>
#include<stack.h>
#include<iostream.h>
class calculator{
public:
calculator();//我认为书上的是不对的
void run();
void clear();
private:
void addoperand( double value);
boolean get2operands( double& left,double& right);
void dooperator(char op);
stack<double> s;
};
void calculator::addoperand(double value){
s.push(value);
}
boolean calculator::get2operands(double& left,double& right){
if(s.isempty()) {
cerr<<"Missing operand!"<<endl; return false;
}
right=s.pop();
if(s.isempty()){
cerr<<"Missing operand!"<<endl; return false;
}
left=s.pop();
return true;
}
void calculator::dooperator(char op){
double left,right; boolean result;
result=get2operands(letf,right);
if(result==true)
switch(op){
case'+': s.push(left+right); break;
case'-': s.push(left-right); break;
case'*': s.push(left*right); break;
case'/': if(right==0.0){
cerr<<"Divide by 0!"<<endl; clear();
}
else s.push(left/right); break;//和书上的不一样
case'^': s.push( pow(left,right) ); break;
}
else clear();
}
void calculator::run(){
char ch; double newoperand;
while(cin>>ch, ch!='='){
switch(ch){
case'+': case'-': case'*': case'/': case'^':
dooperator(ch); break;
default: cin.putback(ch);
cin>> newoperand;
addopreand(newoperand);
break;
}
}
}
void calculator::clear(){
s.makeempty();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -