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 = 1b = 2, ..., n = 14o = 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 expressiona + 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 题意:给一行字符串,代表一个表达式,表达式中只包含+,-,前置++,后置++,前置--,后置--,而且不会有违法的表达式出现,a,b,c,d.....初始时代表的值分别是1,2,3,4......
计算每个表达式的值,并且按照字典序大小输出每个出现在表达式中的字母的最终值的大小。 解析:使用栈这一数据结构,这题最好手写,不要用STL中的stack,扫一遍字符串,如果是'+',先判断他是否是某个数的后置++,直接找栈中元素的rear和rear-1是否分别是'+'和一个字母
(rear越界则不可能),再把那个字母的权值加1,弹出最顶上的元素。如果不存在这种情况,则加入到栈中,'-'跟'+'的操作一样,如果是字母,则先判断他是否有前置++或前置--,直接查找
栈中元素,如果rear和rear-1都是'+',则权值加1,并弹出栈中两个元素,答案加上该字母的权值(还要判断栈中rear是+(或没有)还是- ),最后按照格式输出,格式比较麻烦,自己要注意。 代码如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iterator>
#include<utility>
#include<sstream>
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
const int INF=1000000007;
const double eps=0.00000001;
int digit[27];  // 保存相应字母的权值
bool have[27]; // 判断某个字母是否在字符串中
string S;
int GetVal(char c){ return c-'a'+1; } // 得到这个字母的大小
vector<char> save;
char SK[10005]; // 栈
void solve()
{
for(int i=1;i<=26;i++) digit[i]=i;
memset(have,false,sizeof(have));
int ret=0; // 答案
int rear=0; // 栈指针
int len=save.size();
for(int i=0;i<len;i++)
{
if(save[i]=='+')
{
if(rear>1&&SK[rear]=='+'&&islower(SK[rear-1])) //是否为后置++
{
digit[GetVal(SK[rear-1])]++;
rear--;
}
else SK[++rear]='+'; // 否则加入栈
}
else if(save[i]=='-')
{
if(rear>1&&SK[rear]=='-'&&islower(SK[rear-1])) // 是否为后置--
{
digit[GetVal(SK[rear-1])]--;
rear--;
}
else SK[++rear]='-';
}
else
{
int id=GetVal(save[i]);
have[id]=true; // 标记
if(rear>1&&SK[rear]=='+'&&SK[rear-1]=='+')  // 前置++
{
digit[id]++;
rear--;
rear--;
}
else if(rear>1&&SK[rear]=='-'&&SK[rear-1]=='-') // 前置--
{
digit[id]--;
rear--;
rear--;
} if(rear==0||SK[rear]=='+') ret+=digit[id];
else ret-=digit[id];
SK[++rear]=save[i];
}
}
printf("Expression: ");
cout<<S<<endl;
printf(" value = %d\n",ret);
for(int i=1;i<=26;i++) if(have[i]) printf(" %c = %d\n",i-1+'a',digit[i]);
}
int main()
{
while(getline(cin,S))
{
save.clear();
for(int i=0;i<S.size();i++) if(S[i]!=' ') save.push_back(S[i]);
solve();
}
return 0;
}

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 exp ...

  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 442 矩阵链乘(栈)

    Input Specification Input consists of two parts: a list of matrices and a list of expressions. The f ...

  5. UVa 1451 (数形结合 单调栈) Average

    题意: 给出一个01串,选一个长度至少为L的连续子串,使得串中数字的平均值最大. 分析: 能把这道题想到用数形结合,用斜率表示平均值,我觉得这个想法太“天马行空”了 首先预处理子串的前缀和sum,如果 ...

  6. UVa 442 Matrix Chain Multiplication(栈的应用)

    题目链接: https://cn.vjudge.net/problem/UVA-442 /* 问题 输入有括号表示优先级的矩阵链乘式子,计算该式进行的乘法次数之和 解题思路 栈的应用,直接忽视左括号, ...

  7. 【UVA】673 Parentheses Balance(栈处理表达式)

    题目 题目     分析 写了个平淡无奇的栈处理表达式,在WA了5发后发现,我没处理空串,,,,(或者说鲁棒性差?     代码 #include <bits/stdc++.h> usin ...

  8. UVA - 442 Matrix Chain Multiplication(栈模拟水题+专治自闭)

    题目: 给出一串表示矩阵相乘的字符串,问这字符串中的矩阵相乘中所有元素相乘的次数. 思路: 遍历字符串遇到字母将其表示的矩阵压入栈中,遇到‘)’就将栈中的两个矩阵弹出来,然后计算这两个矩阵的元素相乘的 ...

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

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

随机推荐

  1. 这10篇 iOS 热文,你别错过哦

    <移动开发必读书单> 某一领域的技术人,在他的职业生涯中,一定有一些绕不过去的技术和非技术的知识.有的时候,靠自己摸索.到处偷师,倒也能掌握.但是,这些别人早就趟过去的坎,大多已经有了非常 ...

  2. Java基础知识强化之IO流笔记15:递归之删除带内容的目录案例

    1. 需求:递归删除带内容的目录 分析:   (1)封装目录   (2)获取该目录下的所有文件或者文件夹的File数组   (3)遍历该File数组,得到每一个File对象   (4)判断该File对 ...

  3. Android 网络框架Volley的使用

    Volley简介 在平时的开发过程中,我们的应用几乎总是在和网络打交道, 在android下的网络编程一般都是基于Http协议的 ,常见的是HttpURLConnection和HttpClient 两 ...

  4. Android 设计模式之观察者模式(转载自:“http://blog.csdn.net/fangchongbory/article/details/7774044”)

    /* * 观察者模式 *      定义对象间的一种一个(Subject)对多(Observer)的依赖关系,当一个对象的状态发送改变时,所以依赖于它的 * 对象都得到通知并被自动更新 * * 当然, ...

  5. 简单html以及css的用法

    我将利用三天的时间来完成制作京东首页的静态页面效果,其中包含的内容有html以及css. 1.在开发进行之前,首先要配置开发环境:我们需要安装sublime  webstorm  vscode  Hb ...

  6. Node.js中url的详解

    var url = require('url');var str = 'http://zhufengnodejs:123@github.com:80/2016jsnode?name=zfpx& ...

  7. PHP 文件上传功能

    <?php /** * TestGuest Version1.0 * ================================================ * Web:2955089 ...

  8. Cognos开发报表如何隐藏列

    情景:当报表必须用到一列的存在,但是不需要显示该列的时候,我们就需要隐藏该列了,所有对象. 如何隐藏呢? 步骤1:选择要隐藏列的列标题和列正文两个部分 步骤2:分别找到左侧属性的条件样式,新建条件样式 ...

  9. eclipse中启动tomcat

    0.以下即使部署好,点小猫启动tomcat,有一个问题,修改jsp文件,本地tomcat中的此jsp并没有修改,如果右键项目启动,则会修改,不知道为什么 1. 首先发布项目,项目右键,run serv ...

  10. phpstudy apache 刚启动便停止

    1.添加站点 2.重启服务 3.遇见问题 apache 刚启动,1秒钟中后就停止 4.解决问题 发现是自己添加的网站中包含中文路径的问题,建议不要在自己的网站目录下包含中文.