80分求助

P1957 口算练习题

tiangongyuanv @ 2024-07-12 21:39:25

#include<bits/stdc++.h>
using namespace std;
struct b
{
    char s[100];
    int m,n,c;
}a[55];
char k;
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i].s;
        a[i].c+=2;
        if(isalpha(a[i].s[0]))
        {
            k=a[i].s[0];
            cin>>a[i].m>>a[i].n;
            int f=a[i].m,g=a[i].n;
            while(f>0)
            {
                a[i].c++;
                f/=10;
            }
            while(g>0)
            {
                a[i].c++;
                g/=10;
            }
        }

        else
        {
            a[i].m=atoi(a[i].s);
            cin>>a[i].n;
            int f=a[i].m,g=a[i].n;
            while(f>0)
            {
                a[i].c++;
                f/=10;
            }
            while(g>0)
            {
                a[i].c++;
                g/=10;
            }
        }
        if(k=='a')
        {
            int h=a[i].m+a[i].n;
            while(abs(h)>0)
            {
                a[i].c++;
                h/=10;
            }
            if(h<0)
            {
                a[i].c++;
            }
            cout<<a[i].m<<"+"<<a[i].n<<"="<<a[i].m+a[i].n<<endl;
        }

        else if(k=='b')
        {
            int h=a[i].m-a[i].n,u=abs(h);

            while(u>0)
            {
                a[i].c++;
                u/=10;
            }
            if(h<0)
            {
                a[i].c++;
            }
            cout<<a[i].m<<"-"<<a[i].n<<"="<<a[i].m-a[i].n<<endl;

        }

        else
        {
            int h=a[i].m*a[i].n;
            while(abs(h)>0)
            {
                a[i].c++;
                h/=10;
            }
            if(h<0)
            {
                a[i].c++;
            }
            cout<<a[i].m<<"*"<<a[i].n<<"="<<a[i].m*a[i].n<<endl;
        }

        cout<<a[i].c<<endl;
    }
    return 0;
}

by Maisie586_ @ 2024-07-12 21:44:57

@tiangongyuanv 求关

#include <bits/stdc++.h>
int n,x,y;
char ss[20],ans[100],k;
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++) 
    {
        scanf("%s",&ss);
        if(isalpha(ss[0])) 
        {
            k=ss[0];
            scanf("%d%d",&x,&y);
        } 
        else 
        {
            x=atoi(ss);
            scanf("%d",&y);
        }
        if(k=='a')
            sprintf(ans,"%d+%d=%d",x,y,x+y);
        else if(k=='b')
            sprintf(ans,"%d-%d=%d",x,y,x-y);
        else
            sprintf(ans,"%d*%d=%d",x,y,x*y);
        printf("%s\n%d\n",ans,strlen(ans));
    }
    return 0;
}

|