实验4 运算符重载
1、 实验目的和要求
(1) 掌握运算符重载的语法要点,理解成员函数与友元函数重载运算符的区别。
(2) 掌握各种运算符的重载方法,理解引用形式作为参数和返回值的特点。
2、 实验内容
(1)定义描述平面点类Point,重载减号运算符计算两个点的距离,分别用成员函数与友元函数实现。重载运算符<<输出点的坐标[x,y],给出类以及相关函数的定义。
#include
#include
#include
#include
using namespace std;
#define pow(a) (a*a)
class point{
public:
point(int = 0, int = 0);
void setpoint(int, int);
double operator-(point&);
friend ostream&operator<<(ostream&, point&);
// friend double operator-(point&, point&);
private:
int x; int y;
};
point::point(int a , int b ) :x(a), y(b){}
double point::operator-(point &a){
return sqrt((double)pow(x - a.x) + (double)pow(y - a.y));
}
void point::setpoint(int a, int b){
x = a, y = b;
}
ostream& operator<<(ostream&out, point&a){
out << a.x << " " << a.y;
return out;
}
/*double operator-(point&a, point&b){
return sqrt((double)pow(a.x - b.x) + (double)pow(a.y - b.y));
}*/
int main(){
point p1(2, 1), p2;
double d = p1 - p2;
cout << p1 << "->" << p2 << "=" << d << endl;
}
(2) 描述有理数的Rational类如下,请补充类的其他成员使其能够执行各种运算。
1)重载算术运算符“+”、“-”、“*”、“/”,使之能够适用于有理数的四则运算。
2)重载比较运算符“>”、“ <=” 和“==”,使之能够比较两个有理数。
3)重载运算符“<<”,使其能以规范的方式输出分数,如1/2,-1/3,分母不能为0。
#include
#include
#include
#include
using namespace std;
#define pow(a) (a*a)
#define LL long long int
LL gcd(LL a, LL b){
a = abs(a);
b = abs(b);
if (a < b)swap(a, b);
return b ? (gcd(b, a%b)) : a;
}
class rational{
private:
LL num;
LL den;
public:
friend ostream&operator<<(ostream&,rational&a);
bool operator<=(const rational&a);
bool operator>=(const rational&a);
bool operator==(const rational&a);
rational operator+(const rational&a);
rational operator-(const rational&a);
rational operator*(const rational&a);
rational operator/(const rational&a);
rational(LL = 0, LL = 1);
void simple();
};
rational::rational(LL a, LL b) :num(a), den(b){}
ostream&operator<<(ostream&out, rational&a){
if(!a.den){
out<<"error"<return out;
}
else if(!a.num){
out<<0<return out;
}
else
out << a.num << "/" << a.den << endl;
return out;
}
bool rational::operator<=(const rational&a){
return (double)num / den <= (double)a.num / a.den;
}
bool rational::operator>=(const rational&a){
return (double)num / den >= (double)a.num / a.den;
}
bool rational::operator==(const rational&a){
return (double)num / den == (double)a.num / a.den;
}
rational rational::operator+(const rational&a){
LL d = den / gcd(den, a.den)*a.den;
LL n = num*d /den + a.num*d / a.den;
rational temp;
temp.den = d;
temp.num = n;
temp.simple();
return temp;
}
rational rational::operator-(const rational&a){
rational temp = a;
temp.num *= -1;
return *this + temp;
}
rational rational::operator*(const rational&a){
rational temp;
temp.den = den*a.den;
temp.num = num*a.num;
temp.simple();
return temp;
}
rational rational::operator / (const rational &a){
rational temp;
temp.num = num*a.den;
temp.den = den*a.num;
temp.simple();
return temp;
}
void rational::simple(){
LL g = gcd(num, den);
den /= g;
num /= g;
}
int main(){
rational n1(1, 2);
rational n2(0, 4);
cout << n1 << endl << n2 << endl;
cout << n1 + n2 << endl;
cout << n1 - n2 << endl;
cout << n1*n2 << endl;
cout << n1 / n2 << endl;
if (n1 <= n2)cout << n2 << endl;
if (n1 == n2)cout << "==" << endl;
if (n1 >= n2)cout << n2 << endl;
}
(3)定义一个集合类Set,最多存放100个不重复的整数,实现集合的如下操作:
1)增加某个整型元素时,保证集合中没有重复元素;删除指定的元素,查找该元素在集合中则从集合中删除该元素;
2)重载运算符“+”,实现两个集合对象的合并操作,重载运算符“”,求两个集合对象的交集;例如Set s, s1, s2; s = s1+s2; s = s1 s2;
3)重载赋值运算符=和复合赋值运算符-= , 实现两个集合对象的赋值操作与差集操作。例如Set s1,s2;s1 = s2; s1-=s2; 集合S1中去掉S2中存在的元素。
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define pow(a) (a*a)
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define N 100
#define LL long long int//个别编译器可能不识别long long int,可改成long int
class set{
private:
int *p;
public:
void add(int);
void del(int);
set operator+(set&);
set operator*(set&);
set&operator=(set&);
set&operator-=(set&);
set();
~set();
void show();
};
void set::show(){
for (int i = 0; i < N; i++){
if (p[i]){
cout << i << " ";
}
}
cout << endl;
}
set::set(){
p = new int[N];
for (int i = 0; i < N; i++)p[i] = 0;
// mem(p, 0);
}
set::~set(){
if (!p)delete[]p;
}
void set::add(int x){
if (x >= N||x<0){
cout << "mistake" << endl;
return;
}
p[x] = 1;
}
void set::del(int x){
if (x >= N || x < 0||!p[x]){
cout << "mistake" << endl;
return;
}
p[x] = 0;
}
set set::operator+(set&a){
set temp;
for (int i = 0; i < N; i++)
{
if (a.p[i] || p[i]){
temp.p[i] = 1;
}
}
return temp;
}
set set::operator*(set&a){
set temp;
for (int i = 0; i < N; i++){
if (a.p[i] && p[i])
temp.p[i] = 1;
}
return temp;
}
set& set::operator=(set&a){
for (int i = 0; i < N; i++){
p[i] = a.p[i];
}
return *this;
}
set&set::operator-=(set&a){
for (int i = 0; i < N; i++){
if (a.p[i]){
p[i] = 0;
}
}
return *this;
}
int main(){
set s1, s2;
for (int i = 0; i < 10; i++){
s1.add(i);
}
for (int i = 5; i < 15; i++){
s2.add(i);
}
s1.show();
s2.show();
set s3 = s1 + s2;
s3.show();
set s4 = s1 * s2;
s4.show();
s1 -= s2;
s1.show();
set s5;
s5 = s1;
s5.show();
}
(4)定义描述时间的Time类,包括数据成员小时hour、分钟minute和秒second,定义相关函数实现如下操作:
1)重载运算符“+”与“-”,能够实现时间对象与整数秒的加减操作。
2)重载运算符“<<”输出时间对象,能够按照“小时:分钟:秒”的方式显示时间。
3)重载运算符“++”与“–”,要求能够实现时间的合理自增自减功能(秒数的增减)。
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define pow(a) (a*a)
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define N 100
#define LL long long int
class Time{
private:
int hour, minue, second;
public:
Time operator+(int);
Time operator-(int);
friend ostream&operator<<(ostream&, Time&);
Time &operator++();
Time operator--(int);
Time(int h,int m,int s);
void change(Time&);
};
void Time::change(Time& a){
if(a.second<0){
second+=60;
minue--;
if(minue<0)
{minue+=60;
hour--;
}
if(hour<0){
hour+=24;
}
}
}
ostream&operator<<(ostream&out, Time &a){
out << a.hour << " " << a.minue << " " << a.second << endl;
return out;
}
Time Time::operator+(int x){
second += x;
int s = second % 60;
int m = second / 60;
second = s;
minue += m;
int mm = minue % 60;
int h = minue / 60;
minue = mm;
hour += h;
hour %= 24;
return *this;
}
Time Time::operator-(int a){
second -= a;
if (a < 0){
minue--;
if (minue < 0){
hour--;
}
}
change(*this);
return *this;
}
Time&Time::operator++(){
//*this.operator+(1);
return *this+1;
}
Time Time::operator--(int){
Time temp = *this;
*this = *this - 1;
change(*this);
change(temp);
return temp;
}
Time::Time(int h, int m, int s) :hour(h), minue(m), second(s){}
int main(){
Time s(23, 59, 59);
s++;
cout << s << endl;
s--;
cout << s << endl;
Time s1 = s + 3;
cout << s1 << endl;
s1 = s - 3;
cout << s1 << endl;
}
(5)设计字符串类String,若有String类对象s1、s2和s3,重载运算符实现如下操作:
1)重载“+”实现字符串连接功能,使表达式s1= s2+s3成立;
2)重载“<”判断两个字符串的大小
3)重载“<<”与“>>”,实现字符串的输入与输出操作
4)重载运算符“()”,从字符串对象中返回一个子串。
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define pow(a) (a*a)
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define N 100
#define LL long long int//个别编译器可能不识别long long int,可改成long int
class String{
private:
string s;
int m;
public:
String operator()(int,int);
friend ostream&operator<<(ostream&, String&);
friend istream&operator>>(istream&in, String&a);
String operator+(String &);
bool operator<(String &);
};
String String::operator()(int a, int b){
String temp;
temp.s.assign(s, a, b);
return temp;
}
String String::operator+(String &a){
String temp;
temp.s = s + a.s;
return temp;
}
istream&operator>>(istream&in, String&a){
in >> a.s;
return in;
}
bool String::operator<(String&a){
return s < a.s;
}
ostream& operator<<(ostream&out, String &a){
out << a.s << endl;
return out;
}
int main(){
String s1, s2;
cin >> s1;
cin >> s2;
String s3 = s1 + s2;
cout << s3;
cout << s1(1, 3) << endl;
if (s1 < s2)cout << "s2" << endl;
else cout << "s1" << endl;
}
(6)【选作】开发多项式类Polynomial,多项式的每一项用数组或结构体表示,每项包含一个系数和一个指数。例如2x4的指数为4,系数为2。请开发一个完整的Polynomial类,包括构造函数、析构函数以及get函数和set函数。该类还要提供下述重载的运算符:
1)重载运算符“+”和“-”,将两个多项式相加或相减。
2)重载乘法运算符“*”,将两个多项式相乘。
3)重载赋值运算符“=”,将一个多项式赋给另外一个多项式。
怎么说呢,反正是应付作业,写的代码很时间空间复杂度都不小,而且有限制。。就这样吧,,,。
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//#define pow(a) (a*a)
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define N 100
#define LL long long int//个别编译器可能不识别long long int,可改成long int
class node{
public:
int n, a;
};
class pol{
node*s;
public:
void set(int a, int n);
pol();
~pol();
LL get(int x);
void show();
pol operator+(pol&);
pol operator-(pol&);
pol operator*(pol&);
pol&operator=(pol&);
};
pol::pol(){
s = new node[N];
for (int i = 0; i < N; i++)s[i].a = 0, s[i].n = 0;
}
pol::~pol(){
cout << "delete" << endl;
}
void pol::set(int a, int n){
s[n].a = a;
s[n].n = n;
}
void pol::show(){
bool flag = false;
for (int i = 0; i < N; i++){
if (s[i].a != 0){
if (!flag)
cout << s[i].a << "x^" << s[i].n, flag = true;
else{
cout << "+" << s[i].a << "x^" << s[i].n;
}
}
}
cout << endl;
}
pol pol::operator+(pol&a){
pol temp;
for (int i = 0; i < N; i++){
if (a.s[i].a){
temp.s[i].a = a.s[i].a + s[i].a;
temp.s[i].n = a.s[i].n;
}
}
return temp;
}
pol pol::operator-(pol&a){
pol temp;
for (int i = 0; i < N; i++){
if (a.s[i].a){
temp.s[i].a = s[i].a - a.s[i].a;
}
}
return temp;
}
pol pol::operator*(pol&a){
pol temp;
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
if (s[i].a&&a.s[j].a){
temp.s[i + j].a += s[i].a*a.s[j].a;
temp.s[i + j].n = i + j;
}
}
}
return temp;
}
pol&pol::operator=(pol&a){
for (int i = 0; i < N; i++){
s[i].a = a.s[i].a;
s[i].n = a.s[i].n;
}
return *this;
}
LL pol::get(int x){
LL sum = 0;
for (int i = 0; i < N; i++){
sum += s[i].a*pow(x, s[i].n);
}
return sum;
}
int main(){
pol s1, s2;
for (int i = 0; i < 3; i++){
s1.set(i, i);
s2.set(i, 3 - i);
}
s1.show();
s2.show();
pol s = s1 + s2;
s.show();
s = s1*s2;
s.show();
}