_Phoebe__ 2022-03-29 12:34 采纳率: 96.9%
浏览 34
已结题

void Test函数是错误的 想知道怎么改

分数计算器


#include<bits/stdc++.h>
using namespace std;
class Rational{
    int numerator;                                            
    int denominator;
    void normalize_();
public:
    Rational(int numerator=0, int denominator=0);  
    friend Rational operator+(Rational r1, Rational r2);        
    friend Rational operator-(Rational r1, Rational r2);
    friend Rational operator*(Rational r1, Rational r2);
    friend Rational operator/(Rational r1, Rational r2);
    friend istream &operator>>(istream &in, Rational& r);        
    friend ostream &operator<<(ostream &out, Rational& r);    
    friend bool operator==(Rational r1,Rational r2);
    Rational normalize();      
};
Rational::Rational(int num, int denom)
{
    numerator = num;
    if (denominator != 0)   denominator = denom;                // 分母不能为0,在流输入运算符重载中也有定义
    else denominator = 1;
    normalize_();
}
Rational Rational::normalize()                            
{                                                                //求出分子和分母的最大公约数,用欧几里得算法
    int a = abs(numerator);
    int b = abs(denominator);
    while (b > 0)
    {
        int t = a % b;
        a = b;
        b = t;
    }
    Rational R(numerator / a, denominator / a);
    return R;
}
Rational operator+(Rational r1, Rational r2)                    //重载加法运算符,分数的加法
{
    int a = r1.numerator;
    int b = r1.denominator;
    int c = r2.numerator;
    int d = r2.denominator;
    int e = a * d + b * c;
    int f = b * d;
    Rational R(e, f);
    return R;
}
void Rational::normalize_()
{

                                                                //求出分子和分母的最大公约数,用欧几里得算法
    int a = abs(numerator);
    int b = abs(denominator);
    while (b > 0)
    {
        int t = a % b;
        a = b;
        b = t;
    }
    numerator /= a;
    denominator /= a;
}

Rational operator-(Rational r1, Rational r2)                    //重载减法运算符,函数结构和加法一致,返回加法运算
{
    int a = r1.numerator;
    int b = r1.denominator;
    int c = r2.numerator;
    int d = r2.denominator;
    int e = a * d - b * c;
    int f = b * d;
    Rational R(e, f);
    return R;
}

Rational operator*(Rational r1, Rational r2)                    //重载乘法运算符,调用初始化类是化简了分数
{
    int a = r1.numerator;
    int b = r1.denominator;
    int c = r2.numerator;
    int d = r2.denominator;
    int e = a * c;
    int f = b * d;
    Rational R(e, f);
    return R;
}

Rational operator/(Rational r1, Rational r2)                    //返回乘法运算函数体
{
    int t = r2.numerator;
    r2.numerator = r2.denominator;
    r2.denominator = t;
    return operator*(r1, r2);
}
bool operator==(Rational r1,Rational r2)
{
     if(r1.numerator ==r2.numerator && r1.denominator ==r2.denominator )
        return true;
    else
        return false;
}
istream &operator>>(istream &in, Rational& r)                    //流输入运算符
{
    in >> r.numerator>> r.denominator;                            //避免除0错误,进行判断,分母为0时程序结束
    if (r.denominator != 0)
        return in;
    else
    {
        cout << "输入有误";
        exit(0);
    }
}

ostream &operator<<(ostream &out, Rational& r)                    //流输出运算符
{
    if (r.numerator%r.denominator == 0)                            //进行判断,当为一个整数时,改变输出
        out << r.numerator / r.denominator;
    else
        out << r.numerator << "\\"<<r.denominator;
        return out;
}
void Test(){//有问题
   cout<<"共10道题,做100以内的加减运算,满分100分:\n";
   int score=0;
   char op;
   for(int i=0;i<=9;i++)
{
    int m1 = rand() % 10 + 1; //生成1-100的随机数
    int n1 = rand() % 100 + 1;
    int m2 = rand() % 10 + 1;
    int n2 = rand() % 100 + 1;
    Rational a(m1,n1),b(m2,n2);
    int m3 = rand()%20-10;
    int n3 = rand()%20-10;
    int m4 = rand()%20-10;
    int n4 = rand()%20-10;
    Rational c(m3,n3),d(m4,n4);
    Rational answer;
    op=rand()%4; //产生随机加减乘除运算的4个值
    switch(op)
 {
    case 0:
    answer=a+b;
    cout<<a<<"加上"<<b<<"等于";
    break;
    case 1:
    cout<<a<<"减去"<<b<<"等于";
    answer =a-b;
    break;
    case 2:
    answer=c*d;
    cout<<c<<"乘以"<<d<<"等于";
    break;
    case 3:
    answer=c/d;
    cout<<c<<"除以以"<<d<<"等于";
    break;    
}
      Rational temp;
    cin>>temp; //输入用户计算值
    if(answer==temp) //比较用户计算值
{
score+=10;
}
else
{
cout<<"此题做错了\n";
cout<<"正确答案为:"<<answer<<endl;
}
}
cout<<"你的最后得分是:"<<score<<endl;
}
int main(){
    Rational r1(1,2);
    Rational r2(1,3);
    Rational r3(1,4);
do{
    system("cls");
    cout << "\t这是一个简单的计算器程序,可以实现以下功能,请按对应的按键(1-5)\n\n\n";
    cout << "\t=========================MENU===========================\n";
    cout << "\t1:有理数加法,以0结束\n";
    cout << "\t2:有理数减法,以0结束\n";    
    cout << "\t3:有理数乘法,以0结束\n";
    cout << "\t4:有理数除法,以0结束\n";
    cout << "\t5测试:每次10题 由计算机随机出题 每题10分 共100分";
    cout << "\t0:退出程序\n\n:";
    cout << "\t请您选择:";
    int choise;
    cin >> choise;
    if (choise == 1)
    {   
        cout << "first number r1:"<<endl;
        cin >> r1;
        cout << "last number r2:"<<endl;
        cin >> r2;
        r3=r1+r2;
        cout<<r3<<endl; 
    }
    else if (choise == 2)
    {   cout << "first number r1:"<<endl;
        cin >> r1;
        cout << "last number r2:"<<endl;
        cin >> r2;
        r3=r1-r2;
        cout<<r3<<endl;
    }
    else if (choise == 3)
    {   cout << "first number r1:"<<endl;
        cin >> r1;
        cout << "last number r2:"<<endl;
        cin >> r2;
        r3=r1*r2;
        cout<<r3<<endl;
    }
    else if (choise == 4)
    {   cout << "first number r1:"<<endl;
        cin >> r1;
        cout << "last number r2:"<<endl;
        cin >> r2;
        r3=r1/r2;
        cout<<r3<<endl; 
    }
    else if(choise == 5){
        Test();
    }
    else if (choise == 0) //用户选0则结束调用函数
    {
            cout << "\n\n\t欢迎下次继续使用计算器!\n";
            break;
     }
    else
    {
            cout << "\n\t输入错误,请按任意键继续!\n";
    }system("pause");
}while(1);
}

运行完计算是错误的 哪里错了捏

  • 写回答

3条回答 默认 最新

  • CSDN专家-link 2022-03-29 13:10
    关注
    Rational answer;===这行有问题,也就是    Rational 类的构造函数 Rational(int numerator=0, int denominator=0);有问题
    

    当定义answer时,由于没有传入参数,导致两个参数值默认都是0,这样在normalize_();函数中就崩溃了,因为 numerator /= a;
    denominator /= a;时,a是0,所以出现除零错误。
    在构造函数中对参数为零进行判断,为0则不进行normalize_()函数处理,或者增加一个无参构造函数,什么也不用做,然后当前构造函数中去掉默认值。这样也可以

    using namespace std;
    class Rational{
        int numerator;                                            
        int denominator;
        void normalize_();
    public:
        Rational() {}
        Rational(int numerator, int denominator);  
        friend Rational operator+(Rational r1, Rational r2);        
        friend Rational operator-(Rational r1, Rational r2);
        friend Rational operator*(Rational r1, Rational r2);
        friend Rational operator/(Rational r1, Rational r2);
        friend istream &operator>>(istream &in, Rational& r);        
        friend ostream &operator<<(ostream &out, Rational& r);    
        friend bool operator==(Rational r1,Rational r2);
        Rational normalize();      
    };
    Rational::Rational(int num, int denom)
    {
        numerator = num;
        if (denominator != 0)   denominator = denom;                // 分母不能为0,在流输入运算符重载中也有定义
        else denominator = 1;
        normalize_();
    }
    Rational Rational::normalize()                            
    {                                                                //求出分子和分母的最大公约数,用欧几里得算法
        int a = abs(numerator);
        int b = abs(denominator);
        while (b > 0)
        {
            int t = a % b;
            a = b;
            b = t;
        }
        Rational R(numerator / a, denominator / a);
        return R;
    }
    Rational operator+(Rational r1, Rational r2)                    //重载加法运算符,分数的加法
    {
        int a = r1.numerator;
        int b = r1.denominator;
        int c = r2.numerator;
        int d = r2.denominator;
        int e = a * d + b * c;
        int f = b * d;
        Rational R(e, f);
        return R;
    }
    void Rational::normalize_()
    {
     
                                                                    //求出分子和分母的最大公约数,用欧几里得算法
        int a = abs(numerator);
        int b = abs(denominator);
        while (b > 0)
        {
            int t = a % b;
            a = b;
            b = t;
        }
        numerator /= a;
        denominator /= a;
    }
     
    Rational operator-(Rational r1, Rational r2)                    //重载减法运算符,函数结构和加法一致,返回加法运算
    {
        int a = r1.numerator;
        int b = r1.denominator;
        int c = r2.numerator;
        int d = r2.denominator;
        int e = a * d - b * c;
        int f = b * d;
        Rational R(e, f);
        return R;
    }
     
    Rational operator*(Rational r1, Rational r2)                    //重载乘法运算符,调用初始化类是化简了分数
    {
        int a = r1.numerator;
        int b = r1.denominator;
        int c = r2.numerator;
        int d = r2.denominator;
        int e = a * c;
        int f = b * d;
        Rational R(e, f);
        return R;
    }
     
    Rational operator/(Rational r1, Rational r2)                    //返回乘法运算函数体
    {
        int t = r2.numerator;
        r2.numerator = r2.denominator;
        r2.denominator = t;
        return operator*(r1, r2);
    }
    bool operator==(Rational r1,Rational r2)
    {
         if(r1.numerator ==r2.numerator && r1.denominator ==r2.denominator )
            return true;
        else
            return false;
    }
    istream &operator>>(istream &in, Rational& r)                    //流输入运算符
    {
        in >> r.numerator>> r.denominator;                            //避免除0错误,进行判断,分母为0时程序结束
        if (r.denominator != 0)
            return in;
        else
        {
            cout << "输入有误";
            exit(0);
        }
    }
     
    ostream &operator<<(ostream &out, Rational& r)                    //流输出运算符
    {
        if (r.numerator%r.denominator == 0)                            //进行判断,当为一个整数时,改变输出
            out << r.numerator / r.denominator;
        else
            out << r.numerator << "\\"<<r.denominator;
            return out;
    }
    void Test(){//有问题
       cout<<"共10道题,做100以内的加减运算,满分100分:\n";
       int score=0;
       char op;
       for(int i=0;i<=9;i++)
    {
        int m1 = rand() % 10 + 1; //生成1-100的随机数
        int n1 = rand() % 100 + 1;
        int m2 = rand() % 10 + 1;
        int n2 = rand() % 100 + 1;
        Rational a(m1,n1),b(m2,n2);
        int m3 = rand()%20-10;
        int n3 = rand()%20-10;
        int m4 = rand()%20-10;
        int n4 = rand()%20-10;
        Rational c(m3,n3),d(m4,n4);
        Rational answer;
        op=rand()%4; //产生随机加减乘除运算的4个值
        switch(op)
     {
        case 0:
        answer=a+b;
        cout<<a<<"加上"<<b<<"等于";
        break;
        case 1:
        cout<<a<<"减去"<<b<<"等于";
        answer =a-b;
        break;
        case 2:
        answer=c*d;
        cout<<c<<"乘以"<<d<<"等于";
        break;
        case 3:
        answer=c/d;
        cout<<c<<"除以以"<<d<<"等于";
        break;    
    }
          Rational temp;
        cin>>temp; //输入用户计算值
        if(answer==temp) //比较用户计算值
    {
    score+=10;
    }
    else
    {
    cout<<"此题做错了\n";
    cout<<"正确答案为:"<<answer<<endl;
    }
    }
    cout<<"你的最后得分是:"<<score<<endl;
    }
    int main(){
        Rational r1(1,2);
        Rational r2(1,3);
        Rational r3(1,4);
    do{
        system("cls");
        cout << "\t这是一个简单的计算器程序,可以实现以下功能,请按对应的按键(1-5)\n\n\n";
        cout << "\t=========================MENU===========================\n";
        cout << "\t1:有理数加法,以0结束\n";
        cout << "\t2:有理数减法,以0结束\n";    
        cout << "\t3:有理数乘法,以0结束\n";
        cout << "\t4:有理数除法,以0结束\n";
        cout << "\t5测试:每次10题 由计算机随机出题 每题10分 共100分";
        cout << "\t0:退出程序\n\n:";
        cout << "\t请您选择:";
        int choise;
        cin >> choise;
        if (choise == 1)
        {   
            cout << "first number r1:"<<endl;
            cin >> r1;
            cout << "last number r2:"<<endl;
            cin >> r2;
            r3=r1+r2;
            cout<<r3<<endl; 
        }
        else if (choise == 2)
        {   cout << "first number r1:"<<endl;
            cin >> r1;
            cout << "last number r2:"<<endl;
            cin >> r2;
            r3=r1-r2;
            cout<<r3<<endl;
        }
        else if (choise == 3)
        {   cout << "first number r1:"<<endl;
            cin >> r1;
            cout << "last number r2:"<<endl;
            cin >> r2;
            r3=r1*r2;
            cout<<r3<<endl;
        }
        else if (choise == 4)
        {   cout << "first number r1:"<<endl;
            cin >> r1;
            cout << "last number r2:"<<endl;
            cin >> r2;
            r3=r1/r2;
            cout<<r3<<endl; 
        }
        else if(choise == 5){
            Test();
        }
        else if (choise == 0) //用户选0则结束调用函数
        {
                cout << "\n\n\t欢迎下次继续使用计算器!\n";
                break;
         }
        else
        {
                cout << "\n\t输入错误,请按任意键继续!\n";
        }system("pause");
    }while(1);
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月7日
  • 已采纳回答 3月30日
  • 创建了问题 3月29日

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)