面向对象程序设计-实验四 类的运算符重载

2022/4/24
1.补充完成以下程序
#include<iostream.h>
#include<stdlib.h>
class vector
{ public :
     vector( int size =1 ) ;       ~vector() ;
     int & operator[] ( int i ) ;
     friend ostream & operator << ( ostream & output , vector & ) ;
     friend istream & operator >> ( istream & input, vector & ) ;
  private :  
      int * v ;
int len ;
};
void main()
{ int k ;
  cout << "Input the length of vector A :\n" ;     cin >> k ;
  vector A( k ) ;
  cout << "Input the elements of vector A :\n" ;     cin >> A ;
  cout << "Output the elements of vector A :\n" ;
  cout << A ;
}
#include<iostream>
#include<stdlib.h>
using namespace std;
class vector{
public:
    vector(int size = 1);
    ~vector();
    int& operator[] (int i);
    friend ostream& operator << (ostream& output, vector&);
    friend istream& operator >> (istream& input, vector&);
private:
    int* v;
    int len;
};
vector::vector(int size) :len(size) {
    if (size == 0) {
        v = nullptr;
    }
    else {
        v = new int[size];
    }
}//开辟数组空间
vector::~vector(){
    if (v) {
        delete[]v;
    }
}//析构函数
int& vector::operator[](int i) {
    return v[i];
}//访问数组元素
ostream& operator<<(ostream& output, vector& x) {
    for (int i = 0; i < x.len; i++) {
        output << x.v[i] << " ";
    }
    return output;
}//流输出
istream& operator>>(istream& input, vector& x) {
    for (int i = 0; i < x.len; i++) {
        input >> x.v[i];
    }
    return input;
}//流插入
int main(){
    int k;
    cout << "Input the length of vector A :\n";
    cin >> k;
    vector A(k);
    cout << "Input the elements of vector A :\n";
    cin >> A;
    cout << "Output the elements of vector A :\n";
    cout << A;
}
2.建立一个数组类,它能检测范围,以确保数组下标不会越界,允许用赋值运算符把一个数组赋给另外一个数组。数组对象自动知道数组的大小,不需要将数组的大小传递给函数。
#include<iostream>
#include<string>
using namespace std;
class CArray {
public:
    CArray(int s = 5);
    ~CArray();
    CArray(CArray& a);
    int& operator[] (int i);
    CArray& operator=(const CArray& a);
    friend ostream& operator << (ostream& output, CArray& a);
    friend istream& operator >> (istream& input, CArray& a);
private:
    int* ptr;
    int size = 5;//数组长度为5
};
CArray::CArray(int s) {
    ptr = new int[s];
}//开辟数组空间
CArray::~CArray() {
    if (ptr) {
        delete[]ptr;
    }
}//析构函数
CArray::CArray(CArray& a) {
    if (!a.ptr) {
        ptr = nullptr;
        size = 0;
        return;
    }
    ptr = new int[a.size];
    memcpy(ptr, a.ptr, sizeof(int) * a.size);
    size = a.size;
}
int& CArray::operator[](int i) {
    return ptr[i];
}//访问数组元素
CArray& CArray::operator=(const CArray& a) {
    if (ptr == a.ptr) {
        return *this;
    }
    if (a.ptr == nullptr) {
        if (ptr) {
            delete[]ptr;
        }
        ptr = nullptr;
        size = 0;
        return *this;
    }
    memcpy(ptr, a.ptr, sizeof(int) * a.size);
    size = a.size;
    return *this;
}//赋值
ostream& operator<<(ostream& output, CArray& x) {
    for (int i = 0; i < x.size; i++) {
        output << x.ptr[i] << " ";
    }
    return output;
}//流输出
istream& operator>>(istream& input, CArray& x) {
    int i = 0;
    do {
        if (i >= 5) {
            cout << "下标越界" << endl;
            return input;
        }
        input >> x.ptr[i];
        i = i + 1;
    } while (cin.get() != '\n');
    return input;
}//流插入
int main() {
    CArray a, b;
    cout << "请输入数组a:" << endl;
    cin >> a;
    cout << "请输入数组b:" << endl;
    cin >> b;
    cout << "将数组b赋值给数组a 输出数组a" << endl;
    a = b;
    cout << b;
    return 0;
}
3.RationalNumber(分数类)

1)建立构造函数,它能防止分母为0,当分数不是最简形时进行约分以及避免分母为负数
2)重载加法、减法、乘法以及除法运算符
3)重载关系运算符和相等运算符。
4)能利用类型转换方法,实现分数与整数类型相加

#include<iostream>
using namespace std;
void f(double& x, double& y) {
	int k = x;
	if (x > y) {
		k = y;
	}
	for (int i = 2; i < k; i++) {
		if ((int)x % i == 0 && (int)y % i == 0) {
			x = x / i;
			y = y / i;
		}
	}
	if (y < 0) {
		x = -x;
		y = -y;
	}
}//化简函数
class RationalNumber {
public:
	RationalNumber(double z = 0, double m = 1) :fz(z), fm(m) {
		f(fz, fm);
	}//构造函数
	RationalNumber(int i);//类型转换函数
	void display() {
		cout << fz << "/" << fm << endl;
	}
	friend RationalNumber operator+(const RationalNumber& a, const RationalNumber& b);//重载运算符+
	friend RationalNumber operator-(const RationalNumber& a, const RationalNumber& b);//重载运算符-
	friend RationalNumber operator*(const RationalNumber& a, const RationalNumber& b);//重载运算符*
	friend RationalNumber operator/(const RationalNumber& a, const RationalNumber& b);//重载运算符/
	friend bool operator>(const RationalNumber& a, const RationalNumber& b);//重载运算符>
	friend bool operator>=(const RationalNumber& a, const RationalNumber& b);//重载运算符>=
	friend bool operator<(const RationalNumber& a, const RationalNumber& b);//重载运算符<
	friend bool operator<=(const RationalNumber& a, const RationalNumber& b);//重载运算符<=
	friend bool operator==(const RationalNumber& a, const RationalNumber& b);//重载运算符==
	friend bool operator!=(const RationalNumber& a, const RationalNumber& b);//重载运算符!=
private:
	double fz;//分子
	double fm;//分母
};
RationalNumber::RationalNumber(int i) {
	fz = i;
	fm = 1;
	f(fz, fm);
}
RationalNumber operator+(const RationalNumber& a, const RationalNumber& b) {
	return RationalNumber(a.fz * b.fm + a.fm * b.fz, a.fm * b.fm);
}
RationalNumber operator-(const RationalNumber& a, const RationalNumber& b) {
	return RationalNumber(a.fz * b.fm - a.fm * b.fz, a.fm * b.fm);
}
RationalNumber operator*(const RationalNumber& a, const RationalNumber& b) {
	return RationalNumber(a.fz * b.fz, a.fm * b.fm);
}
RationalNumber operator/(const RationalNumber& a, const RationalNumber& b) {
	return RationalNumber(a.fz * b.fm, a.fm * b.fz);
}
bool operator>(const RationalNumber& a, const RationalNumber& b) {
	if (a.fz * b.fm > a.fm * b.fz) {
		return 1;
	}
	else {
		return 0;
	}
}
bool operator>=(const RationalNumber& a, const RationalNumber& b) {
	if (a.fz * b.fm >= a.fm * b.fz) {
		return 1;
	}
	else {
		return 0;
	}
}
bool operator<(const RationalNumber& a, const RationalNumber& b) {
	if (a.fz * b.fm < a.fm * b.fz) {
		return 1;
	}
	else {
		return 0;
	}
}
bool operator<=(const RationalNumber& a, const RationalNumber& b) {
	if (a.fz * b.fm <= a.fm * b.fz) {
		return 1;
	}
	else {
		return 0;
	}
}
bool operator==(const RationalNumber& a, const RationalNumber& b) {
	if (a.fz * b.fm == a.fm * b.fz) {
		return 1;
	}
	else {
		return 0;
	}
}
bool operator!=(const RationalNumber& a, const RationalNumber& b) {
	if (a.fz * b.fm != a.fm * b.fz) {
		return 1;
	}
	else {
		return 0;
	}
}
int main() {
	double x, y;
	cout << "请输入分子和分母:";
	cin >> x >> y;
	RationalNumber Rational1(x, y);
	cout << "请输入加上的数:";
	cin >> x;
	Rational1 = Rational1 + x;
	cout << "请输入减去的数的分子和分母:";
	cin >> x >> y;
	RationalNumber Rational2(x, y);
	Rational1 = Rational1 - Rational2;
	cout << "请输入乘上的数的分子和分母:";
	cin >> x >> y;
	RationalNumber Rational3(x, y);
	Rational1 = Rational1 * Rational3;
	cout << "请输入除以的数的分子和分母:";
	cin >> x >> y;
	RationalNumber Rational4(x, y);
	Rational1 = Rational1 / Rational4;
	cout << "分数1为:";
	Rational1.display();
	cout << "分数2为:";
	Rational2.display();
	cout << "分数1是否大于分数2:";
	if (Rational1 > Rational2) {
		cout << "True" << endl;
	}
	else {
		cout << "False" << endl;
	}
	cout << "分数1是否大于等于分数2:";
	if (Rational1 >= Rational2) {
		cout << "True" << endl;
	}
	else {
		cout << "False" << endl;
	}
	cout << "分数1是否小于分数2:";
	if (Rational1 < Rational2) {
		cout << "True" << endl;
	}
	else {
		cout << "False" << endl;
	}
	cout << "分数1是否小于等于分数2:";
	if (Rational1 <= Rational2) {
		cout << "True" << endl;
	}
	else {
		cout << "False" << endl;
	}
	cout << "分数1是否等于分数2:";
	if (Rational1 == Rational2) {
		cout << "True" << endl;
	}
	else {
		cout << "False" << endl;
	}
	cout << "分数1是否不等于分数2:";
	if (Rational1 != Rational2) {
		cout << "True" << endl;
	}
	else {
		cout << "False" << endl;
	}
	return 0;
}
4.开发一个多项式类Polynomial,多项式的每一项用数组表示,每项包含一个系数和一个指数。

开发一个完整的Polynomial类,包含构造函数、析构函数以及 get函数和set函数,该类还要提供下述重载的运算符
a)重载+
b)重载-
c)重载赋值运算符
d)重载乘法运算符
e)重载+=

#include<iostream>
#include<string>
using namespace std;
class Polynomial {
public:
    Polynomial(int s = 0);
    ~Polynomial();
    Polynomial(const Polynomial& po);
    int get(int ex);
    int gets();
    void set(double co, int ex);
    Polynomial& operator=(const Polynomial& po);//重载运算符=
    friend Polynomial operator+(const Polynomial& po1, const Polynomial& po2);//重载运算符+
    friend Polynomial operator-(const Polynomial& po1, const Polynomial& po2);//重载运算符-
    friend Polynomial operator*(const Polynomial& po1, const Polynomial& po2);//重载运算符*
    friend Polynomial& operator+= (Polynomial& po1, Polynomial& po2);//重载运算符+=
private:
    double* coef;
    int size;
};
Polynomial::Polynomial(int s) {
    size = s;
    coef = new double[size];
    int i = 0;
    for (int i = 0; i < size; i++) {
        coef[i] = 0;
    }
}//开辟数组空间
Polynomial::~Polynomial() {
    if (coef) {
        delete[] coef;
    }
}//析构函数
Polynomial::Polynomial(const Polynomial& po) {
    int i = 0;
    size = po.size;
    coef = new double[size];
    while (i < size) {
        this->set(po.coef[i], i);
        i = i + 1;
    }
}//复制构造函数
int Polynomial::get(int ex) {
    return coef[ex];
}
int Polynomial::gets() {
    return size;
}
void Polynomial::set(double co, int ex) {
    coef[ex] = co;
}
Polynomial& Polynomial::operator=(const Polynomial& po) {
    int i = 0;
    size = po.size;
    delete[] coef;
    coef = new double[size];
    while (i < size) {
        this->set(po.coef[i], i);
        i = i + 1;
    }
    return *this;
}
Polynomial operator+(const Polynomial& po1, const Polynomial& po2) {
    int i = 0;
    Polynomial po3(max(po1.size, po2.size));
    while (i < po3.size) {
        if (i < min(po1.size, po2.size)) {
            po3.coef[i] = po1.coef[i] + po2.coef[i];
        }
        else {
            if (po1.size > po2.size) {
                po3.coef[i] = po1.coef[i];
            }
            else {
                po3.coef[i] = po2.coef[i];
            }
        }
        i = i + 1;
    }
    return po3;
}
Polynomial operator-(const Polynomial& po1, const Polynomial& po2) {
    int i = 0;
    Polynomial po3(max(po1.size, po2.size));
    while (i < po3.size) {
        if(i < min(po1.size, po2.size)) {
            po3.coef[i] = po1.coef[i] - po2.coef[i];
        }
        else {
            if (po1.size > po2.size) {
                po3.coef[i] = po1.coef[i];
            }
            else {
                po3.coef[i] = -po2.coef[i];
            }
        }
        i = i + 1;
    }
    return po3;
}
Polynomial operator*(const Polynomial& po1, const Polynomial& po2) {
    int i = 0;
    Polynomial po3(po1.size + po2.size - 1);
    while (i < po1.size) {
        int j = 0;
        while (j < po2.size) {
            po3.coef[i + j] = po3.coef[i + j] + po1.coef[i] * po2.coef[j];
            j = j + 1;
        }
        i = i + 1;
    }
    return po3;
}
Polynomial& operator+= (Polynomial& po1, Polynomial& po2) {
    po1 = po1 + po2;
    return po1;
}
void display(Polynomial po) {
    int flag = 1;
    for (int i = 0; i < po.gets(); i++) {
        if (flag) {
            if (po.get(i) != 0) {
                if (i == 0) {
                    cout << po.get(i);
                }
                else {
                    cout << po.get(i) << "x^" << i;
                }
                flag = 0;
            }
        }
        else {
            if (po.get(i) > 0) {
                cout << "+" << po.get(i) << "x^" << i;
            }
            else if (po.get(i) < 0) {
                cout << po.get(i) << "x^" << i;
            }
        }
    }
    cout << endl;
}
int main() {
    Polynomial po1(5);
    po1.set(2, 0);
    po1.set(3, 4);
    cout << "多项式1为:";
    display(po1);
    Polynomial po2(6);
    po2.set(2, 1);
    po2.set(1, 2);
    cout << "多项式2为:";
    display(po2);
    Polynomial po3(po1);
    cout << "多项式3为:";
    display(po3);
    Polynomial po4(po3);
    po4 = po1 + po3;
    cout << "多项式1加多项式3为:";
    display(po4);
    po4 = po2 - po3;
    cout << "多项式2减多项式3为:";
    display(po4);
    po4 = po1 * po3;
    cout << "多项式1乘多项式3为:";
    display(po4);
    po3 += po2;
    cout << "多项式3加上多项式2后为:";
    display(po3);
    return 0;
}
实验总结
  1. 注意new和delete的使用
  2. 注意指针的深拷贝
  3. 注意运算符重载函数是成员函数还是友元函数
  4. 注意项数相乘数目下标变化
  5. 注意函数中指针数组的越界
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闻闻闻闻笛声

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值