L2-033 简单计算器 (25 分) 纯模拟

本题要求你为初学数据结构的小伙伴设计一款简单的利用堆栈执行的计算器。如上图所示,计算器由两个堆栈组成,一个堆栈 S
1

存放数字,另一个堆栈 S
2

存放运算符。计算器的最下方有一个等号键,每次按下这个键,计算器就执行以下操作:

从 S
1

中弹出两个数字,顺序为 n
1

和 n
2


从 S
2

中弹出一个运算符 op;
执行计算 n
2

op n
1


将得到的结果压回 S
1


直到两个堆栈都为空时,计算结束,最后的结果将显示在屏幕上。

输入格式:
输入首先在第一行给出正整数 N(1<N≤10
3
),为 S
1

中数字的个数。

第二行给出 N 个绝对值不超过 100 的整数;第三行给出 N−1 个运算符 —— 这里仅考虑 +、-、*、/ 这四种运算。一行中的数字和符号都以空格分隔。

输出格式:
将输入的数字和运算符按给定顺序分别压入堆栈 S
1

和 S
2

,将执行计算的最后结果输出。注意所有的计算都只取结果的整数部分。题目保证计算的中间和最后结果的绝对值都不超过 10
9

如果执行除法时出现分母为零的非法操作,则在一行中输出:ERROR: X/0,其中 X 是当时的分子。然后结束程序。

输入样例 1:
5
40 5 8 3 2
/ * - +
输出样例 1:
2
输入样例 2:
5
2 5 8 4 4

  • / - +
    输出样例 2:
    ERROR: 5/0
#include<bits/stdc++.h>
using namespace std;
stack<int> s1;
stack<char> s2;
int main(){
	int n;cin>>n;
	for(int i=0;i<n;++i){
		int x;cin>>x;s1.push(x);
	}
	for(int i=0;i<n-1;++i){
		char x;cin>>x;s2.push(x);
	}
	while(s2.size()){
		char op=s2.top();s2.pop();
		int y=s1.top();s1.pop();
		int x=s1.top();s1.pop();
		if(op=='+')s1.push(x+y);
		if(op=='-')s1.push(x-y);
		if(op=='*')s1.push(x*y);
		if(op=='/'){
			if(y==0){
				printf("ERROR: %d/0",x);return 0;
			}
			s1.push(x/y);
		}
	}
	cout<<s1.top();
	return 0;
}
#include<bits/stdc++.h>

using namespace std;

int n,x,a,b;
char c;
stack<int> s1;
stack<char> s2;

int main(){
    cin>>n;
    for(int i=0;i<n;++i)
    {
    	cin>>x;
    	s1.push(x);
    }
    for(int i=0;i<n-1;++i)
    {
    	cin>>c;
    	s2.push(c);
    }
    
    while(1)
    {
    	a=s1.top();s1.pop();
    	b=s1.top();s1.pop();
    	c=s2.top();s2.pop();
    	int ans;
    	if(c=='+')
    	{
    		ans=a+b;
    	}
    	if(c=='-')
    	{
    		ans=b-a;
    	}
    	if(c=='*')
    	{
    		ans=b*a;
    	}
    	if(c=='/')
    	{
    		if(a==0)
    		{
    			printf("ERROR: %d/0",b);
    			return 0;
    		}
    		ans=b/a;
    	}
    	if(s1.size()==0&&s2.size()==0)
    	{
    		cout<<ans;
    		return 0;
    	}
    	s1.push(ans);
    }
    return 0;
}
posted @ 2022-11-17 23:03  林动  阅读(20)  评论(0编辑  收藏  举报