精华内容
下载资源
问答
  • 复数类Complex

    2021-01-29 01:12:42
    定义一个复数类 Complex,虚部和实部为私有数据类型 相关阅读 相关阅读 完整代码 #include<bits/stdc++.h> using namespace std; class Complex{ private: double re, im; public: Complex(){ re = 0; im...

    题目

    定义一个复数类 Complex,虚部和实部为私有数据类型

    相关阅读

    相关阅读

    完整代码

    #include<bits/stdc++.h>
    using namespace std;
    
    class Complex{
    private:
        double re, im;
    public:
        Complex(){
            re = 0;
            im = 0;
        };
        double real() const {return  re;};
        double imag() const {return im;};
        void show(){
            if (imag() >= 0)
                cout << real() << " + " << imag() << "i" << endl;
            else
                cout << real() << " - " << -imag() << "i" << endl;
        }
    
        Complex& operator+=(Complex complex){
            this->re += complex.re;
            this->im += complex.im;
            return *this;
        };
        Complex& operator+=(double i){
            this->re += i;
            return *this;
        };
        Complex& operator-(){
            this->re = -re;
            this->im = -im;
            return *this;
        }
        Complex& operator+(){
            return *this;
        }
        friend Complex operator+(Complex complex1, Complex complex2);
        friend Complex operator+(double real, Complex complex2);
        friend Complex operator+(Complex complex1, double real);
        friend Complex operator-(Complex complex1, Complex complex2);
        friend Complex operator-(double real, Complex complex2);
        friend Complex operator-(Complex complex1, double real);
        friend ostream& operator<<(ostream& out, Complex complex1);
        friend istream& operator>>(istream& in, Complex &complex1);
        friend Complex operator*(Complex complex1, Complex complex2);
        friend Complex operator*(double real, Complex complex2);
        friend Complex operator*(Complex complex1, double real);
        friend Complex operator/(Complex complex1, Complex complex2);
        friend Complex operator/(double real, Complex complex2);
        friend Complex operator/(Complex complex1, double real);
    };
    Complex operator+(Complex complex1, Complex complex2){
        Complex temp;
        temp.re = complex1.re + complex2.re;
        temp.im = complex1.im + complex2.im;
        return temp;
    }
    Complex operator+(double real, Complex complex2){
        Complex temp;
        temp.re = real + complex2.re;
        temp.im = complex2.im;
        return temp;
    }
    Complex operator+(Complex complex1, double real){
        Complex temp;
        temp.re = real + complex1.re;
        temp.im = complex1.im;
        return temp;
    }
    Complex operator-(Complex complex1, Complex complex2){
        Complex temp;
        temp.re = complex1.re - complex2.re;
        temp.im = complex1.im - complex2.im;
        return temp;
    }
    Complex operator-(double real, Complex complex2){
        Complex temp;
        temp.re = real - complex2.re;
        temp.im = -complex2.im;
        return temp;
    }
    Complex operator-(Complex complex1, double real){
        Complex temp;
        temp.re = complex1.re - real;
        temp.im = complex1.im;
        return temp;
    }
    Complex operator*(Complex complex1, Complex complex2){
        Complex temp;
        temp.re = complex1.re*complex2.re - complex1.im*complex2.im;
        temp.im = complex1.im*complex2.re + complex1.re*complex2.im;
        return temp;
    }
    Complex operator*(double real, Complex complex2){
        Complex temp;
        temp.re = real*complex2.re;
        temp.im = real*complex2.im;
        return temp;
    }
    Complex operator*(Complex complex1, double real){
        Complex temp;
        temp.re = real*complex1.re;
        temp.im = real*complex1.im;
        return temp;
    }
    Complex operator/(Complex complex1, Complex complex2){
        Complex temp;
        temp.re = (complex1.re*complex2.re + complex1.im*complex2.im) /
                (complex2.re*complex2.re + complex2.im*complex2.im);
        temp.im = (complex1.im*complex2.re - complex1.re*complex2.im) /
                (complex2.re*complex2.re + complex2.im*complex2.im);
        return temp;
    }
    Complex operator/(double real, Complex complex2){
        Complex temp;
        temp.re = (real*complex2.re) /
                  (complex2.re*complex2.re + complex2.im*complex2.im);
        temp.im = (-real*complex2.im) /
                  (complex2.re*complex2.re + complex2.im*complex2.im);
        return temp;
    }
    Complex operator/(Complex complex1, double real){
        Complex temp;
        temp.re = (complex1.re*real) /
                  (real*real);
        temp.im = (complex1.im*real) /
                  (real*real);
        return temp;
    }
    ostream& operator<<(ostream& out, Complex complex1){
        if (complex1.imag() >= 0)
            out << complex1.real() << " + " << complex1.imag() << "i" << endl;
        else
            out << complex1.real() << " - " << -complex1.imag() << "i" << endl;
    }
    istream& operator>>(istream& in, Complex& complex1){
        in >> complex1.re >> complex1.im;
    }
    int main(){
        Complex c1, c2;
        double i = 2.0;
        cin >> c1 >> c2;
        cout << c1+c2;
        cout << i+c2;
        cout << c1+i;
        cout << c1-c2;
        cout << i-c2;
        cout << c2-i;
        c1 += c2;
        cout << -c2;
        cout << +c2;
        cout << c1*c2;
        cout << i*c2;
        cout << c1*i;
        cout << c1/c2;
        cout << i/c2;
        cout << c1/i;
    }
    
    展开全文
  • 复数类 complex

    2017-09-23 11:06:43
    定义复数类Complex,包括两个double型的数据成员real和image,要求对+、-、、/、+=、-+、=、/=、几个运算符进行重载以实现复数的运算,并进行验证。#define _CRT_SECURE_NO_WARNINGS #include <iostream> using ...

    定义复数类Complex,包括两个double型的数据成员real和image,要求对+、-、、/、+=、-+、=、/=、几个运算符进行重载以实现复数的运算,并进行验证。

    #define _CRT_SECURE_NO_WARNINGS   
    #include <iostream>  
    using namespace std;
    
    class Complex
    {
    public:
        //默认的构造函数(含缺省值,并初始化参数)  
        Complex(const double real = 0.0, const double image = 0.0)
            :_real(real)  //初始化
            , _image(image)
        {}
    
        //拷贝构造函数  
        Complex(const Complex& c)
        {
            _real = c._real;
            _image = c._image;
        }
        //析构函数  
        ~Complex()
        {}
        //赋值运算符的重载  
        Complex& operator=(const Complex& c)
        {
            this->_real = c._real;
            //_real = c._real;//隐含的this指针
            this->_image = c._image;
            //_image = c._image;
            return *this;
        }
        //加法运算符的重载  
        Complex operator+(const Complex& c)
        {
            Complex tmp;
            tmp._real = _real + c._real;
            tmp._image = _image + c._image;
            return tmp;
        }
        //减法
        Complex operator-(const Complex& c)
        {
            Complex tmp;
            tmp._real = _real - c._real;
            tmp._image = _image - c._image;
            return tmp;
        }
        //乘法
        Complex operator*(const Complex& c)
        {
            Complex tmp;
            tmp._real = _real * c._real - _image*c._image;
            tmp._image = _real * c._image + _image*c._real;
            return tmp;
        }
        //除法
        Complex operator/(const Complex& c)
        {
            Complex tmp;
            tmp._real = (_real / c._real + _image*c._image) / (c._real*c._real + c._image*c._image);
            tmp._image = (_image*c._real - _real*c._image) / (c._real*c._real + c._image*c._image);
            return tmp;
        }
        Complex operator+=(const Complex& c)
        {
            _real += c._real;
            _image += c._image;
            return *this;
        }
        Complex operator-=(const Complex& c)
        {
            _real -= c._real;
            _image -= c._image;
            return *this;//返回当前对象
        }
        Complex operator*=(const Complex& c)
        {
            double real0 = _real;
            double image0 = _image;
            _real = (real0 * c._real - image0*c._image);
            _image = (real0 * c._image + image0*c._real);
            return *this;
        }
        Complex operator/=(const Complex& c)
        {
            double real0 = _real;
            double image0 = _image;
            _real /= (real0 / c._real + image0*c._image) / (c._real*c._real + c._image*c._image);
            _image /= (image0*c._real - real0*c._image) / (c._real*c._real + c._image*c._image);
            return *this;
        }
    
        void Display()
        {
            cout << _real << " " << _image << endl;
        }
    
    private:
        double _real;
        double _image;
    };
    //测试函数  
    void Funtest1()
    {
        Complex c1(2.0, 3.0);
        c1.Display();
        Complex c2(c1);
        c2.Display();
        Complex c3;
        c3 = c1+c2;  
        c3.Display();
    
    }
    void Funtest2()
    {
        Complex c1(2.0, 3.0);
        c1.Display();
        Complex c2(c1);
        c2.Display();
        c1 *= c2;
        c1.Display();
    }
    
    int main()
    {
        cout << "Funtest1:" << endl;
        Funtest1();
        cout << "Funtest2:" << endl;
        Funtest2();
        getchar();
        return 0;
    }

    运行结果

    展开全文
  • 复数类 Complex

    2017-07-06 19:21:34
    ---》:原则上复数是不能比较大小的,此处主要是练习运算符重载才写的复数比较大小: #include using namespace std; class Complex { public: Complex(double real = 0.0,double image = 0.0) //构造函数 :_...

    ---》:原则上复数是不能比较大小的,此处主要是练习运算符重载才写的复数比较大小:

    #include<iostream>
    using namespace std;
    class Complex 
    { 
    public:  
    	Complex(double real = 0.0,double image = 0.0) //构造函数
    		:_real(real)
    		,_image(image)
    	{}
    	Complex(const Complex& c)  //拷贝构造函数
    		:_real(c._real)
    		,_image(c._image)
    	{}
    	~Complex()  //析构函数
    	{}
    	void Display()  //显示函数
    	{
    		cout<<_real<<"+"<<_image<<"i"<<endl;
    	}
    	Complex operator+(Complex c)  //重载+函数
    	{
    		_real = _real + c._real;
    		_image = _image + c._image;
    		return *this;
    	}
    	bool operator>(const Complex& c)const  //重载>函数
    	{
    		if(_real>c._real)
    			return true;
    		else if(_real==c._real)
    		{
    			if(_image>c._image)
    			{
    				return true;
    			}
    			else
    				return false;
    		}
    		else
    			return false;
    	}
    	bool operator<(const Complex& c)const //重载<函数
    	{
    		if(_real<c._real)
    			return true;
    		else if(_real==c._real)
    		{
    			if(_image<c._image)
    			{
    				return true;
    			}
    			else
    				return false;	
    		}
    		else
    			return false;	
    	}
    	bool operator ==(const Complex& c)const  //重载==函数
    	{
    		if((_real == c._real)&&(_image == c._image))
    			return true;
    		else
    			return false;
    	}
    	bool operator!=(const Complex& c)  //重载!=函数
    	{
    		return !(*this == c);
    	}
    	Complex& operator=(const Complex& c)
    	{
    		_real = c._real;
    		_image = c._image;
    		return *this;
    	}
    	
    	Complex operator++()  //前置++
    	{
    		_real++;
    		return *this;
    	}
    	Complex operator++(int)  //后置++
    	{
    		Complex temp = *this;
    		_real++;
    		return temp;
    	}
    	Complex operator--()  //前置--
    	{
    		_real--;
    		return *this;
    	}
    	Complex operator--(int)  //后置--
    	{
    		Complex temp = *this;
    		_real--;
    		return temp;
    	}
    	Complex operator+=(double x)  //重载+=函数
    	{
    		_real += x;
    		return *this;
    	}
    protected: 
    	double _real; 
    	double _image; 
    }; 
    void Test1()  // +(加)、++(后置)、++(前置)
    {
    	Complex c1(1,2);
    	Complex c2(2,3);
    	Complex c3(3,5);
    	Complex c4,c5,c6;
    	c4=(c1+c2);
    	c4.Display();
    	c5=c2++;
    	c5.Display();
    	c6=++c3;
    	c6.Display();
    }
    void Test2()// >(大于)、<(小于)、+=(加等)、==(等于)
    {
    	Complex c1(2,2);
    	Complex c2(4,3);
    	Complex c3(3,5);
    	bool c = true;
    	c=c1<c2;
    	cout<<c<<endl;
    	c=c1>c3;
    	cout<<c<<endl;
    	Complex c4(2,2);
    	Complex c5;
    	c5=c4+=3;
    	c5.Display();
    	Complex c6(2,2);
    	c=c1==c6;
    	cout<<c<<endl;	
    }
    int main()
    {
    	Test1();
    	Test2();
    	return 0;
    }
    吐舌头


    展开全文
  • (1)复数类Complex的属性有: RealPart:int型,代表复数的实数部分, ImaginPart;int型,代表复数的虚数部分 (2)复数类Complex的方法有, Complex():构造函数,将复数的实部和虚部都置0。 Complex(int r.int i):...

    (1)复数类Complex的属性有:
    RealPart:int型,代表复数的实数部分,
    ImaginPart;int型,代表复数的虚数部分
    (2)复数类Complex的方法有,
    Complex():构造函数,将复数的实部和虚部都置0。
    Complex(int r.int i):构造函数,形参r为实部的初值,为虚部的初值。
    Complex complexAdd(Complex a):将当前复数对象与形参复数对象相加,所得的结
    果仍是一个复数值,返回给此方法的调用者。
    String ToString():把当前复数对象的实部、虚部组合成a+bi的字符串形式,其中a和b分别为实部和虚部的数据。
    Calca类

    public class Calca {
    	public static void main(String[] args) {
    		Complex a=new Complex(1,2);
    		Complex b=new Complex(3,4);
    		System.out.println("a="+a.ToString());
    		System.out.println("b="+b.ToString());
    		Complex c=a.complexAdd(b);
    		System.out.println("a+b="+c.ToString());
    	}
    
    }
    
    

    Complex类

    public class Complex {
    	int RealPart;
    	int ImaginPart;
    	Complex(){
    		RealPart=0;
    		ImaginPart=0;
    	}
    	Complex(int r,int i){
    		RealPart=r;
    		ImaginPart=i;
    	}
    	Complex complexAdd(Complex a) {
    		this.RealPart+=a.RealPart;
    		this.ImaginPart+=a.ImaginPart;
    		return this;
    	}
    	String ToString() {
    		String str=RealPart+"+"+ImaginPart+"i";
    		return str;
    	}
    }
    
    展开全文
  • 题目:定义一个复数类Complex,使得下面的代码能够工作Complex c1(3,5); //用复数3+5i初始化c1Complex c2=4.5; //用实数4.5初始化c2c1.add(c2); //将c1与c2相加,结果保存在c1中c1.show(); //将c1输出(这时的结果应该...
  • Java声明复数类Complex

    千次阅读 2020-03-24 07:34:42
    声明复数类Complex,成员变量包括实部和虚部,成员方法包括实现由字符串构造复数、复数加法、减法,字符串描述、比较相等、计算复数的模等操作。 百度百科—对复数的介绍 import java.util.Scanner; public class ...
  • 【其它】复数类的定义 声明复数类Complex,成员变量包括double类型的实部real和double类型的虚部im,请定义如下成员方法: 1、两个构造方法: Complex(double real, double im); Complex(),该无参构造方法内部用 this(0,...
  • 1.定义一个复数类Complex,该类有代表实部与虚部的成员变量,有两个构造函数Complex() 2016-08-23 0 0 0 暂无评分 其他 1积分下载 如何获取积分?1.定义一个复数类Complex,该类有代表实部与虚部的成员变量,有两...
  • Java实验——复数类complex 创建一个复数类complex,以复数进行数学运算、复数具有如下格式:RealPart+ImaginaryRart*I ,其中,I为-1的平方根。 要求如下: 1、利用浮点变量表示此类的私有数据。提供两个构造方法,...
  • 定义满足以下要求的复数类Complex 属性: realPart(int型,代表复数的实数部分) imagePart(int型,代表复数的虚数部分) 方法: Complex():将实部和虚部设置为0; Complex(int r,int i):将实部和...
  • 设计复数类Complex

    2014-12-11 22:00:38
    // Copyright (c) 2014软件...  // All rights reserved.  // 作 者: B31 ... // 完成日期:2014年12月11日 ... // 问题描述:设计复数类Complex,计算两个复数之和、差,同时以a+bi 的字符串形式显示。  
  • c++复数类Complex的编写

    2017-07-17 22:35:36
    c++复数类Complex的编写。复数:由实部和虚部组成。主要实现几个运算符的重载:=/==/+/-/+=/-+/前置++/前置–/后置++/后置–主要实现代码如下:头文件Complex.h 函数声明和类定义#include #pragma onceclass Complex...
  • // All rights reserved. // 作 者:B39 // 完成日期:2014年 12 月 8 日 // 版 本 号:v1.0 // 问题描述:设计复数类Complex进行计算 //输入描述:设计复数类Complex,计算两个复数之和、差,同
  • 再来个复数类complexNumber,定义了a+bi型的复数及其相关的加减乘除运算,以及输入输出,取实部虚部,设置实部虚部,求取幅值和幅角等函数,较为简单,不再赘述,直接看代码。
  • C# 复数类 Complex

    2020-03-03 19:37:02
    1. 前言 在.net 中没有提供复数类的操作。第三方库Math.net中包含了复数的相关...复数类编写不具有很大难度,但多数复数类均只支持使用实部虚部模式构造一个复数,而幅值相位模式也是构造复数的一个方式。现给出可...
  • C#复数类Complex的封装

    千次阅读 2015-07-20 21:31:06
    C#复数类Complex的封装 ----------------------------------------------------------------------------------------------------------------------------------------------------------本文作者:随煜而...
  • 题目内容:定义复数类Complex,并使用成员函数add实现复数加法,并输出结果。注意虚部为负数时,要写成3-4i,不能写成3±4i,如果实部或者虚部为0,则可以不输出,例如:4i不能输出为0+4i,而是直接输出4i,4不能输出为4+...
  • 编写一个程序,使用复数类Complex验证两个复数 1+2i 和3+4i 相加产生一个新的复数 4+6i 。 复数类Complex必须满足如下要求: (1) 复数类Complex 的属性有: realPart : int型,代表复数的实数部分 imaginPart : ...
  • 复数类complex的实现——答案 首先是头文件ClassComplexNumber.h,其给出了复数类complex的定义。 // 复数类头文件_示例程序 #ifndef complex_H #define complex_H class complex { private: double ...
  • 创建一个复数类Complex,用来执行复数的算术运算,并编写一个程序来测试该类。(1)该类有两个成员变量,即实部realPart 和虚部imaginaryPart,均为实数类型。(2年一个构造函数,用来对类对象进行初始化。(3)定 义一个add...
  • 定义一个复数类 Complex ,重载运算符 “+” ,使之能用于复数的加法运算。参加运算的两个运算量可以都是复数类对象,也可以其中一个是整数。例如:c1+c2, c1+i, i+c1 均合法(设 i 为整数,c1、c2为复数)。编程序...
  • 展开全部#includeusingnamespacestd;classComplex{private:doublere;doubleim;public:Complex(doublere,doubleim=0){this->re=re;this->im=im;}voidadd(constComplex&c){this->re+=c.re;t...
  • 定义一个复数类Complex,使下面的代码能够工作: Complex c1(3, 5); // 用复数3+5i初始化c1 Complex c2=4.5; // 用实数4.5初始化c2 c1.add(c2); // 将C1与c2相加,结果保存在c1中 c1.show(); //...
  • 建立一个复数类Complex,其私有数据成员mX和mY表示复数的实部和虚部,构造函数Complex用于对复数的实部和虚部初始化,友员函数Add,Sub,Mul和Div分别用于进行复数的加、减、乘和除法运算,函数Show用于显示运算结果。...
  • C++实现的复数类Complex

    千次阅读 2018-09-13 19:58:45
    使用C++实现的复数类计算: //Coded by ZZ //#define COMPLEX_H #if !defined(COMPLEX_H) #define COMPLEX_H #endif #include &lt;iostream&gt; #include &lt;iomanip&gt; class Complex { ...
  • 设计一个名为Complex复数类来表示复数以及完成复数运算的add、substract、multiply、divide和 calAbs(绝对值)方法,并且覆盖toString方法以返回一个表示复数的字符串。方法toString返回字符串 a+bi。如果b是0,...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,914
精华内容 765
关键字:

复数类complex