#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
///////////////////////定义有理数类/////////////////////////////////
class youlishu
{
private: //分子和分母说明为私有成员
int fenmu;
int fenzi;
public:
int get_fenzi(){return fenzi;}//取得分子
int get_fenmu(){return fenmu;}//取得分母
int set_fenmu(int ttmmpp){fenmu=ttmmpp;return fenmu;} //设置分母
int set_fenzi(int ttmmpp){fenzi=ttmmpp;return fenzi;} //设置分子
friend youlishu operator +(youlishu,youlishu);//利用友元重载
friend youlishu operator -(youlishu,youlishu);
friend youlishu operator *(youlishu,youlishu);
friend youlishu operator /(youlishu,youlishu);
friend int operator ==(youlishu,youlishu);
youlishu yhyoulishu(youlishu); //优化约分
void print(youlishu); //显示有理数
float floatyoulishu(youlishu); //有理数转化为实数
youlishu(){};//声明构造函数
};
///////////////////////////////////////////////////////////////////////////
//////////////////////////有理数优化算法(约分)////////////////////////////
youlishu yhyoulishu(youlishu b1)
{
int a,b,c,min;
b=b1.get_fenzi();c=b1.get_fenmu();
if(b<c) min=b;
else min=c;
for(a=1;a<=min;a++)
{if((b%a==0)&&(c%a==0))
{b=b/a;
c=c/a;
if(b<c)min=b;
else min=c;
a=1;
}
}
b1.set_fenzi(b);
b1.set_fenmu(c);
return b1;
}
///////////////////////////////////////////////////////////////////////
////////////////运算符重载开始////////////////////////////////////////
youlishu operator +(youlishu temp1,youlishu temp2) //重载+
{
youlishu temp;//定义公共对象
temp.set_fenzi(temp1.get_fenzi() * temp2.get_fenmu()+temp1.get_fenmu() * temp2.get_fenzi());//相加算法
temp.set_fenmu(temp1.get_fenmu() * temp2.get_fenmu());
return temp; //返回公共对象
}
youlishu operator -(youlishu temp1,youlishu temp2) //重载-
{
youlishu temp;
temp.set_fenzi(temp1.get_fenzi() * temp2.get_fenmu()-temp1.get_fenmu() * temp2.get_fenzi());//相减算法
temp.set_fenmu(temp1.get_fenmu() * temp2.get_fenmu());
return temp;
}
youlishu operator *(youlishu temp1,youlishu temp2) //重载*
{
youlishu temp;
temp.set_fenzi(temp1.get_fenzi() * temp2.get_fenzi());//乘算法
temp.set_fenmu(temp1.get_fenmu() * temp2.get_fenmu());
return temp;
}
youlishu operator /(youlishu temp1,youlishu temp2) //重载/
{
youlishu temp;
temp.set_fenzi(temp1.get_fenzi() * temp2.get_fenmu());//除乘算法
temp.set_fenmu(temp1.get_fenmu() * temp2.get_fenzi());
return temp;
}
int operator ==(youlishu temp1,youlishu temp2)//重载==运算符
{
if((temp1.get_fenzi()==temp2.get_fenzi())&&(temp1.get_fenmu()==temp2.get_fenmu()))
{
return 1; //2个有理数相等,返回1
}
else
{ return 0; //2个有理数不相等,返回0
}
}
/////////////////////////////////////////////////////////////////////
/////////////////显示有理数,有理数实数化///////////////////////////
void print(youlishu tempp)
{
cout<<tempp.get_fenzi()<<'/'<<tempp.get_fenmu();
}
float floatyoulishu(youlishu temp1)
{
float x,y,k//定义实数
x=float(temp1.get_fenzi());
y=float(temp1.get_fenmu());
k=x/y;
return (k);
}
///////////////////////////////////////////////////////////////////////
////////////////////////主函数////////////////////////////////////////
main()//主函数
{ char ttnn; int temp_fenzi,temp_fenmu;
loop://程序可以从此处循环执行
cout<<"\n\n\n #########有理数四则运算演示########"<<endl;
youlishu a1;
youlishu a2;
cout<<"请输入分子 : ";
cin>>temp_fenzi;
a1.set_fenzi(temp_fenzi);
cout<<endl;
cout<<"请输入分母 : ";
cin>>temp_fenmu;
a1.set_fenmu(temp_fenmu);
cout<<endl;
while (a1.get_fenmu()==0) //分母为0,则出错
{
cout<<"分母不能为0,请输入正确的数据"<<endl;
cout<<" 请输入分母 : ";
cin>>temp_fenmu;
a1.set_fenmu(temp_fenmu);
cout<<endl;
}
a1=yhyoulishu(a1);
cout<<"你输入的第一个有理数是:";
cout<<a1.get_fenzi()<<'/'<<a1.get_fenmu();
cout<<"请继续输入新的有理数的数据"<<endl;
cout<<"请输入分子 : ";
cin>>temp_fenzi;
a2.set_fenzi(temp_fenzi);
cout<<endl;
cout<<"请输入分母 : ";
cin>>temp_fenmu;
a2.set_fenmu(temp_fenmu);
cout<<endl;
while (a2.get_fenmu()== 0) //分母为0,则出错
{
cout<<"分母不能为0,请输入正确的数据"<<endl;
cout<<" 请输入分母 : ";
cin>>temp_fenmu;
a2.set_fenmu(temp_fenmu);
cout<<endl;
}
a2=yhyoulishu(a2);
cout<<"你输入的第二个有理数是:";
cout<<a2.get_fenzi()<<'/'<<a2.get_fenmu()<<endl;
cout<<"两个有理数四则计算结果为:"<<endl;//开始运算
cout<<"相加结果: ";
print(a1);
cout<<" + ";
print(a2);
cout<<" = ";
print(yhyoulishu(a1+a2));
cout<<endl;
cout<<"相减结果: ";
print(a1);
cout<<" - ";
print(a2);
cout<<" = ";
print(yhyoulishu(a1-a2));
cout<<endl;
cout<<"相乘结果: ";
print(a1);
cout<<" * ";
print(a2);
cout<<" = ";
print(yhyoulishu(a1*a2));
cout<<endl;
cout<<"相除结果: ";
print(a1);
cout<<" / ";
print(a2);
cout<<" = ";
print(yhyoulishu(a1/a2));
cout<<endl;
if(a1==a2) //2个有理数相等,则输出实数
{cout<<"您输入的两个有理数"<<a1.get_fenzi()<<'/'<<a1.get_fenmu();
cout<<"="<<a2.get_fenzi()<<'/'<<a2.get_fenmu()<<"="<<floatyoulishu(a1)<<endl;
}
cout<<"计算结束,您想继续演示吗?Y/N (请选择大写的Y或者N继续)";
ttnn=getch();
if(ttnn=='Y')goto loop;
return 0;
}