首页 > 试题广场 >

Emacs计算器

[编程题]Emacs计算器
  • 热度指数:3026 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
Emacs号称神的编辑器,它自带了一个计算器。与其他计算器不同,它是基于后缀表达式的,即运算符在操作数的后面。例如“2 3 +”等价于中缀表达式的“2 + 3”。
请你实现一个后缀表达式的计算器。

输入描述:
输入包含多组数据。

每组数据包括两行:第一行是一个正整数n (3≤n≤50);紧接着第二行包含n个由数值和运算符组成的列表。

“+-*/”分别为加减乘除四则运算,其中除法为整除,即“5/3=1”。


输出描述:
对应每一组数据,输出它们的运算结果。
示例1

输入

3
2 3 +
5
2 2 + 3 *
5
2 2 3 + *

输出

5
12
10
是哦。。我咋没想到用栈呢。。。
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            String str = sc.nextLine();
            int[] nums = new int[n];
            char[] arr = sc.nextLine().toCharArray();
            int i = 0 , size = 0;
            for(char ch : arr){
                if(ch == '+'){
                    add(nums,--size);
                    --size;
                }else if(ch == '-'){
                    sub(nums,--size);
                    --size;
                }else if(ch == '*'){
                    mul(nums,--size);
                    --size;
                }else if(ch == '/'){
                    div(nums,--size);
                    --size;
                }else if(ch != ' '){
                    nums[size] = nums[size] * 10 + ch - '0';
                }else{
                    size++;
                }
            }
            System.out.println(nums[0]);
        }
    }
    private static void add(int[] nums,int index){
        nums[index-1] = nums[index-1] + nums[index];
        nums[index] = 0;
    }
    private static void sub(int[] nums,int index){
        nums[index-1] = nums[index-1] - nums[index];
        nums[index] = 0;
    }
    private static void mul(int[] nums,int index){
        nums[index-1] = nums[index-1] * nums[index];
        nums[index] = 0;
    }
    private static void div(int[] nums,int index){
        nums[index-1] = nums[index-1] / nums[index];
        nums[index] = 0;
    }
}

发表于 2022-05-20 19:58:25 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = Integer.parseInt(sc.nextLine());
            String[] str = sc.nextLine().split(" ");
            Stack<String> stack = new Stack<>();
            int sum = 0;
            for(int i = 0; i < n; i++){
                if(!str[i].equals("+") && !str[i].equals("-") 
                   && !str[i].equals("*") && !str[i].equals("/")){
                    stack.push(str[i]);
                }
                else{
                    int a = Integer.parseInt(stack.pop());
                    int b = Integer.parseInt(stack.pop());
                    if(str[i].equals("+")){
                        sum = a + b;
                    }
                    if(str[i].equals("-")){
                        sum = b - a;
                    }
                    if(str[i].equals("*")){
                        sum = a * b;
                    }
                    if(str[i].equals("/")){
                        sum = b / a;
                    }
                    stack.push(String.valueOf(sum));
                }
            }
            System.out.println(stack.pop());
        }
    }
}

发表于 2021-07-30 19:13:30 回复(0)