求助,长度不对

P1957 口算练习题

xkdd @ 2024-01-03 13:00:06

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,sum=2,z,x,y;
    cin>>n;
    char a,s;
    while(n--){
        if(!(cin>>x)){//如果s输入的是数字导致cin>>x被锁上
            cin.clear();//重置cin
            cin>>s>>x;
        }
        cin>>y;
        a=s;
        if(a=='a'){//+
            cout<<x<<"+"<<y<<"="<<x+y<<endl;
            z=x+y;
        }else if(a=='b'){//-
            if(x<y){
                sum++;
            }
            cout<<x<<"-"<<y<<"="<<x-y<<endl;
            z=x-y;
        }else if(a=='c'){//*
            cout<<x<<"*"<<y<<"="<<x*y<<endl;
            z=x*y;
        }
        while(x!=0){
            x%=10,x/=10;
            sum++;
        }
        while(y!=0){
            y%=10,y/=10;
            sum++;
        }
        while(z!=0){
            z%=10,z/=10;
            sum++;
        }
        cout<<sum+2<<endl;
        sum=0;
    }
    return 0;
}

|