abc换成plus minus和multiple?
查看原帖
abc换成plus minus和multiple?
855093
0x00AC3375楼主2023/2/15 20:58

abc可以用scanf("%x")来读入
下面这个已经AC了 如果换成plus,minus和multiple该怎么处理?

#include<cstdio>
#include<cmath>
#define elif else if
int len(int x)
{
	if(0<=x && x<=9) return 1;
	elif(x>=10 && x<=99) return 2;
	elif(x>=100 && x<=999) return 3;
	elif(x>=1000 && x<=9999) return 4;
	elif(x>=10000 && x<=99999) return 5;
	elif(x>=100000 && x<=999999) return 6;
	elif(x>=1000000 && x<=9999999) return 7;
	elif(x>=10000000 && x<=99999999) return 8;
	elif(x>=100000000) return 9;
	if(x<=-1)
	{
		if(0<=-x && -x<=9) return 2;
		elif(-x>=10 && -x<=99) return 3;
		elif(-x>=100 && -x<=999) return 4;
		elif(-x>=1000 && -x<=9999) return 5;
		elif(-x>=10000 && -x<=99999) return 6;
		elif(-x>=100000 && -x<=999999) return 7;
		elif(-x>=1000000 && -x<=9999999) return 8;
		elif(-x>=10000000 && -x<=99999999) return 9;
		elif(-x>=100000000) return 10;
	}
	
	
	
}
int power=1;
int command[60][3];
int length[60],result[60];
char op[60];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<=n-1;i++)
	{
		scanf("%x",&command[i][0]);//将代表运算操作的ABC当做十六进制读入(也即10,11,12)
		if(command[i][0]==0xA)//A代表加法 
		{
			op[i]='+';
			scanf("%d%d",&command[i][1],&command[i][2]);
		}
		elif(command[i][0]==0xB)//B代表减法 
		{
			op[i]='-';
			scanf("%d%d",&command[i][1],&command[i][2]);
		}
		elif(command[i][0]==0xC)//C代表乘法 
		{
			op[i]='*';
			scanf("%d%d",&command[i][1],&command[i][2]);
		}
		else//不输入运算符号的字母 
		{
			scanf("%d",&command[i][2]); 
			op[i]=op[i-1];//继承上一个运算符号
			command[i][1]=0;
			power=1;
			while(command[i][0]!=0)
			{
				command[i][1]+=power*(command[i][0]%16);
				command[i][0]/=16;
				power*=10;//按照每一位转化为十进制
			}
			
		}
	}
	for(int i=0;i<=n-1;i++)
	{
		printf("%d%c%d=",command[i][1],op[i],command[i][2]);
		switch(op[i])
		{
			case '+':
			{
				printf("%d\n",command[i][1]+command[i][2]);
				printf("%d\n",len(command[i][1])+len(command[i][2])+len(command[i][1]+command[i][2])+2);
				break;
			}
			case '-':
				{
				printf("%d\n",command[i][1]-command[i][2]);
				printf("%d\n",len(command[i][1])+len(command[i][2])+len(command[i][1]-command[i][2])+2);
				break;
			}
			case '*':
				{
				printf("%d\n",command[i][1]*command[i][2]);
				printf("%d\n",len(command[i][1])+len(command[i][2])+len(command[i][1]*command[i][2])+2);
				break;
			}
			default:break;
		}
		
	}
	return 0;
}

2023/2/15 20:58
加载中...