POJ-3337 Expression Evaluator-表达式求值
来源:互联网 发布:网络推广的岗位职责 编辑:程序博客网 时间:2024/06/05 02:33
Description
This problem is about evaluating some C-style expressions. The expressions to be evaluated will contain only simple integer variables and a limited set of operators; there will be no constants in the expressions. There are 26 variables in the program, named by lower case letters a through z. Before evaluation, the initial values of these variables area = 1,b = 2, ...,z = 26.
The operators allowed are addition and subtraction (binary + and -), with their known meaning. So, the expressiona +c -d + b has the value 2 (1 + 3 - 4 + 2). Additionally, ++ and –- operators are allowed in the input expression too, which are unary operators, and may come before or after variables. If the ++ operator comes before a variable, then that variable's value is increased (by one) before the variable's value is used in calculating the value of the whole expression. Thus the value of ++c -b is 2. When ++ comes after a variable, that variable is increased (by one) after its value is used to calculate the value of the whole expression. So, the value of thec ++ -b is 1, thoughc is incremented after the value for entire expression is computed; its value will be 4 too. The -- operator behaves the same way, except that it decreases the value of its operand.
More formally, an expression is evaluated in the following manner:
- Identify every variable that are preceded by ++. Write an assignment statement for incrementing the value of each of them, and omit the ++ from before that variable in the expression.
- Do similarly for the variables with ++ after them.
- At this point, there is no ++ operator in the expression. Write a statement evaluating the remaining expression after the statements determined in step 1, and before those determined in step 2.
- Execute the statements determined in step 1, then those written in step 3, and finally the one written in step 2.
This way, evaluating ++ a + b ++ is the same as computing a = a + 1, result = a + b, and b = b + 1.
Input
The first line of the input contains a single integer T which is the number of test cases, followed byT lines each containing the input expression for a test case. Ignore blanks in the input expression. Be sure that no ambiguity is in the input expressions (likea+++b). Similarly, ++ or -- operators do not appear both before and after one single variable (like ++a++). You may safely assume each variable appears only once in an expression.
Output
For each test case, write each expression as it appears in the input (exactly), then write the value of the complete expression. After this, on separate lines, write the value of each variable after evaluating the expression (write them in sorted order of the variable names). Write only the values of the variables that are used in the expressions. To find out about the output format, follow the style used in the sample output below.
Sample Input
2a+bc+f--+--a
Sample Output
Expression: a+bvalue = 3a = 1b = 2Expression: c+f--+--avalue = 9a = 0c = 3f = 5
Source
/* *Copyright (c)2015,烟台大学计算机与控制工程学院 *All rights reserved. *作 者:单昕昕 *完成日期:2015年8月22日 *版 本 号:v1.0 */ #include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>using namespace std;int main(){ int k,i,t,l,v[26],val,b,c,flag; char s[1000]; cin>>t;//测试用例数 getchar(); while(t--) { bool occur[26];//标记字母是否用过 //以下作初始化 memset(occur,false,sizeof(occur)); for(i=0; i<26; ++i) v[i]=i+1; val=0; b=1; gets(s); l=strlen(s); cout<<"Expression: "<<s<<endl;//输出表达式 for(i=0; i<l; ++i)//去掉表达式中的空格!!!这个一定要有,一开始以为木有关系结果WA了11次! { if(s[i]==' ') { k=i; for(; s[k]!='\0'; ++k) s[k]=s[k+1]; --i; } } for(k=0; k<l; ++k) { //是运算符 if((s[k]=='+')||(s[k]=='-')) { if(((s[k]=='+'&&s[k+1]=='+')||(s[k]=='-'&&s[k+1]=='-'))&&(s[k+2]<='z'&&s[k+2]>='a'))//如果是++或-- { v[int(s[k+2]-'a')]+=(s[k]=='+'?1:-1); ++k; c=int(s[k+1]-'a'); occur[c]=true; } else b=(s[k]=='+'?1:-1);//只是+或- } //是变量 else if(s[k]>='a'&&s[k]<='z') { flag=0;//标记 c=int(s[k]-'a'); val+=b*v[c]; if(!occur[c])//没用过 { flag=1; occur[c]=true; } if(flag==1&&((s[k+1]=='+'&&s[k+2]=='+')||(s[k+1]=='-'&&s[k+2]=='-')))//变量++或-- { v[c]+=(s[k+1]=='+'?1:-1); k+=2; } } } cout<<"value = "<<val<<endl;//输出各个值 for(i=0; i<26; ++i) if(occur[i]) cout<<char('a'+i)<<" = "<<v[i]<<endl; } return 0;}
呵呵呵呵哒,WA11次的惨痛经历。
对着书上的提示看了半天写了半天,好不容易编译木有错误了。。结果一直WA的心情真是难以言表。。。
- POJ-3337 Expression Evaluator-表达式求值
- Java数学表达式计算(Expression Evaluator)
- (字符串的处理4.7.22)POJ 3337 Expression Evaluator(解析C风格的字符串)
- ZOJ_3585 Equivalent Expression 表达式求值
- Expression Evaluation(表达式求值)
- Sequence_postfix expression(后缀表达式求值)
- Infix Expression Evaluator
- Postfix Expression Evaluator
- Runtime C# Expression Evaluator
- An expression evaluator
- .NET平台开源项目速览(8)Expression Evaluator表达式计算组件使用
- poj 1686 表达式求值判断
- POJ-1539(表达式求值)
- Internal error in the expression evaluator
- LeetCode-282. Expression Add Operators (JAVA)表达式求值
- poj 2269 Friends(表达式求值)
- 单调栈的应用 Largest Rectangle in Histogram,Max Tree,expression tree, 表达式求值,
- 基于表达式模版(expression template)的惰性求值(lazy evaluation)
- 智慧旅游项目技术文档
- 弹出窗口之Cookie控制
- 简单几何 CF Gym 100338B Geometry Problem:
- Struts1与Struts2的那些事
- 软工视频总结Part Two
- POJ-3337 Expression Evaluator-表达式求值
- C单链表的操作
- ScheduleFactory
- oc Block / NSComparisonResult排序 / 字面量
- 【LeetCode】(242)Valid Anagram(Easy)
- 【洛谷】P1089 津津的储蓄计划
- windows编程之进程通信:内存共享
- js 不同页面间传递值并取值
- ios学习之旅---oc面向对象的特点