无括号的计算器(int)和有括号的计算器(int)
来源:互联网 发布:房地产大数据来源 编辑:程序博客网 时间:2024/05/29 02:39
//无括号的计算器 #include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <stack>#include <map>using namespace std;map<char,int> mp; //映射 stack<int> sa1; //定义一个int栈 stack<char> sa2; //定义一个char栈 int fun(int a1,int a2,char c3) //运算操作 {int ans;if(c3=='+') ans=a2+a1;if(c3=='-') ans=a2-a1;if(c3=='*') ans=a2*a1;if(c3=='/') ans=a2/a1;return ans;}int main(){mp['+']=1;mp['-']=1;mp['*']=2;mp['/']=2;mp['#']=0; //通过映射建立优先级 char s[1000],c3;int c1,c2;scanf("%s",s);sa2.push('#');for(int i=0;i<strlen(s);){if(s[i]>='0'&&s[i]<='9'){int a=0; while(s[i]>='0'&&s[i]<='9') //将字符串转化为整型 { a=a*10+s[i]-'0'; i++; } sa1.push(a); //将a入栈 }else{while(mp[s[i]]<=mp[sa2.top()]) //优先级判断 {c1=sa1.top();sa1.pop() ; c2=sa1.top();sa1.pop() ; c3=sa2.top();sa2.pop() ; int x=fun(c1,c2,c3); sa1.push(x); }sa2.push(s[i]) ; i++; } } while(sa2.empty()==0) {if(sa2.top() =='#') break; c1=sa1.top();sa1.pop() ;c2=sa1.top();sa1.pop() ;c3=sa2.top();sa2.pop() ;int x=fun(c1,c2,c3); sa1.push(x);} int ans=sa1.top() ; printf("%d\n",ans);return 0;}
有括号的计算器
//有括号的计算器 #include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <stack>#include <map>using namespace std;map<char,int> mp;stack<int> sa1;stack<char> sa2;int fun(int a1,int a2,char c3){int ans;if(c3=='+') ans=a2+a1;if(c3=='-') ans=a2-a1;if(c3=='*') ans=a2*a1;if(c3=='/') ans=a2/a1;return ans;}int main(){mp['+']=1;mp['-']=1;mp['*']=2;mp['/']=2;mp['#']=0;mp['(']=3;mp[')']=-1;char s[1000],c3;int c1,c2;scanf("%s",s);sa2.push('#');for(int i=0;i<strlen(s);){if(s[i]>='0'&&s[i]<='9'){int a=0; while(s[i]>='0'&&s[i]<='9') { a=a*10+s[i]-'0'; i++; } sa1.push(a); }else{while(mp[s[i]]<=mp[sa2.top()]){if(sa2.top()=='(') break;c1=sa1.top();sa1.pop() ; c2=sa1.top();sa1.pop() ; c3=sa2.top();sa2.pop() ; int x=fun(c1,c2,c3); sa1.push(x); }if(s[i]==')') sa2.pop() ;elsesa2.push(s[i]) ; i++; } } while(sa2.empty()==0){if(sa2.top() =='#') break;c1=sa1.top();sa1.pop() ;c2=sa1.top();sa1.pop() ;c3=sa2.top();sa2.pop() ;int x=fun(c1,c2,c3); sa1.push(x);} int ans=sa1.top() ;printf("%d\n",ans);return 0;}
hdu 1237(小数型的)
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <stack>#include <map>using namespace std;map<char,int> mp;double fun(double a1,double a2,char c3){double ans;if(c3=='+') ans=a2+a1;if(c3=='-') ans=a2-a1;if(c3=='*') ans=a2*a1;if(c3=='/') ans=a2/a1;return ans;}int main(){mp['+']=1;mp['-']=1;mp['*']=2;mp['/']=2;mp['#']=0;char s1[1000];char s[1000],c3;double c1,c2;while(gets(s1)){if(strcmp(s1,"0")==0) break;stack<double> sa1; stack<char> sa2;int j=0;for(int i=0;i<strlen(s1);i++) //预处理 除去空格 {if(s1[i]!=' ')s[j++]=s1[i];}s[j]='\0';sa2.push('#');for(int i=0;i<strlen(s);){if(s[i]>='0'&&s[i]<='9'){double a=0; while(s[i]>='0'&&s[i]<='9') { a=a*10+s[i]-'0'; i++; } sa1.push(a); }else{while(mp[s[i]]<=mp[sa2.top()]){c1=sa1.top();sa1.pop() ; c2=sa1.top();sa1.pop() ; c3=sa2.top();sa2.pop() ; double x=fun(c1,c2,c3); sa1.push(x); }sa2.push(s[i]) ; i++; } } while(sa2.empty()==0){if(sa2.top() =='#') break;c1=sa1.top();sa1.pop() ;c2=sa1.top();sa1.pop() ;c3=sa2.top();sa2.pop() ;double x=fun(c1,c2,c3); sa1.push(x);} double ans=sa1.top() ;printf("%.2f\n",ans);}return 0; }
0 0
- 无括号的计算器(int)和有括号的计算器(int)
- 简单计算器(不支持括号)
- 简单的计算器小程序(不带括号)
- Java实现的计算器(带括号和错误输入提示)
- 栈实现的带括号的计算器
- 栈实现的带括号的计算器
- MFC 实现 加减乘除,括号,乘方的 计算器
- 带括号带键盘输入事件的计算器
- 基于C#的计算器(1) 带括号
- 带括号 非负数 的计算器
- 简单加减乘除括号的计算器实现
- 九度1019简单计算器到带括号的计算器
- 简易计算器(3)- 扩展(支持括号和负数)
- C语言实现计算器(包含加减乘除和括号)
- 【C语言】计算器(支持括号和长式子)
- 计算器(能识别括号)代码
- C语言编辑计算器(含括号)
- Delphi计算器控制台版(含括号)
- MySQL基础语法
- 检查二叉树B是否为二叉树A的子树
- 解决Mysql读写分离数据延迟
- nefu481最小路径覆盖问题【网络流24题】
- 程序占用的内存分类
- 无括号的计算器(int)和有括号的计算器(int)
- SVM 详解7
- Android仿超级课程表的课程界面设计
- php远程操作linux,ssh2
- 网易笔试题--寻找优雅点
- 17. Letter Combinations of a Phone Number
- Android学习之路
- LeetCode 231. Power of Two
- SVM 详解8