Small calculator (BigInteger clever use)

Problem description
  simulation calculator program, are sequentially input instructions, comprising instructions may have

1. Digital: 'NUM X', X is a alphanumeric and contains only a character string representing a current binary number
  2. The operation instructions: 'ADD', 'SUB' , 'MUL', 'DIV', 'MOD', respectively, subtraction, multiplication, division takes provider, division modulo
  3. the binary conversion instruction: 'CHANGE K', K is the current binary conversion hexadecimal (2≤K≤36)
  4. output instructions: 'EQUAL', the current binary output
  5. reset command: 'CLEAR', clear current

Instruction is given according to the following rules:
  a digital, continuous operation instruction is not given, binary conversion command, the output command, it is possible to continuously reset instruction given
  operation instruction after the first number, represents a number involved in computing. Without producing output command execution instruction and the operation instruction and the digital intermediate
  the first number after the reset instruction, it represents the base value. And the output command execution instruction and reset instruction and do not appear in the first intermediate digital
  binary conversion instructions may appear anywhere

Intermediate calculation process variables are non-negative integers, and less than 2 ^ 63.
  With a capital 'A' 'the Z' indicates a 10- 35
input format
  at line 1: 1 n, the number of instructions
  of 2 ... n + 1 line: an instruction is given for each row. A certain sequence of instructions to 'CLEAR' as a start, and satisfying instruction rule
output format
  given sequentially every 'EQUAL' resultant
sample input
. 7
the CLEAR
NUM 1024
the CHANGE 2
the ADD
NUM 100000
the CHANGE. 8
EQUAL
sample output
2040

BigInteger class with more convenient, especially the conversion between binary
as:

BigInteger a=new BigInteger("0",10);//输入一个十进制得数
String str1 = new BigInteger(a.toString()).toString(jinzhishu);//变换//成任意进制的字符串,a是BigInteger的实例
在这里插入代码片

import java.math.BigInteger;
import java.util.*;

public class Main {
	public static void main(String []args)
	{Scanner input= new Scanner(System.in);
	int zhilingshu=input.nextInt();
	BigInteger a=new BigInteger("0",10);
    int jinzhishu=10;
    String zhiling,zl;
    while(zhilingshu-->0) {
    	zhiling=input.next();
    	if(zhiling.equals("CLEAR"))
    		a=new BigInteger("0",jinzhishu);
    	else if(zhiling.equals("NUM"))
    		a=new BigInteger(input.next(),jinzhishu);
    	else if(zhiling.equals("CHANGE"))
    		jinzhishu=input.nextInt();
    	else if(zhiling.equals("ADD")) {
    		zl=input.next();
    		while(zl.equals("CHANGE"))
    		{jinzhishu=input.nextInt();
    		zhilingshu--;
    		zl=input.next();}
    			a=a.add(new BigInteger(input.next(),jinzhishu));
    			zhilingshu--;
    	}
    	else if(zhiling.equals("SUB")) {
    		zl=input.next();
    		while(zl.equals("CHANGE"))
    		{jinzhishu=input.nextInt();
    		zhilingshu--;
    		zl=input.next();}
    			a=a.subtract(new BigInteger(input.next(),jinzhishu));
    			zhilingshu--;
    	}
    	else if(zhiling.equals("MUL")) {
    		zl=input.next();
    		while(zl.equals("CHANGE"))
    		{jinzhishu=input.nextInt();
    		zhilingshu--;
    		zl=input.next();}
    			a=a.multiply(new BigInteger(input.next(),jinzhishu));
    			zhilingshu--;
    	}
    	else if(zhiling.equals("DIV")) {
    		zl=input.next();
    		while(zl.equals("CHANGE"))
    		{jinzhishu=input.nextInt();
    		zhilingshu--;
    		zl=input.next();}
    			a=a.divide(new BigInteger(input.next(),jinzhishu));
    			zhilingshu--;
    	}
    	else if(zhiling.equals("MOD")) {
    		zl=input.next();
    		while(zl.equals("CHANGE"))
    		{jinzhishu=input.nextInt();
    		zhilingshu--;
    		zl=input.next();}
    			a=a.remainder(new BigInteger(input.next(),jinzhishu));
    			zhilingshu--;
    	}
    	else if(zhiling.equals("EQUAL"))
    	{
		String str1 = new BigInteger(a.toString()).toString(jinzhishu);
		for(int i=0;i<str1.length();i++) {
			if(str1.charAt(i)>='a'&&str1.charAt(i)<='z') {
				System.out.print((char)(str1.charAt(i)-'a'+'A'));
			}
			else {
				System.out.print(str1.charAt(i));
			}
		}
		}
    	
    }
	}
}

在这里插入代码片
Published 130 original articles · won praise 16 · views 30000 +

Guess you like

Origin blog.csdn.net/feiqipengcheng/article/details/104541420