c++实现复数运算(运算符重载)

#include <iostream>

using namespace std;

class Complex

{

public:

//构造函数

Complex(int real=2,int image=4)

:_real(real)

,_image(image)

{

cout<<"构造函数"<<endl;

}

//拷贝构造函数

Complex(Complex& c)

{

cout<<"拷贝构造函数"<<endl;

_real = c._real;

_image = c._image;

}

//析构函数

~Complex()

{

cout<<"析构函数"<<endl;

}

Complex& operator=(Complex& c)//赋值运算符的重载

{

cout<<"赋值运算符的重载"<<endl;

if(this != &c)

{

_real = c._real;

_image = c._image;

}

return *this;

}

void Print()

{

if(_image > 0 )

{

cout<<_real<<"+"<<_image<<"i"<<endl;

}

else

{

cout<<_real<<_image<<"i"<<endl;

}

}

Complex operator+(Complex& c)//加

{

Complex tmp(*this);

tmp._real += c._real;

tmp._image += c._image;

return tmp;

}

Complex& operator+=(Complex& c)//a+=b  a=a+b

{

this->_real += c._real;

this->_image += c._image;

return *this;

}

Complex operator-(Complex& c)//减

{

Complex tmp(*this);

tmp._real -= c._real;

tmp._image -= c._image;

return tmp;

}

Complex& operator-=(Complex& c)//a-=b  a=a-b

{

this->_real -= c._real;

this->_image -= c._image;

return *this;

}

Complex operator*(Complex &c)//乘

{

Complex tmp(*this);

tmp._real = this->_real * c._real - this->_image * c._image;

tmp._image = this->_real * c._image + this->_image * c._real;

return tmp;

}

Complex& operator*=(Complex &c)    //a*=b  a=a*b

{

this->_real = this->_real * c._real - this->_image * c._image;

this->_image = this->_real * c._image + this->_image * c._real;

return *this;

}

Complex operator/(Complex &c)//除

{

Complex tmp(*this);

tmp._real = (this->_real * c._real + this->_image * c._image)/(c._real*c._real+c._image*c._image);

tmp._image = (this->_real * c._image * (-1) + this->_image * c._real)/(c._real*c._real+c._image*c._image);

return tmp;

}

Complex& operator/=(Complex &c)

{

this->_real = (this->_real * c._real + this->_image * c._image)/(c._real*c._real+c._image*c._image);

this->_image = (this->_real * c._image * (-1) + this->_image * c._real)/(c._real*c._real+c._image*c._image);

return *this;

}

Complex& operator++()//前置++

{

this->_real++;

this->_image++;

return *this;

}

Complex operator++(int)//后置++

{

Complex tmp(*this);

this->_real++;

this->_image++;

return tmp;

}

Complex& operator--()//前置--

{

this->_real--;

this->_image--;

return *this;

}

Complex operator--(int)//后置--

{

Complex tmp(*this);

this->_real--;

this->_image--;

return tmp;

}

bool operator==(Complex &c)//相等

{

return (this->_real == c._real)&&(this->_image == c._image);

}

bool operator!=(Complex &c)//不等

{

return !(this->_real == c._real)&&(this->_image == c._image);

}

bool operator<(Complex &c)//小于

{

return ((this->_real<c._real)&&(this->_image == 0)&&(c._image ==0));

}

bool operator<=(Complex &c)//小于等于

{

return ((this->_real<=c._real)&&(this->_image == 0)&&(c._image ==0));

}

bool operator>(Complex &c)//大于

{

return ((this->_real>c._real)&&(this->_image == 0)&&(c._image ==0));

}

bool operator>=(Complex &c)//大于等于

{

return ((this->_real>=c._real)&&(this->_image == 0)&&(c._image ==0));

}

ostream &operator <<(ostream &os)//输出

{

os<<this->_real<<"+"<<this->_image<<"i"<<endl;

return os;

}

istream &operator >>(istream &is)//输入

{

is>>this->_real>>this->_image;

return is;

}

private:

int _real;

int _image;

/*double _real;

double _image;*/

};

int main()

{

Complex c;

Complex c1(5,8);

Complex c3 = c.operator /=(c1);//Complex c3 = (c/c1);

c3.Print();

//c.operator=(c1);

//Complex c3 = c.operator+(c1);

/*Complex c3;

c3 = c.operator +(c1);*/

/*Complex c4 = c.operator-(c1);

c4.Print();*/

/*Complex c5 = c.operator -=(c1);

c5.Print();*/

/*Complex c6 = c.operator*=(c1);

c6.Print();*/

//c1.operator++();

//c1.Print();

/*int d = c.operator !=(c1);

cout<<d<<endl;*/

//int e = c.operator >(c1);//int d = (c > c1);

//cout<<e<<endl;

//c<<cout;

//int f = c.operator >=(c1);//int f = (c >= c1);

//cout<<f<<endl;

/* c>>cin;

c.Print();*/

return 0;

}

时间: 2024-08-05 04:09:27

c++实现复数运算(运算符重载)的相关文章

【C++】复数的运算符重载

运算符重载的定义: 用户对于自定义类型的运算操作,例如复数的运算.需要重新定义运算符号(创建函数). 除了类属关系运算符".".成员指针运算符".*".作用域运算符"::".sizeof运算符和三目运算符"?:"以外,C++中的所有运算符都可以重载. 复数的运算符重载如下: <span style="font-size:18px;"> #include<iostream> #incl

[c++]复数的运算符重载

类的定义和声明 <span style="font-size:32px;">#include<iostream> #include<string> using namespace std; class Complex { friend istream& operator>>(istream &in, Complex &c); friend ostream& operator<<(ostream

复数类的相关运算(判断大小及四则运算)-&gt;(构造,析构,拷贝复制,运算符重载)

问题描述: 创建一个Plural(复数)的class类,不借助系统的默认成员函数,在类体中写入构造函数,析构函数,拷贝复制函数以及运算符重载函数.并依次实现复数的大小比较(bool)和复数的四则运算(+,-,*,/). #include<iostream> using  namespace std; class Plural { public: void  Display() { cout << "Real->:" << _real; cout

C++复数运算符重载(+与&lt;&lt;)

Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算与输出操作.(1)参加运算的两个运算量可以都是类对象,也可以其中有一个是实数,顺序任意.例如,c1+c2,d+c1,c1+d均合法(设d为实数,c1,c2为复数).(2)输出的算数,在复数两端加上括号,实部和虚部均保留两位小数,如(8.23+2.00i).(7.45-3.40i).(-3.25+4.13i)等.编写程序,分别求两个复数之和.整数和复数之和,并且输出. 请在下面的程序段基础上完

sdut 4-1 复数类的运算符重载

4-1 复数类的运算符重载 Time Limit: 1000MS Memory limit: 65536K 题目描述 通过本题目的练习可以掌握成员运算符重载及友元运算符重载 要求定义一个复数类,重载加法和减法运算符以适应对复数运算的要求,重载插入运算符(<<)以方便输出一个复数的要求. 输入 要求在主函数中创建对象时初始化对象的值. 输出 输出数据共有4行,分别代表a,b的值和它们求和.求差后的值 示例输入 无 示例输出 a=3.2+4.5i b=8.9+5.6i a+b=12.1+10.1i

4-1复数类的运算符重载(SDUTOJ2677)

http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2677&cid=1275 题目描述 通过本题目的练习可以掌握成员运算符重载及友元运算符重载 要求定义一个复数类,重载加法和减法运算符以适应对复数运算的要求,重载插入运算符(<<)以方便输出一个复数的要求. 输入 要求在主函数中创建对象时初始化对象的值. 输出 输出数据共有4行,分别代表a,b的值和它们求和.求差后的值 示例输入 无 示例输出 a=3.2+4.5i b=8.9+5.6i

第九周项目一-复数类的中的运算符重载(续)

在复数类中的运算符重载基础上 (1)再定义一目运算符 -,-c相当于0-c. (2)定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然 /* * Copyright (c) 2015,烟台大学计算机学院 * All right reserved. * 作者:赵嵩 * 文件:Demo.cpp * 完成时间:2015年05月16日 * 版本号:v1.0 */ #include <iostream> using

复数中的运算符重载(续)

输入代码: /* *Copyright (c)2015,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:sum123.cpp *作 者:林海云 *完成日期:2015年5月11日 *版 本 号:v2.0 * *问题描述:在复数类中的运算符重载基础上 (1)再定义一目运算符 -,-c相当于0-c. (2)定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然. *程序输入:按要求输入两个复

C++复数运算 重载

近期整理下很久前写的程序,这里就把它放在博文中了,有些比较简单,但是很有学习价值. 下面就是自己很久前实现的复数重载代码,这里没有考虑特殊情况,像除法中,分母不为零情况. #include <iostream> /* #include <conio.h> #include<stdio.h> #include<iomanip> #include<string> #include<sstream> */ using namespace s

高精度运算类 bign 和常用运算符重载

#include <stdio.h> #include <string.h> #include <string> using std::string; const int maxn = 1000; struct bign { int len,s[maxn]; bign(){ memset(s, 0, sizeof(s));len =1;} //重新定义赋值运算符 bign operator =(const char* num) { len = strlen(num);