实验目的
- 掌握用成员函数重载运算符的方法;
- 掌握用友元函数重载运算符的方法.
实验要求
- 定义一个复数类,描述一些必须的成员函数,如:构造函数,析构函数,赋值函数,返回数据成员值的函数等;
- 定义运算符重载函数,通过重载运算符:
+,-,*,/,直接实现二个复数之间的加减乘除运算.编写一个完整的程序,测试重载运算符的正确性.要求加法和乘法"+","*"用友元函数实现重载,减法和除法"-","/"用成员函数实现重载,参数是复数或实数; - 通过重载运算符:
>>,<<,=,直接实现复数的输入、输出及赋值运算,通过重载运算符:==,!=直接实现复数的比较运算,编写一个完整的程序,测试重载运算符的正确性.
操作菜单可参考如下格式:
1. 输入复数
2. 查看输入的复数
3. 复数相加
4. 复数相减
5. 复数相乘
6. 复数相除
7. 输出结果
0. 退出
代码
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
class Complex{
public:
Complex(double r=0,double i=0)
{
real=r;
image=i;
}
~Complex()
{
}
double get_real();
double get_image();
friend Complex operator+(Complex &C1,Complex &C2);//友元+
friend Complex operator*(Complex &C1,Complex &C2);//友元*
Complex operator-(Complex &C1);//成员函数-
Complex operator/(Complex &C1);//成员函数/
friend istream& operator>>(istream&in,Complex&c);//输入
friend ostream& operator<<(ostream&out,Complex&c);//输出
Complex operator=(const Complex &C1);//赋值
friend int operator==(Complex &C1,Complex &C2);//相等
friend int operator!=(Complex &C1,Complex &C2);//不等
void display();
private:
double real;
double image;
};
double Complex::get_real()
{
return real;
}
double Complex::get_image()
{
return image;
}
Complex operator+(Complex &C1,Complex &C2)
{
return Complex(C1.real+C2.real,C1.image+C2.image);
}
Complex operator*(Complex &C1,Complex &C2)
{
//(a+bi)(c+di)=ac+(bc+ad)i+bdi^2=(ac-bd)+(bc+ad)i
return Complex(C1.real*C2.real-C1.image*C2.image,C1.real*C2.image+C1.image*C2.real);
}
Complex Complex::operator-(Complex &C1)
{
return Complex(get_real()-C1.get_real(),get_image()-C1.get_image());
}
Complex Complex::operator/(Complex &C1)
{
/*Markdown语法
$\frac{a+bi}{c+di}=\frac{(a+bi)(c-di)}{(c+di)(c-di)}$
$=\frac{ac-adi+bci-bdi^2}{c^2-d^2\times i^2}$
$=\frac{(ac+bd)+(bc-ad)i}{c^2+d^2}$
*/
if(C1.get_real()==0&&C1.get_image()==0)
{
cout << "操作失败,分母不为0" << endl;
exit(0);
}
else
{
double r=get_real()*C1.get_real()+get_image()*C1.get_image();
double i=get_image()*C1.get_real()-get_real()*C1.get_image();
double fenmu=C1.get_real()*C1.get_real()+C1.get_image()*C1.get_image();
return Complex(r/fenmu,i/fenmu);
}
}
istream& operator>>(istream&in,Complex&c)
{
cout << "请分别输入复数的实部和虚部(中间用空格隔开): ";
in >> c.real >> c.image ;
return in;
}
ostream& operator<<(ostream&out,Complex&c)
{
out << c.real << '+' << c.image << 'i' << endl;
return out;
}
Complex Complex::operator=(const Complex &C1)
{
this->real=C1.real;
this->image=C1.image;
return *this;
}
int operator==(Complex &C1,Complex &C2)
{
if(C1.real==C2.real&&C1.image==C2.image)
return 1;
else return 0;
}
int operator!=(Complex &C1,Complex &C2)
{
if(C1.real!=C2.real||C1.image!=C2.image)
return 1;
else return 0;
}
void Complex::display()
{
cout << get_real() << '+' << get_image() << 'i' << endl;
}
int displaymenu()
{
int op;
cout << "-------------功能菜单-------------" << endl;
cout << " 1.输入复数" << endl;
cout << " 2.查看输入的复数" << endl;
cout << " 3.复数相加" << endl;
cout << " 4.复数相减" << endl;
cout << " 5.复数相乘" << endl;
cout << " 6.复数相除" << endl;
cout << " 7.复数判断相等" << endl;
cout << " 8.复数判断相等" << endl;
cout << " 9.输出结果" << endl;
cout << " 0.退出系统" << endl;
cout << "----------------------------------" << endl;
cout << "请输入你要选择的功能编号(0-9): ";
while(cin >> op)
{
if(op>=0&&op<=9)
break;
else
cout << "输入出错,请重新输入你要选择的功能编号(0,9): ";
}
return op;
}
int main()
{
Complex C,res,temp1,temp2;
while(1)
{
int op=displaymenu();
if(op==1)
cin >> C;
else if(op==2)
cout << C ;
else if(op==3)
{
cin >> temp1 >> temp2;
res=temp1+temp2;
}
else if(op==4)
{
cin >> temp1 >> temp2;
res=temp1-temp2;
}
else if(op==5)
{
cin >> temp1 >> temp2;
res=temp1*temp2;
}
else if(op==6)
{
cin >> temp1 >> temp2;
res=temp1/temp2;
}
else if(op==7)
{
cin >> temp1 >> temp2;
if(temp1==temp2)
cout << "两复数相等" << endl;
else cout << "两复数不等" << endl;
}
else if(op==8)
{
cin >> temp1 >> temp2;
if(temp1!=temp2)
cout << "两复数不等" << endl;
else cout << "两复数相等" << endl;
}
else if(op==9)
{
cout << "结果为: " ;
res.display();
}
else if(op==0)
break;
}
return 0;
}