PTA C++编程题 7-6 加减乘除运算符(运算符重载)
/*7-6 加减乘除运算符 (10 分)
编写程序实现 + - * / 运算符重载,主要功能如下: 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.0 2.0
3.0 4.0
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, 10.00i)
c1 / num = (0.20, 0.40i)
num + c1 = (6.00, 2.00i)
num - c1 = (4.00, -2.00i)
num * c1 = (5.00, 10.00i)
num / c1 = (1.00, -2.00i)*/
代码如下:
#include <iostream>
using namespace std;
#include<iomanip>
class Complex
{
public:
Complex() { real = 0; imag = 0; }
Complex(double r, double i) { real = r; imag = i; }
Complex operator-();
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+(Complex& c1, double d1);
friend Complex operator+(double d2, Complex& c1);
friend Complex operator-(Complex& c1,double d1);
friend Complex operator-(double d2, Complex& c1);
friend Complex operator*(Complex& c1, double d1);
friend Complex operator*(double d2, Complex& c1);
friend Complex operator/(Complex& c1, double d1);
friend Complex operator/(double d2, Complex& c1);
void display();
private:
double real;
double imag;
};
Complex Complex::operator-()
{
return(0- * this);
}
Complex operator+(Complex &c1, Complex &c2)
{
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
Complex operator-(Complex& c1, Complex& c2)
{
return Complex(c1.real - c2.real, c1.imag - c2.imag);
}
Complex operator*(Complex& c1, Complex& c2)
{
return Complex(c1.real * c2.real- c1.imag*c2.imag, c1.imag * c2.real+ c1.real*c2.imag);
}
Complex operator/(Complex& c1, Complex& c2)
{
return Complex((c1.real * c2.real+ c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag), (c1.imag *c2.real- c1.real * c2.imag)/(c2.real * c2.real + c2.imag * c2.imag));
}
Complex operator+(double d2, Complex& c1)
{
Complex c(d2, 0);
return(c1 + c);
}
Complex operator-(Complex& c1, double d1)
{
Complex c(d1, 0);
return(c1 - c);
}
Complex operator-(double d2, Complex& c1)
{
Complex c(d2, 0);
return(c - c1);
}
Complex operator*(Complex& c1, double d1)
{
Complex c(d1, 0);
return(c1 * c);
}
Complex operator*(double d2, Complex& c1)
{
Complex c(d2, 0);
return(c * c1);
}
Complex operator+(Complex& c1, double d1)
{
Complex c(d1, 0);
return(c1 + c);
}
Complex operator/(Complex& c1, double d1)
{
Complex c(d1, 0);
return(c1 / c);
}
Complex operator/(double d2, Complex& c1)
{
Complex c(d2, 0);
return(c / c1);
}
void Complex::display()
{
cout<<fixed<<setprecision(2) << "(" << real << "," << imag << "i" << ")" << endl;
}
int main()
{
double r, m,num;
cin >> r >> m;
Complex c1(r, m);
cin >> r >> m;
Complex c2(r, m);
cin >> num;
Complex c3 = c1 + c2;
cout << "c1+c2=";
c3.display();
c3 = c1 - c2;
cout << "c1-c2=";
c3.display();
c3 = c1 * c2;
cout << "c1*c2=";
c3.display();
c3 = c1 / c2;
cout << "c1/c2=";
c3.display();
c3 = c1 + num;
cout << "c1+num=";
c3.display();
c3 = c1 - num;
cout << "c1-num=";
c3.display();
c3 = c1 * num;
cout << "c1*num=";
c3.display();
c3 = c1 / num;
cout << "c1/num=";
c3.display();
c3 = num + c1;
cout << "num+c1=";
c3.display();
c3 = num - c1;
cout << "num-c1=";
c3.display();
c3 = num * c1;
cout << "num*c1=";
(num * c1).display();
c3 = num / c1;
cout << "num/c1=";
c3.display();
system("pause");
return 0;
}