C++实现简易计算器
2016-10-14 23:19
190 查看
1.原理:利用栈的原理,将表达式转化为后缀表达式然后计算
2.代码:
2.代码:
#include<iostream> #include<cmath> #include<sstream> #include<cstdio> using namespace std; template <class T> class Stack{ private : T data[1000]; int t; public : Stack(){t=0;} void clear(){t=0;} T pop(){ if (t<=0) { cout<<"stack is empty"<<endl; return data[0]; } t--; //cout<<"pop "<<data[t]<<endl; return data[t]; } void push(T x){ if(t>=1000){ cout<<"the stack is full"<<endl; return; } //cout<<"push "<<x<<endl; data[t++]=x; } T top(){ if(t<=0){ cout<<"the stack is empty"<<endl; return data[0]; } return data[t-1]; } bool is_empty(){ if(t<=0) return true; return false; } }; class Calculator{ private : string postfixExp; string infixExp; double ans; public : Calculator(string s=""){ infixExp=s; ans=0; } void display(); double calculate(string infixExp); bool infix_to_postfix(); double cal_postfix(); double cal(double ,double ,char); string change(double); int get_priority(char); double read_num(string s,int *pos); }; void Calculator::display() { cout<<"后缀表达式为:"<<postfixExp<<endl; cout<<"表达式的值为:"; printf("%.2f\n",ans); } double Calculator::calculate(string infixExp){ this->infixExp= infixExp; if (infix_to_postfix()){ return ans=cal_postfix(); }else{ cout<<"语法错误,请检查表达式"<<endl; return 0.0; } } double Calculator::read_num(string s,int *pos) { double x=0; int flag=0,w=0,h=1,i; if(s[(*pos)]=='-') {(*pos)++;h=-1;} for ( i=(*pos);i<s.length();i++) if (s[i]!='#') if(s[i]=='.'){ flag=1; }else { if(flag) w++; x=x*10+s[i]-'0'; }else break; (*pos)=i; return x/pow(10,w)*h; } string Calculator::change(double x){ int y = (int)(x*10000),yy = y%10000; stringstream ss; ss<<y/10000<<'.'<<yy/1000<<(yy%1000)/100<<(yy%100)/10<<(yy%10); string r; ss>>r; int i; for (i=r.length()-1;i>=0;i--) if(r[i]!='0') break; if (r[i]=='.') i--; return r.assign(r,0,i+1); } double Calculator::cal(double num1,double num2,char op) { switch(op){ case '+': return num1+num2; case '-': return num1-num2; case '*': return num1*num2; case '/': return num1/num2; default: return 0.0; } } int Calculator::get_priority(char c){ switch(c){ case '+':case '-': return 1; case '*':case '/': return 2; case '(':return 0; case ')':return 0; case '#':return -2; default: return -1; } } bool Calculator::infix_to_postfix() { Stack<char> stk; int a_num_end=0,is_negative=0; string result="#"; stk.push('#'); for (int i=0;i<infixExp.length();i++) { char c = infixExp[i]; if(c=='-'&&(i==0||infixExp[i-1]=='(')) {result+='-'; is_negative=1;continue;} if((c<='9'&&c>='0')||c=='.') { if(a_num_end&&!is_negative) {result+='#'; } is_negative=0; result+=c; a_num_end=0; } else{a_num_end=1; if(c=='(') stk.push('('); else if (c==')') { while (stk.top()!='(') { result+='#'; result+=stk.pop(); } stk.pop(); } else { int p = get_priority(c); if (p==-1) return false; while (p<=get_priority(stk.top())){ result+='#'; result+=stk.pop(); } stk.push(c); } } } while (stk.top()!='#'){ result+='#'; result+=stk.pop(); } result+='#'; cout<<result<<endl; postfixExp = result; return true; } double Calculator::cal_postfix() { int l = postfixExp.length(); Stack<double> stk1; for (int i=1;i<l;i++){ char c = postfixExp[i]; if (c=='#') continue; if((c=='-'&&postfixExp[i+1]!='#')||(c>='0'&&c<='9')) stk1.push(read_num(postfixExp,&i)); else { double y = stk1.pop(); double x = stk1.pop(); stk1.push(cal(x,y,c)); } } return stk1.pop(); } int main() { 4000 Calculator c; string ini; while (1){ cout<<"输入要计算的表达式(以字母Q结束程序):"<<endl; cin>>ini; if(ini.length()==1&&ini[0]=='Q') break; c.calculate(ini); c.display(); } //c.calculate("(-5)*10+20/20-2*3*6/36"); //c.display(); return 0; }
相关文章推荐
- C++面向对象特性实现机制的初步分析 Part1
- 在Delphi与C++之间实现函数与对象共享
- C++机理:虚拟机制的实现[兼谈对比于传统机制]
- 用C++ std::priority_queue 实现哈夫曼算法
- 用C++实现C#中的委托/事件(标准C++之升级版)
- 用 C++ 实现 C# 中的 委托/事件 (2-delegate event functor)
- 在C++中实现属性
- 用PHP实现通过Web执行C/C++程序
- 使用c++实现Format函数
- 在Delphi与C++之间实现函数与对象共享
- 分析模式-计量的C++实现——完美版本
- Singleton模式的C++实现研究(转贴)
- C++设计模式:Singleton的模板实现之一
- C++实现单件的初探
- 单件模式的C++模板实现
- C++面向对象特性实现机制的初步分析 Part2
- 分析模式-计量的C++实现——回复ch0877
- 小写转大写金额在C++中的实现
- 用 C++ 实现 C# 中的 委托/事件 (5-functor2)
- 在你的C++程序里实现委托