题目1019:简单计算器(栈的使用)

题目链接:http://ac.jobdu.com/problem.php?pid=1019

 

题目具体分析见:https://github.com/zpfbuaa/JobduInCPlusPlus

 

参考代码:

//
//  1019 简单计算器.cpp
//  oj
//
//  Created by PengFei_Zheng on 04/04/2017.
//  Copyright © 2017 PengFei_Zheng. All rights reserved.
//
 
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stack>
 
#define run ;
//#define debug ;
 
using namespace std;
int num1;
double temp;
 
stack<double> myStack;
 
int main(){
 
#ifdef debug
    while(scanf("%d ",&num1)&&num1!=0){
        cout<<num1<<" ";
        char a,b;
        int num;
        while(scanf("%c %d%c",&a,&num,&b)!=EOF){
            if(b!=' '){
                cout<<a<<" "<<num<<endl;
                break;
            }
            else{
                cout<<a<<" "<<num<<b;
            }
        }
    }
#endif
     
#ifdef run
    char space0;
    while(scanf("%d%c",&num1,&space0)&&num1!=0&&space0==' '){//ignore the space and using space0!=' ' and num1==0 the jump out of loop
        while(!myStack.empty()){
            myStack.pop();
        }
        myStack.push(num1);
//        cout<<"myStack push: "<<num1<<endl;
        char op,space1,space2;
        int num2;
         
        while(scanf("%c%c%d%c",&op,&space1,&num2,&space2)!=EOF){//ignore the space and using space2 to jump out of this calculation
            if(op=='+'){
//                cout<<"myStack push: "<<num2<<endl;
                myStack.push(num2);
            }
            else if(op=='-'){
//                cout<<"myStack push: "<<0-num2<<endl;
                myStack.push(0-num2);
            }
            else if(op=='*'){
                temp = myStack.top();
                myStack.pop();
//                cout<<"myStack push: "<<temp*num2<<endl;
                myStack.push(temp*num2);
            }
            else if(op=='/'){
                temp = myStack.top();
                myStack.pop();
//                cout<<"myStack push: "<<temp/num2<<endl;
                myStack.push(temp/num2);
            }
            if(space2!=' ')//jump out of loop
                break;
        }
        while(!myStack.empty()){
            if(myStack.size()==1){
                printf("%.2lf\n",myStack.top());
                break;
            }
            double x = myStack.top();
            myStack.pop();
            double y = myStack.top();
            myStack.pop();
            myStack.push(x+y);
        }
    }
#endif
     
}
/**************************************************************
    Problem: 1019
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1524 kb
****************************************************************/

 

转载于:https://www.cnblogs.com/zpfbuaa/p/6680719.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值