美文网首页
算法训练 前缀表达式

算法训练 前缀表达式

作者: 就这样吧嘞 | 来源:发表于2019-03-19 19:25 被阅读0次

    问题描述
      编写一个程序,以字符串方式输入一个前缀表达式,然后计算它的值。输入格式为:“运算符 对象1 对象2”,其中,运算符为“+”(加法)、“-”(减法)、“*”(乘法)或“/”(除法),运算对象为不超过10的整数,它们之间用一个空格隔开。要求:对于加、减、乘、除这四种运算,分别设计相应的函数来实现。
      输入格式:输入只有一行,即一个前缀表达式字符串。
      输出格式:输出相应的计算结果(如果是除法,直接采用c语言的“/”运算符,结果为整数)。
      输入输出样例
    样例输入

    • 5 2
      样例输出
      7
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String fuhao = sc.next();
        int a=sc.nextInt();
        int b=sc.nextInt();
        if(fuhao.equals("+")) {
            System.out .println(a+b);
        }
        if(fuhao.equals("-")) {
            System.out .println(a-b);
        }
        if(fuhao.equals("*")) {
            System.out .println(a*b);
        }
        if(fuhao.equals("/")) {
            System.out .println(a/b);
        }
        }   
    }
    

    相关文章

      网友评论

          本文标题:算法训练 前缀表达式

          本文链接:https://www.haomeiwen.com/subject/cmnnmqtx.html