HDOJ 1237 简单计算器(栈)

简单计算器

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


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
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1230  1235  1236  1233  1232
思路:利用两个栈,一个存double数字,一个存char运算符,比如 4 + 2 * 5 - 7 / 11 ,遇到空格略过,遇到字符型数字转换成double数字并存栈,遇到运算符:  1. 要运算并清空两栈的情况:是+或-(运算并清空) &&&&&是*或/,并且其前的运算符栈内符号为*或/,按优先级来说就应该先计算前一个*或/(运算)。  2.运算符先存入栈的情况:是 *或 /,并且其前的运算符栈内符号不为*或 /。别忘了最后的最后要清空栈。
清楚思想请看:http://blog.csdn.net/jinjide_ajin/article/details/47108889
AC代码,有一些很需要注意的地方都标记上了:


#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
stack <double> num;//数字栈
stack <char> sta;//字符栈

double math(char c){//变量为识别的运算符
    double a,b;//结果
    a=num.top(); num.pop();
    b=num.top(); num.pop();
    switch(c){
        case '*': return b*a;
        case '/': return b/a;
        case '+': return b+a;
        case '-': return b-a;
    }//注意这里 a,b 顺序不能反
}//只进行一次运算

int main(){
    char oper,str[210];
    int len,i;
    double nnum;
    memset(str,0,sizeof(str));
    while(gets(str)&&strcmp(str,"0")!=0){
        len=strlen(str);
        /********************** for *******************/
        for(i=0;i<len;i++){
            while(str[i]==' ') i++;  //略过空格

            /************下面为转换数字**********/
            nnum=0;
            while(str[i]>='0'&&str[i]<='9'&&i<len){
                nnum=nnum*10+str[i]-'0';
                i++;
            } //将数字由char转换为double
            num.push(nnum); //数字进栈

            /************下面为略过空格**********/
            while(str[i]==' ') i++;//略过空格

            /************下面为判断操作符**********/
            if(str[i]=='+'||str[i]=='-'){
                while(!sta.empty()){
                    num.push(math(sta.top()));
                    sta.pop();
                }
                sta.push(str[i]);
            }//运算符为‘+’,‘-’就清栈

            else if(str[i]=='*'||str[i]=='/'){
                if(!sta.empty()&&(sta.top()=='*'||sta.top()=='/')){
                    num.push(math(sta.top()));
                    sta.pop();
                }
                sta.push(str[i]);
            }//运算符为‘*’,‘/’时,先判断前方是否是‘*’,‘/’,若是就按运算顺序算前一个运算符,否者此运算符进栈
        }
        /*********************** for *************************/
        while(!sta.empty()){
                nnum=math(sta.top());
                sta.pop();
                num.push(nnum);
            }//清空剩余栈
            printf("%.2lf\n",num.top());
            num.pop();
            memset(str,0,sizeof(str));
    }
    return 0;
}

转载请注明出处:http://blog.csdn.net/jinjide_ajin/article/details/47186649

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值