头文件:
#ifndef FRACTION_H_INCLUDED
#define FRACTION_H_INCLUDED
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <istream>
#include <cmath>
#include <math.h>
# include <time.h>
#include <stdlib.h> //atoi()
#include <ctype.h> //atof()
#include <string>
#include <stdio.h>
using namespace std;
class Fraction;
Fraction operator + (Fraction f1,Fraction f2); //实现分数的加法
Fraction operator - (Fraction f1,Fraction f2); //实现分数的减法
Fraction operator * (Fraction f1,Fraction f2); //实现分数的乘法
Fraction operator / (Fraction f1,Fraction f2); //实现分数的除法
bool operator >(Fraction f1,Fraction f2);//重载>
bool operator >=(Fraction f1,Fraction f2);//重载>=
bool operator < (Fraction f1,Fraction f2);//重载<
bool operator <= (Fraction f1,Fraction f2);//重载<=
bool operator ==(Fraction f1,Fraction f2);//重载==
bool operator != (Fraction f1,Fraction f2);//重载!=
Fraction str2Frac(char * strX);//将串strX转化为分数:返回分数(外部函数)。
istream& operator >>(istream&input,Fraction& f);
ostream& operator <<(ostream&output,const Fraction& f);
Fraction simplify(Fraction f);//用辗转相除法化简分数:外部函数
Fraction simplify(int numerator, int denominator);//用辗转相除法化简分数
Fraction simplify_other(int numerator, int denominator);//其他化简方法
class Fraction //分数类
{
public:
Fraction(int Numerator=0,int Denominator=1):numerator(Numerator),denominator(Denominator) //根据所给数构造一个分数Numerator/Denominator
{
simplify();
}
Fraction(const Fraction & f):numerator(f.numerator),denominator(f.denominator){} //复制构造函数
Fraction(char * strX);//由串strX构造一个分数
Fraction simplify(); //化简分数
bool str2Frac(char * strX); //将串strX转化为分数:串中有不合法的字符,返回false
int getNumerator()//返回分子
{
return numerator;
}
int getDenominator()//返回分母
{
return denominator;
}
Fraction & operator = (const Fraction &f);//重载=
friend Fraction operator + (Fraction f1,Fraction f2);//重载加法
friend Fraction operator - (Fraction f1,Fraction f2);//重载减法
friend Fraction operator * (Fraction f1,Fraction f2);//重载乘法
friend Fraction operator / (Fraction f1,Fraction f2);//重载除法
friend istream& operator >>(istream&,Fraction&); //重载输入流
friend ostream& operator << (ostream&,const Fraction&);//重载输出流
friend bool operator > (Fraction f1,Fraction f2);//重载>
friend bool operator <