| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1362 人关注过本帖
标题:求助,怎样一用与复制构造函数啊
只看楼主 加入收藏
ct850230163
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-11-11
收藏
 问题点数:0 回复次数:0 
求助,怎样一用与复制构造函数啊
#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;
}

搜索更多相关主题的帖子: void include public 
2012-11-11 23:36
快速回复:求助,怎样一用与复制构造函数啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021080 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved