设计有理(分数)数计算器,要求如下:
(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;
}