abc换成plus minus和multiple?

P1957 口算练习题

0x00AC3375 @ 2023-02-15 20:58:22

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;
}

by yuchenren @ 2023-02-15 21:06:00

第一次见这么逆天的 if else


by mashduihca @ 2023-02-15 21:17:32

用%s读字符串……

这代码我感觉有点nb


by fukozowa4 @ 2023-02-17 22:53:43

@yuchenren 计算一个数有多少位可以ceil(log10(fabs()))来实现 我试了结果红了 怀疑是不是和浮点误差有关


by yuchenren @ 2023-02-18 09:50:55

@fukozowa4 为什么要这样,你直接写个 while 循环多香


by hzxphy @ 2023-02-18 18:36:31

好厉害


|