Blue Bridge Cup PREV-55 small calculator

Problem description

Analog program calculator, input the instructions in turn, and the possible instructions include

1. Number: 'NUM X', X is a string containing only uppercase letters and numbers, indicating a current base number
2. Operation instructions: 'ADD', 'SUB', 'MUL', 'DIV' and 'MOD', respectively, represent addition, subtraction, multiplication, division, quotient and division, and remainder
3. Base conversion instruction: 'CHANGE K', convert current base to base K (2 ≤ K ≤ 36)
4. Output instruction: 'EQUAL', output the result in current base
5. Reset command: 'CLEAR', CLEAR the current number

The instructions are given according to the following rules:
The number, operation instruction will not be given continuously, and the decimal conversion instruction, output instruction and reset instruction may be given continuously
The first number after the operation instruction indicates the number participating in the operation. And there will be no operation instruction and output instruction between the operation instruction and the number
The first number that appears after the reset instruction indicates the base value. And there is no operation instruction or output instruction between the reset instruction and the first number
Hexadecimal conversion instructions may appear anywhere

During the operation, the intermediate variables are all non negative integers and less than 2 ^ 63.
1035 in capital 'A' Z '

Input format

Line 1: 1 n, indicating the number of instructions
Second n+1 lines: each line gives an instruction. The instruction sequence must start with 'CLEAR' and meet the instruction rules

Output format

Give the results of each 'EQUAL' in turn

sample input

7
CLEAR
NUM 1024
CHANGE 2
ADD
NUM 100000
CHANGE 8
EQUAL

sample output

2040

Answer

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
//This oj class name must use Main
public class Main {
	public static long[] num= {0,0};
	public static int hex=10;
	public static String op="";
	public static long Calcu() {
		switch(op) {
			case "ADD":
				num[0]=num[0]+num[1];
				break;
			case "SUB":
				num[0]=num[0]-num[1];
				break;
			case "MUL":
				num[0]=num[0]*num[1];
				break;
			case "DIV":
				num[0]=num[0]/num[1];
				break;
			case "MOD":
				num[0]=num[0]%num[1];
				break;
			default:
				break;
		}
		//System.out.println("calculated" + num[0]);
		return num[0];
	}
	public static void Recog(String[] term){
		switch(term[0]) {
			case "NUM":
				if(op.equals(""))
					//parseLong(term[1],hex); converts hex base numbers to decimal strings
					num[0]=Long.parseLong(term[1],hex);
				else {
					num[1]=Long.parseLong(term[1],hex);
					num[0]=Calcu();
					op="";
				}
				break;
			case "ADD":
				op="ADD";
				break;
			case "SUB":
				op="SUB";
				break;
			case "MUL":
				op="MUL";
				break;
			case "DIV":
				op="DIV";
				break;
			case "MOD":
				op="MOD";
				break;
			case "CHANGE":
				hex=Integer.parseInt(term[1]);
				break;
			case "CLEAR":
				num[0]=num[1]=0;
				op="";
				break;
			case "EQUAL":
				//toUpperCase() output letters above decimal must be uppercase
				//toString(num[0],hex) converts a decimal number to a hex string
				System.out.println(Long.toString(num[0],hex).toUpperCase());
				break;
			default:
				break;
		}
	}
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		int n=0;
		String s=new String();
		//Scanner sc=new Scanner(System.in);
		//BufferedReader
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		//n=sc.nextInt(); cannot input with this method, you need to receive "\ n" to stop input
		n = Integer.parseInt(br.readLine());
		for(int i=0;i<n;i++) {
			s=br.readLine();
			String[] term=s.split(" ");
			Recog(term);
		}	
	}
}

Published 39 original articles, won praise 5, visited 7018
Private letter follow

Keywords: Java calculator less

Added by LarryK on Wed, 29 Jan 2020 19:05:18 +0200