重载模板关系运算符

发布于 2024-08-12 18:21:16 字数 7512 浏览 6 评论 0原文

我在模板 ARRAY 类方面遇到问题。我有另一个 Rational 类添加到这个 ARRAY 类中。 我需要它做的是将有理数作为分数(exp 1/2)并对它们进行排序。我相信我需要重载关系运算符,但这就是我陷入困境的地方。我是否在 ARRAY 类或 Rational 类中重载它们? 下面是我的代码

//generic.cpp
using namespace std;

template<class T>
void Quicksort (T& a, int first, int last);

template<class T>
int split (T& a, int first, int last);

template<class T>
void change (T& e1, T& e2); 

template<class T>
void Mergesort(T& a, int first, int last);

template<class T>
void Merge(T& a, int first, int last);

int main()  
{
    int num;

    Rational r1;

    cout << "\nHow many rationals? ";
    cin >> num;
    Array<Rational> r2(num);
    cout << "Enter the " << num << " rationals below:\n";
    for (int i=0; i<num ; i++)  
        cin >> r2[i];
    cout << "\nThank you!!\n";
    cout << "Initially, the rationals are\n"
         <<  " r2 = " << r2 << "\n";

    // Copy the original array and sort it using Quicksort
    Array<Rational> r3(r2);
    Quicksort(r3, 0, num-1);
    cout << "\nElements sorted using quicksort:\n";
    for (int i=0; i<num ; i++)  
        cout << r3[i]<< " ";
    cout << "\n";

// Print original list of elements.
cout << "\nOriginal elements:\n";
    for (int i=0; i<num ; i++)  
        cout << r2[i]<< " ";
    cout << "\n";

    // Copy original array and sort it using MergeSort
    Array<Rational> r4(r2);
    Mergesort(cm, 0, num-1);
    cout << "\nElements sorted using mergesort:\n";
    for (int i=0; i<num ; i++)  
        cout << r4[i]<< " ";
    cout << "\n";

    return 0;
}

template<class T>
int split (T& a, int first, int last) 
{
    T::value_type pivot =  a[first]; 
    int left = first;
    int right = last;
    while (left<right)
    {
        while (pivot < a[right])  //search from right for <=pivot
            right--;
            while (left<right &&(a[left]<pivot || a[left]==pivot)) 
                left++;
            if (left<right)
                change(a[left],a[right]);
    }
    int pivotPosition = right;
    a[first] = a[pivotPosition];
    a[pivotPosition] = pivot;
    return pivotPosition;
}

template<class T>
void Quicksort (T& a, int first, int last)
{
    int pos;
    if (first < last)
    {
        pos=split(a,first,last);
        Quicksort(a,first,pos); //sort lsft sublist
        Quicksort(a,pos+1,last); //sort right sublist
    }
}

template<class T>
void change (T& e1, T& e2) 
{
    T tmp = e1; 
    e1 = e2; 
    e2 = tmp;
}

template<class T>
void Mergesort(T& a, int first, int last) 
{
    if (first < last) 
    {
        int mid = (first + last) / 2;
        Mergesort(a, first, mid);
        Mergesort(a, mid+1, last);
        Merge(a, first, last);
    }
}

template<class T>
void Merge(T& a, int first, int last) 
{
    int mid = (first + last) / 2;
    int one = 0, two = first, three = mid + 1;
    Array<T::value_type> temp(a.get_size());

    while (two <= mid && three <= last) // Neither sublist is done
        if (a[two] < a[three])          // Value in first half is smaller
           temp[one++] = a[two++];
        else                            // Value in second half is smaller
           temp[one++] = a[three++];
    while (two <= mid)                  // Finish copying first half
           temp[one++] = a[two++];
while (three <= last)               // Finish copying second half
           temp[one++] = a[three++];
    for (one = 0, two = first; two <= last; a[two++] = temp[one++]);
}

//ARRAY.h

using namespace std;

template<class T> class Array 
{
public:
    typedef T value_type;
    Array(int s);
    Array(int l, int h);

    Array(const Array& other);
    ~Array();

    T& operator[](int index);
    const T& operator[](int index) const;

    int get_size() const {return arraySize;}

private:
    int low;
    int high;
    int arraySize; //size of array
    int offset; //to adjust back to an index of zero
    T *array_;

    void Copy(const Array&);
};

//rational.cpp
using namespace std;

Rational::Rational()
{
    num = 0;
    den = 1;
}

Rational::Rational(int n, int d)
{
    if (d==0){
       cout << "Error: division by zero." << endl;
       exit(1);
    }
    num = n;
    den = d;
    simplify();
}

Rational& Rational::operator+=(const Rational& r)
{
    num = (num * r.den) + (den * r.num);
    den = den * r.den;
    simplify();
    return *this;
}

Rational& Rational::operator-=(const Rational& r)
{
    num = (num * r.den) - (den * r.num);
    den = den * r.den;
    simplify();
    return *this;
}

Rational& Rational::operator*=(const Rational& r)
{
    num *= r.num;
    den *= r.den;
    simplify();
    return *this;
}

Rational& Rational::operator/=(const Rational& r)
{
    if (r.num == 0) {
       cout << "Error: division by zero." << endl;
       exit(1);
    }
    num *= r.den;
    den *= r.num;
    simplify();
    return *this;
}

const Rational& Rational::operator= (const Rational& rightObj)
{
    if (this != &rightObj)
    {
       num = rightObj.num;
       den = rightObj.den;
    }
    return *this;
}

const Rational Rational::operator-() const
{
    Rational answer(-num, den);
    return answer;
}

const Rational operator+(const Rational& q, const Rational& r)
{
    Rational answer = q ;
    answer += r ;
    return answer;
}

const Rational operator-(const Rational& q, const Rational& r)
{
    Rational answer = q ;
    answer -= r ;
    return answer;
}

const Rational operator*(const Rational& q, const Rational& r)
{
    Rational answer = q ;
    answer *= r ;
    return answer;
}

const Rational operator/(const Rational& q, const Rational& r)
{
    Rational answer = q ;
    answer /= r ;
    return answer;
}

istream& operator>>(istream& in, Rational& r)
{
    char ch;
    in >> r.num >> ch >> r.den;
    r.simplify();
    return in;
}

ostream& operator<<(ostream& out, const Rational& r)
{
    if (r.den == 1)
    {
        out << r.num;
    }else
    {
        out << r.num << "/" << r.den;
    }
    return out;
}

//rational.h

using namespace std;

class Rational
{
    friend ostream& operator<< (ostream&, const Rational&);
    friend istream& operator>> (istream&, Rational&);

 public:
    Rational();
    Rational(int, int);

    double value() const;
    Rational reciprocal() const;

    Rational& operator+=(const Rational&);
    Rational& operator-=(const Rational&);
    Rational& operator*=(const Rational&);
    Rational& operator/=(const Rational&);

    const Rational& operator= (const Rational&);

    const Rational operator-() const;

private:
    int num;
    int den;
    void simplify();
};

const Rational operator+(const Rational&, const Rational&);
const Rational operator-(const Rational&, const Rational&);
const Rational operator*(const Rational&, const Rational&);
const Rational operator/(const Rational&, const Rational&);

I'm having a problem with a template ARRAY class. I have another Rational class that i added to this ARRAY class.
what i need it to do is take in rational numbers as fractions (exp 1/2) and sort them. i believe i need to overload the relational operators but thats where im stuck. do i over load them in the ARRAY class or the Rational Class.
below is my code

//generic.cpp
using namespace std;

template<class T>
void Quicksort (T& a, int first, int last);

template<class T>
int split (T& a, int first, int last);

template<class T>
void change (T& e1, T& e2); 

template<class T>
void Mergesort(T& a, int first, int last);

template<class T>
void Merge(T& a, int first, int last);

int main()  
{
    int num;

    Rational r1;

    cout << "\nHow many rationals? ";
    cin >> num;
    Array<Rational> r2(num);
    cout << "Enter the " << num << " rationals below:\n";
    for (int i=0; i<num ; i++)  
        cin >> r2[i];
    cout << "\nThank you!!\n";
    cout << "Initially, the rationals are\n"
         <<  " r2 = " << r2 << "\n";

    // Copy the original array and sort it using Quicksort
    Array<Rational> r3(r2);
    Quicksort(r3, 0, num-1);
    cout << "\nElements sorted using quicksort:\n";
    for (int i=0; i<num ; i++)  
        cout << r3[i]<< " ";
    cout << "\n";

// Print original list of elements.
cout << "\nOriginal elements:\n";
    for (int i=0; i<num ; i++)  
        cout << r2[i]<< " ";
    cout << "\n";

    // Copy original array and sort it using MergeSort
    Array<Rational> r4(r2);
    Mergesort(cm, 0, num-1);
    cout << "\nElements sorted using mergesort:\n";
    for (int i=0; i<num ; i++)  
        cout << r4[i]<< " ";
    cout << "\n";

    return 0;
}

template<class T>
int split (T& a, int first, int last) 
{
    T::value_type pivot =  a[first]; 
    int left = first;
    int right = last;
    while (left<right)
    {
        while (pivot < a[right])  //search from right for <=pivot
            right--;
            while (left<right &&(a[left]<pivot || a[left]==pivot)) 
                left++;
            if (left<right)
                change(a[left],a[right]);
    }
    int pivotPosition = right;
    a[first] = a[pivotPosition];
    a[pivotPosition] = pivot;
    return pivotPosition;
}

template<class T>
void Quicksort (T& a, int first, int last)
{
    int pos;
    if (first < last)
    {
        pos=split(a,first,last);
        Quicksort(a,first,pos); //sort lsft sublist
        Quicksort(a,pos+1,last); //sort right sublist
    }
}

template<class T>
void change (T& e1, T& e2) 
{
    T tmp = e1; 
    e1 = e2; 
    e2 = tmp;
}

template<class T>
void Mergesort(T& a, int first, int last) 
{
    if (first < last) 
    {
        int mid = (first + last) / 2;
        Mergesort(a, first, mid);
        Mergesort(a, mid+1, last);
        Merge(a, first, last);
    }
}

template<class T>
void Merge(T& a, int first, int last) 
{
    int mid = (first + last) / 2;
    int one = 0, two = first, three = mid + 1;
    Array<T::value_type> temp(a.get_size());

    while (two <= mid && three <= last) // Neither sublist is done
        if (a[two] < a[three])          // Value in first half is smaller
           temp[one++] = a[two++];
        else                            // Value in second half is smaller
           temp[one++] = a[three++];
    while (two <= mid)                  // Finish copying first half
           temp[one++] = a[two++];
while (three <= last)               // Finish copying second half
           temp[one++] = a[three++];
    for (one = 0, two = first; two <= last; a[two++] = temp[one++]);
}

.

//ARRAY.h

using namespace std;

template<class T> class Array 
{
public:
    typedef T value_type;
    Array(int s);
    Array(int l, int h);

    Array(const Array& other);
    ~Array();

    T& operator[](int index);
    const T& operator[](int index) const;

    int get_size() const {return arraySize;}

private:
    int low;
    int high;
    int arraySize; //size of array
    int offset; //to adjust back to an index of zero
    T *array_;

    void Copy(const Array&);
};

.

//rational.cpp
using namespace std;

Rational::Rational()
{
    num = 0;
    den = 1;
}

Rational::Rational(int n, int d)
{
    if (d==0){
       cout << "Error: division by zero." << endl;
       exit(1);
    }
    num = n;
    den = d;
    simplify();
}

Rational& Rational::operator+=(const Rational& r)
{
    num = (num * r.den) + (den * r.num);
    den = den * r.den;
    simplify();
    return *this;
}

Rational& Rational::operator-=(const Rational& r)
{
    num = (num * r.den) - (den * r.num);
    den = den * r.den;
    simplify();
    return *this;
}

Rational& Rational::operator*=(const Rational& r)
{
    num *= r.num;
    den *= r.den;
    simplify();
    return *this;
}

Rational& Rational::operator/=(const Rational& r)
{
    if (r.num == 0) {
       cout << "Error: division by zero." << endl;
       exit(1);
    }
    num *= r.den;
    den *= r.num;
    simplify();
    return *this;
}

const Rational& Rational::operator= (const Rational& rightObj)
{
    if (this != &rightObj)
    {
       num = rightObj.num;
       den = rightObj.den;
    }
    return *this;
}

const Rational Rational::operator-() const
{
    Rational answer(-num, den);
    return answer;
}

const Rational operator+(const Rational& q, const Rational& r)
{
    Rational answer = q ;
    answer += r ;
    return answer;
}

const Rational operator-(const Rational& q, const Rational& r)
{
    Rational answer = q ;
    answer -= r ;
    return answer;
}

const Rational operator*(const Rational& q, const Rational& r)
{
    Rational answer = q ;
    answer *= r ;
    return answer;
}

const Rational operator/(const Rational& q, const Rational& r)
{
    Rational answer = q ;
    answer /= r ;
    return answer;
}

istream& operator>>(istream& in, Rational& r)
{
    char ch;
    in >> r.num >> ch >> r.den;
    r.simplify();
    return in;
}

ostream& operator<<(ostream& out, const Rational& r)
{
    if (r.den == 1)
    {
        out << r.num;
    }else
    {
        out << r.num << "/" << r.den;
    }
    return out;
}

.

//rational.h

using namespace std;

class Rational
{
    friend ostream& operator<< (ostream&, const Rational&);
    friend istream& operator>> (istream&, Rational&);

 public:
    Rational();
    Rational(int, int);

    double value() const;
    Rational reciprocal() const;

    Rational& operator+=(const Rational&);
    Rational& operator-=(const Rational&);
    Rational& operator*=(const Rational&);
    Rational& operator/=(const Rational&);

    const Rational& operator= (const Rational&);

    const Rational operator-() const;

private:
    int num;
    int den;
    void simplify();
};

const Rational operator+(const Rational&, const Rational&);
const Rational operator-(const Rational&, const Rational&);
const Rational operator*(const Rational&, const Rational&);
const Rational operator/(const Rational&, const Rational&);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○愚か者の日 2024-08-19 18:21:16

如果您尝试比较有理类的两个或多个对象,那么关系运算符应该是有理类的方法或友元。如果需要比较类数组的两个或多个对象,那么关系运算符应该是类数组的方法或友元。

If you are trying to compare two or more objects of class rational then the relational operators should be methods or friends of class rational. If you need to compare two or more objects of class array then the relational operators should be methods or friends of class array.

饭团 2024-08-19 18:21:16

如果您使用 Rational 类进行排序,则可以在 Rational 类中重载它们。运算符:

bool operator < (const Rational& rhs) const {  // rhs = right hand side
    // return true if 'this' less than 'rhs'
}

或者

bool operator < (const Rational&lhs, const Rational& rhs) {
    // return true if 'lhs' less than 'rhs'
}

如果您选择不使用成员函数

/AB

You overload them in the Rational class, if you're sorting using the < operator:

bool operator < (const Rational& rhs) const {  // rhs = right hand side
    // return true if 'this' less than 'rhs'
}

or

bool operator < (const Rational&lhs, const Rational& rhs) {
    // return true if 'lhs' less than 'rhs'
}

if you choose to not use a member function

/A.B.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文