C++程序求教
题目:自定义分数类fraction,使用该类可以完成分数的输入、分数的加、减、乘、除二目运算和
一目减运算、分数的约分操作、分数的通分操作、分数的倒数运算、对两个分数进行六种比较
运算、以及对分数的输出等操作。
将其中使用的普通函数尽量改写成运算符重载函数。如,可重载+、-、*
、/、>、>=、<、<=、==、!=,以实现分数的各种运算。并且实现分数与整数的混合运算。
从分数类派生出一个用来描述整型的Integer类,完善整型类Integer,使其能完成整数的各
种操作。为分数类fraction和整型类Integer各增加一个成员函数digit_number(),用来统计一
个分数中数字字符的数量(如:13/34,数字字符的数量为4)和一个整数中的数字字符的数量
代码如下:
//fraction.h
#ifndef FRACTION_H
#define FRACTION_H
class fraction
{
public:
fraction()
{
numerator = 0;
denominator = 1;
}
// fraction(int m)
// {
// numerator = m;
// denominator = 1;
// }
int maxdiv(int a, int b); // get the max divsion of |a| && |b|
void error(); //deal with errors;
void get_frc(); //the the value of the fraction
void display(); //output the fraction
void sim_frc(); //simplied the fraction
template <class T>
int uni_frc(T &f2); // get the smallest mutilple of the two denominators
template <class T>
T operator+(T f2); // + f2
template <class T>
T operator-(T f2); // - f2
template <class T>
T operator*(T f2); // * f2
template <class T>
T operator/(T f2); // / f2
fraction rec_frc(); // reciprocal
void operator--(); // --
template <class T>
bool operator>(T f2); // ">"
template <class T>
bool operator>=(T f2); // ">="
template <class T>
bool operator<(T f2); // "<"
template <class T>
bool operator<=(T f2); // "<="
template <class T>
bool operator==(T f2); // "=="
template <class T>
bool operator!=(T f2); // "!="
int digit_number();
protected:
int numerator;
int denominator;
};
#endif
//fra_function.cpp
#include <iostream>
#include "fraction.h"
using namespace std;
int fraction::maxdiv(int a, int b) // get the max divsion of |a| && |b|
{
int c,t;
if (a < 0)
a = -a;
if(b < 0)
b = -b;
if (a < b)
{
t = a;
a = b;
b = t;
}
c = a % b;
while (c != 0)
{
a = b;
b = c;
c = a % b;
}
return b;
}
void fraction:: error() // deal with errors;
{
if(denominator == 0)
{
cout << "Error:denominator cannot be zero !" << endl;
exit(0);
}
}
void fraction::get_frc() // the the value of the fraction
{
cout << "numerator: ";
cin >> numerator;
cout << "denominator: ";
cin >> denominator;
error();
sim_frc();
}
void fraction::display() //output the fraction
{
if(denominator == 1 || numerator == 0)
cout << numerator << endl;
else
cout << numerator << "/" << denominator << endl;
}
void fraction::sim_frc() //simplied the fraction
{
int a, b;
a = denominator;
b = numerator;
if (a*b != 0)
{
denominator /= maxdiv(a,b);
numerator /= maxdiv(a,b);
}
if(denominator < 0) // make the denominator be positive
{
numerator = - numerator;
denominator = - denominator;
}
}
template <class T>
T fraction::operator-(T f2) // - f2
{
T f;
numerator *= uni_frc(f2) / denominator;
f2.numerator *= uni_frc(f2) / f2.denominator;
f.numerator = numerator - f2.numerator;
f.denominator = uni_frc(f2);
f.sim_frc();
return f;
}
template <class T>
T fraction::operator*(T f2) // * f2
{
T f;
f.numerator = numerator * f2.numerator;
f.denominator = denominator * f2.denominator;
f.sim_frc();
return f;
}
template <class T>
T fraction::operator/(T f2) // / f2
{
T f;
f.numerator = numerator * f2.denominator;
f.denominator = denominator * f2.numerator;
f.error();
f.sim_frc();
return f;
}
fraction fraction::rec_frc() // reciprocal
{
fraction f;
f.numerator = denominator;
f.denominator = numerator;
f.error();
f.sim_frc();
return f;
}
void fraction::operator--() // --
{
numerator -= denominator;
}
template <class T>
bool fraction::operator>(T f2) // ">"
{
T f;
f = operator-(f2);
if (f.numerator > 0)
return true;
else
return false;
}
template <class T>
bool fraction::operator>=(T f2) // ">="
{
T f;
f = operator-(f2);
if (f.numerator >= 0)
return true;
else
return false;
}
template <class T>
bool fraction::operator<(T f2) // "<"
{
T f;
f = operator-(f2);
if (f.numerator < 0)
return true;
else
return false;
}
template <class T>
bool fraction::operator<=(T f2) // "<="
{
T f;
f = operator-(f2);
if (f.numerator <= 0)
return true;
else
return false;
}
template <class T>
bool fraction::operator==(T f2) // "=="
{
T f;
f = operator-(f2);
if (f.numerator == 0)
return true;
else
return false;
}
template <class T>
int fraction::uni_frc(T &f2) // the min mulitple of the denominators
{
return denominator * f2.denominator / maxdiv(denominator, f2.denominator);
}
template <class T>
T fraction::operator+ (T f2) // + f2
{
T f;
numerator *= uni_frc(f2) / denominator;
f2.numerator *= uni_frc(f2) / f2.denominator;
f.numerator = numerator + f2.numerator;
f.denominator = uni_frc(f2);
f.sim_frc();
return f;
}
template <class T>
bool fraction::operator!=(T f2) // "!="
{
T f;
f = operator-(f2);
if (f.numerator != 0)
return true;
else
return false;
}
int fraction::digit_number()
{
int i,j,count = 0;
i = numerator;
j = denominator;
while(i != 0)
{
count++;
i /= 10;
}
while(j != 0)
{
count++;
j /= 10;
}
return count;
}
//Integer.h
#include "fraction.h"
class Integer:public fraction
{
public:
Integer(int n = 1):denominator(n)
{
numerator = 0;
}
void get_Int();
void display();
int digit_number();
protected:
const int denominator ;
};
//Int_function.cpp
#include <iostream>
#include "Integer.h"
using namespace std;
void Integer::get_Int()
{
cout << "Integer: ";
cin >> numerator;
}
void Integer::display()
{
cout << "The Integer is:" << numerator <<endl;
}
int Integer::digit_number()
{
int k, count = 0;
k = numerator;
while (k != 0)
{
count++;
k /= 10;
}
return count;
}
//main.cpp
//JUST FOR TEST
#include <iostream>
#include "Integer.h"
using namespace std;
int main()
{
fraction i1,i2,i3;
i1.get_frc();
i2.get_frc();
i1.display();
i3= i1 + i2;
i3.display();
return 0;
}