作者在 2011-06-27 08:59:14 发布以下内容
为了领积分哈!随便贴一段代码哈,O(∩_∩)O~!
#include<iostream>
#include<string>
using namespace std;
class fraction;
//const fraction operator=(const fraction &f1,const fraction &f2);
const fraction operator+(const fraction &f1,const fraction &f2);
const fraction operator-(const fraction &f1,const fraction &f2);
const fraction operator*(const fraction &f1,const fraction &f2);
const fraction operator/(const fraction &f1,const fraction &f2);
class fraction
{
public:
fraction(int aa,int bb);
fraction();
~fraction();
int getab(int a,int b);
void const show();
const fraction & operator=(const fraction &f);
friend const fraction operator+(const fraction &f1,const fraction &f2);
friend const fraction operator-(const fraction &f1,const fraction &f2);
friend const fraction operator*(const fraction &f1,const fraction &f2);
friend const fraction operator/(const fraction &f1,const fraction &f2);
private:
int a;//分子
int b;//分母
};
//fraction 类成员函数的实现
fraction::fraction(int aa ,int bb ):a(aa),b(bb)
{
//a = aa;
//b = bb;
}
fraction::fraction()
{
a = 0;
b = 1;
}
fraction::~fraction()
{
}
int fraction::getab(int aa,int bb)
{
a = aa;
b = bb;
return 0;
}
void const fraction::show()
{
cout<<"("<<a<<"/"<<b<<")";
}
const fraction & fraction::operator=(const fraction &f)
{
a = f.a;
b = f.b;
return *this;
}
//////////////////////////////////////////////////////////////////////
//运算符重载
const fraction operator+(const fraction &f1,const fraction &f2)
{
fraction fra;
fra.a = (f1.a*f2.b + f1.b*f2.a);
fra.b = (f1.b*f2.b);
return fra;
}
const fraction operator-(const fraction &f1,const fraction &f2)
{
fraction fra;
fra.a = (f1.a*f2.b - f1.b*f2.a);
fra.b = (f1.a*f2.b);
return fra;
}
const fraction operator*(const fraction &f1,const fraction &f2)
{
fraction fra;
fra.a = (f1.a*f2.a);
fra.b = (f1.a*f2.b);
return fra;
}
const fraction operator/(const fraction &f1,const fraction &f2)
{
fraction fra;
fra.a = (f1.a*f2.b);
fra.b = (f1.b*f2.a);
return fra;
}
////////////////////////////////////////////////////////////////
int main()
{
fraction fen1(1,2);
//fen1.show();
//cout<<endl;
fraction fen2(5,6);
//fen2.show();
//cout<<endl;
fraction fen3,fen4,fen5,fen6;
fen3 = (fen1 + fen2);
fen1.show();
cout<< "+" ;
fen2.show();
cout<< " = " ;
fen3.show();
cout<< endl;
fen4 = (fen2 - fen1);
fen1.show();
cout<< "-" ;
fen2.show();
cout<< " = " ;
fen4.show();
cout<< endl;
fen5 = (fen2 * fen1);
fen1.show();
cout<< "*" ;
fen2.show();
cout<< " = " ;
fen5.show();
cout<< endl;
fen6 = (fen1 / fen2);
fen1.show();
cout<< " / " ;
fen2.show();
cout<< " = " ;
fen6.show();
cout<< endl;
return 0;
}
#include<string>
using namespace std;
class fraction;
//const fraction operator=(const fraction &f1,const fraction &f2);
const fraction operator+(const fraction &f1,const fraction &f2);
const fraction operator-(const fraction &f1,const fraction &f2);
const fraction operator*(const fraction &f1,const fraction &f2);
const fraction operator/(const fraction &f1,const fraction &f2);
class fraction
{
public:
fraction(int aa,int bb);
fraction();
~fraction();
int getab(int a,int b);
void const show();
const fraction & operator=(const fraction &f);
friend const fraction operator+(const fraction &f1,const fraction &f2);
friend const fraction operator-(const fraction &f1,const fraction &f2);
friend const fraction operator*(const fraction &f1,const fraction &f2);
friend const fraction operator/(const fraction &f1,const fraction &f2);
private:
int a;//分子
int b;//分母
};
//fraction 类成员函数的实现
fraction::fraction(int aa ,int bb ):a(aa),b(bb)
{
//a = aa;
//b = bb;
}
fraction::fraction()
{
a = 0;
b = 1;
}
fraction::~fraction()
{
}
int fraction::getab(int aa,int bb)
{
a = aa;
b = bb;
return 0;
}
void const fraction::show()
{
cout<<"("<<a<<"/"<<b<<")";
}
const fraction & fraction::operator=(const fraction &f)
{
a = f.a;
b = f.b;
return *this;
}
//////////////////////////////////////////////////////////////////////
//运算符重载
const fraction operator+(const fraction &f1,const fraction &f2)
{
fraction fra;
fra.a = (f1.a*f2.b + f1.b*f2.a);
fra.b = (f1.b*f2.b);
return fra;
}
const fraction operator-(const fraction &f1,const fraction &f2)
{
fraction fra;
fra.a = (f1.a*f2.b - f1.b*f2.a);
fra.b = (f1.a*f2.b);
return fra;
}
const fraction operator*(const fraction &f1,const fraction &f2)
{
fraction fra;
fra.a = (f1.a*f2.a);
fra.b = (f1.a*f2.b);
return fra;
}
const fraction operator/(const fraction &f1,const fraction &f2)
{
fraction fra;
fra.a = (f1.a*f2.b);
fra.b = (f1.b*f2.a);
return fra;
}
////////////////////////////////////////////////////////////////
int main()
{
fraction fen1(1,2);
//fen1.show();
//cout<<endl;
fraction fen2(5,6);
//fen2.show();
//cout<<endl;
fraction fen3,fen4,fen5,fen6;
fen3 = (fen1 + fen2);
fen1.show();
cout<< "+" ;
fen2.show();
cout<< " = " ;
fen3.show();
cout<< endl;
fen4 = (fen2 - fen1);
fen1.show();
cout<< "-" ;
fen2.show();
cout<< " = " ;
fen4.show();
cout<< endl;
fen5 = (fen2 * fen1);
fen1.show();
cout<< "*" ;
fen2.show();
cout<< " = " ;
fen5.show();
cout<< endl;
fen6 = (fen1 / fen2);
fen1.show();
cout<< " / " ;
fen2.show();
cout<< " = " ;
fen6.show();
cout<< endl;
return 0;
}