#include "Calculator.h"
Calculator::Calculator(){
}
void Calculator::processInput(){
char c;
while((c=getchar()) != '#'){
charQueue.push(c);
}
/*
while(!charQueue.empty()){
cout<<charQueue.front()<<" ";
charQueue.pop();
}
cout<<endl;
*/
}
bool Calculator::isPriority(char optr1,char optr2){//optr1 is the icp,and the optr2 is the isp
if(optr1 == '^'){
if(optr2 == '^')
return false;
else
return true;
}
else if(optr1 == '*' || optr1 == '/' || optr1 == '%'){
if(optr2 == '^' || optr2 == '*' || optr2 == '%' ||optr2 == '*')
return false;
else
return true;
}
else if(optr1 == '+' || optr1 == '-'){
return false;
}
return false;
}
int Calculator::getResult(int num1,int num2,char optr){
int result = 0;
switch(optr){
case '^':
result = 1;
for(int i=0;i<num1;i++)
result *= num2;
break;
case '*':
result = num1*num2;
break;
case '/':
result = num2/num1;
break;
case '%':
result = num2%num1;
break;
case '+':
result = num1+num2;
break;
case '-':
result = num2-num1;
break;
default:
break;
}
return result;
}
void Calculator::process(){
char c;
while(!charQueue.empty()){
c = charQueue.front();//get the front element
charQueue.pop();
if(isdigit(c)){//the char c is a digit
int temNum = c-'0';
while(!charQueue.empty() && isdigit(charQueue.front())){
c = charQueue.front();
charQueue.pop();
temNum = (temNum*10) + (c-'0');//calculate the current result
}
//push the num to the numStack
numStack.push(temNum);
}else{//it must be a optr
if(c != '^' && c != '%' && c != '/' && c != '*' && c != '+' && c != '-'){
cout<<"invalide input!please check!"<<endl;
return;
}
else{
if(optrStack.empty()){//the optrStack is empty
optrStack.push(c);
}
else{//the optrStack is unempty
if(isPriority(c,optrStack.top())){//icp>isp,push the icp
optrStack.push(c);
}else{//icp<=isp,pop one from optrStack,pop two from numStack,getResult,push the
//result to the numStack,push the icp to optrStack
char temOptr = optrStack.top();
optrStack.pop();
int temNum1 = numStack.top();
numStack.pop();
int temNum2 = numStack.top();
numStack.pop();
//get the result
int temResult = getResult(temNum1,temNum2,temOptr);
//push the result
numStack.push(temResult);
//push the c
optrStack.push(c);
}
}
}
}
}
//finish the input,then get the final result
while(!optrStack.empty()){//while the optrStack is unEmpty
char temOptr = optrStack.top();
optrStack.pop();
int temNum1 = numStack.top();
numStack.pop();
int temNum2 = numStack.top();
numStack.pop();
//get the result
int temResult = getResult(temNum1,temNum2,temOptr);
numStack.push(temResult);
}
int finalResult = 0;
if(!numStack.empty()){
finalResult = numStack.top();
numStack.pop();
}
//check the numStack
if(!numStack.empty()){
cout<<"There is an error in the expression,please check!";
return;
}else{
cout<<"The final result of the input expression is: "<<finalResult<<endl;
return;
}
}
void Calculator::clean(){
while(!optrStack.empty()){
optrStack.pop();
}
while(!numStack.empty())
numStack.pop();
while(!charQueue.empty())
charQueue.pop();
}
void Calculator::calculate(){
processInput();
process();
clean();
}
中缀表达式计算C++实现
3星 · 超过75%的资源 需积分: 48 109 浏览量
2013-01-16
20:26:02
上传
评论
收藏 10KB GZ 举报
鸭脖
- 粉丝: 501
- 资源: 5
最新资源
- 基于Vue和TypeScript的房地产管理系统前端设计源码
- 基于微信小程序的通用订票系统设计源码
- 基于ThinkPHP的轻量级PHP开发框架设计源码
- 基于CSS的响应式鲜花网站全屏效果设计源码
- 基于JavaScript的访客预约系统设计源码
- 基于Vue和ECharts的工作租房数据可视化系统设计源码
- 1040g0cg310ravpiu6ibg5pg00tsipsln3ju2d0g 2
- 基于Python的SAR图像去噪CNN-NLM设计源码
- redhat6升级到redhat7,过程redhat6.x-> redhat6.10->rehat7.9 主版本最高版本
- 基于Django的流程引擎设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈