⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 复数计算器.cpp

📁 这是一个很小的c语言实现的复数计算器
💻 CPP
字号:
#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<<"*****进入系统*****"<<endl;
	complex c1,c2,c3;
	int a,b;
	cout<<"********************************"<<endl;
	cout<<endl;
	cout<<"*****复数计算器的菜单表如下*****"<<endl;
	cout<<"************0  退出系统*********"<<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<<"请按菜单输入一个数进行操作"<<endl;
//		while(1)
//		{
//			cin>>a;
//			if(a>=0&&a<=13)
//				break;
//			else
//			{
//				cout<<"输入错误,请注意菜单重新输入"<<endl;
//				cout<<"如果计算完毕请直接输0退出系统"<<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<<"复数 c1为:"<<endl;
			fprint(c1);
			outfile<<"复数 c2为:"<<endl;
			fprint(c2);
		}
		if(a==7||a==8||a==9||a==10)
		{
			input(c2);
			cout<<"c2=";
			print(c2);
		 	outfile<<"复数-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结果为:*********"<<endl;
			cout<<b<<endl;
			outfile<<"c1>=c2结果为:"<<b<<endl;
			break;
		case 12:
			b=c1<=c2;
			cout<<"*********c1<=c2结果为:*********"<<endl;
			cout<<b<<endl;
			outfile<<"c1<=c2结果为:"<<b<<endl;
			break;
		case 13:
			b=c1!=c2;
			cout<<"*********c1!=c2结果为:*********"<<endl;
			cout<<b<<endl;
			outfile<<"c1!=c2结果为:"<<b<<endl;
			break;
		default:
			{
				cout<<"输入错误,请注意菜单重新输入"<<endl;
				cout<<"如果计算完毕请直接输0退出系统"<<endl;
			}
		}
	}while(a!=0);
	cout<<endl;
	cout<<"*********** 谢谢使用 ***********"<<endl;
	outfile<<"*****退出系统*****"<<endl;
	outfile.close();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -