题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1237

简单计算器

Description

读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。

Input

测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。

Output

对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。

Sample Input

1 + 2
4 + 2 * 5 - 7 / 11
0

Sample Output

3.00
13.36

表达式计算,把前缀表达式变成后缀表达式再计算。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<stack>
#include<set>
using std::cin;
using std::sort;
using std::pair;
using std::stack;
using std::string;
using std::getline;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 5010;
const int INF = 0x3f3f3f3f;
typedef unsigned long long ull;
class ExpCalc {
private:
string ret;
stack<char> op;
stack<double> num;
inline void erase() {
ret = "";
while (!op.empty()) op.pop();
while (!num.empty()) num.pop();
}
inline bool is_digit(char ch) {
return ch >= '0' && ch <= '9';
}
inline bool get_op(char ch) {
char buf[10] = "+-*/";
for (int i = 0; i < 4; i++) {
if (buf[i] == ch) return true;
}
return false;
}
inline int cmp(char ch) {
switch (ch) {
case '+':
case '-': return 1;
case '*':
case '/': return 2;
}
return 0;
}
inline double calc(double d1, double d2, char ch) {
double val = 0.0;
switch (ch) {
case '+': val = d1 + d2;
break;
case '-': val = d2 - d1;
break;
case '*': val = d1 * d2;
break;
case '/': val = d2 / d1;
break;
}
return val;
}
public:
ExpCalc() { erase(); }
~ExpCalc() { erase(); }
inline void InfixToPostfix(const string src) {
int n = src.length();
for (int i = 0; i < n;) {
if (' ' == src[i] || '=' == src[i]) { i++; continue; }
if ('(' == src[i]) op.push(src[i++]);
if (')' == src[i]) {
while (op.top() != '(') {
ret += op.top(); ret += ' ';
op.pop();
}
op.pop(); i++;
} else if (get_op(src[i])) {
while (!op.empty() && cmp(op.top()) >= cmp(src[i])) {
ret += op.top(); ret += ' ';
op.pop();
}
op.push(src[i++]);
} else {
while (is_digit(src[i]) || '.' == src[i]) {
ret += src[i++];
}
ret += ' ';
}
}
while (!op.empty()) {
ret += op.top(); ret += ' ';
op.pop();
}
ret += '=';
}
inline double PostfixCalc() {
int n = ret.length();
for (int i = 0; i < n;) {
if (' ' == ret[i] || '=' == ret[i]) { i++; continue; }
if (get_op(ret[i])) {
double d1 = num.top(); num.pop();
double d2 = num.top(); num.pop();
num.push(calc(d1, d2, ret[i++]));
} else if (is_digit(ret[i]) || '.' == ret[i]) {
double x = 0.0;
while (is_digit(ret[i])) {
x = x * 10 + ret[i++] - '0';
}
if ('.' == ret[i]) {
i++;
double y = 0.0, k = 10.0;
while (is_digit(ret[i])) {
y += (ret[i++] - '0') / k;
k *= 10;
}
x += y;
}
num.push(x);
}
}
return num.top();
}
};
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
string src;
while (getline(cin, src) && src != "0") {
ExpCalc work;
work.InfixToPostfix(src);
printf("%.2lf\n", work.PostfixCalc());
}
return 0;
}

hdu 1237 简单计算器的更多相关文章

  1. hdu 1237 简单计算器(栈处理)

    简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  2. hdu 1237 简单计算器 (表达式求值)【stack】

    <题目链接> 题目大意: 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值.  Input测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符, ...

  3. HDU 1237 简单计算器(栈+stringstream)

    提供几份代码,这题的输入可以用stringsteam处理,先处理乘除后处理加减,正常思路,但是后面统计加减法的时候,对栈的运用错了,我用的时候相当于给它多加了几个括号就错了. 正确的简单解法就是,加法 ...

  4. HDU 1237 简单计算器 栈

    额,题目是中文的,题意就不用说了= =都看懂喽.写个字符串先把这行计算式存进去,不过不能存一个算一个,因为考虑到乘除法比加减法优先的原则,如果是加号减号就先存着等待计算,如果是乘号除号就直接算出来值就 ...

  5. hdoj 1237 简单计算器

    简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  6. HDOJ 1237题 简单计算器

    简单计算器 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

  7. hdu-1237简单计算器(栈的运用)

    http://acm.hdu.edu.cn/showproblem.php?pid=1237 简单的栈的运用. 首先将数字和运算符分离,分别保存在两个数组中,然后按原来的式子的顺序,首先将第一个数和第 ...

  8. 1.C#WinForm基础制作简单计算器

    利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号 MessageBox.S ...

  9. *HDU 1237 栈

    简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

随机推荐

  1. How to deploy JAVA Application on Azure Service Fabric

    At this moment, Azure Service Fabric does not support JAVA application natively (but it's on the sup ...

  2. APP发布Xcode7

    一.准备工作 1>准备3.5寸.4寸.4.7寸.5.5寸的程序截图至少个1张,如果支持iPad,那么iPad截图也要有.这些截图尽量截取页面漂亮的,因为这些截图是要放在AppStore中展示的. ...

  3. SVN与TortoiseSVN实战:补丁详解

    硬广:<SVN与TortoiseSVN实战>系列已经写了五篇,第二篇<SVN与TortoiseSVN实战:标签与分支>和第三篇<SVN与TortoiseSVN实战:Tor ...

  4. HiveSQL解析过程详解 | 学步园

    HiveSQL解析过程详解 | 学步园   http://www.xuebuyuan.com/2210261.html

  5. Oracle笔记 十四、查询XML操作、操作系统文件

    --1.随机数 select dbms_random.value from dual; select mod(dbms_random.random, 10) from dual; --0-9随机数 s ...

  6. 如果公司里有上百个表要做触发器,如果手动写代码的话。很累,所以今天写了一个小程序,自动生成mysql的触发代码。

    <?php $dbname = 'test';//数据库 $tab1 = 'user'; //执行的表 $tab2 = 'user_bak'; //被触发的表 $conn = mysql_con ...

  7. setjmp 与 longjmp

    setjmp和longjmp是C语言独有的,只有将它们结合起来使用,才能达到程序控制流有效转移的目的,按照程序员的预先设计的意图,去实现对程序中可能出现的异常进行集中处理. 先来看一下这两个函数的定义 ...

  8. Let's see if we could reocver Line 5.3 and above deleted chat messages or not

    Forensic is a strict science and we should let the evidence speak for itself. Several months ago I s ...

  9. leetcode 13

    罗马数字是阿拉伯数字传入之前使用的一种数码.罗马数字采用七个罗马字母作数字.即Ⅰ(1).X(10).C(100).M(1000).V(5).L(50).D(500). 记数的方法: 相同的数字连写,所 ...

  10. 根据之前的博文,我把给同学做的三子棋小游戏的代码发出来,只是界面很丑很丑,AI算法很笨很笨,过几天我传到网盘上,提供大家下载娱乐

    background_image_filename = 'blackground.png' black_mouse_image_filename = 'black.png' white_mouse_i ...