栈的应用-算数

该文章描述了一个为初学者设计的基于堆栈的简单计算器,计算器由两个堆栈构成,一个存储数字,一个存储运算符。当按下等号键时,计算器从堆栈中弹出数字和运算符进行计算,处理加、减、乘、除四种运算。如果遇到除数为零的情况,程序会输出错误信息并结束。文章给出了输入输出格式以及一个Java代码实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cal.jpg

本题要求你为初学数据结构的小伙伴设计一款简单的利用堆栈执行的计算器。如上图所示,计算器由两个堆栈组成,一个堆栈 S1​ 存放数字,另一个堆栈 S2​ 存放运算符。计算器的最下方有一个等号键,每次按下这个键,计算器就执行以下操作:

  1. 从 S1​ 中弹出两个数字,顺序为 n1​ 和 n2​;
  2. 从 S2​ 中弹出一个运算符 op;
  3. 执行计算 n2​ op n1​;
  4. 将得到的结果压回 S1​。

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

输入格式:

输入首先在第一行给出正整数 N(1<N≤103),为 S1​ 中数字的个数。

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

输出格式:

将输入的数字和运算符按给定顺序分别压入堆栈 S1​ 和 S2​,将执行计算的最后结果输出。注意所有的计算都只取结果的整数部分。题目保证计算的中间和最后结果的绝对值都不超过 109。

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

输入样例 1:

5

40 5 8 3 2

/ * - +

输出样例 1:

2

输入样例 2:

5

2 5 8 4 4

* / - +

输出样例 2:

ERROR: 5/0

 AC代码:

 

import javax.print.DocFlavor;
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main
{
    static Scanner sc = new Scanner(System.in);
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)40;
    static Stack<Integer> stack_num = new Stack<>();
    static Stack<Character> stack_op = new Stack<>();

    public static void main(String[] args) throws IOException
    {
        br.readLine();
        String[] s = br.readLine().split(" ");
        for(int i = 0; i < s.length ; i ++)
        {
            int temp = Integer.parseInt(s[i]);
            stack_num.push(temp);
        }
        s = br.readLine().split(" ");
        for(int i = 0; i < s.length;i++)
        {
            char temp = s[i].charAt(0);
            stack_op.push(temp);
        }
        while(stack_op.size() != 0)
        {
            int n1 = stack_num.pop();
            int n2 = stack_num.pop();
            char temp = stack_op.pop();
            int res = 0;
            if(temp == '/')
            {
                if(n1 == 0)
                {
                    System.out.printf("ERROR: %d/0",n2);
                    System.exit(0);
                }
                else res = n2 / n1;
            }
            else if(temp == '+')  res = n2 + n1;
            else if(temp == '-')  res = n2 - n1;
            else  res = n2 * n1;
            stack_num.push(res);
        }
        pw.print(stack_num.pop());
        pw.flush();
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class PII
{
    int x,y;
    public PII(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

21RGHLY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值