uestc data structure n 秋实大哥搞算数
来源:互联网 发布:越狱软件源 编辑:程序博客网 时间:2024/05/17 23:01
这道题本身没什么,就是两个栈计算表达式,但我一直re,原来是输入之间有空行导致的,,,,,吸取教训
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<stack>#define MAX 2000010using namespace std;char s[MAX];long long cal(long long a,long long b,char opt){if(opt=='+')return a+b;if(opt=='-')return a-b;if(opt=='*')return a*b;if(opt=='/')return a/b;}int main(){int n,len,start,flag;char ch;cin>>n;getchar();while(n--){len=0;while(1){ch=getchar();if(ch=='\n')break;elseif(ch>='0'&&ch<='9'||ch=='+'||ch=='-'||ch=='*'||ch=='/'){s[len++]=ch;}}if(len==0){n++;continue;}stack<long long>num;stack<char>opt;if(s[0]=='+'||s[0]=='-'){start=1;if(s[0]=='-')flag=1;elseflag=0;}else{flag=0;start=0;}for(int i=start;i<len;i++){if(s[i]>='0'&&s[i]<='9'){long long number=s[i]-'0';int j;for(j=i+1;j<len&&s[j]>='0'&&s[j]<='9';j++)number=number*10+s[j]-'0';i=j-1;if(flag){number=-number;flag=0;}num.push(number);}else if(s[i]=='*'||s[i]=='/'){while(!opt.empty()&&(opt.top()=='*'||opt.top()=='/')){long long b=num.top();num.pop();long long a=num.top();num.pop();char optr=opt.top();opt.pop();num.push(cal(a,b,optr));}opt.push(s[i]);}else if(s[i]=='+'||s[i]=='-'){while(!opt.empty()){long long b=num.top();num.pop();long long a=num.top();num.pop();char optr=opt.top();opt.pop();num.push(cal(a,b,optr));}opt.push(s[i]);}}while(!opt.empty()){long long b=num.top();num.pop();long long a=num.top();num.pop();char optr=opt.top();opt.pop();num.push(cal(a,b,optr));}cout<<num.top()<<endl;}return 0;}
0 0
- uestc data structure n 秋实大哥搞算数
- 2016 UESTC Training for Data Structures N - 秋实大哥搞算数 用栈处理表达式
- [UESTC 1074]秋实大哥搞算数
- uestc data structure n 秋实大哥与快餐店
- 2016 UESTC Training for Data Structures N - 秋实大哥搞算数 CDOJ 1074 栈 表达式求值
- UESTC -- 1074 秋实大哥搞算数 (栈)
- uestc data structure D - 秋实大哥与战争
- cdoj 2015数据结构专题:N - 秋实大哥搞算数
- uestc data structure training f 秋实大哥与妹纸
- 秋实大哥搞算数(字符串模拟)
- 2015 UESTC Training for Data Structures 秋实大哥下棋
- CDOJ 1074 秋实大哥搞算数(栈_表达式求值)
- [UESTC 1060]秋实大哥与快餐店
- [UESTC 1061]秋实大哥与战争
- 秋实大哥与小朋友 UESTC
- 秋实大哥与线段树 UESTC
- UESTC-1057 秋实大哥与花
- UESTC 483 Data Structure Problem
- 如何在MySQL中设置外键约束
- IOS开发语言Swift入门连载---基础部分
- 【索引】Designing Efficient Algorithms
- 垃圾回收机制(二)
- Ogre SDK 18.1(for VS2010)的安装与环境配置资料整理
- uestc data structure n 秋实大哥搞算数
- 【索引】Designing Efficient Algorithms:Examples
- Top-down-Rpg-Start-kit插件的使用与二次开发
- Dijkstra 算法
- 【索引】Designing Efficient Algorithms:Exercises: Beginner
- 阈值函数参数说明
- AWK模式处理语言
- Access保留关键字
- 软件配置项测试要点