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

正确的简单解法就是,加法,就让正数入栈,减法就让该数的相反数入栈,之后的操作也不会影响该数的正负,这样处理最简单。

单栈

#include <iostream>
#include <sstream>
#include <stack>
using namespace std; stack<double> stn; int main()
{
string line;
while (getline(cin,line)) {
while (!stn.empty()) {
stn.pop();
}
if (line=="0") {
break;
} stringstream ss(line);
long long x;
string oper; ss>>x;
stn.push((double)x);
while (ss>>oper>>x) {
//cout<<oper<<endl<<x<<endl;
if (oper=="*") {
double num=stn.top();
stn.pop();
stn.push(num*(double)x);
}
else if (oper=="/") {
double num=stn.top();
stn.pop();
stn.push(num/(double)x);
}
else if (oper=="-") {
stn.push((double)-x);
}
else {
stn.push((double)x);
} }
double sum=0;
while (!stn.empty()) {
sum+=stn.top();
stn.pop();
}
printf("%.2lf\n",sum);
}
return 0;
}

双栈

AC

#include <iostream>
#include <sstream>
#include <stack>
using namespace std; stack<string> sts;
stack<double> stn; int main()
{
string line;
while (getline(cin,line)) {
while (!stn.empty()) {
stn.pop();
}
while (!sts.empty()) {
sts.pop();
}
if (line=="0") {
break;
} stringstream ss(line);
long long x;
string oper; ss>>x;
double sum=0;
stn.push(x);
while (ss>>oper>>x) {
//cout<<oper<<endl<<x<<endl;
if (oper=="*") {
double num=stn.top();
stn.pop();
stn.push(num*(double)x);
}
else if (oper=="/") {
double num=stn.top();
stn.pop();
stn.push(num/(double)x);
}
else {
stn.push((double)x);
sts.push(oper);
} }
while (!sts.empty()) {
string op=sts.top();
sts.pop();
if (op=="+") {
sum+=stn.top();
}
else {
sum-=stn.top();
}
stn.pop();
}
sum+=stn.top();
printf("%.2lf\n",sum);
}
return 0;
}

WA D了好久…

#include <iostream>
#include <sstream>
#include <stack>
using namespace std; stack<string> sts;
stack<double> stn; int main()
{
string line;
while (getline(cin,line)) {
while (!stn.empty()) {
stn.pop();
}
while (!sts.empty()) {
sts.pop();
} stringstream ss(line);
long long x;
string oper; ss>>x;
if (x==0&&line.length()==1) {
break;
}
stn.push((double)x);
while (ss>>oper>>x) {
//cout<<oper<<endl<<x<<endl;
if (oper=="*") {
double num=stn.top();
stn.pop();
stn.push(num*(double)x);
}
else if (oper=="/") {
double num=stn.top();
stn.pop();
stn.push(num/(double)x);
}
else {
stn.push((double)x);
sts.push(oper);
} }
while (!sts.empty()) {
string op=sts.top();
sts.pop();
double num2=stn.top();
stn.pop();
double num1=stn.top();
stn.pop();
if (op=="+") {
stn.push(num1+num2);
}
else {
stn.push(num1-num2);
}
}
printf("%.2lf\n",stn.top());
}
return 0;
}

HDU 1237 简单计算器(栈+stringstream)的更多相关文章

  1. HDU 1237 简单计算器 栈

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

  2. hdu 1237 简单计算器

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1237 简单计算器 Description 读入一个只包含 +, -, *, / 的非负整数计算表达式, ...

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

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

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

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

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

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

  6. hiho #1332 : 简单计算器 栈+递归

    #1332 : 简单计算器 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 编写一个程序可以完成基本的带括号的四则运算.其中除法(/)是整除,并且在负数除法时向0取整.( ...

  7. hdoj 1237 简单计算器

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

  8. F - 简单计算器(栈)

    F - 简单计算器 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descripti ...

  9. HDU1237 简单计算器 栈

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1237 题目大意:读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. 题目分 ...

随机推荐

  1. js获取自定义data属性

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  2. 【16】LRUChache

    题目 LRU 思路 LRU 大家都不陌生,操作系统的作业做过,思路就是一旦添加或者访问某个元素,则将其的"访问属性"置零,而其他元素的访问属性统统减一,这样一来,访问属性最小的元素 ...

  3. 【Python】浮点数用科学计数法表示

  4. [CF276B] Little Girl and Game

    [CF276B] Description 给定字符串 \(S\) ,两人轮流,每次从字符串中任意取出一个字符并从原串中删去.如果某人某次操作前,串种剩余的字符集合经过排列可以得到回文串,那么这个人就胜 ...

  5. C语言二维数组指针与指针数组

    http://c.biancheng.net/view/2022.html http://c.biancheng.net/view/2020.html

  6. hdu1716 排列2

    12  21 123 132  213 231 321 312 .... 每次都将后面n-1位进行全排列.递归的出口当起始坐标等于终止坐标时.需要还原. 设计标记数组.因为需要从小到大输出. #def ...

  7. eclipse sql server 导出excel文件

    Jxl.jar 访问Excel的Jar包 注意:支持以.xls结尾的Excel文件,可能不支持.xlsx结尾的 下载地址: 程序所需要得包: 程序代码: package partice; import ...

  8. C# MVC扩展方法

    控制方法 : 在不修改类原来代码的情况下,给类扩展方法 https://www.cnblogs.com/jxsimon/articles/5043654.html https://www.cnblog ...

  9. HTML的内联框架(iframe)

    HTML的内联框架(iframe) 第一种:打开网页就是带内联框架的页面 可以实现在自己的网页内部,打开另一个网页 语法: <!--src:地址frameborder:0为无边框:1为有边框-- ...

  10. html 未选择复选框不上传

    问题 之前就遇到类似的问题,在一个列表中,如果有复选框,并且不选中 会导致这个复选框不上传,导致后台接收不到复选框数据 解决方法我想到的就是 <td> <input type=&qu ...