?? 復(fù)數(shù)計(jì)算器.cpp
字號(hào):
#include<iostream.h>
#include<fstream.h>
#include<math.h>
class complex
{
public:
complex(double r=0.0,double i=0.0)
{
real=r;imag=i;
}
friend complex operator+(complex& x,complex& y);
friend complex operator-(complex& x,complex& y);
friend complex operator*(complex& x,complex& y);
friend int operator>=(complex& x,complex& y);
friend int operator<=(complex& x,complex& y);
friend int operator!=(complex& x,complex& y);
complex& operator+=(complex& y);
complex& operator-=(complex& y);
complex& operator*=(complex& y);
complex& operator--();
complex& operator++();
complex operator++(int);
complex operator--(int);
friend void input(complex& c);
friend void print(complex& c);
friend void fprint(complex& c);
private:
double real,imag;
};
complex operator+(complex& x,complex& y)
{
return complex(x.real+y.real,x.imag+y.imag);
}
complex operator-(complex& x,complex& y)
{
return complex(x.real-y.real,x.imag-y.imag);
}
complex operator*(complex& x,complex& y)
{
return complex((x.real*y.real)-(x.imag*y.imag),(x.real*y.imag)+(x.imag*y.real));
}
int operator>=(complex& x,complex& y)
{
double m,n;
m=sqrt(x.real*x.real+x.imag*x.imag);
n=sqrt(y.real*y.real+y.imag*y.imag);
return m>=n;
}
int operator<=(complex& x,complex& y)
{
double m,n;
m=sqrt(x.real*x.real+x.imag*x.imag);
n=sqrt(y.real*y.real+y.imag*y.imag);
return m<=n;
}
int operator!=(complex& x,complex& y)
{
return (x.real!=y.real)||(x.imag!=y.imag);
}
complex& complex::operator+=(complex& y)
{
this->real+=y.real;
this->imag+=y.imag;
return *this;
}
complex& complex::operator-=(complex& y)
{
this->real-=y.real;
this->imag-=y.imag;
return *this;
}
complex& complex::operator*=(complex& y)
{
this->real=this->real*y.real-this->imag*y.imag;
this->imag=this->real*y.imag+this->imag*y.real;
return *this;
}
complex& complex::operator++()
{
real++;
imag++;
return *this;
}
complex& complex::operator--()
{
real--;
imag--;
return *this;
}
complex complex::operator++(int)
{
complex temp=*this;
real++;
imag++;
return temp;
}
complex complex::operator--(int)
{
complex temp=*this;
real--;
imag--;
return temp;
}
void input(complex& c)
{
double a,b;
cout<<"Please input one real and imag!"<<endl;
cin>>a>>b;
c.real=a;
c.imag=b;
}
void print(complex& c)
{
if(c.imag<0)
cout<<c.real<<c.imag<<'i'<<endl;
else
cout<<c.real<<'+'<<c.imag<<'i'<<endl;
}
void fprint(complex& c)
{
ofstream outfile;
outfile.open("D:\\zh.txt",ios::app);
if(c.imag<0)
outfile<<c.real<<c.imag<<'i'<<endl;
else
outfile<<c.real<<'+'<<c.imag<<'i'<<endl;
outfile.close();
}
void main()
{
ofstream outfile;
outfile.open("D:\\zh.txt",ios::app);
outfile<<"*****進(jìn)入系統(tǒng)*****"<<endl;
complex c1,c2,c3;
int a,b;
cout<<"********************************"<<endl;
cout<<endl;
cout<<"*****復(fù)數(shù)計(jì)算器的菜單表如下*****"<<endl;
cout<<"************0 退出系統(tǒng)*********"<<endl;
cout<<"************1 '+'**************"<<endl;
cout<<"************2 '-'**************"<<endl;
cout<<"************3 '*'**************"<<endl;
cout<<"************4 '+='*************"<<endl;
cout<<"************5 '-='*************"<<endl;
cout<<"************6 '*='*************"<<endl;
cout<<"************7 '++'(前綴)*******"<<endl;
cout<<"************8 '++'(后綴)*******"<<endl;
cout<<"************9 '--'(前綴)*******"<<endl;
cout<<"***********10 '--'(后綴)*******"<<endl;
cout<<"***********11 '>='*************"<<endl;
cout<<"***********12 '<='*************"<<endl;
cout<<"***********13 '!='*************"<<endl;
cout<<endl;
cout<<"********************************"<<endl;
do
{
cout<<"請(qǐng)按菜單輸入一個(gè)數(shù)進(jìn)行操作"<<endl;
// while(1)
// {
// cin>>a;
// if(a>=0&&a<=13)
// break;
// else
// {
// cout<<"輸入錯(cuò)誤,請(qǐng)注意菜單重新輸入"<<endl;
// cout<<"如果計(jì)算完畢請(qǐng)直接輸0退出系統(tǒng)"<<endl;
// }
// }
cin>>a;
if(a==1||a==2||a==3||a==4||a==5||a==6||a==11||a==12||a==13)
{
input(c1);
input(c2);
cout<<"c1=";
print(c1);
cout<<"c2=";
print(c2);
outfile<<"復(fù)數(shù) c1為:"<<endl;
fprint(c1);
outfile<<"復(fù)數(shù) c2為:"<<endl;
fprint(c2);
}
if(a==7||a==8||a==9||a==10)
{
input(c2);
cout<<"c2=";
print(c2);
outfile<<"復(fù)數(shù)-c2為:"<<endl;
fprint(c2);
}
switch(a)
{
case 1:
c3=c1+c2;
cout<<"************ c1+c2 ***********"<<endl;
cout<<"c1+c2=";
print(c3);
outfile<<"****c1+c2*****"<<endl;
fprint(c3);
break;
case 2:
c3=c2-c1;
cout<<"************ c1-c2 ***********"<<endl;
cout<<"c2-c1=";
print(c3);
outfile<<"****c2-c1*****"<<endl;
fprint(c3);
break;
case 3:
c3=c1*c2;
cout<<"************ c1*c2 ***********"<<endl;
cout<<"c1*c2=";
print(c3);
outfile<<"****c1*c2*****"<<endl;
fprint(c3);
break;
case 4:
c1+=c2;
cout<<"************ c1+=c2 **********"<<endl;
cout<<"c1=";
print(c1);
outfile<<"****c1+=c2****"<<endl;
fprint(c1);
break;
case 5:
c1-=c2;
cout<<"************ c1-=c2 **********"<<endl;
cout<<"c1=";
print(c1);
outfile<<"****c1-=c2****"<<endl;
fprint(c1);
break;
case 6:
c1*=c2;
cout<<"************ c1*=c2 **********"<<endl;
cout<<"c1=";
print(c1);
outfile<<"****c1*=c2****"<<endl;
fprint(c1);
break;
case 7:
c3=++c2;
cout<<"*********** c3=++c2 **********"<<endl;
cout<<"c3=";
print(c3);
outfile<<"****c3=++c2***"<<endl;
fprint(c3);
break;
case 8:
c3=c2++;
cout<<"*********** c3=c2++ **********"<<endl;
cout<<"c3=";
print(c3);
outfile<<"****c3=c2++***"<<endl;
fprint(c3);
break;
case 9:
c3=--c2;
cout<<"*********** c3=--c2 **********"<<endl;
cout<<"c3=";
print(c3);
outfile<<"****c3=--c2***"<<endl;
fprint(c3);
break;
case 10:
c3=c2--;
cout<<"*********** c3=c2-- **********"<<endl;
cout<<"c3=";
print(c3);
outfile<<"****c3=c2--***"<<endl;
fprint(c3);
break;
case 11:
b=c1>=c2;
cout<<"*********c1>=c2結(jié)果為:*********"<<endl;
cout<<b<<endl;
outfile<<"c1>=c2結(jié)果為:"<<b<<endl;
break;
case 12:
b=c1<=c2;
cout<<"*********c1<=c2結(jié)果為:*********"<<endl;
cout<<b<<endl;
outfile<<"c1<=c2結(jié)果為:"<<b<<endl;
break;
case 13:
b=c1!=c2;
cout<<"*********c1!=c2結(jié)果為:*********"<<endl;
cout<<b<<endl;
outfile<<"c1!=c2結(jié)果為:"<<b<<endl;
break;
default:
{
cout<<"輸入錯(cuò)誤,請(qǐng)注意菜單重新輸入"<<endl;
cout<<"如果計(jì)算完畢請(qǐng)直接輸0退出系統(tǒng)"<<endl;
}
}
}while(a!=0);
cout<<endl;
cout<<"*********** 謝謝使用 ***********"<<endl;
outfile<<"*****退出系統(tǒng)*****"<<endl;
outfile.close();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -