POJ---3337 Expression Evaluator[模拟运算符]
2012-09-16 14:03
113 查看
Expression Evaluator
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 are a = 1, b = 2, ..., z = 26.
The operators allowed are addition and subtraction (binary + and -), with their known meaning. So, the expression a + 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 the c ++ - b is 1, though c 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 by T 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 (like a+++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
Sample Output
Source
Tehran 2006 Preliminary
模拟运算符
code:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 1824 | Accepted: 570 |
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 are a = 1, b = 2, ..., z = 26.
The operators allowed are addition and subtraction (binary + and -), with their known meaning. So, the expression a + 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 the c ++ - b is 1, though c 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 by T 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 (like a+++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
2 a+b c+f--+--a
Sample Output
Expression: a+b value = 3 a = 1 b = 2 Expression: c+f--+--a value = 9 a = 0 c = 3 f = 5
Source
Tehran 2006 Preliminary
模拟运算符
code:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int data[26]; char opr[10010]; char opr1[10010]; bool vst[26]; void init() { int i; for(i=0;i<26;i++) data[i]=i+1; memset(vst,false,sizeof(vst)); } int main() { int ans; int t; int i,j; scanf("%d\n",&t); while(t--) { init(); ans=0; bool flag=false; gets(opr1); int len=strlen(opr1); for(i=0,j=0;i<len;i++) { if(opr1[i]!=' ') { opr[j]=opr1[i]; j++; } } for(i=0;i<j;i++) { if(flag==false) { if(opr[i]>='a'&&opr[i]<='z') { if(i-2>=0&&opr[i-2]=='+'&&opr[i-1]=='+') { data[opr[i]-'a']++; ans=data[opr[i]-'a']; vst[opr[i]-'a']=true; } else if(i-2>=0&&opr[i-2]=='-'&&opr[i-1]=='-') { data[opr[i]-'a']--; ans=data[opr[i]-'a']; vst[opr[i]-'a']=true; } else if(opr[i+2]=='+'&&opr[i+1]=='+') { ans=data[opr[i]-'a']++; vst[opr[i]-'a']=true; } else if(opr[i+2]=='-'&&opr[i+1]=='-') { ans=data[opr[i]-'a']--; vst[opr[i]-'a']=true; } else if(opr[i-1]=='-') { ans=-data[opr[i]-'a']; vst[opr[i]-'a']=true; } else { ans=data[opr[i]-'a']; vst[opr[i]-'a']=true; } flag=true; } } else { if(opr[i]>='a'&&opr[i]<='z') { if(i-4>=0&&opr[i-2]=='+'&&opr[i-1]=='+'&&opr[i-3]=='-') { ans=ans-(++data[opr[i]-'a']); vst[opr[i]-'a']=true; } else if(i-4>=0&&opr[i-2]=='-'&&opr[i-1]=='-'&&opr[i-3]=='+') { ans=ans+(--data[opr[i]-'a']); vst[opr[i]-'a']=true; } else if(opr[i+1]=='+'&&opr[i+2]=='+') { if(opr[i-1]=='-') { ans=ans-data[opr[i]-'a']; data[opr[i]-'a']++; vst[opr[i]-'a']=true; } if(opr[i-1]=='+') { ans=ans+data[opr[i]-'a']; data[opr[i]-'a']++; vst[opr[i]-'a']=true; } } else if(opr[i+1]=='-'&&opr[i+2]=='-') { if(opr[i-1]=='+') { ans=ans+data[opr[i]-'a']; data[opr[i]-'a']--; vst[opr[i]-'a']=true; } if(opr[i-1]=='-') { ans=ans-data[opr[i]-'a']; data[opr[i]-'a']--; vst[opr[i]-'a']=true; } } else if(opr[i-1]=='-') { ans=ans-data[opr[i]-'a']; vst[opr[i]-'a']=true; } else if(opr[i-1]=='+') { ans=ans+data[opr[i]-'a']; vst[opr[i]-'a']=true; } } } } printf("Expression: %s\n",opr1); printf("value = %d\n",ans); for(i=0;i<26;i++) { if(vst[i]) printf("%c = %d\n",'a'+i,data[i]); } } return 0; } /* 10 --a+b--+c--+--d Expression: --a+b--+c--+--d value = 9 a = 0 b = 1 c = 2 d = 3 */
相关文章推荐
- POJ 2000 Gold Coins(模拟)
- POJ 1666 Candy Sharing Game(模拟)
- POJ 2259 Team Queue【模拟队列】
- (pojstep1.3.1)1017(构造法模拟)
- POJ3190 Stall Reservations - 模拟 - 贪心 (差分)
- poj 3393 Lucky and Good Months by Gregorian Calendar(模拟)
- poj1051 细心模拟
- POJ 3863坐电梯 模拟推公式
- POJ 2632 Crashing Robots 机器人模拟
- [poj1068]Parencodings_模拟
- 【POJ】3185-The Water Bowls 模拟
- 贪心+模拟-poj-1017-Packets
- POJ 3665 模拟
- 【模拟】POJ_2706_Connect
- poj 2106 Boolean Expressions(模拟)
- POJ 2632 Crashing Robots(简单模拟)
- POJ 1471 模拟?
- POJ1001-烦死人的模拟
- POJ 3125 Printer Queue(打印队列---模拟)
- POJ 2420 A Star not a Tree? 费马点 计算几何 模拟退火