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 题目大意:读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. 题目分 ...
随机推荐
- H5与native 普及
H5与native 普及: H5是基于web,native基于客户端native是使用原生系统内核的,相当于直接在系统上操作.,是我们传统意义上的软件,更加稳定.但是H5的APP先得调用系统的浏览器内 ...
- HDU2020——绝对值排序(java实现,使用map)
Question Description Input Output Sample Input Sample Output 解题思路简述: 在接收每一个数组的过程中,将负数及其绝对值以键值对的形式存入m ...
- Go_go build 和 go install
1.作用 go build:用于测试编译包,在项目目录下生成可执行文件(有main包). go install:主要用来生成库和工具.一是编译包文件(无main包),将编译后的包文件放到 pkg 目录 ...
- oneshot和周期性shot
计数器的使用,oneshot:时刻. 有误差,日.每一些间隔可以产生周期性shot(多个持续性时刻)
- C++-指针阅读能力提升
下面的标识符你肯定在工程中看不到,但是在面试题中却非常常见,掌握掌握还是不错的. int (*p1)(int*, int (*f)(int*)); int (*p2[5])( ...
- 计算a除b的第一位小数 in C++.
my codes: #include<iostream> #include<cstdio> using namespace std; int main() { int a,b; ...
- MySQL 触发器trigger
一.触发器概念 触发器(trigger):监视某种情况,并触发某种操作. 触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(af ...
- python3爬取高清壁纸(1)
这次爬取的目标是:美桌网首页 > 桌面壁纸 > 卡通动漫 类别下的壁纸. 我们先随机选取一个专辑来爬(http://www.win4000.com/wallpaper_detail_545 ...
- 查看ie版本
window10系统还好说,一般都是IE11版本,其他系统或服务器看=查看ie版本就很烦 方法一:按组合键 ALT+H 打开“帮助”,再按 A 选择“关于Internet Explorer” 方法二 ...
- 在网页中插入背景音乐代码(html)
有两种 分别用<bgsound>和<embed></embed>标签,当用<embed>插入背景音乐时可以设置宽度和高度为0,隐藏播放器. 二者的参数如 ...