资源描述
实验11 运算符重载(1)
一、实验目的
1、掌握运算符重载的概念;
2、掌握使用friend重载运算符的方法。
二、实验内容
1、用成员函数重载运算符,使对整型的运算符=、+、-、*、/ 适用于分数运算。要求:
(1)输出结果是最简分数(可以是带分数);
(2)分母为1,只输出分子。
2、用友元函数重载运算符,使对整型的运算符=、+、-、*、/ 适用于分数运算。
3、定义如下集合类的成员函数,并用数据进行测试:
class Set
{
int *elem; //存放集合元素的指针
int count; //存放集合中的元素个数
public:
Set();
Set(int s[],int n);
int find(int x) const; //判断x是否在集合中
Set operator+(const Set &); //集合的并集
Set operator-(const Set &); //集合的差集
Set operator*(const Set &); //集合的交集
void disp(); //输出集合元素
};
4、定义一个人民币类 RMB,包含私有数据成员元、角、分,请用友元函数重载运算符“+”和“++”,以对类对象进行运算。
三、 实验程序及结果
1.
#include<iostream>
using namespace std;
class Complex{
public:
Complex(int X=0,int Y=0) //构造函数初始化
{x=X;y=Y;}
void gys() //求最大公约数
{
int t;
t=x%y;
while(t!=1)
{
x=y;
y=t;
t=x%y;
}
}
void print() //输出分数值
{
int z;
if((x<y)&&(y!=1)) //分母大于分子直接输出
{
cout<<x<<"/"<<y<<endl;
}
if((x>y)&&(y!=1)) //分母小于分子输出带分数
{
z=x/y;
cout<<z<<"("<<x%y<<"/"<<y<<")"<<endl;
}
}
Complex operator+(Complex c); //声明运算符重载函数
Complex operator-(Complex c);
Complex operator*(Complex c);
Complex operator/(Complex c);
private:
int x,y;
};
Complex Complex::operator+(Complex c) //定义+重载函数
{
Complex temp1;
if(y!=c.y)
{
temp1.y=y*c.y;
temp1.x=x*c.y+c.x*y;
}
return temp1;
}
Complex Complex::operator-(Complex c) //定义-重载函数
{
Complex temp1;
if(y!=c.y)
{
temp1.y=y*c.y;
temp1.x=x*c.y-c.x*y;
}
return temp1;
}
Complex Complex::operator*(Complex c) //定义*重载函数
{
Complex temp1;
if(y!=c.y)
{
temp1.y=y*c.y;
temp1.x=x*c.x;
}
return temp1;
}
Complex Complex::operator/(Complex c) //定义/重载函数
{
Complex temp1;
if(y!=c.y)
{
temp1.y=y*c.x;
temp1.x=x*c.y;
}
return temp1;
}
int main()
{
Complex A1(3,2),A2(5,7),A3,A4,A5,A6; //定义六个类的对象
A1.print(); //输出分数
A2.print();
A3=A1+A2; //分数相加
A3.print();
A4=A1-A2; //分数相减
A4.print();
A5=A1*A2; //分数相乘
A5.print();
A6=A1/A2; //分数相除
A6.print();
return 0;
}
2.(注释同上)
#include<iostream.h>
//using namespace std;
class Complex{
public:
Complex(int X=0,int Y=0)
{x=X;y=Y;}
void gys()
{
int t;
t=x%y;
while(t!=1)
{
x=y;
y=t;
t=x%y;
}
}
void print()
{
int z;
if((x<y)&&(y!=1))
{
cout<<x<<"/"<<y<<endl;
}
if((x>y)&&(y!=1))
{
z=x/y;
cout<<z<<"("<<x%y<<"/"<<y<<")"<<endl;
}
}
friend Complex operator+(Complex& a,Complex& b);
friend Complex operator-(Complex& a,Complex& b);
friend Complex operator*(Complex& a,Complex& b);
friend Complex operator/(Complex& a,Complex& b);
private:
int x,y;
};
Complex operator+(Complex& a,Complex& b)
{
Complex temp1;
if(a.y!=b.y)
{
temp1.y=a.y*b.y;
temp1.x=a.x*b.y+b.x*a.y;
}
return temp1;
}
Complex operator-(Complex& a,Complex& b)
{
Complex temp1;
if(a.y!=b.y)
{
temp1.y=a.y*b.y;
temp1.x=a.x*b.y-b.x*a.y;
}
return temp1;
}
Complex operator*(Complex& a,Complex& b)
{
Complex temp1;
if(a.y!=b.y)
{
temp1.y=a.y*b.y;
temp1.x=a.x*b.x;
}
return temp1;
}
Complex operator/(Complex& a,Complex& b)
{
Complex temp1;
if(a.y!=b.y)
{
temp1.y=a.y*b.x;
temp1.x=a.x*b.y;
}
return temp1;
}
int main()
{
Complex A1(3,2),A2(5,7),A3,A4,A5,A6,G;
A1.print();
A2.print();
A3=A1+A2;
A3.print();
A4=A1-A2;
A4.print();
A5=A1*A2;
A5.print();
A6=A1/A2;
A6.print();
return 0;
}
3.
#include<iostream>
using namespace std;
class Set
{
int *elem; //存放集合元素的指针
int count; //存放集合中的元素个数
public:
Set(){};
Set(int s[],int n);
int find(int x) const; //判断x是否在集合中
Set operator+(const Set &a); //集合的并集
Set operator-(const Set &a); //集合的差集
Set operator*(const Set &a); //集合的交集
void disp(); //输出集合元素
};
Set::Set(int s[],int n) //构造函数初始化
{
elem=s;
count=n;
}
int Set::find(int x) const //判断x是否在集合中
{
int n;
for(n=0;n<count;n++)
{
if(x=elem[n])
cout<<"x在集合中"<<endl;
return 1;
}
return 0;
}
Set Set::operator +(const Set& a) //重载运算符+求两数组的并集
{
Set temp;
int k=0,m,n,p;
int *b=new int [k];
for(n=0;n<a.count;n++)
{
b[k]=a.elem[n];
k++;
}
for(m=0;m<count;m++) //定义一个新数组,使其等于elem[],找出另一个数组中不相等的数,放进另一个数组,两个新数组连接着输出,即为他们的并集
{
for(n=0;n<a.count;n++)
{
if(elem[m]==a.elem[n])
{
p=1;
continue;
}
}
}
for(m=0;m<count;m++)
{
if(p!=1)
{
b[k]=elem[m];
k++;
}
}
cout<<endl;
temp.count=k;
temp.elem=b;
return temp;
}
Set Set::operator -(const Set& a) //定义重载运算符-用于计算差集
{
Set temp;
int p=0,m,n,q;
int *b=new int [p];
for(m=0;m<count;m++)
{
for(n=0;n<a.count;n++) //找出两数组不相等的数,把只是属于elem的数放进新数组中,输出即为A-B
{
if(elem[m]==a.elem[n])
{
q=1;
continue;
}
}
}
for(m=0;m<count;m++)
{
if(q!=1)
{
b[p]=elem[m];
p++;
}
}
cout<<endl;
temp.count=p;
temp.elem=b;
return temp;
}
Set Set::operator *(const Set& a) //定义重载运算符*计算两数组交集
{
Set temp;
int k=0,m,n;
int *b=new int [k];
for(m=0;m<count;m++) //如果两数组的值相等,把相等的值放进新数组,输出即为交集
{
for(n=0;n<a.count;n++)
{
if(elem[m]==a.elem[n])
{
b[k]=elem[m];
k++;
}
}
}
cout<<endl;
temp.count=k;
temp.elem=b;
return temp;
}
void Set::disp()
{
for(int i=0;i<count;i++)
{
cout<<elem[i]<<" ";
}
cout<<endl;
}
int main()
{
int aa[4]={1,2,3,4};
int bb[5]={3,4,5,6,7};
Set A,B,C,D,E;
A=Set(aa,4);
B=Set(bb,5);
A.find(2);
A.disp();
B.disp();
E=A*B;
cout<<"两个数组的交集为:"<<endl;
E.disp();
C=A+B;
cout<<"两个数组的并集为:"<<endl;
C.disp();
D=A-B;
cout<<"两个数组的差集为:"<<endl;
D.disp();
return 0;
}
4.
#include<iostream.h>
//using namespace std;
class Complex{
public:
Complex(int X=0,int Y=0,int Z=0)
{x=X;y=Y;z=Z;}
void print()
{
if(z>=10)
{ z=z-10;
y=y+1;
if(y>=10)
{
y=y-10;
x=x+1;
}
}
cout<<x<<"元"<<y<<"角"<<z<<"分"<<endl;
}
Complex operator+(Complex c); //声明运算符重载函数
Complex operator++(); //声明自减运算符--重载成员函数(前缀)
Complex operator++(int); //声明自减运算符--重载成员函数(后缀)
private:
int x,y,z;
};
Complex Complex::operator+(Complex c) //定义+重载函数
{
Complex temp1;
temp1.x=x+c.x;
temp1.y=y+c.y;
temp1.z=z+c.z;
return temp1;
}
Complex Complex::operator++() //定义++重载函数(前缀)
{++x;++y;++z;
return *this;
}
Complex Complex::operator++(int) //定义++重载函数(后缀)
{Complex temp(*this);
x++;y++;z++;
return temp;
}
int main()
{
Complex A(7,8,9),B(4,5,6),C; //定义四个类的对象
A.print(); //输出对象A的值
B.print(); //输出对象B的值
C=A+B; //两对象A、B相加
C.print(); //输出相加后的值
++A; //执行++前缀
A.print(); //输出执行后的值
B++; //执行++后缀
B.print(); //输出执行后的值
return 0;
}
展开阅读全文