首页 > 其他 > 详细

题目1101:计算表达式(栈的使用)

时间:2017-05-06 23:41:18      阅读:317      评论:0      收藏:0      [点我收藏+]

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

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

参考代码:

//
//  1101 计算表达式.cpp
//  Jobdu
//
//  Created by PengFei_Zheng on 06/05/2017.
//  Copyright © 2017 PengFei_Zheng. All rights reserved.
//
 
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <cmath>
#include <climits>
#include <stack>
 
 
using namespace std;
 
stack<int> myStack;
 
int main(){
    int firstNum;
    while(scanf("%d",&firstNum)!=EOF){
        while(!myStack.empty()) {
            myStack.pop();
        }
        myStack.push(firstNum);
        char op;
        while(scanf("%c",&op)!=EOF && op!=\n){
            int nextNum;
            scanf("%d",&nextNum);
            switch(op){
                case +:{
                    myStack.push(nextNum);
                    break;
                }
                case -:{
                    myStack.push(0-nextNum);
                    break;
                }
                case *:{
                    int tmp1 = myStack.top();
                    myStack.pop();
                    myStack.push(tmp1*nextNum);
                    break;
                }
                case /:{
                    int tmp2 = myStack.top();
                    myStack.pop();
                    myStack.push(tmp2/nextNum);
                    break;
                }
                default:
                    break;
            }
        }
         
        int ans = 0;
        while(!myStack.empty()){
            int tmp = myStack.top();
            ans += tmp;
            myStack.pop();
        }
        printf("%d\n",ans);
    }
    return 0;
}
/**************************************************************
    Problem: 1101
    User: zpfbuaa
    Language: C++
    Result: Pending
****************************************************************/

 

题目1101:计算表达式(栈的使用)

原文:http://www.cnblogs.com/zpfbuaa/p/6819096.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!