c++计算器
来源:互联网 发布:backtrack4 linux iso 编辑:程序博客网 时间:2024/05/17 22:11
http://zhidao.baidu.com/link?url=1LOMv7HUIyx6-krghwi0NW-1uRrB1sOrOuCfnMgNgQBp6yjxdotEyAe_WDKZNJs3K1qTcHc76N6_TzmhPdnH9FBOVzVdTT_Z_CcKxUoB1XW
#include<iostream>#include<string>#include<stack>using namespace std; void pop_cal();int p_Rank(char);stack<char> opr_stk;stack<double> num_stk; int main(){ string in_str; int i=0; bool num_flag=0; double x=0; cout<<"Please input the expression string:"<<endl; cin>>in_str; for(i=0;i<in_str.size();i++){ if((in_str[i]>='0')&&(in_str[i]<='9')){ x=x*10+in_str[i]-'0'; num_flag=1; if(i==in_str.size()-1) num_stk.push(x); } else{ if(x){ num_stk.push(x); x=0; num_flag=0; } if(opr_stk.empty()) opr_stk.push(in_str[i]); else if(in_str[i]=='(') opr_stk.push(in_str[i]); else if(in_str[i]==')') { while(opr_stk.top()!='(') pop_cal(); opr_stk.pop(); } else if(p_Rank(in_str[i])<=p_Rank(opr_stk.top())) { pop_cal(); opr_stk.push(in_str[i]); } else{ opr_stk.push(in_str[i]); } } } while(!opr_stk.empty()) pop_cal(); double res=num_stk.top(); cout<<res<<endl;} void pop_cal(){ char op=opr_stk.top(); double a,b,res; b=num_stk.top(); num_stk.pop(); a=num_stk.top(); num_stk.pop(); switch(op) { case '+': res=a+b; break; case '-': res=a-b; break; case '*': res=a*b; break; case '/': res=a/b; break; default: break; } num_stk.push(res); opr_stk.pop();} int p_Rank(char x){ if(x=='(') return 0; else if(x=='+') return 1; else if(x=='-') return 2; else if(x=='*') return 3; else if(x=='/') return 4;}
0 0
- 计算器.c
- 【C#】计算器
- C/C++:找零计算器
- C/C++:计算器
- c语言实现计算器
- windows c 计算器
- C的简易计算器
- 基本计算器(C#)
- C bulider计算器初步
- 计算器c语言代码
- C++-------实现计算器
- 简单计算器实现(C++)
- C#My计算器源代码
- C语言极简版计算器
- 计算器实现(c++)
- C语言极简版计算器
- C语言简易计算器
- C#--加法计算器
- noj 1006 多项式乘法
- POJ 1204 Word Puzzles AC自动机 -
- 关于index
- 使用C++委托实现全方向的摇杆
- std::cout输出unsigned char 型数据
- c++计算器
- 二叉树的遍历
- 棋盘问题(经典DFS)
- 二元多项式
- Xml使用pull解析
- 无穷递归
- HDU 5773 The All-purpose Zero(DP)
- barplot条形图的图例位置设置
- phonegap笔记 -返回上一页并刷新代码整理