我写了这么一段代码,但是效果并不能化简啊,请高手您指点一二,方便的话请留下您的代码,供在下一参考,谢谢了!
int getcommon()
{
int m,n,r;
if(a>b){
m=a;n=b;}
else{m=b;n=a;}
r=b;
while(r!=0)
{
r=m%n;
m=n;
n=r;
}
return m;
}//求最大公约数.
void huajian()
{
int c=getcommon();
a=(a/c);
b=(b/c);
} //a,b分别为分子,分母,为分数类了private成员,这两的函数为成员函数
下面是我写的整个函数,麻烦您看一下,在下实在是找不出哪里出了问题,输出结果老是出错,谢谢了!方便的话留个联系方式!
#include
class Rational
{
public:
Rational()
{
a=3;b=9;
}
Rational(int c,int d):a(c),b(d){}
int getcommon()
{
int m,n,r;
if(a>b){
m=a;n=b;}
else{m=b;n=a;}
r=b;
while(r!=0)
{
r=m%n;
m=n;
n=r;
}
return m;
}
void huajian()
{
int c=getcommon();
a=(a/c);
b=(b/c);
}
void display()
{
huajian();
cout<<"所表示的分数为:\n"<}
int geta(){return a;}
int getb(){return b;}
void add(Rational &ran)
{
if(b/ran.getb()==0 || ran.getb()/b==0)
{
if(b>ran.getb())
a= (b/ran.getb()*ran.geta()+a);
else
{b=ran.getb();
a=ran.getb()/b*a+ran.geta();
}
}
else
b=b*ran.getb();
a=a*ran.getb()+ran.geta()*b;
huajian();
cout<<"两个数的和为"<
}
void multiply(Rational& ren)
{
a=a*(ren.geta());
b=b*(ren.getb());
cout<<"分数的积为:\n";
huajian();
cout< }
void devide(Rational& r)
{
cout<<"分数的商为:\n";
a=a*r.geta();
b=b*r.getb();
huajian();
cout< }
void minus(Rational &ran)
{
if(b/ran.getb()==0 || ran.getb()/b==0)
{
if(b>ran.getb())
a=a-ran.geta()*b/ran.getb();
else
b=ran.getb();
a=a*ran.getb()/b-ran.geta();
}
else
b=b*ran.getb();
a=a*ran.getb()-ran.geta()*b;
{
huajian();
cout<<"两个数的差为 "< }
}
private:
int a ;
int b;
};
int main()
{
Rational a;
cout< Rational b(3,6);
cout< cout<<"the first Rational is"< a.display();
cout<<"the second Rational is"< b.display();
cout<<"the add result is:\n";
a.add(b);
cout<<"the multi result is"<a.multiply( b);
cout<<"the devide result is"<a.devide( b);
cout<<"the minus result is:"<a.minus(b);
return 0;
}
--------------------next---------------------
#include "stdafx.h"
#include
using namespace std;
class Rational
{
public:
Rational()
{
a=3;b=9;
};
Rational(int c,int d):a(c),b(d){};
int getcommon()
{
int m,n,r;
if(a>b)
{
m=a;n=b;
}
else
{
m=b;n=a;
}
r=b;
while(r!=0)
{
r=m%n;
m=n;
n=r;
}
return m;
};
void huajian()
{
int c=getcommon();
a=(a/c);
b=(b/c);
}
void display()
{
huajian();
cout<<"所表示的分数为:\n"< }
int geta(){return a;}
int getb(){return b;}
void add(Rational &ran)
{
int tb=b*ran.getb();
int ta=a*ran.getb()+ran.geta()*b;
b=tb;
a=ta;
huajian();
cout<<"两个数的和为"< }
void multiply(Rational& ren)
{
a=a*(ren.geta());
b=b*(ren.getb());
cout<<"分数的积为:\n";
huajian();
cout< }
void devide(Rational& r)
{
cout<<"分数的商为:\n";
int ta=a*r.getb();
int tb=b*r.geta();
a=ta;
b=tb;
huajian();
cout< }
void minus(Rational &ran)
{
int tb=b*ran.getb();
int ta=a*ran.getb()-ran.geta()*b;
a=ta;
b=tb;
huajian();
cout<<"两个数的差为 "< }
private:
int a ;
int b;
};
int _tmain(int argc, _TCHAR* argv[])
{
Rational a;
cout< Rational b(9,6);
cout< cout<<"the first Rational is"< a.display();
cout<<"the second Rational is"< b.display();
cout<<"the add result is:\n";
a.add(b);
cout<<"the multi result is"< a.multiply( b);
cout<<"the devide result is"< a.devide( b);
cout<<"the minus result is:"< a.minus(b);
return 0;
}
--------------------next---------------------
阅读(1007) | 评论(0) | 转发(0) |