本地测试了好几个例子,都没问题,提交全错,求带佬康康

P1957 口算练习题

希赟xiyun @ 2023-01-14 19:33:36

代码有点长,没用switch,本来想通过在化简的,没想到提交全错,大佬幸苦了

#include<iostream>
#include<string>
using namespace std;
int lenhs(int );
int main()
{
    int sum1,sum2,x,i,n,f,sum,l;
    string s;
    cin>>n;
    cin.get();
    for(i=0;i<n;i++)
    {
        sum1=0;
        sum2=0;
        l=0;
        getline(cin,s);
        if(s[0]=='a')
        {
            f=1;
            x=2;
            while(s[x]!=' ')
            {
                l++;
                sum1=sum1*10+(s[x]-'0');
                x++;
            }
            x++;
            while(x<s.length())
            {
                l++;
                sum2=sum2*10+(s[x]-'0');
                x++;
            }
            sum=sum1+sum2;
            l=l+lenhs(sum)+2;
            cout<<sum1<<"+"<<sum2<<"="<<sum<<endl;
            cout<<l<<endl; 
        }
        else if(s[0]=='b')
        {
            f=2;
            x=2;
            while(s[x]!=' ')
            {
                l++;
                sum1=sum1*10+(s[x]-'0');
                x++;
            }
            x++;
            while(x<s.length())
            {
                l++;
                sum2=sum2*10+(s[x]-'0');
                x++;
            }
            sum=sum1-sum2;
            l=l+lenhs(sum)+2;
            cout<<sum1<<"-"<<sum2<<"="<<sum<<endl;
            cout<<l<<endl; 
        }
        else if(s[0]=='c')
        {
            f=3;
            x=2;
            while(s[x]!=' ')
            {
                l++;
                sum1=sum1*10+(s[x]-'0');
                x++;
            }
            x++;
            while(x<s.length())
            {
                l++;
                sum2=sum2*10+(s[x]-'0');
                x++;
            }
            sum=sum1*sum2;
            l=l+lenhs(sum)+2;
            cout<<sum1<<"*"<<sum2<<"="<<sum<<endl;
            cout<<l<<endl; 
        }
        else if(s[0]<'a')
        {
            if(f==1)
            {
                x=0;
                while(s[x]!=' ')
                {
                    l++;
                    sum1=sum1*10+(s[x]-'0');
                    x++;
                }
                x++;
                while(x<s.length())
                {
                    l++;
                    sum2=sum2*10+(s[x]-'0');
                    x++;
                }
                sum=sum1+sum2;
                l=l+lenhs(sum)+2;
                cout<<sum1<<"+"<<sum2<<"="<<sum<<endl;
                cout<<l<<endl; 
            }
            if(f==2)
            {
                x=0;
                while(s[x]!=' ')
                {
                    l++;
                    sum1=sum1*10+(s[x]-'0');
                    x++;
                }
                x++;
                while(x<s.length())
                {
                    l++;
                    sum2=sum2*10+(s[x]-'0');
                    x++;
                }
                sum=sum1-sum2;
                l=l+lenhs(sum)+2;
                cout<<sum1<<"-"<<sum2<<"="<<sum<<endl;
                cout<<l<<endl; 
            }
            if(f==3)
            {
                x=0;
                while(s[x]!=' ')
                {
                    l++;
                    sum1=sum1*10+(s[x]-'0');
                    x++;
                }
                x++;
                while(x<s.length())
                {
                    l++;
                    sum2=sum2*10+(s[x]-'0');
                    x++;
                }
                sum=sum1*sum2;
                l=l+lenhs(sum)+2;
                cout<<sum1<<"*"<<sum2<<"="<<sum<<endl;
                cout<<l<<endl; 
            }
        }
    }
    return 0;
}
int lenhs(int n)
{
    int l=0;
    if(n<0) l++;
    while(n!=0)
    {
        n=n/10;
        l++;
    }
    return l;
}

|