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 ...
随机推荐
- initialSize,maxTotal,maxIdle,minIdle,maxWaitMillis
初始化连接数:默认值 0 同一时刻可分配最大连接数:默认值 8 ,设置为负数时不做限制 最大空闲连接,默认值 8 ,超出连接将被释放 最小空闲连接数,默认值 0 请求连接最大等待时间(毫秒),默认值 ...
- apache与IIS端口冲突修改和需要使用 SSL 查看该资源”错误
改变Apache端口等配置修改方法 www.educity.cn 发布者:jsb200421 来源:网络转载 发布日期:2014年01月02日 如何改变Apache端口:找到Apache安装目录下co ...
- 前端MVC学习总结(三)——AngularJS服务、路由、内置API、jQueryLite
一.服务 AngularJS功能最基本的组件之一是服务(Service).服务为你的应用提供基于任务的功能.服务可以被视为重复使用的执行一个或多个相关任务的代码块. AngularJS服务是单例对象, ...
- (《数论及应用1.3》NEFU 116 两仪剑法(最小公倍数&&最大公约数))
#include <iostream> using namespace std; long long gcd(long long a, long long b){ if(b == 0){ ...
- Unity3d:加载Format是RGB24位的图片失败(加载图片显示问号)
问题描述:加载图片显示是个红色的问号,调试发现,Texture的Format=RGB24的都加载失败,ARGB32位的都能成功,按照常规,首先去度娘,看是否有人遇到和我同样的问题,结果一无所获.将用N ...
- erlang自定义数据类型
Erlang系统自带的基础数据类型有:atom.tuple.list.binary.pid.float.number.port.reference.record. 用户可以通过通过命令type来自定义 ...
- js 解决原型问题的方案 : 构造器和原型的组合
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- PHP抓取豆瓣读书爬虫代码
<?php//演示地址 http://asizu.sinaapp.com/reptile_douban.php//数据量不是特别大,没有写抓完数据便停止. 喜欢的朋友拿去自己改改就好了 head ...
- IDA Script: Remove empty auto labels
http://simeonpilgrim.com/blog/2010/03/25/ida-script-remove-empty-auto-labels/ #include <idc.idc&g ...
- Java常见排序算法之快速排序
在学习算法的过程中,我们难免会接触很多和排序相关的算法.总而言之,对于任何编程人员来说,基本的排序算法是必须要掌握的. 从今天开始,我们将要进行基本的排序算法的讲解.Are you ready?Let ...