注册 登录
编程论坛 C++ Builder

求助,怎样一用与复制构造函数啊

ct850230163 发布于 2012-11-11 23:36, 1363 次点击
#include<iostream>
using namespace std;
class fraction{
  int above;
  int below;
  void reduction();
  void makeCommond(fraction);

public:
    fraction(int = 0,int = 1);
    fraction(fraction & temp);
    fraction add(fraction);
    fraction sub(fraction);
    fraction mul(fraction);
    fraction div(fraction);
    fraction reciprocal();
    bool epual(fraction);
    bool greatThan(fraction);
    bool lessThan(fraction);
    void diaplay();
    void input();
};

fraction::fraction(int x,int y){
  above = x;
  below = y;
}
inline fraction::fraction(fraction & temp){
 above = temp.above;
 below = temp.below;
}

void fraction::reduction(){
    int a,b,c;
     a=below;
     b=above;
    while(b!=0)
    {
       c=a%b;
       a=b;
       b=c ;
    }
    above=above/a;
    below=below/a;
}
void fraction::makeCommond(fraction f1){
 int temp;
 f1.reduction();
 above*=f1.below;
 f1.above*=below;
 temp = below*f1.below;
 below = f1.below;
 f1.below = temp;
}

fraction fraction::add(fraction f1){
fraction temp;

temp.above = above*f1.below +below* f1.above;
temp.below = below*f1.below;
temp.reduction();
return temp;
}

fraction fraction::sub(fraction f1){
fraction temp;
temp.above = above*f1.below - below* f1.above;
temp.below = below*f1.below;
temp.reduction();
return temp;
}

fraction fraction::mul(fraction f1){
fraction temp;
temp.above = above*f1.above;
temp.below = below*f1.below;
temp.reduction();
return temp;
}

fraction fraction::div(fraction f1){
fraction temp;
 if(f1.above==0)
 {
 cout<<"零不能做除数!"<<endl;
 }
 temp.above = above*f1.below;
 temp.below = below*f1.above;
temp.reduction();
return temp;
}

fraction fraction::reciprocal(){
fraction temp;
temp.above = below;
temp.below = above;
temp.reduction();
return temp;
}

bool fraction::epual(fraction f1){
if(above == f1.above )
{
    return true;
}
else
{
  return false;
}
}

bool fraction::greatThan(fraction f1)
{
 int a,b;
a = above*f1.below;
b = f1.above*below;
if(a>b)
{
return false;
}
else
{
return true;
}
 }

bool fraction::lessThan(fraction f1){
 int a,b;
a = above*f1.below;
b = f1.above*below;
if(a<b)
{
return false;
}
else
{
return true;
}
}

void fraction::diaplay(){
 reduction();
 cout<<"分数:"<<above<<"/"<<below<<endl;
}
void fraction::input()
{
  cout<<"请输入分数的分子和分母(整数):"<<endl;
  cin>>above>>below;
  if(below==0)
      cout<<"分母不能为零"<<endl;
  else
      reduction();
}

int main(void)
{
 fraction f1,f2,f3,f;
 f1.input();
 f2.input();
 cout<<"f1";f1.diaplay();
 cout<<"f2";f2.diaplay();
 f=f1.add(f2);
 cout<<"两数之和"<<endl;
 f.diaplay();
 f=f1.sub(f2);
 cout<<"两数之差"<<endl;
 f.diaplay();
 f=f1.mul(f2);
  cout<<"两数之积"<<endl;
 f.diaplay();
 f= f1.div(f2);
  cout<<"两数之商"<<endl;
 f.diaplay();
 if (f1.epual(f2))
 {
 cout<<"f1==f2"<<endl;
 exit(0);
 }
 else
 {
 cout<<"f1!=f2"<<endl;
 }
 if(!f1.greatThan(f2))
 {
    cout<<"f1"<<'\t'<<"greaterthan"<<'\t'<<"f2"<<endl;
 }
 else
 {
    cout<<"f2"<<'\t'<<"greaterthan"<<'\t'<<"f1"<<endl;
 }

if(!f1.lessThan(f2))
 {
    cout<<"f1"<<'\t'<<"lessthan"<<'\t'<<"f2"<<endl;
 }
 else
 {
    cout<<"f2"<<'\t'<<"lessthan"<<'\t'<<"f1"<<endl;
 }
 f3 = f2.reciprocal();
 cout<<"f3= 1/f2"<<endl;
 f3.diaplay();
 
 cout<<"由复制构造函数生成的temp:"<<endl;
 fraction temp(f1);
 temp.diaplay;
 return 0;
}

0 回复
1