_Phoebe__ 2022-03-28 11:34 采纳率: 96.9%
浏览 63
已结题

只能写成这样 主要是第4个测试和第5个要求不会实现 想知道怎么改

设计有理(分数)数计算器,要求如下:
(1)为有理数定义一个Rational类。有理数可以表示成为两个相除的整数,如3/4、13/3等。将有理数表示成int类型的两个值相除,分别表示分子和分母;
(2)通过接收从键盘输入a/b形式的分数生成有理数类的对象,在程序中可以识别出分子和分母并正确赋值;(要判断分母是否为0);
(3)采用运算符重载方法定义有理数的加、减、乘、除运算;(需要判断除数是否为0)
(4)设计有理数计算器测试程序,采用由计算机随机出题(用随机数),用户答题的形式,对有理数的加法、减法、乘法和除法进行测试。
(5)(选做题)设计记录功能,可以记录下不同用户使用计算器的情况,记录以文件形式保存。程序运行时要求输入用户名,显示该用户以前的记录。用户记录包括:做了多少次加法、减法、乘法和除法,进行测试的次数,测试的平均分等,在退出计算器程序时用新的用户记录代替原有记录。
要求用重载运算符


#include<bits/stdc++.h>
using namespace std;
class Rational{
    Rational(int numerator,int denomintor);//分别表示分子和分母 
    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 &is,Rational& r);//重载流输入 
    friend ostream &operator<<(ostream &os,Rational& r);//重载流输出 
    friend void printReal(Rational &r);
    friend void normalize();//化简函数
    private:
        int numerator;
        int denomintor; 
};
struct User //用户结构体类型定义
{
   char szName[20]; //用户名
   int nTime; //使用次数
   int nTest; //测试次数
   double alAve; //平均成绩
   int nAdd; //加法次数
   int nSub; //减法次数
   int nMul; //乘法次数
   double dlScore[3]; //3次测试得分
}user; 
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;// a/b
    int c = r2.numerator;
    int d = r2.denominator;// c/d
    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;// a/b
    int c = r2.numerator;
    int d = r2.denominator;// c/d
    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;// a/b
    int c = r2.numerator;
    int d = r2.denominator;// c/d
    int e = a * 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;// a/b
    int c = r2.numerator;
    int d = r2.denominator;// c/d
    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;// a/b
    int c = r2.numerator;
    int d = r2.denominator;// c/d a/b * d/c
    int e = a * d;
    int f = b * c;
    Rational R(e,f);
    return R;
}
istream &operator>>(istream &is,Rational &r){
    is>>r.numerator>>r.denominator;
    if(r.denominator!=0){
        return is;
    }
    else{
        cout<<"The denominator is 0, which is illegal. Please try again!" <<endl; 
        exit(0);
    }
}
ostream &operator<<(ostream &os,Rational &r){
    if(r.numerator%r.denominator==0){
        os<<r.numerator/r.denominator<<endl;
        return oa;
    }
    else
        os<<r.numerator<<"/"<<r.denominator;
        return os;
}
void Login() //当前用户信息函数
{
char szName[20];
cout<<"请输入您的姓名:";
cin.getline(szName,20);
ifstream infile;
User user1;
infile.open("user.dat",ios::binary|ios::in);
if(!infile)
{
cout<<"没有原始记录文件,您是第一个用户!\n";
strcpy(user.szName,szName);
user.nTest++;
return;
}
infile.read((char *)&user1,sizeof(User));
while(!infile.eof())
{
if(strcmp(user1.szName,szName)==0)
{
user=user1;
user.nTime++;
cout<<"欢迎您再次使用复数计算器!";
userprint();
cin.get();
infile.close();
return;
}
infile.read((char *) &user1,sizeof(User));
}
cout<<"欢迎您再次使用计算器!";
strcpy(user.szName,szName);
user.nTime++;
infile.close();
return;
}
void SaveFile() //用户资料保存函数
{
userprint();
fstream file;
User user1;
file.open("user.dat",ios::binary|ios::in|ios::out);
if(!file)
{
cout<<"文件打开错误,不能进行更新!\n";
return;
}
file.seekp(0,ios::beg);
while(!file.eof())
{
file.read((char *)&user1,sizeof(User));
if(strcmp(user1.szName,user.szName)==0)
{
file.seekp(-(sizeof(User)),ios::cur);
file.write((char *)&user,sizeof(User));
file.close();
return;
}
}
file.close();
fstream outfile;
outfile.open("user.dat",ios::binary|ios::app);
outfile.write((char *)&user,sizeof(User));
outfile.close();
return;
}
int main(void) 
{
srand(time(NULL)); //初始化随机数种子语句 随机数在哪产生怎么写捏
Login(); //当前用户信息函数
char strChoise[20]; //定义字符串名
do
{
system("cls");
cout<<"\t这是一个简单的计算器程序,可以实现以下功能,请按对应的按键(1-7)\n\n\n";
cout<<"\t=========================MENU===========================\n";
cout<<"\t1:有理数加法,以0结束\n";
cout<<"\t2:有理数减法,以0结束\n";
cout<<"\t3:测试分数加减乘法运算,1次测试10道题\n";
cout<<"\t4:有理数乘法,以0结束\n";
cout<<"\t5:有理数除法,以0结束\n"; 
cout<<"\t0:退出程序\n\n:";
cout<<"\t请您选择:";
cin>>strChoise;
if(strcmp(strChoise,"1")==0) 
 //这里怎么写捏 
else if(strcmp(strChoise,"2")==0) 
 //这里怎么写捏 
else if(strcmp(strChoise,"3")==0) 
 //这里怎么写捏 
else if(strcmp(strChoise,"4")==0) 
 //这里怎么写捏 
else if(strcmp(strChoise,"5")==0) 
 //这里怎么写捏 
else if(strcmp(strChoise,"0")==0) //用户选0则结束调用函数
{
cout<<"\n\n\t欢迎下次继续使用计算器!\n";
break;
}
else
{
cout<<"\n\t输入错误,请按任意键继续!\n";
cin.get();
cin.get();
}
}
while((strcmp(strChoise,"0")));
SaveFile(); //调用用户资料保存函数
return 0;
}

  • 写回答

2条回答 默认 最新

  • 关注

    在main函数中,用随机数生成两个数来构造Rational类的实例就可以了,然后再下面的加减乘除中,用两个Rational实例进行计算即可。参考如下:

    int m1 = rand() % 100 + 1; //生成1-100的随机数
            int n1 = rand() % 100 + 1;
            int m2 = rand() % 100 + 1;
            int n2 = rand() % 100 + 1;
            Rational a(m1,n1);
            Rational b(m2,n2);
    
            if (strcmp(strChoise, "1") == 0)
            {
                Rational c = a + b;
                printReal(c);
            }
            else if (strcmp(strChoise, "2") == 0)
            {
                Rational c = a - b;
                printReal(c);
            }//...后面的类似
    
    

    不同的用户,根据用户名来写文件就可以了,可以定义几个内置的用户,用户登录后,记录下当前登录的是哪个用户就可以了。
    你的代码错误太多了,给你改了一下:

    #define _CRT_SECURE_NO_WARNINGS 1
    //#include<bits/stdc++.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    class Rational {
    public:
        Rational(int numerator = 0, int denomintor = 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& is, Rational& r);//重载流输入 
        friend ostream& operator<<(ostream& os, Rational& r);//重载流输出 
        friend void printReal(Rational& r);
        friend Rational normalize(Rational s);//化简函数
    private:
        int numerator;
        int denomintor;
    };
    struct User //用户结构体类型定义
    {
        char szName[20]; //用户名
        int nTime; //使用次数
        int nTest; //测试次数
        double alAve; //平均成绩
        int nAdd; //加法次数
        int nSub; //减法次数
        int nMul; //乘法次数
        double dlScore[3]; //3次测试得分
    }user;
    Rational::Rational(int numerator, int denomintor)
    {
        this->numerator = numerator;
        this->denomintor = denomintor;
    }
    void printReal(Rational& r)
    {
        cout << r.numerator << "/" << r.denomintor;
    }
    Rational normalize(Rational s)
    {                                                                //求出分子和分母的最大公约数,用欧几里得算法
        int a = abs(s.numerator);
        int b = abs(s.denomintor);
        while (b > 0)
        {
            int t = a % b;
            a = b;
            b = t;
        }
        Rational R(s.numerator / a, s.denomintor / a);
        return R;
    }
    Rational operator+(Rational r1, Rational r2) {
        int a = r1.numerator;
        int b = r1.denomintor;// a/b
        int c = r2.numerator;
        int d = r2.denomintor;// c/d
        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.denomintor;// a/b
        int c = r2.numerator;
        int d = r2.denomintor;// c/d
        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.denomintor;// a/b
        int c = r2.numerator;
        int d = r2.denomintor;// c/d
        int e = a * c;
        int f = b * d;
        Rational R(e, f);
        return R;
    }
    Rational operator/(Rational r1, Rational r2) {
        int a = r1.numerator;
        int b = r1.denomintor;// a/b
        int c = r2.numerator;
        int d = r2.denomintor;// c/d
        int e = a * d + b * c;
        int f = b * d;
        Rational R(e, f);
        return R;
    }
    
    istream& operator>>(istream& is, Rational& r) {
        is >> r.numerator >> r.denomintor;
        if (r.denomintor != 0) {
            return is;
        }
        else {
            cout << "The denominator is 0, which is illegal. Please try again!" << endl;
            exit(0);
        }
    }
    ostream& operator<<(ostream& os, Rational& r) {
        if (r.numerator % r.denomintor == 0) {
            os << r.numerator / r.denomintor << endl;
            return os;
        }
        else
            os << r.numerator << "/" << r.denomintor;
        return os;
    }
    void userprint() {}
    void Login() //当前用户信息函数
    {
        char szName[20];
        cout << "请输入您的姓名:";
        cin.getline(szName, 20);
        ifstream infile;
        User user1;
        infile.open("user.dat", ios::binary | ios::in);
        if (!infile)
        {
            cout << "没有原始记录文件,您是第一个用户!\n";
            strcpy(user.szName, szName);
            user.nTest++;
            return;
        }
        infile.read((char*)&user1, sizeof(User));
        while (!infile.eof())
        {
            if (strcmp(user1.szName, szName) == 0)
            {
                user = user1;
                user.nTime++;
                cout << "欢迎您再次使用复数计算器!";
                userprint();
                cin.get();
                infile.close();
                return;
            }
            infile.read((char*)&user1, sizeof(User));
        }
        cout << "欢迎您再次使用计算器!";
        strcpy(user.szName, szName);
        user.nTime++;
        infile.close();
        return;
    }
    void SaveFile() //用户资料保存函数
    {
        userprint();
        fstream file;
        User user1;
        file.open("user.dat", ios::binary | ios::in | ios::out);
        if (!file)
        {
            cout << "文件打开错误,不能进行更新!\n";
            return;
        }
        file.seekp(0, ios::beg);
        while (!file.eof())
        {
            file.read((char*)&user1, sizeof(User));
            if (strcmp(user1.szName, user.szName) == 0)
            {
                file.seekp(-1 * (sizeof(User)), ios::cur);
                file.write((char*)&user, sizeof(User));
                file.close();
                return;
            }
        }
        file.close();
        fstream outfile;
        outfile.open("user.dat", ios::binary | ios::app);
        outfile.write((char*)&user, sizeof(User));
        outfile.close();
        return;
    }
    int main(void)
    {
        srand(time(NULL)); //初始化随机数种子语句 随机数在哪产生怎么写捏
        Login(); //当前用户信息函数
        int choise; //定义字符串名
    
        do
        {
            system("cls");
            cout << "\t这是一个简单的计算器程序,可以实现以下功能,请按对应的按键(1-7)\n\n\n";
            cout << "\t=========================MENU===========================\n";
            cout << "\t1:有理数加法,以0结束\n";
            cout << "\t2:有理数减法,以0结束\n";
            cout << "\t3:测试分数加减乘法运算,1次测试10道题\n";
            cout << "\t4:有理数乘法,以0结束\n";
            cout << "\t5:有理数除法,以0结束\n";
            cout << "\t0:退出程序\n\n:";
            cout << "\t请您选择:";
            cin >> choise;
    
            int m1 = rand() % 100 + 1; //生成1-100的随机数
            int n1 = rand() % 100 + 1;
            int m2 = rand() % 100 + 1;
            int n2 = rand() % 100 + 1;
            Rational a(m1, n1);
            Rational b(m2, n2);
    
            if (choise == 1)
            {
                Rational c = a + b;
                printReal(c);
            }
            else if (choise == 2)
            {
                Rational c = a - b;
                printReal(c);
            }
            else if (choise == 3)
            {
                //这里怎么写捏 
            }
            else if (choise == 4)
            {
                //这里怎么写捏 
            }
            else if (choise == 5)
            {
                //这里怎么写捏 
            }
            else if (choise == 0) //用户选0则结束调用函数
            {
                cout << "\n\n\t欢迎下次继续使用计算器!\n";
                break;
            }
            else
            {
                cout << "\n\t输入错误,请按任意键继续!\n";
                //cin.get();
                //cin.get();
            }
        } while (1);
        SaveFile(); //调用用户资料保存函数
        return 0;
    }
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月5日
  • 已采纳回答 3月28日
  • 修改了问题 3月28日
  • 创建了问题 3月28日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀