20分求助

P1957 口算练习题

Collei @ 2022-11-16 21:10:30

#include<stdio.h>
#include<math.h>
int len(int a) {
    int i;
    for ( i = 0; a != 0; i++) {
        a /= 10;
    }
    return i;
}

int main()
{
    int n,i;
    char a[51], c[51];
    int b[51][4];
    scanf("%d\n", &n);
    for (i = 1; i <= n; i++) {  
        scanf("%c %d %d\n",&a[i], &b[i][0], &b[i][1]);
        if (a[i] < 'a' || a[i] >'z') {
            b[i][0] += (a[i] - 48) * pow(10, len(b[i][0]));
            a[i] = a[i - 1];
        }
    }
    for(i=1;i<=n;i++){
        if (a[i] == 'a') {
            b[i][2] = b[i][0] + b[i][1];
            c[i] = '+';
        }
        else if (a[i] == 'b') {
            b[i][2] = b[i][0] - b[i][1];
            c[i] = '-';
        }
        else {
            b[i][2] = b[i][0] * b[i][1];
            c[i] = '*';
        }
    }
    for (i = 1; i <= n; i++) {      
        b[i][3] = len(b[i][0]) + len(b[i][1]) + len(b[i][2]);
        printf("%d%c%d=%d\n%d\n", b[i][0], c[i], b[i][1], b[i][2],b[i][3]+2);
    }
    return 0;
}

by 1651436148fzj @ 2022-11-17 17:23:41

你求数字长度的函数没考虑负数


|