萌新求教,无法退出程序是为何,我这个输出格式对吗?

P1957 口算练习题

rabbitzph @ 2021-08-23 22:38:01


#include<iostream>
int m,n,cntm,cntn,cntr= 0;
using namespace std;
int main(){
    int n = 0;
    int sum = 0;
    int res = 0;
    int t1 = 0;
    int t2 = 0;
    int t3 = 0;
    int temp = 0;
    cin>>n;
    for(int x = 1;x<=n;x++){//控制循环次数 
        cntm=0;
        cntn=0;
        cntr=0;
        string str,str1;
        cin>>str;
        if(str[0]!=97&&str[0]!=98&&str[0]!=99){//判断是否输入了a,b,c ,若不是输入了a,b,c,强制使得这轮的运算符于上一轮相同  
            sum = 0;
            for(int i = 0;i<str.size();i++){ 
                if(i==0) temp = str.size();
                for(int u =1;u<temp;u++){
                    str[i]*=10;
                }
                sum+=str[i];
            }
            m = sum;
            cin>>n;
            str = str1;
        }
        else{//若输入了a,b,c,正常输入后两个数字 
            str1 = str;
            cin>>m>>n;                                                                      
        }
        t1=m;
        t2=n;
        while(t1>0){//计算位数 
            t1/=10;cntm++;
        }
        while(t2>0){
            t2/=10;cntn++;
        }
        if(str[0]=='a'){
            t3 = res = m+n;
            while(t3>0) t3/=10,cntr++;  
            cout<<m<<"+"<<n<<"="<<res<<endl;
            cout<<cntr+cntm+cntn+2<<endl;
        }
        else if(str[0] =='b'){
            t3 = res = m - n;
            t3 = labs(t3);
            while(t3>0) t3/=10,cntr++;
            cout<<m<<"-"<<n<<"="<<res<<endl;
            cout<<cntr+cntm+cntn+2<<endl;
        }
        else{
            t3 = res = m * n ;
            while(t3>0) t3/=10,cntr++;
            cout<<m<<"*"<<n<<"="<<res<<endl<<cntr+cntm+cntn+2<<endl;
        }   

    }
    return 0 ;

}

by 芝麻馅儿汤圆 @ 2021-08-24 00:05:40

#include <iostream>
#include <cstdio>
using namespace std;

int n;
string c;
char kind;
int num1, num2;
int ans;
int forbid;

void counting()
{
    ans = 0;
    if(num1 - num2 < 0 && kind == 'b')
        ans++;
    if(num1 == 0)
        ans++;
    if(num2 == 0)
        ans++;
    if(forbid == 0)
        ans++;
    while(num1)
    {
        ans++;
        num1 /= 10;
    }
    while(num2)
    {
        ans++;
        num2 /= 10;
    }
    while(forbid)
    {
        ans++;
        forbid /= 10;
    }
    ans += 2;
}

void thoue()
{
    if(kind == 'a')
    {
        cout << num1 << "+" << num2 << "=" << num1+num2 << endl;
        forbid = num1+num2;
    }
    else if(kind == 'b')
    {
        cout << num1 << "-" << num2 << "=" << num1-num2 << endl;
        forbid = num1-num2;
    }
    else
    {
        cout << num1 << "*" << num2 << "=" << num1*num2 << endl;
        forbid = num1*num2;
    }
}

int main()
{
    cin >> n;
    while(n--)
    {
        cin >> c;
        num1 = 0;
        num2 = 0;
        ans = 0;
        if(c[0] >= 'a' && c[0] <= 'c')
        {
            kind = c[0];
            cin >> num1 >> num2;
            thoue();
            counting();
            cout << ans << endl;
        }
        else
        {
            for(int k = 0; k < c.length(); k++)
            {
                num1 += c[k] - '0';
                if(k != (c.length() - 1))
                num1 = 10 * num1;
            }
            cin >> num2;
            thoue();
            counting();
            cout << ans << endl;
        }

    }
    return 0;
}

|