Description
一个分数类(Fraction)对象由两部分组成,分别是分子(numerator)和分母(denominator)。分子和分母均为整数类(Integer)对象,用以存储一个整型数值。
NumArray类是一个模板数组类,用以存储Integer对象或Fraction对象。NumArray::sort()函数可以按照Integer对象或Fraction对象的数值从小到大进行排序。
用C++编写Fraction类和Integer类来完成代码,调用格式见“Append Code”。
Fraction::numerator()取得分子数值。
Fraction:: denominator ()取得分母数值。
Integer::value()取得整数值。
Fraction类和Integer类的构造函数,根据题意设计。
Input
输入为两部分。第一部分首先输入整数i,后接i个整数,存入i个Integer对象。第二部分首先输入整数j,后接j对整数n和d,n为分子、d为分母,存入j个Fraction对象,并且输入中不存在数值完全相同的Fraction对象。i、j均小于100大于0.
Output
按从小到大的顺序分别输出所有整数和所有分数。
Sample Input
7 4 9 6 7 5 8 3 6 5 3 3 5 1 2 2 1 4 7 7 4
Sample Output
3 4 5 6 7 8 9 1/2 4/7 3/5 5/3 7/4 2/1
HINT
Append Code
#include<iostream>
using namespace std;
class Integer
{
public:
int num_;
public:
Integer(): num_(0) {}
Integer(int num):num_(num)
{
//cout<<"Integer "<<num_<<" construction."<<endl;
//cout<<"Integer "<<num_<<" duplicate."<<endl;
}
Integer(const Integer & p):num_(p.num_)
{
//cout<<"Integer "<<num_<<" duplicate."<<endl;
//cout<<"Integer "<<num_<<" duplicate."<<endl;
}
~Integer()
{
//cout<<"Integer "<<num_<<" destruction."<<endl;
// cout<<"Integer "<<den_<<" duplicate."<<endl;
}
int value()
{
return num_;
}
bool operator<(Integer& a)
{
if(this->value()<a.value())return true;
else return false;
}
bool operator>(Integer& a)
{
if(this->value()>a.value())return true;
else return false;
}
};
class Fraction
{
private:
Integer n_,d_;
public:
Fraction(): n_(0), d_(1) {}
Fraction(Integer &n,Integer &d):n_(n),d_(d){
//cout<<"Fraction "<<n_.value()<<"/"<<d_.value()<<" construction."<<endl;
}
~Fraction(){
//cout<<"Fraction "<<n_.value()<<"/"<<d_.value()<<" destruction."<<endl;
}
Integer& numerator()
{
return n_;
}
Integer& denominator ()
{
return d_;
}
double value()
{
return 1.0*n_.value()/d_.value();
}
Fraction operator++(int)//后置
{
Fraction temp=*this;
this-> n_.num_+=d_.num_;
return temp;
}
bool operator<(Fraction& a)
{
if(this->value()<a.value())return true;
else return false;
}
bool operator>(Fraction& a)
{
if(this->value()>a.value())return true;
else return false;
}
Fraction &operator++()//前置
{
this-> n_.num_+=d_.num_;
return *this;
}
void print()
{
cout<<n_.value()<<"/"<<d_.value()<<endl;
}
};
template <typename T>
class NumArray
{
private:
T *ar_;
int size_;
public:
NumArray():size_(150){ar_=new T[size_];}
~NumArray(){delete []ar_;}
void set_length(int len)
{
size_=len;
}
int length()
{
return size_;
}
T& operator[](int i){return ar_[i];}
void sort() {
T temp;
int n = size_;
for (int i = 1; i < n; i++) {
for (int j = n - 1; j >= i; j--) {
if (ar_[j] < ar_[j - 1]) {
temp = ar_[j];
ar_[j] = ar_[j - 1];
ar_[j - 1] = temp;
}
}
}
}
};
int main()
{
NumArray<Integer> ia;
NumArray<Fraction> fa;
int cases, n, d;
cin >> cases;
for(int i = 0; i < cases; i++)
{
cin >> n;
ia[i] = n;
}
ia.set_length(cases);
cin >> cases;
for(int i = 0; i < cases; i++)
{
cin >> n >> d;
Integer num(n), den(d);
Fraction f(num, den);
fa[i] = f;
}
fa.set_length(cases);
ia.sort();
fa.sort();
for(int i = 0; i < ia.length(); i++)
cout << ia[i].value() << " ";
cout << endl;
for(int i = 0; i < fa.length(); i++)
cout << fa[i].numerator().value() << "/" << fa[i].denominator().value() << " ";
cout << endl;
}