分数计算器
#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);
}
运行完计算是错误的 哪里错了捏