Evaluating Simple C Expressions 

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 c being 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:

  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 ++ operator 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 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
#include <iostream>
#include <stack>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <fstream>
#include <stack>
#include <list>
#include <sstream>
#include <cmath> using namespace std; #define ms(arr, val) memset(arr, val, sizeof(arr))
#define mc(dest, src) memcpy(dest, src, sizeof(src))
#define N 200
#define INF 0x3fffffff
#define vint vector<int>
#define setint set<int>
#define mint map<int, int>
#define lint list<int>
#define sch stack<char>
#define qch queue<char>
#define sint stack<int>
#define qint queue<int>
char res[N], ori[N];
int val[N], tag[N], exist[N], ans;//tag存放--|++在后面的,--为-1,++为1
int skip()//清除空格
{
mc(res, ori);
ms(exist, );
int n = ;
for (int i = ; res[i]; i++)
{
if (res[i] != ' ')
{
res[n++] = res[i];
}
if (islower(res[i]))
{
exist[res[i]] = ;
}
}
res[n] = '\0';
for (int i = 'a', j = ; i <= 'z'; i++, j++)
{
tag[i] = ;
val[i] = j;
}
return n;
} bool judge(int i, int j)
{
return (res[i] == res[j] && (res[i] == '-' || res[i] == '+'));
} void step1()//清除--|++在前的
{
int n = ;
for (int i = ; res[i]; i++)
{
if (islower(res[i]) && i > && judge(i - , i -))
{
if (res[i - ] == '-')
{
val[res[i]]--;
}
else
{
val[res[i]]++;
}
n -= ;
}
res[n++] = res[i];
}
res[n] = '\0';
} void step2()//清除--|++在后的
{
int n = ;
for (int i = ; res[i]; i++)
{
res[n++] = res[i];
if (islower(res[i]) && res[i + ] && res[i + ] && judge(i + , i + ))
{
if (res[i + ] == '-')
{
tag[res[i]] = -;
}
else
{
tag[res[i]] = ;
}
i += ;
}
}
res[n] = '\0';
} void step3()
{
step1();
step2();
ans = val[res[]];
for (int i = ; res[i]; i += )
{
if (res[i] == '-')
{
ans -= val[res[i + ]];
}
else
{
ans += val[res[i + ]];
}
}
}
void input()
{
printf("Expression: %s\n", ori);
printf(" value = %d\n", ans);
for (int i = 'a'; i <= 'z'; i++)
{
if (exist[i])
{
printf(" %c = %d\n", i, val[i] + tag[i]);
}
}
}
int main()
{
while (gets(ori))
{
if(skip())
{
step3();
input();
}
}
return ;
}

uva 327 - Evaluating Simple C Expressions的更多相关文章

  1. UVA 327 -Evaluating Simple C Expressions(栈)

    Evaluating Simple C Expressions The task in this problem is to evaluate a sequence of simple C expre ...

  2. uva 327 Evaluating Simple C Expressions 简易C表达式计算 stl模拟

    由于没有括号,只有+,-,++,--,优先级简单,所以处理起来很简单. 题目要求计算表达式的值以及涉及到的变量的值. 我这题使用stl的string进行实现,随便进行练手,用string的erase删 ...

  3. uva 1567 - A simple stone game(K倍动态减法游戏)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=4342">题目链接:uva 1567 - ...

  4. UVA - 11954 Very Simple Calculator 【模拟】

    题意 模拟二进制数字的位运算 思路 手写 位运算函数 要注意几个坑点 一元运算符的优先级 大于 二元 一元运算符 运算的时候 要取消前导0 二元运算符 运算的时候 要将两个数字 数位补齐 输出的时候 ...

  5. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  6. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第六章 2(Binary Trees)

    112 - Tree Summing 题目大意:给出一个数,再给一颗树,每个头节点的子树被包含在头节点之后的括号里,寻找是否有从头节点到叶子的和与给出的数相等,如果有则输出yes,没有输出no! 解题 ...

  7. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  8. Regular Expressions in Grep Command with 10 Examples --reference

    Regular expressions are used to search and manipulate the text, based on the patterns. Most of the L ...

  9. 组合数学第一发 hdu 2451 Simple Addition Expression

    hdu 2451 Simple Addition Expression Problem Description A luxury yacht with 100 passengers on board ...

随机推荐

  1. bzoj 3503: [Cqoi2014]和谐矩阵【高斯消元】

    如果确定了第一行,那么可以推出来整个矩阵,矩阵合法的条件是n+1行全是0 所以推出来n+1行和1行的关系,然后用异或高斯消元来解即可 #include<iostream> #include ...

  2. bzoj 2016: [Usaco2010]Chocolate Eating【二分+贪心】

    二分答案,贪心判断,洛谷上要开long long #include<iostream> #include<cstdio> using namespace std; const ...

  3. bzoj 4568: [Scoi2016]幸运数字【树链剖分+线段树+线性基】

    一眼做法,好处是好想好写坏处是常数大,容易被卡(bzoj loj 洛谷开O2 能AC,不开有90分-- 大概就是树剖之后维护线段树,在线段树的每个节点上上维护一个线性基,暴力\( 60^2 \)的合并 ...

  4. Luogu P1197 [JSOI2008]星球大战 By cellur925

    题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的以太隧 ...

  5. Hdu 5384 Danganronpa (AC自动机模板)

    题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222  Keywords ...

  6. [ZPG TEST 109] 兔子跳跃【构图】

    兔子跳跃 (jumping.pas/c/cpp) [问题描述] 兔子常常感到孤独,所以当他们决定出去走走,去见见他们的朋友,他们跳的很快. Iris正走在一条无限长的直线道路上.这条道路上点的编号.. ...

  7. post和get提交服务器编码过程

    参考资料:http://blog.csdn.net/z55887/article/details/46975679 先说出一个知识点: 如果浏览器端编码是UTF-8,那在服务器端解决乱码问题的方法有两 ...

  8. React.js 基本环境安装

    安装 React.js React.js 单独使用基本上是不可能的事情.不要指望着类似于 jQuery 下载放到 <head /> 标签就开始使用.使用 React.js 不管在开发阶段生 ...

  9. NBA15-16赛季半程有感

    2015-2016 新赛季NBA已经开打半程又多了,这半个赛季我们见证了 勇士的三节打卡:骑士的磨合反复:马刺的老骥伏枥: 当然还有窝火的XJBD. 豪哥200W刀签约黄蜂,打出来800W刀的身价,无 ...

  10. java中字节和字符的转换操作

    package com.ywx.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputSt ...