运算符重载 实验7 期末回顾

实验目录:点击打开链接


第一题

定义复数类,分别用成员函数和友元函数重载+-运算符。并编写主函数进行测试

我用的友元函数重载了+  -  >>  <<,成员函数重载 * /

#include<bits/stdc++.h>
using namespace std;
class Complex
{
	double  re,im;
public:
	double getre();
	double getim();
	friend istream &operator >>(istream&in,Complex &p)
	{
		cout<<"输入复数的实部和虚部:"<<endl;;
		in>>p.re;
		in>>p.im;
		return in;
	}
	friend ostream &operator <<(ostream&out,Complex &p)
	{
	 out<<"c="<<p.re;
	 out<<setiosflags(ios::showpos);//显示正负符号
     out<<p.im << "i" << endl;
     return out;
	}
	friend Complex operator +(Complex &b,Complex &a)
	{
		Complex c;
		c.re=a.re+b.re;
		c.im=a.im+b.im;
		return c;
	}
	friend Complex operator -(Complex &b,Complex &a)
	{
		Complex c;
		c.re=a.re-b.re;
		c.im=a.im-b.im;
		return c;
	}	
	Complex operator *(Complex &b)
	{
		Complex c;
		c.re=re*b.re-im*b.im;
		c.im=re*b.im+im*b.re;
		return c;
	}
	Complex operator /(Complex &b)
	{
		Complex c;
		c.re=(re*b.re+im*b.im)/(b.re*b.re+b.im*b.im);
		c.im=(im*b.re-re*b.im)/(b.re*b.re+b.im*b.im);
		return c;
	}		
};
double Complex::getre()
{
	return re;
}
double Complex::getim()
{
	return im;
}
int main()
{
	Complex a,b,c;
	
	cin>>a;
	cin>>b;
	char ch;
	cout<<"请输入操作符进行运算,输出‘#’退出程序"<<endl;
	while(cin>>ch)
	{
		switch(ch)
		{
			case '+':c=a+b;cout<<c<<endl;break;
			case '-':c=a-b;cout<<c<<endl;break;
		    case '*':c=a*b;cout<<c<<endl;break;
		    case '/':c=a/b;cout<<c<<endl;break;
			case '#':break;
			default:cout<<"输入错误,请重新输入"<<endl;
		}
		if(ch=='#') break;
		cout<<"请输入操作符进行运算"<<endl;
	}
	
	
	return 0;
}

第二题

有两个矩阵ab,均为23列,求两个矩阵之和。重载运算符“+”使之能用于矩阵相加,如c=a+b

#include<bits/stdc++.h>
using namespace std;
class  Array
{
    int a[2][3];
public:
	int in()        //注意不要写在构造函数里,下面出现临时对象,会要求输入
	{
		cout<<"请输入2行3列矩阵"<<endl;
		for(int i=0;i<2;i++)
			for(int j=0;j<3;j++)
				cin>>a[i][j];
	}
	Array operator +(Array &b)
	{
		 Array c;
		for(int i=0;i<2;i++)
			for(int j=0;j<3;j++)
			c.a[i][j]=a[i][j]+b.a[i][j];	
			return c;
	}
	void display()
	{
		for(int i=0;i<2;i++)
			{for(int j=0;j<3;j++)
			cout<<a[i][j]<<" ";
			cout<<endl;
			}
	}
};

int main()
{
	Array x,y,z;
	x.in();
	y.in();
	z=x+y;
	z.display();
	return 0;
	
}

第三题

定义point类,分别用成员函数和友元函数重载前置--和后置的--运算符

成员函数:

#include<bits/stdc++.h>
using namespace std;
class  Point
{
  double x,y;
public:
	Point (double i,double j)
	{
		  x=i;y=j;
	}
	Point operator --()
	{
	   --x;
	   --y;
		
	}
	Point operator --(int)
	{
		x--;
		y--;
	}
	int out()
	{
		cout<<x<<" "<<y<<endl;
	}
};

int main()
{
      Point p(5,5);
      --p;
      p.out();
      p--;
	  p.out();
	return 0;
	
}

友元函数:

#include<bits/stdc++.h>
using namespace std;
class  Point
{
  double x,y;
public:
	Point (double i,double j)
	{
		  x=i;y=j;
	}
	friend Point operator --(Point &p)
	{
	   --p.x;
	   --p.y;
	   return p;
		
	}
	friend Point operator --(Point &p,int a)
	{
		p.x--;
		p.y--;
		return p;
	}
	int out()
	{
		cout<<x<<" "<<y<<endl;
	}
};

int main()
{
      Point p(5,5);
      --p;
      p.out();
      p--;
	  p.out();
	return 0;
	
}

第四题


6、用友元类和类的组合解决下面的问题

1)设计一个point

     数据成员:

        点的坐标x,y

     成员函数:

        带有参的构造函数(不带默认值)

2)定义一个line

     数据成员:

        线上的两个点point1,point2(用定义好的point

     成员函数:

       定义一条直线

       计算线段的长度

   


#include<bits/stdc++.h>
using namespace std;
class line;
class  Point
{
  double x,y;
public:
	Point (double i,double j)
	{
		  x=i;y=j;
	}
	int out()
	{
		cout<<x<<" "<<y<<endl;
	}
	friend class line;
};
class line
{
	Point a,b;
public:
	line(double i,double j,double k,double t):a(i,j),b(k,t)
	{
		
	}
	int def()
	{
		double k;
		if(fabs(a.x-b.x)<=1e-8)  cout<<"y="<<b.x<<endl;
		else if(fabs(b.y-a.y)<=1e-8)cout<<"x="<<b.y<<endl;
		 else
		 {
		 	k=(a.x-b.x)/(a.y-b.y);
		 	cout<<"y="<<k<<"(x-"<<a.x<<")+"<< a.y<<endl;
		 }
	}
	int d()
	{
		cout<<sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))<<endl;
	}
};
int main()
{
      line l(1,1,1,2);
      l.def();
      l.d();
	return 0;
	
}

猜你喜欢

转载自blog.csdn.net/sdz20172133/article/details/80905854