我有点费解???

P1957 口算练习题

LubsWangKillThemAll @ 2022-06-03 18:21:15

#include<iostream>
#include<string.h>
#include<ctype.h>
//isalpha():检查这个字符是否为字母,
//真返回1,假返回0;
using namespace std;
int intsize(int n)
{
    int sum = 0;
    while (n)
    {
        n /= 10;
        sum++;
    }
    return sum;
}
int main()
{
    int n;
    cin >> n;
    int a, b;
    char ss[1005];
    for (int i = 1; i <= n; i++)
    {
        cin >> ss[i];
        if (isalpha(ss[0]))
        {
            cin >> a >> b;
        }
        else
        {
            //atol:字符串转整形
            //ascii to integer
            a = atol(ss);
            cin >> b;
        }
        if (ss == "a")
        {
            int ans;
            ans = a + b;
            cout << a << "+" << b << "=" << ans << endl;
            cout << intsize(a) + intsize(b) + intsize(ans) + 2 << endl;
        }
        else if (ss == "b")
        {
            int ans;
            ans = a - b;
            cout << a << "-" << b << "=" << ans << endl;
            cout << intsize(a) + intsize(b) + intsize(ans) + 2 << endl;
        }
        else if (ss == "c")
        {
            int ans;
            ans = a * b;
            cout << a << "*" << b << "=" << ans << endl;
            cout << intsize(a) + intsize(b) + intsize(ans) + 2 << endl;
        }
    }
    return 0;
}

by rongcan @ 2022-06-10 17:44:28

intsize() 中 需要考虑 x 为0 时, 也占一个长度 0*1000=1000 9


|