利用栈做的中缀计算器雏形
2016-02-17 12:11
246 查看
#include<iostream> #include<cstdlib> #include<stdio.h> #include<cstring> #include<queue> #include<algorithm> #include<stack> using namespace std; stack<double>num;//有关数字的栈 stack<char>op;//有关运算符的栈 bool Is_op_character(char ch)//判断是否是运算符 { return ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='='||ch==' '||ch=='('||ch==')'; } int operation(char ch)//对数字的操作 { double a,b; switch (ch) { case '+': { if(num.empty()) { cout<<"表达式不规范\n"; return 0; } a=num.top(); num.pop(); if(num.empty()) { cout<<"表达式不规范\n"; return 0; } b=num.top(); num.pop(); num.push(a+b); }break; case '-': { if(num.empty()) { cout<<"表达式不规范\n"; return 0; } a=num.top(); num.pop(); if(num.empty()) { cout<<"表达式不规范\n"; return 0; } b=num.top(); num.pop(); num.push(b-a); }break; case '*': { if(num.empty()) { cout<<"表达式不规范\n"; return 0; } a=num.top(); num.pop(); if(num.empty()) { cout<<"表达式不规范\n"; return 0; } b=num.top(); num.pop(); num.push(a*b); }break; case '/': { if(num.empty()) { cout<<"表达式不规范\n"; return 0; } a=num.top(); num.pop(); if(num.empty()) { cout<<"表达式不规范\n"; return 0; } b=num.top(); num.pop(); if(a==0) { cout<<"除数为零"<<endl; return 0; } else num.push(b/a); }break; default: return 1; } return 1; } int do_in_operation(char ch)//有关运算符的操作 { bool right=1; switch (ch) { case '+': { while(!op.empty()&&op.top()!='(') { ch=op.top(); op.pop(); right=operation(ch); } op.push('+'); }break; case '-': { while(!op.empty()&&op.top()!='(') { ch=op.top(); op.pop(); right=operation(ch); } op.push('-'); }break; case '*': { while(!op.empty()&&op.top()!='+'&&op.top()!='-'&&op.top()!='(') { ch=op.top(); op.pop(); right=operation(ch); } op.push('*'); }break; case '/': { while(!op.empty()&&op.top()!='+'&&op.top()!='-'&&op.top()!='(') { ch=op.top(); op.pop(); right=operation(ch); } op.push('/'); }break; case ')': { while(!op.empty()&&op.top()!='(') { ch=op.top(); op.pop(); right=operation(ch); } if(!op.empty()) op.pop(); else { cout<<"表达式不规范\n"; return 1; } }break; case '(': { op.push('('); }break; case '=': { while(!op.empty()) { ch=op.top(); op.pop(); right=operation(ch); } cout<<num.top()<<endl; }break; } return right; } int main() { char ch; char number[15]; cout<<"计算器1.3------------------------------------\n支持四则运算,支持小数点后十五位的小数,支持括号,不支持负数T.T\n"; cout<<"使用方法:输入运算式,输入等号并回车后输出结果,按'#'结束程序 \n"; int i=0; double a,b; cin>>ch; while(ch!='#') { while(ch>='0'&&ch<='9'||ch=='.')//对小数的支持 { number[i]=ch; i++; number[i]='\0'; if(i>=10) { cout<<"输入单个数据精度过大"<<endl; return -1; } cin>>ch; if(Is_op_character(ch)) { a=atof(number); num.push(a); i=0; break; } } if(do_in_operation(ch)) cin>>ch; else break; } cout<<"程序即将退出\n" ; system("pause"); }
相关文章推荐
- 如何自定义android chooser 应用选择器
- 将Cocos项目部署到Android Studio工程中
- 打印1到最大的n位数
- PHP位运算 详解
- UIToolBar的使用
- 快速排序代码
- android 项目中log信息的正确处理
- leetcode64-Minimum Path Sum(最小路径和)
- 史上最全最强SpringMVC详细示例实战教程(转)
- 2016 入驻CSDN 开始在这里记录技术与生活的点点
- 提高Web页面性能的技巧
- Linux(1):目录结构(linux文件系统)
- 对cell重新布局
- 实例解析C#设计模式编程中简单工厂模式的使用
- 苹果
- (p125)9.3-8找两个数组元素的中位数
- 移动端问题列表
- TCMalloc原理
- Bash . configure permission denied错
- android studio 安装时错误:unable to run maksdcard sdk tools