杭电1237--简单计算器 (getchar() + 栈)

简单计算器

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14590    Accepted Submission(s): 4957

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

浙大计算机研究生复试上机考试-2006年

//getchar(); 较难理解;定义一个栈;存储处理后的数据;最后把栈中所有元素相加;

#include <stdio.h>
#include<stack>
using namespace std;
int main()
{
    double m,n; char ch;
    while(~scanf("%lf",&m))
    {
        if(getchar() == ‘\n‘ && m == 0 )
        break ;
        stack <double> s;
        s.push(m) ;
        scanf("%c",&ch);          //执行一次;
        while(~scanf("%lf",&m))
        {
            if(ch == ‘*‘)
            {
                n=s.top() ;
                n*=m ;
                s.pop() ;
                s.push(n) ;
            }

            if( ch == ‘/‘)
            {
                n=s.top() ;
                n/=m ;
                s.pop() ;
                s.push(n) ;
            }

            if(ch == ‘+‘)
            s.push(m) ;

            if(ch == ‘-‘)
            s.push(-m) ;
            if(getchar() == ‘\n‘)
            break;
            //ch=getchar();     //gethchar() ; 相当于输入一个人字符;
            scanf("%c",&ch);
        }
        double total =0;
        while(!s.empty())
        {
            total+=s.top() ;
            s.pop() ;
        }
        printf("%.2lf\n",total);
    }
    return 0;
}
时间: 2024-08-26 23:38:32

杭电1237--简单计算器 (getchar() + 栈)的相关文章

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

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26553    Accepted Submission(s): 9626 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整

杭电ACM1237——简单计算器

简单的表达式求值,A了好久,坑. AC的代码: #include <iostream> #include <cstdio> #include <cstring> #include <stack> using namespace std; int main() { char str, c; double a, b; stack <double> num; while(scanf("%lf", &a) != EOF) {

HDU 1237 简单计算器 (栈 )

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<stack> using namespace std; char str[300]; double ans; double chcd(char x) { return (x-'0')*1.0; } double ch(int l,int r) { //printf("%d

HDU1237 简单计算器 【栈】+【逆波兰式】

版本:1.0 日期:2014.5.17 2014.6.1 版权:© 2014 kince 转载注明出处 在介绍SwitchButton之前,先来看一下系统Button是如何实现的.源码如下: @RemoteView public class Button extends TextView { public Button(Context context) { this(context, null); } public Button(Context context, AttributeSet att

简单计算器(杭电1237)(栈的运用)

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12594    Accepted Submission(s): 4139 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,

HDU 1237 简单计算器(后缀式+栈)

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16351    Accepted Submission(s): 5604 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,

HDU 1237:简单计算器【栈】

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6429    Accepted Submission(s): 2044 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整

hdoj 1237 简单计算器

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14512    Accepted Submission(s): 4920 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整

HDOJ 1237 简单计算器(堆栈)

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14833    Accepted Submission(s): 5050 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,