60分,麻烦大佬看看

P1957 口算练习题

superbFsj @ 2021-03-10 17:10:10


#include<iostream>
using namespace std;
int length(int a){
    int i;
    if(a<0){
        i=1;
    }else if(a==0){
        return 1;
    }else if(a>0){
        i=0;
    }
    while(a){
        i++;
        a /= 10;
    }
    return i;
}
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        string s;
        char ch;
        int a,b;
        cin>>s;
        switch(s[0]){
            case 'a' :cin>>a>>b;
                cout<<a<<"+"<<b<<"="<<a+b<<endl;
                cout<<length(a)+length(b)+length(a+b)+2<<endl;
                ch =s[0];
                break;
            case 'b' :cin>>a>>b;
                cout<<a<<"-"<<b<<"="<<a-b<<endl;
                cout<<length(a)+length(b)+length(a-b)+2<<endl;
                ch =s[0];
                break;
            case 'c' :cin>>a>>b;
                cout<<a<<"*"<<b<<"="<<a*b<<endl;
                cout<<length(a)+length(b)+length(a*b)+2<<endl;
                ch =s[0];
                break;
            default:
                cin>>b;
                a=0;
                for(int i=0;i<s.length();i++){
                    a = a*10+s[i]-'0';
                }
                switch(ch){
                    case 'a' :
                        cout<<a<<"+"<<b<<"="<<a+b<<endl;
                        cout<<length(a)+length(b)+length(a+b)<<endl;
                        ch =s[0];
                        break;
                    case 'b' :
                        cout<<a<<"-"<<b<<"="<<a-b<<endl;
                        cout<<length(a)+length(b)+length(a-b)<<endl;
                        ch =s[0];
                        break;
                    case 'c' :
                        cout<<a<<"*"<<b<<"="<<a*b<<endl;
                        cout<<length(a)+length(b)+length(a*b)<<endl;
                        ch =s[0];
                        break;
                    default: break;
                }
                break;
        } 
    }
    return 0;
} 

|