首页 技术 正文
技术 2022年11月18日
0 收藏 686 点赞 3,222 浏览 1933 个字

历届试题 小计算器

时间限制:1.0s 内存限制:256.0MB

问题描述

  模拟程序型计算器,依次输入指令,可能包含的指令有

1. 数字:‘NUM X’,X为一个只包含大写字母和数字的字符串,表示一个当前进制的数

  2. 运算指令:‘ADD’,‘SUB’,‘MUL’,‘DIV’,‘MOD’,分别表示加减乘,除法取商,除法取余

  3. 进制转换指令:‘CHANGE K’,将当前进制转换为K进制(2≤K≤36)

  4. 输出指令:‘EQUAL’,以当前进制输出结果

  5. 重置指令:‘CLEAR’,清除当前数字

指令按照以下规则给出:

  数字,运算指令不会连续给出,进制转换指令,输出指令,重置指令有可能连续给出

  运算指令后出现的第一个数字,表示参与运算的数字。且在该运算指令和该数字中间不会出现运算指令和输出指令

  重置指令后出现的第一个数字,表示基础值。且在重置指令和第一个数字中间不会出现运算指令和输出指令

  进制转换指令可能出现在任何地方

运算过程中中间变量均为非负整数,且小于2^63。

  以大写的’A’‘Z’表示1035

输入格式

  第1行:1个n,表示指令数量

  第2…n+1行:每行给出一条指令。指令序列一定以’CLEAR’作为开始,并且满足指令规则

输出格式

  依次给出每一次’EQUAL’得到的结果

样例输入

7

CLEAR

NUM 1024

CHANGE 2

ADD

NUM 100000

CHANGE 8

EQUAL

样例输出

2040

这道题我只能过70分,还请路过的大佬帮忙指点指点

import java.math.BigInteger;
import java.util.Scanner;
import java.util.StringTokenizer;public class 小计算器 {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
int n = in.nextInt();String command;
BigInteger res = null;
boolean isStart = false;
int ope = 0;
int PN = 10;
in.nextLine();
for(int i = 0; i < n; i++){
StringTokenizer stringTokenizer = new StringTokenizer(in.nextLine());
command = stringTokenizer.nextToken();
if(command.equals("CHANGE")){
String str = stringTokenizer.nextToken();
PN = Integer.parseInt(str);
}
else if(command.equals("EQUAL")){
String string = res.toString(PN).toUpperCase();
System.out.println(string);
}
else if(command.equals("NUM")){
String str = stringTokenizer.nextToken();
String num = Long.valueOf(str, PN).toString();
if(isStart == true){
res = new BigInteger(num);
isStart =false;
}
else{
switch (ope) {
case 1:
res = res.add(new BigInteger(num));
break;
case 2:
res = res.subtract(new BigInteger(num));
break;
case 3:
res = res.divide(new BigInteger(num));
break;
case 4:
res = res.multiply(new BigInteger(num));
break;
case 5:
res = res.mod(new BigInteger(num));
break;
default:
break;
}
}
}
else if(command.equals("ADD")){
ope = 1;
}
else if(command.equals("SUB")){
ope = 2;
}
else if(command.equals("DIV")){
ope = 3;
}
else if(command.equals("MUL")){
ope = 4;
}
else if(command.equals("MOD")){
ope = 5;
}
else if(command.equals("CLEAR")){
res = new BigInteger("0");
isStart = true;
}
}
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898