编译环境 dev c++
#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
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-5)\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)
{
Rational c = a * b;
printReal(c);
}
else if (choise == 5)
{
Rational c = a / b;
printReal(c);
}
else if (choise == 0) //用户选0则结束调用函数
{
cout << "\n\n\t欢迎下次继续使用计算器!\n";
break;
}
else
{
cout << "\n\t输入错误,请按任意键继续!\n";
}
} while (1);
SaveFile(); //调用用户资料保存函数
return 0;
}
太奇怪了 调试好像也没啥错误 到底哪里错了呢