【P1957 口算练习题】RE 0分

P1957 口算练习题

鸭鸭吃香蕉吗 @ 2021-03-25 13:15:36

在线下运行时并没有报错,提交上去就RuntimError了,为什么啊?

#include <iostream>
#include <cstdio>
using namespace std;

char a[1100][1100] = {0};

int main()
{

    int n;
    cin >> n;
    cin.get(); //删除缓冲区中cin剩下下的'\n'

    int tmp1 = 0, tmp2 = 0, tmp3 = 0; //运算数
    char a2 = 0;                      //存储运算符

    for (int i = 0; i < n; i++)
    {
        cin.getline(a[i], 110); ///按行输入

        //读入运算符
        int j = 2;
        if ('a' <= a[i][0] && a[i][0] <= 'c' )
        {
            a2 = a[i][0];
            j = 2;
        }
        else
        {
            j = 0;
        }

        //读入运算数
        tmp1 = 0, tmp2 = 0;
        while (a[i][j] < '0' || a[i][j] > '9')
            j++;
        for (; a[i][j] != ' ' && a[i][j] != 0; j++)
        {
            tmp1 *= 10;
            tmp1 += a[i][j] - '0';
        }
        while (a[i][j] < '0' || a[i][j] > '9')
            j++;
        for (; a[i][j] != ' ' && a[i][j] != 0; j++)
        {
            tmp2 *= 10;
            tmp2 += a[i][j] - '0';
        }

        //计算
        switch (a2)
        {
        case 'a':
            tmp3 = tmp1 + tmp2;
            printf("%d+%d=%d\n", tmp1, tmp2, tmp3);
            break;
        case 'b':
            tmp3 = tmp1 - tmp2;
            printf("%d-%d=%d\n", tmp1, tmp2, tmp3);
            break;
        case 'c':
            tmp3 = tmp1 * tmp2;
            printf("%d*%d=%d\n", tmp1, tmp2, tmp3);
            break;
        }
        //计算运算式的总长度
        int cnt = 2;
        if (tmp3 <= 0)//
            cnt++;
        while (tmp1 != 0)
        {
            tmp1 /= 10;
            cnt++;
        }
        while (tmp2 != 0)
        {
            tmp2 /= 10;
            cnt++;
        }
        while (tmp3 != 0)
        {
            tmp3 /= 10;
            cnt++;
        }
        cout << cnt << endl;
    }
    return 0;
}

by 鸭鸭吃香蕉吗 @ 2021-03-25 13:16:37

@wheneveright @caijianhong 召唤犇犇


by wheneveright @ 2021-03-25 13:41:20

我人看傻了也不知道为什么出错

头文件也改了

变量名也改了

等一下我让用Linux 的同学帮忙看一下


by cmll02 @ 2021-03-25 13:56:11

我人看傻了也不知道为什么出错

头文件也改了

变量名也改了

等一下我让 @wheneveright 同学帮忙看一下


by wheneveright @ 2021-03-25 14:09:53

在Linux下面试过了,是可以的。。。。

真的不知道是什么问题

还有cmll02水帖警告


by cmll02 @ 2021-03-25 14:14:48

@鸭鸭吃香蕉吗 本数据于windows制造,所以换行符号是\r\n.

然而改完还是90,因为tmp1和2是0时还是要cnt++

改完的代码:

#include <iostream>
#include <cstdio>
using namespace std;

char a[1100][1100] = {0};

int main()
{

    int n;
    cin >> n;
    cin.get();cin.get(); //删除缓冲区中cin剩下下的'\r\n'

    int tmp1 = 0, tmp2 = 0, tmp3 = 0; //运算数
    char a2 = 0;                      //存储运算符

    for (int i = 0; i < n; i++)
    {
        cin.getline(a[i], 110); ///按行输入

        //读入运算符
        int j = 2;
        if ('a' <= a[i][0] && a[i][0] <= 'c' )
        {
            a2 = a[i][0];
            j = 2;
        }
        else
        {
            j = 0;
        }

        //读入运算数
        tmp1 = 0, tmp2 = 0;
        while (a[i][j] < '0' || a[i][j] > '9')
            j++;
        for (; a[i][j] != ' ' && a[i][j] != 0&& a[i][j]!= '\r'; j++)
        {
            tmp1 *= 10;
            tmp1 += a[i][j] - '0';
        }
        while (a[i][j] < '0' || a[i][j] > '9')
            j++;
        for (; a[i][j] != ' ' && a[i][j] != 0 && a[i][j]!= '\r'; j++)
        {
            tmp2 *= 10;
            tmp2 += a[i][j] - '0';
        }

        //计算
        switch (a2)
        {
        case 'a':
            tmp3 = tmp1 + tmp2;
            printf("%d+%d=%d\n", tmp1, tmp2, tmp3);
            break;
        case 'b':
            tmp3 = tmp1 - tmp2;
            printf("%d-%d=%d\n", tmp1, tmp2, tmp3);
            break;
        case 'c':
            tmp3 = tmp1 * tmp2;
            printf("%d*%d=%d\n", tmp1, tmp2, tmp3);
            break;
        }
        //计算运算式的总长度
        int cnt = 2;
        if (tmp3 <= 0)//
            cnt++;
        if(tmp1==0)cnt++;
        if(tmp2==0)cnt++;
        while (tmp1 != 0)
        {
            tmp1 /= 10;
            cnt++;
        }
        while (tmp2 != 0)
        {
            tmp2 /= 10;
            cnt++;
        }
        while (tmp3 != 0)
        {
            tmp3 /= 10;
            cnt++;
        }
        cout << cnt << endl;
    }
    return 0;
}

by wheneveright @ 2021-03-25 14:19:57

@鸭鸭吃香蕉吗 没错,这个就是用Linux的同学(指cmll02)


by 鸭鸭吃香蕉吗 @ 2021-03-25 15:46:03

@cmll02 收到,已关注


|