C语言的,全WA,为什么各位大佬求看看

P1957 口算练习题

苏智白君 @ 2020-11-21 14:35:44

#include<stdio.h>

int select(char x, int x1, int x2) {
    int sum;
    switch (x) {
    case('a'):sum = x1 + x2; break;
    case('b'):sum = x1 - x2; break;
    case('c'):sum = x1 * x2; break;
    }
    return sum;
}

int weishu(int x) {
    int cnt = 0;
    if (x == 0) { return 1; }
    else if (x != 0) {
        while (x!=0) {
            x /= 10;
            cnt++;
        }
        return cnt;
    }
}

int main() {
    int n; char n1; char a1; int b, c; int sum; char ch;
    int s;
    scanf_s("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf_s("%c", &a1);
        if (a1 == 'a' || a1 == 'b' || a1 == 'c') {
            scanf_s("%d %d", &b, &c);
            sum=select(a1, b, c);
            s = weishu(sum) + weishu(b) + weishu(c) + 2;
            if (sum < 0) { s++; }
            if (ch == 'a') { printf("%d+%d=%d\n%d", b, c, sum, s); }
            else if (ch == 'b') { printf("%d-%d=%d\n%d", b, c, sum, s); }
            else if (ch == 'c') { printf("%d*%d=%d\n%d", b, c, sum, s); }
        }
        else {
            b= (int)a1;
            scanf_s("%d", &c);
            sum=select(ch, b, c);
            s = weishu(sum) + weishu(b) + weishu(c) + 2;
            if (sum < 0) { s++; }
            if (ch == 'a') { printf("%d+%d=%d\n%d", b, c, sum, s); }
            else if (ch == 'b') { printf("%d-%d=%d\n%d", b, c, sum, s); }
            else if (ch == 'c') { printf("%d*%d=%d\n%d", b, c, sum, s); }
        }
        ch = a1;
    }
    return 0;
}

|