蓝桥杯历届试题——小计算器(java版)

import java.util.Scanner;

public class Main {
     

	public static void main(String[] args) {
     
		long sum = 0; // 指令数量
		String result = null;// 存储结果
		Integer K0 = 10;// 默认进制位数为10
		Scanner cin = new Scanner(System.in);
		sum = Long.parseLong(cin.nextLine());
		while (sum > 0) {
     
			String s = cin.nextLine();
			sum = sum - 1;
			if (s.equals("CLEAR")) {
     
				result = null;
			} else if (s.equals("ADD")) {
     
				while (true) {
     

					if (sum > 0) {
     
						s = cin.nextLine();
						sum--;
						if (s.substring(0, 3).equals("NUM")) {
     
							long new_data = Long.parseLong(s.substring(4), K0);
							long r = Long.parseLong(result, K0);// r存储结果的十进制数,下同
							r = add(r, new_data);
							result = change(r, 10, K0);
							break;
						} else if (s.equals("CLEAR")) {
     
							result = null;
							break;
						} else if (s.substring(0, 3).equals("CHA")) {
     
							if (result != null) {
     
								long r = Long.parseLong(result, K0);
								K0 = Integer.parseInt(s.substring(7));
								result = change(r, 10, K0);
							} else
								K0 = Integer.parseInt(s.substring(7));
						}
					} else
						break;
				}
			} else if (s.equals("SUB")) {
     
				while (true) {
     

					if (sum > 0) {
     
						s = cin.nextLine();
						sum--;
						if (s.substring(0, 3).equals("NUM")) {
     
							long new_data = Long.parseLong(s.substring(4), K0);
							long r = Long.parseLong(result, K0);// r存储结果的十进制数,下同
							r = sub(r, new_data);
							result = change(r, 10, K0);
							break;
						} else if (s.equals("CLEAR")) {
     
							result = null;
							break;
						} else if (s.substring(0, 3).equals("CHA")) {
     
							if (result != null) {
     
								long r = Long.parseLong(result, K0);
								K0 = Integer.parseInt(s.substring(7));
								result = change(r, 10, K0);
							} else
								K0 = Integer.parseInt(s.substring(7));
						}
					} else
						break;
				}

			} else if (s.equals("MUL")) {
     
				while (true) {
     

					if (sum > 0) {
     
						s = cin.nextLine();
						sum--;
						if (s.substring(0, 3).equals("NUM")) {
     
							long new_data = Long.parseLong(s.substring(4), K0);
							long r = Long.parseLong(result, K0);// r存储结果的十进制数,下同
							r = mul(r, new_data);
							result = change(r, 10, K0);
							break;
						} else if (s.equals("CLEAR")) {
     
							result = null;
							break;
						} else if (s.substring(0, 3).equals("CHA")) {
     
							if (result != null) {
     
								long r = Long.parseLong(result, K0);
								K0 = Integer.parseInt(s.substring(7));
								result = change(r, 10, K0);
							} else
								K0 = Integer.parseInt(s.substring(7));
						}
					} else
						break;
				}
			} else if (s.equals("DIV")) {
     
				while (true) {
     
					if (sum > 0) {
     
						s = cin.nextLine();
						sum--;
						if (s.substring(0, 3).equals("NUM")) {
     
							long new_data = Long.parseLong(s.substring(4), K0);
							long r = Long.parseLong(result, K0);// r存储结果的十进制数,下同
							r = div(r, new_data);
							result = change(r, 10, K0);
							break;
						} else if (s.equals("CLEAR")) {
     
							result = null;
							break;
						} else if (s.substring(0, 3).equals("CHA")) {
     
							if (result != null) {
     
								long r = Long.parseLong(result, K0);
								K0 = Integer.parseInt(s.substring(7));
								result = change(r, 10, K0);
							} else
								K0 = Integer.parseInt(s.substring(7));
						}
					} else
						break;
				}
			} else if (s.equals("MOD")) {
     
				while (true) {
     
					if (sum > 0) {
     
						s = cin.nextLine();
						sum--;
						if (s.substring(0, 3).equals("NUM")) {
     
							long new_data = Long.parseLong(s.substring(4), K0);
							long r = Long.parseLong(result, K0);// r存储结果的十进制数,下同
							r = mod(r, new_data);
							result = change(r, 10, K0);
							break;
						} else if (s.equals("CLEAR")) {
     
							result = null;
							break;
						} else if (s.substring(0, 3).equals("CHA")) {
     
							if (result != null) {
     
								long r = Long.parseLong(result, K0);
								K0 = Integer.parseInt(s.substring(7));
								result = change(r, 10, K0);
							} else
								K0 = Integer.parseInt(s.substring(7));
						}
					} else
						break;
				}
			} else if (s.substring(0, 3).compareTo("NUM") == 0) {
     
				result = s.substring(4);
			} else if (s.substring(0, 3).equals("CHA")) {
     
				if (result != null) {
     
					long r = Long.parseLong(result, K0);
					K0 = Integer.parseInt(s.substring(7));
					result = change(r, 10, K0);
				} else
					K0 = Integer.parseInt(s.substring(7));

			} else if (s.equals("EQUAL")) {
     
				System.out.println(result);
			}
			
		}
		cin.close();

	}

	public static long add(long a, long b) {
     
		return a + b;
	}

	public static long sub(long a, long b) {
     
		return a - b;
	}

	public static long mul(long a, long b) {
     
		return a * b;
	}

	public static long div(long a, long b) {
     
		return a / b;
	}

	public static long mod(long a, long b) {
     
		return a % b;
	}

	public static String change(long a, Integer K0, int K) {
     
		String str = String.valueOf(a);
		a = Long.parseLong(str, K0);// 转化为10进制
		str = Long.toString(a, K);// 转化为目标进制的字符串
		str = str.toUpperCase();// 返回大写形式
		return str;

	}

}

进行代码测试,以较高的效率顺利通过各个评测点。
蓝桥杯历届试题——小计算器(java版)_第1张图片

你可能感兴趣的:(蓝桥杯,java,字符串)