P1957求助

P1957 口算练习题

wei_6 @ 2024-02-06 15:40:00

求助

P1957口算练习题

  • 本人代码如下

    #include <iostream>
    #include <cstring>
    #include <string>
    #include <cstdio>
    using namespace std;
    int le(int a)
    {
    
    int p = 0;
    if (a < 0)
    {
        p++;
        a = -a;
    }
    if (a == 0)
    {
        return 1;
    }
    while (a)
    {
        a /= 10;
        p++;
    }
    return p;
    } // 返回一个数字的位数
    int main()
    {
    char cmd[1000][100] = {};
    int p = 0, n = 0;
    cin >> n; // p用于存储上一个指令类型,123依次对应abc,+-*;
    cin.get();
    int turn = 0;
    for (int l = 0; l < n; l++)
    {
    
        cin.getline(cmd[l], 1000);
    }
    for (int l = 0; l < n; l++)
    {
        int i = 0, j = 0;
        char fu;
        int s1 = 0, s2 = 0, s3 = 0;
        int a = 0, b = 0, sum = 0; // 存储三项数据
        int len = strlen(cmd[l]);
        if (cmd[l][0] == 'a' || cmd[l][0] == 'b' || cmd[l][0] == 'c')
            turn = 2;
        else
            turn = 1; // 判断指令的种类
        if (turn == 1)
        {
            for (i = 0; i < len; i++)
            {
                if (cmd[l][i] == ' ')
                    break;
                a = a * 10 + cmd[l][i] - '0';
            }
            j = i;
            for (j = i + 1; j < len; j++)
            {
                b = b * 10 + cmd[l][j] - '0';
            } // 执行数据b
        }     // 两个数据类型的指令
        else
        {
            for (i = 2; i < len; i++)
            {
                if (cmd[l][i] == ' ')
                    break;
                a = a * 10 + cmd[l][i] - '0';
            }
            j = i;
            for (j = i + 1; j < len; j++)
            {
                b = b * 10 + cmd[l][j] - '0';
            } // 执行数据b
            p = cmd[l][0] - 'a' + 1;
        }
        if (p == 1)
        {
            sum = a + b;
            fu = '+';
        }
        else if (p == 2)
        {
            sum = a - b;
            fu = '-';
        }
        else
        {
            sum = a * b;
            fu = '*';
        }
        s1 = le(a);
        s2 = le(b);
        s3 = le(sum);
        cout << a << fu << b << '=' << sum << '\n';
        cout << s1 + s2 + s3 + 2 << '\n';
    } //
    return 0;
    }

    不知道为什么结果全WA,本地IDE跑的样例都是对的。求大佬help!


|