Fork me on GitHub

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

题目链接: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
****************************************************************/
复制代码

 

posted @   伊甸一点  阅读(202)  评论(0编辑  收藏  举报
编辑推荐:
· Brainfly: 用 C# 类型系统构建 Brainfuck 编译器
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
阅读排行:
· Tinyfox 发生重大改版
· DeepSeek 全面指南,95% 的人都不知道的9个技巧(建议收藏)
· 对比使用DeepSeek与文新一言,了解DeepSeek的关键技术论文
· Brainfly: 用 C# 类型系统构建 Brainfuck 编译器
· DeepSeekV3+Roo Code,智能编码好助手
历史上的今天:
2016-05-06 设计模式之简单工厂模式
点击右上角即可分享
微信分享提示