POJ - 1539 Evaluating Simple C Expressions(简单的C表达式)

该博客介绍了如何处理简单的C表达式,涉及识别并处理前置和后置递增操作符。通过编写赋值语句替换++操作符,并按照特定顺序执行这些语句以正确评估表达式。
摘要由CSDN通过智能技术生成
Evaluating Simple C Expressions
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u

[]  [Go Back]  [Status]  

Description

The task in this problem is to evaluate a sequence of simple C expressions, but 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 80 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, ...). 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 c being incremented to 4 prior to evaluation 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 value of the expression -- c + b -- has the value 4, with variables b and c having the values 1 and 2 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:

  1. 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 ++ from before that variable in the expression.
  2. 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.
  3. 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.
  4. 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

Your program is to read expressions, one per line, until a totally blank (or empty) line is read.
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.

Output

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 output format.

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


题目大意:
字符间的运算。
a-z代替1-26。输出最后式子的值以及各变量运算结束后的值。
注意a++ 与 ++a 的区别。注意空格。


解题思路:

先把字符串去掉空格,再对每个字符进行判断。

依次判断前缀,中缀和后缀。

如果是减号,就用sum减去这个符号的值,否则加上这个这个符号的值。

以下的注释写的很详细。仅供参考。


#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;

struct Map {
	int v;
	int flag;
};

Map map[256];
int len;
//初始化
void init() {
	for(int i = 0; i < 26; i++) {
		map[i + 'a'].v = i+1;
		map[i + 'a'].flag = 0; 
	}
}

void solve(string tmp,int pos,int &sum) {

	int i = pos-1; //查找字母前一个字符
	
	if( i >= 0 && tmp[i] == tmp[i-1]) { //前缀为--或者++
		if(tmp[i] == '-') //前缀先做自增加或自减
			--map[tmp[pos]].v;
		else
			++map[tmp[pos]].v;

		i -= 2; //将位置向前移动两位,查找前缀之前的符号,如果是减法就减去相应的值,否则增加
		if(tmp[i] == '-') {
			sum -= map[tmp[pos]].v;
		}
		else {
			sum += map[tmp[pos]].v;
		}
	}
	else { //判断中缀
		if(tmp[i] == '-') {
			sum -= map[tmp[pos]].v;
		}
		else {
			sum += map[tmp[pos]].v;
		}
	}
	i = pos+1; //判断后缀,如果是后缀就先自增加再运算
	if( i+1 < len && tmp[i] == tmp[i+1]) {
		if(tmp[i] == '-') {
			--map[tmp[pos]].v;
		}
		else {
			++map[tmp[pos]].v;
		}
	}
}

int main() {
	string str;	
	while(getline(cin,str)) {
		cout << "Expression: " <<str<<endl;
	
		string tmp;
		init();	
		len = str.size();
		//去掉多余的括号
		for (int i = 0; i < len; i++) {
			if(str[i] != ' ') {
				tmp += str[i];
			}
		}
		
		int sum = 0; //保存最后的总值
		len = tmp.size();
		for(int i = 0; i < len; i++) {
			if( isalpha(tmp[i]) ) {
				map[tmp[i]].flag = 1; //标记该字母已经被使用过
				solve(tmp,i,sum);	//解决函数
			}
		}

		cout<<"    value = "<<sum<<endl; //输出sum的值
		for(int i = 0; i < 26; i++) { //输出每个元素的值
			if(map[i + 'a'].flag != 0) {
				cout<<"    "<<(char)(i + 'a') << " = "<<map[i + 'a'].v <<endl;
			}
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值