zoukankan      html  css  js  c++  java
  • 简单计算器(栈)


     

     

    简单计算器

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

    Problem Description
    读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
     
    Input
    测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
     
    Output
    对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
     
    Sample Input
    1 + 2 4 + 2 * 5 - 7 / 11 0
     
    Sample Output
    3.00 13.36
     

    题解:不好搞:::

    代码:

     1 #include<stdio.h>
     2 #include<string.h>
     3 double m[201];
     4 int top;
     5 void push(double x){
     6     m[++top]=x;
     7 }
     8 bool pop(){
     9     if(top==-1)return false;
    10     top--;
    11     return true;
    12 }
    13 int main(){char sign;double temp,flot,sum;
    14 while(1){top=-1;sum=0;scanf("%lf",&flot);push(flot);
    15     while(getchar()!='
    '){flot=1;
    16         scanf("%c%lf",&sign,&temp);
    17         switch(sign){
    18             case '+':push(temp);break;
    19             case '-':temp=-temp;push(temp);break;
    20             case '*':m[top]*=temp;break;
    21             case '/':m[top]/=temp;break;
    22         }
    23     }
    24     if(flot==0)break;
    25     while(1){
    26         sum+=m[top];
    27         if(!pop())break;
    28     }
    29     printf("%.2lf
    ",sum);
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    堆和栈究竟有什么区别?
    堆和栈的区别
    POJ 1528问题描述
    Facial Detection and Recognition with opencv on ios
    10个免费学习编程的好地方
    目标检测的图像特征提取之(一)HOG特征
    行人检测综述
    Introduction to Face Detection and Face Recognition
    opencv hog+svm行人检测
    苹果检测
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4685886.html
Copyright © 2011-2022 走看看