C++编程,定义一个复数类

定义一个复数类,并重载运算符,以实现复数的加法与减法运算,及显示运算结果

第1个回答  推荐于2017-11-24
//complex类
#include<iostream.h>
#include<math.h>

class complex{
double real,imag;
public:
complex(){real=5;imag=5;}//缺省构造函数
complex(double r){real=r;imag=0;}//只给实部赋值的构造函数
complex(double r,double i){real=r;imag=i;}//同时给实、虚部赋值的函数
double displayreal(){return real;}//返回复数实部
double displayimag(){return imag;}//返回复数虚部
complex operator+(complex c);//实现复数相加
complex operator-(complex c);//实现复数相减
complex operator*(complex c);//实现复数相乘
double cab(complex c);//求复数绝对值(模)
complex sqr(complex c);//求复数平方根
friend ostream &operator<<(ostream &out,complex &obj);//实现复数的输出
};
complex complex::operator+(complex c)//实现复数相加
{
real+=c.real;
imag+=c.imag;
return *this;
}

complex complex::operator-(complex c)//实现复数相减
{
real-=c.real;
imag-=c.imag;
return *this;
}

complex complex::operator*(complex c)//实现复数相乘
{
real=real*c.real-imag*c.imag;
imag=real*c.imag+imag*c.real;
return *this;
}

double complex::cab(complex c)//求复数绝对值(模)
{
double ri;
ri=sqrt(c.real*c.real+c.imag*c.imag);
return ri;
}

complex complex::sqr(complex c)//求复数平方根
{
real=sqrt((cab(c)+c.real)/2);
imag=sqrt((cab(c)-c.real)/2);
return *this;
}

ostream &operator<<(ostream &out,complex &obj)//实现复数的输出
{
if(obj.imag==0)out<<obj.real;
else out<<obj.real<<"+"<<obj.imag<<"i";
return out;
}

void answer(double a,double b,double c)//求根函数
{
complex answer1,answer2;
double an=b*b-4*a*c;
if(an>=0)
{
answer1=complex((-b+sqrt(an))/(2*a));
answer2=complex((-b-sqrt(an))/(2*a));
}
else
{
answer1=complex(-b/(2*a),sqrt(-an)/(2*a));
answer2=complex(-b/(2*a),-sqrt(-an)/(2*a));
}
cout<<"The answer is:"<<endl;
cout<<answer1<<" and "<<answer2<<endl;
}
int main()//主函数
{
complex a,b(2),c(6,9);//以下测试类中定义的各个函数,你可以删除的
cout<<"a="<<a<<", b="<<b<<", c="<<c<<endl;
c=a+b;
cout<<"a+b="<<c<<endl;
cout<<"a="<<a<<", b="<<b<<", c="<<c<<endl;
c=a-b;
cout<<"a-b="<<c<<endl;
cout<<"a="<<a<<", b="<<b<<", c="<<c<<endl;
c=a*b;
cout<<"a*b="<<c<<endl;
cout<<"a="<<a<<", b="<<b<<", c="<<c<<endl;
cout<<"cab(a)="<<a.cab(a)<<endl;
cout<<"a="<<a<<", b="<<b<<", c="<<c<<endl;
cout<<"sqr(a)="<<a.sqr(a)<<endl;//以上测试类中定义的各个函数

answer(1,1,1);//方程解为虚数的情况
answer(1,3,1);//方程解为实数的情况
return 0;
}本回答被提问者采纳

定义一个描述复数的类,数据成员包括实部和虚部;成员函数包括输出复数以 ...
C++实现的复数类,代码如下 \/\/complex.cpp include <iostream> using namespace std;class complex{ \/\/复数类 public:complex(double r=0.0, double i=0.0);complex operator+(const complex& p)const;\/\/重载运算符+ friend ostream& operator <<(ostream& out,const complex& pp);\/\/重载操作...

(C++)定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算...
include <iostream>using namespace std;class Complex{public: Complex(double r=0,double i=0):real(r),imag(i) { }; friend Complex operator +(Complex &c1,Complex &c2); friend Complex operator +(Complex &c,double i); friend Complex operator +(double i,Complex &c); friend ostr...

急!C++定义一个复数类Complex,使下面的代码能够工作?
c2.show();c1.add(c2);c1.show();c2.sub(c1);c2.show();return 0;}

C++设计一个类 实现复数加减乘除的功能,?? 代码版的啊 最好能有注释...
char temp[32];void add();void cut();void multiply();void divide();private:void init();};void calc::init(){ cout<<"请输入第一个数的实部:";cin>>a1;cout<<"请输入第一个数的虚部:";cin>>b1;cout<<"请输入第二个数的实部:";cin>>a2;cout<<"请输入第二个数的虚部:";...

C++定义一个复数类Complex,包括实数部分real和虚数部分imaginary两个私...
class Complex { double real;double imaginary;public:Complex(double & r=0,double & i=0):real(r),imaginary(i){} void OutPut(void){ cout << real;if (imaginary>=0) cout << '+';cout << imaginary;} };

编写一个复数类
1、由于没有指定语言,假设你问的是C++语言吧。2、C++语言实现的复数类代码 include <iostream.h>class plural{\/* 定义私有变量——实部,虚部; *\/ double real; double imag;public: plural( double r = 0.0, double i = 0.0 ) { real = r; imag = i; } \/* 初始化; *\/ do...

求C++程序代码: 建立 一个复数类 Complex
{ cout<<"该复数的模为:"<<sqrt(real*real+imaginary*imaginary)<<endl;}void Complex::out_view(){ cout<<"该复数的辐角为:"<<atan(real\/imaginary)<<endl; \/\/修改后的 }\/\/void Complex::out_it(float a,float b) 这句也是一样,定义与声明不同 void Complex::out_it(){ co...

用C++编写一个复数计算器。为复数定义一个类,形式a+bi。a,b为double...
c3.imag=(c1.imag*c2.real-c1.real*c2.imag)\/t;t=c2.real*c2.real-c2.imag*c2.imag;return c3;} void menu(){ cout<<"1 加法\\n";cout<<"2 减法\\n";cout<<"3 乘法\\n";cout<<"4 除法\\n";cout<<"0 退出\\n";cout<<"请选择:";} void main(){ do{ menu();i...

C++的问题:定义描述复数的结构体类型变量,并实现复数之间的加减法运算和...
include "stdafx.h"#include <iostream>using namespace std;struct complex \/\/定义一个复数结构体{ int real; \/\/实数,数据类型根据自己的要求改吧 int imaginary; \/\/虚数,同上 friend complex operator + (complex a, complex b); \/\/重载运算符“+”,其运算结果为复数,即返回类型为comp...

用C++语言设计一个虚数类,要求虚数类中重载运算符加减,主函数定义类...
\/*1.复数(实部运算 +虚部运算) 1+2i 1-3i 加:2-i 减 0-5i1 重载实现复数一个输入和输出 普通写法实现输入 调用函数的形式实现输出2. 类重载实现复数的加法和减法 加法:类重载 减法:友元重载*\/#include<iostream>using namespace std;class A{private:int x;int ...

相似回答