7-2 +、-、*、/运算符重载
编写程序实现+ - * / 运算符重载,主要功能如下:
1、实现两复数(c1与c2)的加减乘除运算
2、实现复数c1与整数num的加减乘除运算
3、实现整数num与复数c1的加减乘除运算
输入格式:
c1实部 c1虚部
c2实部 c2虚部
整数num
具体格式见样例
输出格式:
c1+c2结果
c1-c2结果
c1c2结果
c1/c2结果
c1+num结果
c1-num结果
c1num结果
c1/num结果
num+c1结果
num-c1结果
num*c1结果
num/c1结果
具体格式见输出样例
输入样例:
在这里给出一组输入。例如:
1 2
3 4
5
输出样例:
在这里给出相应的输出。例如:
c1+c2=(4.00,6.00i)
c1-c2=(-2.00,-2.00i)
c1*c2=(-5.00,10.00i)
c1/c2=(0.44,0.08i)
c1+num=(6.00,2.00i)
c1-num=(-4.00,2.00i)
c1*num=(5.00,2.00i)
c1/num=(0.20,2.00i)
num+c1=(6.00,2.00i)
num-c1=(4.00,2.00i)
num*c1=(5.00,2.00i)
num/c1=(5.00,2.00i)
#include<iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex(double real=0, double imag=0)
{
this->imag = imag;
this->real = real;
}
void setreal_imag()
{
cin >> this->real >> this->imag;
}
friend Complex operator*(Complex& c1, Complex& c2);
friend Complex operator+(Complex& c1, Complex& c2);
friend Complex operator-(Complex& c1, Complex& c2);
friend Complex operator/(Complex& c1, Complex& c2);
friend Complex operator+(int a, Complex& c1);
friend Complex operator-(int a, Complex& c1);
friend Complex operator*(int a, Complex& c1);
friend Complex operator/(int a, Complex& c1);
friend Complex operator+(Complex& c1, int a);
friend Complex operator-(Complex& c1, int a);
friend Complex operator*(Complex& c1, int a);
friend Complex operator/(Complex& c1, int a);
void show_1()
{
printf("(%0.2f,%0.2fi)\n", this->real, this->imag);
}
};
/*1、实现两复数(c1与c2)的加减乘除运算
2、实现复数c1与整数num的加减乘除运算
3、实现整数num与复数c1的加减乘除运算*/
Complex operator+(Complex & c1,Complex&c2)
{
Complex c;
c.real = c1.real + c2.real;
c.imag = c1.imag + c2.imag;
return c;
}
Complex operator-(Complex& c1, Complex& c2)
{
Complex c;
c.real = c1.real - c2.real;
c.imag = c1.imag - c2.imag;
return c;
}
Complex operator*(Complex& c1, Complex& c2)
{
Complex c;
c.real = c1.real*c2.real-c1.imag*c2.imag;
c.imag = c1.real*c2.imag+c1.imag*c2.real;
return c;
}//有问题
Complex operator/(Complex& c1, Complex& c2)
{
Complex c;
double t = c2.real * c2.real +c2.imag * c2.imag;
c.real = (c1.real*c2.real+c1.imag*c2.imag)/t;
c.imag = (-c1.real*c2.imag+c1.imag*c2.real)/t;
return c;
}
Complex operator+(Complex& c1, int a)
{
Complex c;
c.real = c1.real + a;
c.imag = c1.imag;
return c;
}
Complex operator+(int a,Complex& c1)
{
Complex c;
c.real = c1.real + a;
c.imag = c1.imag;
return c;
}
Complex operator-(Complex& c1, int a)
{
Complex c;
c.real = c1.real - a;
c.imag = c1.imag;
return c;
}
Complex operator*(Complex& c1, int a)
{
Complex c;
c.real = c1.real * a;
c.imag = c1.imag;
return c;
}
Complex operator/(Complex& c1, int a)
{
Complex c;
c.real = c1.real / a;
c.imag = c1.imag;
return c;
}
Complex operator-(int a, Complex& c1)
{
Complex c;
c.real = a-c1.real;
c.imag = c1.imag;
return c;
}
Complex operator*(int a, Complex& c1)
{
Complex c;
c.real = c1.real * a;
c.imag = c1.imag;
return c;
}
Complex operator/(int a, Complex& c1)
{
Complex c;
c.real = a / c1.real;
c.imag = c1.imag;
return c;
}
int main()
{
Complex c1, c2, c3; int num = 0;
c1.setreal_imag();
c2.setreal_imag();
cin >> num;
c3 = c1 + c2;
cout << "c1+c2="; c3.show_1();
c3 = c1 - c2;
cout << "c1-c2="; c3.show_1();
c3 = c1 * c2;
cout << "c1*c2="; c3.show_1();
c3 = c1 / c2;
cout << "c1/c2="; c3.show_1();
c3 = c1 + num;
cout << "c1+num="; c3.show_1();
c3 = c1 - num;
cout << "c1-num="; c3.show_1();
c3 = c1 * num;
cout << "c1*num="; c3.show_1();
c3 = c1 / num;
cout << "c1/num="; c3.show_1();
c3 = num + c1;
cout << "num+c1="; c3.show_1();
c3 = num - c1;
cout << "num-c1="; c3.show_1();
c3 = num * c1;
cout << "num*c1="; c3.show_1();
c3 = num / c1;
cout << "num/c1="; c3.show_1();
}