uva 327 Evaluating Simple C Expressions 简易C表达式计算 stl模拟
由于没有括号,只有+,-,++,--,优先级简单,所以处理起来很简单。
题目要求计算表达式的值以及涉及到的变量的值。
我这题使用stl的string进行实现,随便进行练手,用string的erase删掉全部空格,然后对++、--进行处理然后删去,最后就只剩简单式子了,简单循环下就出来了。
这里有几个坑点:
1.erase函数删除字符后,后面字符的下标都会发生变化,刚开始使用i++去检查空格,结果删除后会跳掉字符。
2.++、--的后缀处理要注意,我开了两个数组放后缀运算的。
3.输出的变量值是当前后自增后的数。
唉,我发现我每次写stl,代码都很乱哪,果然缺欠练习呢。
代码:
#include <iostream>
#include <string>
using namespace std; int v[27], used[27];
int p[27], m[27]; //record the i++ i-- int main() {
string str;
while (getline(cin, str) != NULL) {
cout << "Expression: " << str << endl;
int value = 0;
for (int i = 0; i < 26; i++)
v[i] = i + 1, used[i] = p[i] = m[i] = 0;
for (int i = 0; i < str.size();)
if (str[i] == ' ')
str.erase(i, 1);
else
i++;
size_t f1, f2;
while (1) {
f1 = str.find("++");
if (f1 != string::npos) {
if (str[f1 - 1] >= 'a' && str[f1 - 1] <= 'z')
p[str[f1 -1] - 'a']++;
else
v[str[f1 + 2] - 'a']++;
str.erase(f1, 2);
}
// cout << str << endl;
f2 = str.find("--");
if (f2 != string::npos) {
if (str[f2 - 1] >= 'a' && str[f2 - 1] <= 'z')
m[str[f2 -1] - 'a']--;
else
v[str[f2 + 2] - 'a']--;
str.erase(f2, 2);
}
if (f1 == string::npos && f2 == string::npos)
break;
// cout << str << endl;
}//while
// cout << value << endl;
value += v[str[0] - 'a'];
used[str[0] - 'a'] = 1;
for (int i = 1; i < str.size(); i++) {
if (str[i++] == '+')
value += v[str[i] - 'a'], used[str[i] - 'a'] = 1;
else
value -= v[str[i] - 'a'], used[str[i] - 'a'] = 1;
// cout << str[i-1] << str[i]<<' ' << value << endl;
}//for
cout << " value = " << value << endl;
for (int i = 0; i < 26; i++)
if (used[i])
cout << " " << char('a' + i) << " = "<< v[i] + p[i] + m[i] << endl;
}//while
return 0;
}
uva 327 Evaluating Simple C Expressions 简易C表达式计算 stl模拟的更多相关文章
- UVA 327 -Evaluating Simple C Expressions(栈)
Evaluating Simple C Expressions The task in this problem is to evaluate a sequence of simple C expre ...
- uva 327 - Evaluating Simple C Expressions
Evaluating Simple C Expressions The task in this problem is to evaluate a sequence of simple C exp ...
- uva 1567 - A simple stone game(K倍动态减法游戏)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=4342">题目链接:uva 1567 - ...
- Simplifying Conditional Expressions(简化条件表达式)
1.Decompose Conditional(分解条件表达式) 2.Consolidate Conditional Expressions(合并条件表达式) 3.Consolidate Duplic ...
- UVA - 11954 Very Simple Calculator 【模拟】
题意 模拟二进制数字的位运算 思路 手写 位运算函数 要注意几个坑点 一元运算符的优先级 大于 二元 一元运算符 运算的时候 要取消前导0 二元运算符 运算的时候 要将两个数字 数位补齐 输出的时候 ...
- UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据
题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...
- OAuth2简易实战(二)-模拟客户端调用
1. OAuth2简易实战(二) 1.1. 目标 模拟客户端获取第三方授权,并调用第三方接口 1.2. 代码 1.2.1. 核心流程 逻辑就是从数据库读取用户信息,封装成UserDetails对象,该 ...
- Boolean Expressions POJ - 2106 (表达式求值)
The objective of the program you are going to produce is to evaluate boolean expressions as the one ...
- 【STL+模拟】UVa 506 - System Dependencies
System Dependencies Components of computer systems often have dependencies--other components that m ...
随机推荐
- 根据路径获得文件名以及Aspose.Cells.dll操作excel 以及使用iTextSharp.text.pdf.PdfReader.dll对PDF的操作
string result = Regex.Match(str,@"[^\\]+$").Value;//正则表达式 this.listBox1.Items.Add(Path.Get ...
- Notes on Probabilistic Latent Semantic Analysis (PLSA)
转自:http://www.hongliangjie.com/2010/01/04/notes-on-probabilistic-latent-semantic-analysis-plsa/ I hi ...
- Unity3d:编辑器中运行正常,发布后的exe提示找不到文件
解决方案1:查看文件路径拼写方式,如果是用“+”拼接的,请改用System.IO.Path.Combine()方式拼接.经过测试,两种拼接方式打印出来的路径是一样的,但为什么 加号 的方式拼接unit ...
- Custom ReadOnlyProperty【PluraSight】
Limited functionality: Not settable No data binding No validation No animation No Inheritance When t ...
- Android ImageView scaleType属性
scaleType属性 文章来源:http://blog.csdn.net/xilibi2003/article/details/6628668 使用ImageView时经常会用到scaleType属 ...
- windows apache vhost 403 error
<Directory D:\workspace\ecshop> Options FollowSymLinks AllowOverride None Order deny,allow all ...
- C# 绘制统计图(柱状图, 折线图, 扇形图)
统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的:这里我们用 ...
- 可配置多功能门 SN74LVC1G57, 1G58, 1G97, 1G98, 1G99
Configurable Multiple-Function Gate SN74LVC1G57 SN74LVC1G58 SN74LVC1G97 SN74LVC1G98 SN74LVC1G99
- 会吓人的概念证明病毒: Chameleon
近期有这么一条新闻指出,有一对家长发现,黑客入侵了他们为10个月女儿所准备的婴儿监视器(baby monitor).该黑客除了远程操控该监视器的录像角度,还大声对着小孩喊叫.婴儿的爸爸冲进女儿房间后, ...
- SAP MRP的计算步骤
SAP MRP的计算步骤,物料需求计划(简称为MRP)与主生产计划一样属于ERP计划管理体系,它主要解决企业生产中的物料需求与供给之间的关系,即无论是对独立需求的物料,还是相关需求的物料, ...