题目描述
Fraction类的基本形式如下:
要求如下:
1.实现Fraction类;common_divisor()和contracted()函数体可为空,不实现具体功能。
2.编写main函数,初始化两个Fraction对象的,计算它们之间的加减乘除。
输入
第1行:依次输入第1个和第2个Fraction对象的分子和分母值。
输出
每行依次分别输出加减乘除计算后的Fraction对象(直接输出分数值,不需要约简)。
输入样例
1 3 2 5
输出样例
fraction=11/15
fraction=-1/15
fraction=2/15
fraction=5/6
代码
#include<iostream>
using namespace std;
class Fraction{
private:
int numerator,denominator;
int common_dixisor();
void contracted();
public:
Fraction(int=0,int=1);
Fraction(Fraction&);
Fraction operator+(Fraction);
Fraction operator-(Fraction);
Fraction operator*(Fraction);
Fraction operator/(Fraction);
void Set(int=0,int=1);
void disp(){cout<<"fraction="<<numerator<<"/"<<denominator<<endl;}
};
Fraction::Fraction(int x,int y):numerator(x),denominator(y){}
Fraction::Fraction(Fraction& t){numerator=t.numerator;denominator=t.denominator;}
void Fraction::Set(int x,int y)
{
numerator = x;
denominator = y;
}
Fraction Fraction::operator+(Fraction t){
Fraction temp;
int temp1,temp2,sum1,sum2;
sum1=numerator*t.denominator+t.numerator*denominator;
sum2=t.denominator*denominator;
temp.Set(sum1,sum2);
return temp;
}
Fraction Fraction::operator-(Fraction t){
Fraction temp;
int temp1,temp2,sum1,sum2;
sum1=numerator*t.denominator-denominator*t.numerator;
sum2=denominator*t.denominator;
temp.Set(sum1,sum2);
return temp;
}
Fraction Fraction::operator*(Fraction t){
Fraction temp;
int sum1,sum2;
sum1=numerator*t.numerator;
sum2=denominator*t.denominator;
temp.Set(sum1,sum2);
return temp;
}
Fraction Fraction::operator/(Fraction t){
Fraction temp;
int sum1,sum2;
sum1=numerator*t.denominator;
sum2=denominator*t.numerator;
temp.Set(sum1,sum2);
return temp;
}
int main()
{
int a1,b1,a2,b2;
cin>>a1>>b1>>a2>>b2;
Fraction t1(a1,b1);
Fraction t2(a2,b2);
Fraction T;
T=t1+t2;
T.disp();
T.Set(0,1);
T=t1-t2;
T.disp();
T.Set(0,1);
T=t1*t2;
T.disp();
T.Set(0,1);
T=t1/t2;
T.disp();
return 0;
}