试题 历届试题 小计算器
时间限制 | 内存限制 |
---|---|
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’表示10~35
输入格式
第1行:1个n,表示指令数量
第2…n+1行:每行给出一条指令。指令序列一定以’CLEAR’作为开始,并且满足指令规则
输出格式
依次给出每一次’EQUAL’得到的结果
样例输入 |
---|
7
CLEAR
NUM 1024
CHANGE 2
ADD
NUM 100000
CHANGE 8
EQUAL
样例输出 |
---|
2040
代码 |
---|
#include<bits/stdc++.h>
//#define DEBUG
using namespace std;
typedef long long ll;
int BASE = 10;//表示当前进制
int n;//命令数
ll num[2];//模拟CPU内的两个数,其中num[0]是基准数,保存中间结果
char temp[50];//输入临时的数,任意进制
char **stop;//
bool flag;
bool w[5];//表示采取哪个运算
bool hasFirstNum;//是否有基准数
//将2~36进制的字符串转化为10进制long long
ll toDecimal(char *num) {
ll res = 0;
int len = strlen(num);
for (int i = 0; i < len; i++) {
res *= BASE;
if (num[i] >= '0' && num[i] <= '9') {
res += num[i] - '0';
}
else if (num[i] >= 'A'&&num[i] <= 'Z') {
res += num[i] - 'A' + 10;
}
}
//printf("res:%lld\n", res);
return res;
}
void add() {
#ifdef DEBUG
printf("%lld + %lld = %lld\n", num[0], num[1], num[0] + num[1]);
#endif // DEBUG
num[0] += num[1];
}
void sub() {
#ifdef DEBUG
printf("%lld - %lld = %lld\n", num[0], num[1], num[0] - num[1]);
#endif // DEBUG
num[0] -= num[1];
}
void mod() {
#ifdef DEBUG
printf("%lld mod %lld = %lld\n", num[0], num[1], num[0] % num[1]);
#endif // DEBUG
num[0] = num[0] % num[1];
}
void mul() {
#ifdef DEBUG
printf("%lld * %lld = %lld\n", num[0], num[1], num[0] * num[1]);
#endif // DEBUG
num[0] *= num[1];
}
void div() {
if (num[1] != 0) {
#ifdef DEBUG
printf("%lld / %lld = %lld\n", num[0], num[1], num[0] / num[1]);
#endif // DEBUG
num[0] /= num[1];
}
}
void equal() {
string res="";
ll m;
ll tem = num[0];
if (tem == 0) {//坑点,如果是0,要特殊处理
printf("%d\n", tem);
return;
}
while (tem) {
m = tem % BASE;
tem /= BASE;
if (m < 10) {
res += ('0' + m);
}
else {
res += ('A' + (m - 10));
}
}
reverse(res.begin(), res.end());
printf("%s\n", res.c_str());
}
int main() {
//freopen("input.txt", "r", stdin);
string comm;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
cin >> comm;
if (comm == "EQUAL") {
equal();
}
else if (comm == "NUM") {
scanf("%s", &temp);
if (!hasFirstNum) {
hasFirstNum = 1;
num[0] = toDecimal(temp);
}
else {
num[1] = toDecimal(temp);
if (w[0]) {
add();
w[0] = 0;
}
else if (w[1]) {
sub();
w[1] = 0;
}
else if (w[2]) {
mul();
w[2] = 0;
}
else if (w[3]) {
div();
w[3] = 0;
}
else if (w[4]) {
mod();
w[4] = 0;
}
}
}
else if (comm == "ADD") {
w[0] = 1;
}
else if (comm == "SUB") {
w[1] = 1;
}
else if (comm == "MUL") {
w[2] = 1;
}
else if (comm == "DIV") {
w[3] = 1;
}
else if (comm == "MOD") {
w[4] = 1;
}
else if (comm == "CHANGE") {
scanf("%d", &BASE);
}
else if (comm == "CLEAR") {
memset(temp, 0, sizeof temp);
memset(num, 0, sizeof num);
memset(w, 0, sizeof w);
hasFirstNum = 0;
}
}
return 0;
}
MISS假老练
发布了11 篇原创文章 · 获赞 32 · 访问量 8018
私信
关注