[bo][un]tfg0116[/un] 在 2008-10-29 22:31 的发言:[/bo]
#include<iostream>
using namespace std;
class Fract
{
int num;
int den;
public:
Fract(int a = 0, int b = 1)
{
num = a;
den = b;
}
int gcd(int m, int n)
{
int r;
if(m < n)
{
r = m;
m = n;
n = r;
}
while(r = m%n)
{
m = n;
n = r;
}
return n;
}
Fract add(Fract f);
void show()
{
cout<<num<<"/"<<den<<endl;
}
};
Fract Fract::add(Fract f)
{
Fract sum;
sum.den = den * f.den;
sum.num = f.den*num + den*f.num;
int x = gcd(sum.num, sum.den);
sum.num = sum.num/x;
sum.den = sum.den/x;
return sum;
}
void main()
{
int x , y, i, j;
cout<<"Enter x , y, i, j:"<<endl;
cin>>x>>y>>i>>j;
Fract f1(x, y), f2(i, j), f3;
f3 = f1.add(f2);
f3.show();
}
很好的