当前位置:网站首页>HDU_p1237_简单计算器_stack
HDU_p1237_简单计算器_stack
2022-07-06 02:06:00 【这题AC再睡.】
Problem Description
读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
Input
测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
Output
对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
Sample Input
1 + 2 4 + 2 * 5 - 7 / 11 0
Sample Output
3.00 13.36
Source
//
// #include<bits/stdc++.h>
#include<iostream>
#include<stack>
using namespace std;
int main()
{
char ch;
double ans,x;
while( cin>>x )
{
ch=getchar();
if( !x && ch=='\n' ) break;
ans=0;
stack<double> sk;
sk.push( x );
while( cin>>ch>>x )
{
switch( ch )
{
case '+': sk.push(x); break;
case '-': sk.push(-x); break;
case '*': sk.top()*=x; break;
case '/': sk.top()/=x; break;
}
if( getchar()=='\n' ) break;
}
while( !sk.empty() ) { ans+=sk.top(); sk.pop(); }
printf("%.2lf\n",ans );
}
}
边栏推荐
- Spark accumulator
- leetcode-2.回文判断
- leetcode3、实现 strStr()
- VIM usage guide
- 论文笔记: 图神经网络 GAT
- I like Takeshi Kitano's words very much: although it's hard, I will still choose that kind of hot life
- NiO related knowledge (II)
- How to upgrade kubernetes in place
- Unreal browser plug-in
- Computer graduation design PHP enterprise staff training management system
猜你喜欢
Redis list
leetcode3、实现 strStr()
Open source | Ctrip ticket BDD UI testing framework flybirds
Multi function event recorder of the 5th National Games of the Blue Bridge Cup
[depth first search notes] Abstract DFS
Leetcode3. Implement strstr()
RDD conversion operator of spark
Selenium waiting mode
Jisuanke - t2063_ Missile interception
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
随机推荐
机器学习训练与参数优化的一般过程 (讨论)
Concept of storage engine
Initialize MySQL database when docker container starts
Spark accumulator
Bidding promotion process
D22:indeterminate equation (indefinite equation, translation + problem solution)
Sword finger offer 12 Path in matrix
Derivation of Biot Savart law in College Physics
[network attack and defense training exercises]
Formatting occurs twice when vs code is saved
MySQL index
2022年版图解网络PDF
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
更改对象属性的方法
2022 PMP project management examination agile knowledge points (8)
Executing two identical SQL statements in the same sqlsession will result in different total numbers
【coppeliasim】6自由度路径规划
1. Introduction to basic functions of power query
SQL statement
Thinking about the best practice of dynamics 365 development collaboration