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 题目大意:读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. 题目分 ...
随机推荐
- 整理收集的一些常用java工具类
1.json转换工具 package com.taotao.utils; import java.util.List; import com.fasterxml.jackson.core.JsonPr ...
- Python_内置函数和匿名函数
楔子 在讲新知识之前,我们先来复习复习函数的基础知识. 问:函数怎么调用? 函数名() 如果你们这么说...那你们就对了!好了记住这个事儿别给忘记了,咱们继续谈下一话题... 来你们在自己的环境里打印 ...
- MySql -- not null 非空约束
2.not null 非空约束 用于确保当前列的值不为空:在创建表时,如果不指定是否可以为空,字段默认可以为NULL. -- 这是上一篇默认约束创建的表 CREATE TABLE `test`.`us ...
- 解决Creating Server TCP listening socket 54.179.160.162:7001: bind: Cannot assign requested address
背景:之前在测试环境搭过一个redis集群,运维把服务器重启之后我重新开启redis集群始终起不来,但是有没有任何日志,经过如下步骤最终解决问题 1.修改日志路径,根据日志查看为什么会启动失败[前期操 ...
- 生产环境实践:Cana实现MySQL到ES实时同步
注:由于文章篇幅有限,完整文档可扫下面二维码免费获取,更有深受好评的大数据实战精英+架构师好课等着你. 速点链接加入高手战队:http://www.dajiangtai.com/course/112. ...
- jdk8-》日期时间及其格式处理类特性
一.JDK8之时间⽇期处理类 核⼼类: LocalDate:不包含具体时间的⽇期. LocalTime:不含⽇期的时间. LocalDateTime:包含了⽇期及时间. LocalDate 常⽤API ...
- AcWing 285. 没有上司的舞会
//f[u][0]是所有以u为根的子树中选择,并且不选u这个点的方案 //f[u][1]是所有以u为根的子树中选择,并且 选u这个点的方案 #include <cstring> #incl ...
- 用python实现密码校验程序
密码需要符合下面的要求: 8个字符以上,包含数字,大小写,开头不能为特殊字符. #! /usr/bin/pythonimport re password = str(input()) def lenO ...
- python 多线程,多进程,高效爬虫
1.多线程from concurrent.futures import ThreadPoolExecutor import requests def fetch_async(url): respons ...
- PyQt5遇到的一个坑 "ImportError: unable to find Qt5Core.dll on PATH" 及解决办法
最近再实现一个功能,主要是将自动化测试界面化 环境组合为:Windows 64bit + PyCharm + Python + PyQt5 + Pyinstaller + Inno Setup PS ...