#include<iostream>
using namespace std;
class Fraction
{
public:
//Fraction();
//Fraction(int n);
Fraction(int n, int d);
void setValue(int n,int d);
//int getNum();
//int getDen();
//double getDoubleValue();
void output();
~Fraction();
private:
int num,den;
void normalize();
int gcd(int n,int d);
};
#include "fraction.h"
#include<iostream>
Fraction::Fraction(int n,int d)
{
num=n;den=d;
Fraction::normalize();
}
void Fraction::setValue(int n,int d)
{
num=n;den=d;
Fraction::normalize();
}
void Fraction::normalize()
{
if (den==0)
den=1;
else if(den<0)
{
den=-den; num=-num;
}
int g=gcd( num, den);
num=num/g; den=den/g;
}
int Fraction::gcd(int n,int d)
{
int mid;
for (;n!=0;) { mid=n; n=d%n; d=mid; }
return d;
}
void Fraction::output()
{
if (num==0) cout<<"0"<<endl;
else if (num/den==0 || num/den==-1) cout<<num<<"/"<<den<<endl;
else if (num/den>0) cout<<num/den<<" "<<num%den<<"/"<<den<<endl;
else cout<<num/den+1<<" "<<den-num%den<<"/"<<den<<endl;
}
#include<iostream>
#include "fraction.h"
using namespace std;
int main()
{
Fracion f1(8,10);
f1.output();
f1.setValue(18,15);
f1.output();
f1.setValue(-7,4);
f1.output();
f1.setValue(0,31);
f1.output();
Fraction f1(3,0);
f1.output();
f1.setValue(10,-4);
f1.output();
f1.setValue(14,2);
f1.output();
f1.setValue(-4,5);
f1.output();
return 0;
}
慕森卡
凤凰求蛊
相关分类