题目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 ****************************************************************/
作者: 伊甸一点
出处: http://www.cnblogs.com/zpfbuaa/
本文版权归作者伊甸一点所有,欢迎转载和商用(须保留此段声明),且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文链接 如有问题, 可邮件(zpflyfe@163.com)咨询.