任务
《电路理论》中相量法常需进行极坐标形式复数的运算,计算器上没有直接的计算方式,需要记录很多运算结果,往往很复杂。
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);//输入
friend ostream& operator<<(ostream& os, Complex& c);//输出
friend double A2R(double);//将角度制转化为弧度制
friend double R2A(double);//将弧度制转化为角度制
Complex standardize();//标准化表示
void display();//显示
private:
double r, a;//极值与极角
};
double A2R(double a);//将角度制转化为弧度制
double R2A(double a);//将弧度制转化为角度制
/****Complex_p.cpp****/
#include"Complex_p.h"
Complex::Complex()
{
r = 0;
a = 0;
}
Complex::Complex(double x0)
{
r = x0;
a = x0;
}
Complex::Complex(double r0, double a0)
{
r = r0;
a = a0;
}
Complex operator+(Complex& c1, Complex& c2)
{
Complex temp;
double re, im;
re = c1.r * cos(A2R(c1.a)) + c2.r * cos(A2R(c2.a));
im = c1.r * sin(A2R(c1.a)) + c2.r * sin(A2R(c2.a));
temp.r = sqrt(re * re + im * im);
temp.a = R2A(atan(im / re));
return temp;
}
Complex Complex::operator -()const
{
Complex temp;
temp.r = r;
temp.a = a + 180;
return temp;
}
Complex operator-(Complex& c1, Complex& c2)
{
Complex temp;
double re, im;
re = c1.r * cos(A2R(c1.a)) - c2.r * cos(A2R(c2.a));
im = c1.r * sin(A2R(c1.a)) - c2.r * sin(A2R(c2.a));
temp.r = sqrt(re * re + im * im);
temp.a = R2A(atan(im / re));
return temp;
}
Complex operator/(Complex& c1, Complex& c2)
{
return Complex(c1.r / c2.r, c1.a - c2.a);
}
Complex operator*(Complex& c1, Complex& c2)
{
return Complex(c1.r * c2.r, c1.a + c2.a);
}
istream& operator>>(istream& is, Complex& c)
{
cout << "radius: ";
is >> c.r;
cout << "argument: ";
is >> c.a;
return is;
}
ostream& operator<<(ostream& os, Complex& c)
{
cout << c.r << "<" << c.a << "*" ;
return os;
}
Complex Complex::standardize()
{
if (r < 0)
{
r = -r;
a += 180;
}
a = int(a) % 360 + a - int(a);
if (a > 180)
{
a -= 360;
}
return Complex(r, a);
}
void Complex:: display()
{
cout << "the value is " << r << "<" << a << "*" << endl;
}
double A2R(double a)
{
return a * PAI / 180;
}
double R2A(double a)
{
return a * 180 / PAI;
}
/****main.cpp****/
#include<iostream>
using namespace std;
#include"Complex_p.h"
int main(void)
{
Complex c1, c2,c;
getOperands(c1, c2);
char choice;
tips();
cin >> choice;
while (choice!='q')
{
switch (choice)
{
case'a':
c = c1 + c2;
cout << c1 << "+" << c2 << "=" << c << endl;
break;
case'b':
c = c1 - c2;
cout << c1 << "-" << c2 << "=" << c << endl;
break;
case'c':
c = c1 * c2;
cout << c1 << "*" << c2 << "=" << c << endl;
break;
case'd':
c = c1 / c2;
cout << c1 << "/" << c2 << "=" << c << endl;
break;
case'e': getOperands(c1, c2);
break;
case'f':c.standardize();
cout << "the result is " << c;
default:
break;
}
cin >> choice;
}
return 0;
}
void getOperands(Complex& c1, Complex& c2)
{
cout << "first operand: " << endl;
cin >> c1;
cout << "second operand: " << endl;
cin >> c2;
}
void tips()
{
cout << "/******caculator of complex in polar form******/" << endl;
cout << "a.addition b.subtract\nc.multiplication d.division" << endl;
cout << "e.change operands f.standardize result \nq.quit" << endl;
cout << "/**********************************************/" << endl;
}