HDU 1237 简单计算器(栈+stringstream)
提供几份代码,这题的输入可以用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)的更多相关文章
- HDU 1237 简单计算器 栈
额,题目是中文的,题意就不用说了= =都看懂喽.写个字符串先把这行计算式存进去,不过不能存一个算一个,因为考虑到乘除法比加减法优先的原则,如果是加号减号就先存着等待计算,如果是乘号除号就直接算出来值就 ...
- hdu 1237 简单计算器
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1237 简单计算器 Description 读入一个只包含 +, -, *, / 的非负整数计算表达式, ...
- hdu 1237 简单计算器(栈处理)
简单计算器 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 1237 简单计算器 (表达式求值)【stack】
<题目链接> 题目大意: 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符, ...
- hdu-1237简单计算器(栈的运用)
http://acm.hdu.edu.cn/showproblem.php?pid=1237 简单的栈的运用. 首先将数字和运算符分离,分别保存在两个数组中,然后按原来的式子的顺序,首先将第一个数和第 ...
- hiho #1332 : 简单计算器 栈+递归
#1332 : 简单计算器 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 编写一个程序可以完成基本的带括号的四则运算.其中除法(/)是整除,并且在负数除法时向0取整.( ...
- hdoj 1237 简单计算器
简单计算器 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- F - 简单计算器(栈)
F - 简单计算器 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descripti ...
- HDU1237 简单计算器 栈
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1237 题目大意:读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. 题目分 ...
随机推荐
- 电脑进不去BIOS解决办法
把所有外设(主要是硬盘,包括装在主板上的固态硬盘)拆下来,拆下纽扣电池给主板放电,装回纽扣电池,重启F1进入BIOS. 最终查到原因,是固态那里出的问题,固态作为启动硬盘,被自己搞得有问题了,有两个启 ...
- EntityFramework之原始查询及性能优化
之前做海信项目,数据量自交大,为了提高查询效率用的 https://www.cnblogs.com/CreateMyself/p/4746258.html
- orcal时区
查询数据库时区 select dbtimezone from dual 修改时区 alter database set time_zone='+8:00';
- sql查询 —— 连接查询
-- 执行sql文件 test.sql -- 打开终端 -- cd sql文件所在路径 -- 进入mysql -- use 数据库 -- 执行 source test.sql; -- 自关联 -- 一 ...
- javascript增强typeof 对复杂类型的判断
js中有六种数据类型,包括五种基本数据类型(Number,String,Boolean,Undefined,Null),和一种复杂数据类型(Object). typeof 由于js中的变量是松散类型的 ...
- Codeforces Hello2020 A-E简要题解
contest链接:https://codeforces.com/contest/1284 A. New Year and Naming 思路:签到,字符串存一下,取余拼接. #include< ...
- protobuf-net简单使用
第一个测试的proto文件 syntax = "proto3"; package ProtoMsg; message Foo { ; int32 id = ; repeated b ...
- mysql数据库事务的操作与理解
--------------------事务----------------------------------------------查询mysql事务隔离级别1.查看当前会话隔离级别select ...
- Ninject 2.x细说---2.绑定和作用域
Ninject 2.x细说---2.绑定和作用域 转载weixin_33725272 最后发布于2011-11-06 00:03:00 阅读数 9 收藏 Ninject中提供多种接口和实现类的绑 ...
- AcWing 898. 数字三角形
//从上往下 #include <iostream> #include <algorithm> using namespace std; , INF = 1e9; int n; ...