- #include<iostream.h>
- #include<math.h>
- //定义复数类
- class Complex
- {
- private:
- double real;//复数实部
- double imag;//复数虚部
- public:
- Complex(){real=0;imag=0;}//构造函数
- Complex(double r,double i){real=r;imag=i;}
- ~Complex(); //析构函数
- 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,Complex &c2);//重载+=运算符
- friend Complex operator -=(Complex &c1,Complex &c2);//重载-=运算符
- friend bool operator ==(Complex &c1,Complex &c2);//重载==运算符
- friend ostream& operator <<(ostream&,Complex&);//重载<<运算符
- friend iostream& operator >>(iostream&,Complex&);//重载>>运算符
- void set_compx(double r,double i)
- {
- real=r;
- imag=i;
- }
- void display();
- };
- 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)
- {
- double realx=c1.real*c2.real-c1.imag*c2.imag;
- double imagy=c1.imag*c2.real+c1.real*c2.imag;
- return Complex(realx,imagy);
- }
- Complex operator /(Complex &c1,Complex &c2)
- {
- double realx=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
- double imagy=(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
- return Complex(realx,imagy);
- }
- 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);
- }
- bool operator == (Complex &c1,Complex &c2)
- {
- double compx_1=sqrt(c1.real*c1.real+c1.imag*c1.imag);//c1的模
- double compx_2=sqrt(c2.real*c2.real+c2.imag*c2.imag);//c2的模
- if(compx_1 == compx_2)
- return true;
- else
- return false;
- }
- istream& operator >> (istream &input,Complex &c)
- {
- double re,im;
- input >>re>>im;
- c.set_compx(re,im);
- return input;
- }
- ostream& operator << (ostream& output,Complex& c)
- {
- if(c.imag<0)
- output<<"("<<c.real<<c.imag<<"i)"<<endl;
- else
- output<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl;
- return output;
- }
- void Complex::display()
- {
- cout<<"("<<real<<","<<imag<<"i)";
- }
- Complex::~Complex(){}
- #include<iostream.h>
- #include<string.h>
- #include<stdlib.h>
- #include "Complex.h"
- int main()
- {
- void surface();//声明首页函数
- void AchieveComplex();//声明实现复数各种运算的函数
- surface();
- AchieveComplex();
- return 0;
- }
- //首页函数
- void surface()
- {
- cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
- cout<<" ┃ 复数计算器 ┃"<<endl;
- cout<<" ┃ ┃"<<endl;
- cout<<" ┃功能选项: ┃"<<endl;
- cout<<" ┃ Y.继续 ┃"<<endl;
- cout<<" ┃ N.不继续 ┃"<<endl;
- cout<<" ┃ E.退出 ┃"<<endl;
- cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
- }
- //实现复数各种运算的函数
- void AchieveComplex()
- {
- Complex c1,c2,c3;//复数对象
- char sign[4];//运算符号
- char s;//功能判断变量
- do{
- cout<<"请输入两个复数:"<<endl;
- cin>>c1>>sign>>c2;
- if(strcmp(sign,"+")==0)
- {
- c3=c1+c2;
- c1.display();
- cout<<"+";
- c2.display();
- cout<<"="<<c3<<endl;
- cout<<"是否继续?(Y/N/E)"<<endl;
- cin>>s;
- if(s=='Y')continue;
- else if(s=='N')break;
- else if(s=='E')exit(0);
- }else if(strcmp(sign,"-")==0)
- {
- c3=c1-c2;
- c1.display();
- cout<<"-";
- c2.display();
- cout<<"="<<c3<<endl;
- cout<<"是否继续?(Y/N/E)"<<endl;
- cin>>s;
- if(s=='Y')continue;
- else if(s=='N')break;
- else if(s=='E')exit(0);
- }else if(strcmp(sign,"*")==0)
- {
- c3=c1*c2;
- c1.display();
- cout<<"*";
- c2.display();
- cout<<"="<<c3<<endl;
- cout<<"是否继续?(Y/N/E)"<<endl;
- cin>>s;
- if(s=='Y')continue;
- else if(s=='N')break;
- else if(s=='E')exit(0);
- }
- else if(strcmp(sign,"/")==0)
- {
- c3=c1/c2;
- c1.display();
- cout<<"/";
- c2.display();
- cout<<"="<<c3<<endl;
- cout<<"是否继续?(Y/N/E)"<<endl;
- cin>>s;
- if(s=='Y')continue;
- else if(s=='N')break;
- else if(s=='E')exit(0);
- }
- else if(strcmp(sign,"+=")==0)
- {
- c1.display();
- cout<<"+=";
- c2.display();
- c1+=c2;
- cout<<"="<<c1<<endl;
- cout<<"是否继续?(Y/N/E)"<<endl;
- cin>>s;
- if(s=='Y')continue;
- else if(s=='N')break;
- else if(s=='E')exit(0);
- }
- else if(strcmp(sign,"-=")==0)
- {
- c1.display();
- cout<<"-=";
- c2.display();
- c1-=c2;
- cout<<"="<<c1<<endl;
- cout<<"是否继续?(Y/N/E)"<<endl;
- cin>>s;
- if(s=='Y')continue;
- else if(s=='N')break;
- else if(s=='E')exit(0);
- }
- else if(strcmp(sign,"==")==0)
- {
- c1.display();
- c2.display();
- if(c1==c2)
- cout<<"两个复数相等"<<endl;
- else
- cout<<"两个复数不相等"<<endl;
- cout<<"是否继续?(Y/N/E)"<<endl;
- cin>>s;
- if(s=='Y')continue;
- else if(s=='N')break;
- else if(s=='E')exit(0);
- }else{
- cout<<"运算符号输入错入,请重新输入!"<<endl;
- continue;
- }
- }while(1);
- }
- //该片段来自于http://www.codesnippet.cn/detail/170320149057.html
来源: http://www.codesnippet.cn/detail/170320149057.html