Calculate the expression
来源:互联网 发布:学java跟不上 编辑:程序博客网 时间:2024/06/03 05:24
Problem Description
You may find it’s easy to calculate the expression such as:
a = 3
b = 4
c = 5
a + b + c = ?
Isn’t it?
Input
The first line contains an integer stands for the number of test cases.
Each test case start with an integer n stands for n expressions will follow for this case.
Then n – 1 expressions in the format: [variable name][space][=][space][integer] will follow.
You may suppose the variable name will only contain lowercase letters and the length will not exceed 20, and the integer will between -65536 and 65536.
The last line will contain the expression you need to work out.
In the format: [variable name| integer][space][+|-][space][variable name| integer] …= ?
You may suppose the variable name must have been defined in the n – 1 expression and the integer is also between -65536 and 65536.
You can get more information from the sample.
Output
For each case, output the result of the last expression.
Sample Input
3
4
aa = 1
bb = -1
aa = 2
aa + bb + 11 = ?
1
1 + 1 = ?
1
1 + -1 = ?
Sample Output
12
2
0
#include<iostream>#include<cstdio>#include<map>using namespace std;string name,name2;map<string,int> cal;int zhengxing(string str)//将字符串中的数字转化为整型{ int fflag=0; int shi=1,anss=0; if(str[0]=='-') fflag=1; for(int i=str.size()-1;i>=fflag;i--) { anss+=(str[i]-'0')*shi; shi=shi*10; } if(fflag) return -anss; else return anss;}int main(){ int T,n,integer; char c; cin>>T; while(T--) { cin>>n; for(int i=0;i<n-1;i++) { cin>>name>>c>>integer; cal[name]=integer; } int flag=1,ans=0; while(cin>>name2) { if(name2[0]>='a'&&name2[0]<='z') { if(flag) { ans+=cal[name2]; } else { ans-=cal[name2]; } } else if(name2=="?") break; else if(name2=="=") continue; else if(name2=="+") flag=1; else if(name2=="-") flag=0; else { if(flag) ans+=zhengxing(name2); else ans-=zhengxing(name2); } } cout<<ans<<endl; } return 0;}
- Calculate the expression
- hdu 3347 Calculate the expression
- HDU 3347:Calculate the expression
- hdu 3347 Calculate the expression
- hdu 3347 Calculate the expression
- HDU 3347 Calculate the expression【模拟】
- HDU 3347 Calculate the expression 模拟 map
- HDU 3347 Calculate the expression(模拟 + map)
- HDU 3347 Calculate the expression By Assassin 模拟水题
- calculate the average score
- Calculate the formula 2139
- HDU2139 Calculate the formula
- ZOJ3772-Calculate the Function
- HDOU2139 Calculate the formula
- Calculate the code run time
- calculate the length of char*
- hdu 2139 Calculate the formula
- Sicily 4423. Calculate the Sum
- Intelij IDEA 常用快捷键
- pl/sql developer 查询语句无法使用中文条件
- L1-030. 一帮一
- 1.1. Implementing a RouteBuilder Class
- mysqldump导入导出数据库总结;MySQLDump在使用之前一定要想到的事情
- Calculate the expression
- 模拟ls -l命令简易代码demo
- Volley源码分析
- qq项目,,实现了加好友,,和单聊,,
- 在学习zookeeper时候遇见了log4j错误No appenders could be found for logger
- Centos 6 下配置nginx 反向代理Google
- Python实现简单网络爬虫--转自极客头条
- Mac下安装Memcache
- Python如何搜索模块