Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
2.0 kB
2
Indexable
Never
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication58;

import java.util.Scanner;
import java.util.Stack;

/**
 *
 * @author Admin
 */
public class Main2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int test = Integer.parseInt( sc.nextLine() );
        while(test-- > 0) {
            int character = Integer.parseInt( sc.nextLine() );            
            String value = sc.nextLine();
//            System.out.println(value);
            Resolve(value);

        }
    }
    public static void Resolve(String value) {
        value = value.trim();
        String[] values = value.split(" ");
//        System.out.println(value);
        long num = 0;
        Stack<Long> S = new Stack<>();
//        for (String v : values) {
//            System.out.println(v);
//        }
        for (int i = values.length - 1; i >= 0; i--) {
            String value1 = values[i];
            if (value1.equals("+") || value1.equals("-") || value1.equals("*") || value1.equals("/")) {
                long num1 = 0, num2 = 0;

                char c = value1.charAt(0);
                num2 = S.pop();
                num1 = S.pop();
                num = 0;
                switch (c) {
                    case '+' : num = num2 + num1; break;
                    case '-' : num = num2 - num1; break;
                    case '*' : num = num2 * num1; break;
                    case '/' : num = num2 / num1; break;
                    default:
                        break;
                }
                S.push(num);
                
            } else {
                long numm = Integer.parseInt(value1);
                S.push(numm);
            }
        }
        System.out.println(S.pop());
    }
    
    
}