The task in this problem is to evaluate a sequence of simple C expressions, buy you need not know C to solve the problem! Each of the expressions will appear on a line by itself and will contain no more than 110 characters. 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 which may appear in our simple expressions, namely those with the names a through z (lower-case letters only). At the beginning of evaluation of each expression, these 26 variables will have the integer values 1 through 26, respectively (that is, a = 1, b = 2, ..., n = 14, o = 15, ..., z = 26). Each variable will appear at most once in an expression, and many variables may not be used at all.
The operators that may appear in expressions include the binary (two-operand) + and -, with the usual interpretation. Thus the expression a + c - d + b has the value 2 (computed as 1 + 3 - 4 + 2). The only other operators that may appear in expressions are ++ and--. These are unary (one-operand) operators, and may appear before or after any variable. When the ++ operator appears before a variable, that variable's value is incremented (by one) before the variable's value is used in determining the value of the entire expression. Thus the value of the expression ++c - b is 2, with cbeing incremented to 4 prior to evaluating the entire expression. When the ++ operator appears after a variable, that variable is incremented (again, by one) after its value is used to determine the value of the entire expression. Thus the value of the expression c++ - b is 1, but c is incremented after the complete expression is evaluated; its value will still be 4. The --operator can also be used before or after a variable to decrement (by one) the variable; its placement before or after the variable has the same significance as for the ++ operator. Thus the expression --c + b-- has the value 4, with variables c and b having the values 2 and 1 following the evaluation of the expression.
Here's another, more algorithmic, approach to explaining the ++and -- operators. We'll consider only the ++ operator, for brevity:
- Identify each variable that has a ++ operator before it. Write a simple assignment statement that increments the value of each such variable, and remove the ++ operator from before that variable in the expression.
- In a similar manner, identify each variable that has a ++operator after it. Write a simple assignment statement that increments the value of each of these, and remove the ++operator from after that variable in the expression.
- Now the expression has no ++ operators before or after any variables. Write the statement that evaluates the remaining expression after those statements written in step 1, and before those written in step 2.
- Execute the statements generated in step 1, then those generated in step 3, and finally the one generated in step 2, in that order.
Using this approach, evaluating the expression ++a + b++ is equivalent to computing
- a = a + 1 (from step 1 of the algorithm)
- expression = a + b (from step 3)
- b = b + 1 (from step 2)
where expression would receive the value of the complete expression.
Input and Output
Your program is to read expressions, one per line, until the end of the file is reached. Display each expression exactly as it was read, then display the value of the entire expression, and on separate lines, the value of each variable after the expression was evaluated. Do not display the value of variables that were not used in the expression. The samples shown below illustrate the desired exact output format.
Blanks are to be ignored in evaluating expressions, and you are assured that ambiguous expressions like a+++b (ambiguous because it could be treated as a++ + b or a + ++b) will not appear in the input. Likewise, ++ or -- operators will never appear both before and after a single variable. Thus expressions like ++a++ will not be in the input data.
Sample Input
a + b b - z a+b--+c++ c+f--+--a f-- + c-- + d-++e
Sample Output
Expression: a + b value = 3 a = 1 b = 2 Expression: b - z value = -24 b = 2 z = 26 Expression: a+b--+c++ value = 6 a = 1 b = 1 c = 4 Expression: c+f--+--a value = 9 a = 0 c = 3 f = 5 Expression: f-- + c-- + d-++e value = 7 c = 2 d = 4 e = 6 f = 5
这个题目题意还是很好弄明白的,就是模拟C中的加减算式表达式进行运算,我是从刘汝佳那本书上的推荐题目看到的,很奇怪的是他归为的是二叉树的那一类题,但此题我是想不出如何运用二叉树了,希望有知道的人可以留言讲解下。下面是我的思路,我采用的是便读入边处理的方法,先读取操作符再读一个字母进行处理,看起来其实挺简单的,但写的我好复杂···主要手残吧,老写错,考虑也不周全,空格太丫蛋疼了。
#include<stdio.h> #include<stdlib.h> #include<string.h> char str[10],s[150];//s来储存表达式 int letter[200];// 用来存储字母所代表的值 int len,size=0;//size为表达式的长度,len为操作符长度 void init(){//初始化letter,-1表示当前字母不存在于表达式中 for(int i=97;i<=122;i++)letter[i]=-1; } int main(){ char ch,temp;//ch为当前所读字符,temp为前一个所读字符 int ans=0; init(); while(scanf("%c",&ch)==1){ len=0; while(ch==' '){ s[size++]=ch; scanf("%c",&ch); }//有空格就继续读 while(ch=='-'||ch=='+'){//用str来储存加减字符串 str[len++]=ch; s[size++]=ch; scanf("%c",&ch); while(ch==' '){ s[size++]=ch; scanf("%c",&ch); }//有空格就继续读 } str[len]='\0'; while(ch==' '){ s[size++]=ch; scanf("%c",&ch); }//有空格就继续读 if(ch=='\n'){//如果遇见回车代表该表达式已经输入完成,需要输出结果 if(2==len){//此处是对最后一个字母如果出现还有操作符的情况的处理 if(strcmp(str,"--")==0)letter[temp]--; else letter[temp]++; } printf("Expression: "); for(int i=0;i<size;i++)printf("%c",s[i]); printf("\n"); printf(" value = %d\n",ans); for(int i=0;i<=25;i++)if(letter['a'+i]>=0)printf(" %c = %d\n",('a'+i),letter['a'+i]); size=0,ans=0,init();//完成一个以后记得初始化 } else{//此时读取字符为字母,根据不同情况进行处理 s[size++]=ch; letter[ch]=ch-96; if(len==2){ if(strcmp(str,"--")==0){ letter[ch]--; ans+=letter[ch]; } if(strcmp(str,"++")==0){ letter[ch]++; ans+=letter[ch]; } } if(len==3){ if(strcmp(str,"++-")==0){ letter[temp]++; ans-=letter[ch]; } if(strcmp(str,"--+")==0){ letter[temp]--; ans+=letter[ch]; } if(strcmp(str,"+--")==0){ letter[ch]--; ans+=letter[ch]; } if(strcmp(str,"-++")==0){ letter[ch]++; ans-=letter[ch]; } } if(len==5){ if(strcmp(str,"++-++")==0){ letter[temp]++; letter[ch]++; ans-=letter[ch]; } if(strcmp(str,"+++--")==0){ letter[temp]++; letter[ch]--; ans+=letter[ch]; } if(strcmp(str,"---++")==0){ letter[temp]--; letter[ch]++; ans-=letter[ch]; } if(strcmp(str,"++---")==0){ letter[temp]++; letter[ch]--; ans-=letter[ch]; } if(strcmp(str,"--+++")==0){ letter[temp]--; letter[ch]++; ans+=letter[ch]; } if(strcmp(str,"--+--")==0){ letter[temp]--; letter[ch]--; ans+=letter[ch]; } } if(len==1){ if(str[0]=='-'){ ans-=letter[ch]; } if(str[0]=='+'){ ans+=letter[ch]; } } if(len==0){//这里是个特例主要是为了读取第一个字符就为字母的情况 ans+=letter[ch]; } temp=ch;//记得这里传递一下 } } return 0; }