参考资源:
谷歌大神,《C++程序设计语言》
一,设计要求
要求:复数 由两个实数组成。因此该类至少应包含两个“私有”成员变量,
还应具有如下成员函数:
1)多种构造函数;
2)析构函数;
3)取实部和虚部;
4)求模;
5)求主幅角;
6)单目运算:++等;
7)双目运算:重构运算符操作+ , - , x , /等
8)混合运算
二,代码实现
1,Complex.h代码如下(友元实现的重载运算符,其实也可以用成员函数实现)
#include <iostream> #include "math.h" using namespace std; class Complex { public: Complex( ) {real=0;imag=0;} Complex(double r) {real=r;imag=0;} Complex(double r,double i) {real=r;imag=i;} Complex(const Complex& c)//复制构造函数(一定要用引用) {real=c.real;imag=c.imag;} ~Complex(){} double getReal() { return this->real; } double getImag() { return this->imag; } //使用友元来重载运算符(注意,这些函数都不属于这个类,他没有this指针) //输入输出运算符重载 friend istream& operator>>(istream &in,Complex c); friend ostream& operator<<(ostream &out,Complex c); //双目运算:加减乘除运算符重载 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 c);//前置 friend Complex operator++(Complex c,int);//hou置 //双目运算:混合运算 friend Complex operator+=(Complex c,double d); friend Complex operator+(Complex c1,double d); friend Complex operator+(double d,Complex c1); //比较运算 friend bool operator==(Complex c1,Complex c2); friend bool operator!=(Complex c1,Complex c2); //成员函数 Complex abs();//求绝对值 double mod(); double arg(); private: double real; double imag; }; //输入运算符重载 istream & operator>>(istream& in,Complex c) { in>>c.real>>c.imag; return in; } //输出运算符重载 ostream & operator<<(ostream &out,Complex c) { if (c.getImag()>=0) { out<<"("<<c.real<<"+"<<c.imag<<"i)"<<" "; } else { out<<"("<<c.real<<c.imag<<"i)"<<" "; } return out; } Complex operator+(Complex c1,double d) { Complex c; c.real=c1.real+d; return c; } Complex operator+(double d,Complex c1) { Complex c; c.real=c1.real+d; return c; } bool operator==(Complex c1,Complex c2) { if (c1.imag==c2.imag&&c1.real==c2.real) { return true; }else { return false; } } bool operator!=(Complex c1,Complex c2) { if (c1.imag!=c2.imag||c1.real!=c2.real) { return true; }else { return false; } } //加减乘除运算符重载 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.imag*c2.real+c1.real*c2.imag; return c; } Complex operator/(Complex c1,Complex c2) { Complex c; c.real=(c1.real/c2.real+c1.imag/c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); c.imag=(c1.imag/c2.real-c1.real/c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); return c; } Complex operator++(Complex c) { ++c.real; ++c.imag; return c; } Complex operator++(Complex c,int) { ++c.real; ++c.imag; return c; } Complex Complex::abs() { Complex temp; temp.real=(this->real>=0)?this->real:(-this->real); temp.imag=(this->imag>=0)?this->imag:(-this->imag); return temp; } double Complex::mod() { double temp=0.0; temp=pow(this->real,2.0)+pow(this->imag,2.0); temp=sqrt(temp); return temp; } double Complex::arg() { double temp=0.0; temp=atan(imag/real); temp=temp*57.296; return temp; }
// ConsoleAppComplex.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "ComplexNum.h" int _tmain(int argc, _TCHAR* argv[]) { system("color 0A"); cout<<"/******************************华丽的分割线*****************************/"<<endl; Complex ans; Complex c1(-2.0),c2(1,-0.5),c3=3,c4(c2); cout<<"您设定的四个复数"<<"c1:"<<c1<<" c2:"<<c2<<" c3:"<<c3<<" c4:"<<c4<<endl; ans=c1*c2; cout<<"c1*c2="<<ans<<endl; ans=c1/c2; cout<<"c1/c2="<<ans<<endl; ans=c1+c2; cout<<"c1+c2="<<ans<<endl; ans=c1-c2; cout<<"c1-c2="<<ans<<endl; ans=ans.abs(); cout<<"c1-c2的结果绝对值后:"<<ans<<endl; cout<<endl<<"/******************************华丽的分割线*****************************/"<<endl; if (c1==c3) { cout<<"复数c1与c3相等!"<<endl; }else { cout<<"复数c1与c3不相等!"<<endl; } if (c4==c2) { cout<<"复数c2与c4相等!"<<endl; }else { cout<<"复数c2与c4不相等!"<<endl; } cout<<endl<<"/******************************华丽的分割线*****************************/"<<endl; double ans1=0.0; ans1=ans.mod(); cout<<"c1-c2的结果取模后:"<<ans1<<endl; Complex ans3; ans3=c1+ans1; cout<<"c1+ans1(模值)="<<ans3<<endl; ans3=ans1+c2; cout<<"ans1(模值)+c2="<<ans3<<endl; double ans2=0.0; ans2=ans.arg(); cout<<"c1-c2的结果主幅角:"<<ans2<<"度"<<endl; cout<<"单目运算:第一个复数前置自增"<<++c1<<endl; cout<<"单目运算:第二个复数后置自增"<<c2++<<endl; cout<<endl<<"/******************************华丽的分割线*****************************/"<<endl; cout<<"请输入第一个复数的实部与虚部"<<endl; cin>>c1; cout<<"请输入第二个复数的实部与虚部"<<endl; cin>>c2; cout<<"您输入的两个复数为"<<c1<<" "<<c2<<endl; ans=c1+c2; cout<<"c1+c2="<<ans<<endl; ans=c1-c2; cout<<"c1-c2="<<ans<<endl; system("pause"); return 0; }
(3)测试结果: