任务
《电路理论》中相量法常需进行极坐标形式复数的运算,计算器上没有直接的计算方式,需要记录很多运算结果,往往很复杂。
C++备考时对运算符的重载掌握比较薄弱,一直没有练习的机会,因此决定实现极坐标形式复数类的计算。
已有
- C++运行环境
- 复数运算规则
代码实现
/****Complex_p.h****/
#pragma once
#include<iostream>
#include<math.h>
#define PAI 3.1415926
using namespace std;
class Complex
{
public:
Complex(); //constructor without parameter
Complex(double); //constructor with one parameter
Complex(double,double); //constructor with two parameters
inline void operator =(const Complex& c)//重载双目运算符赋值
{
r = c.r;
a = c.a;
}
inline void operator +=(Complex c)//重载双目运算符加赋值
{
*this = *this + c;
}
inline void operator -=(Complex c)//重载双目运算符减赋值
{
*this = *this - c;
}
Complex operator -()const;//重载单目运算符减
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 istream& operator>>(istream& is, Complex& c);//输入
<