Huawei OD Computer Test - Expression Evaluation (C++ & Java & JS & Python)

describe

Given an arithmetic expression described by a string, compute the resulting value.

The length of the input string does not exceed 100 characters. Legal characters include "+, -, *, /, (, )" and "0-9".

Data range: Both the operation process and the final result satisfy ∣���∣≤231−1 ∣val∣≤231−1 , that is, only integer operations are performed to ensure that the input expression is legal.

Enter description:

Enter an arithmetic expression

Output description:

Calculate the result value

Example 1

enter:

400+5

Output:

405

Java:

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
      //将其他括号,替换成小括号
        s=s.replace("{","(");
        s=s.replace("[","(");
        s=s.replace("}",")");
        s=s.replace("]",")");
        System.out.println(slove(s));
    }
    public static int slove(String s){
        Stack<Integer> stack=ne

Guess you like

Origin blog.csdn.net/m0_68036862/article/details/132809937