运算符重载(复数类(Complex))
来源:互联网 发布:淘宝哪里可以回收手机 编辑:程序博客网 时间:2024/06/04 00:43
环境:win10,vs2013
复数运算法则
运算后结果仍然是一个复数
1.加法两个复数的和依然是复数,它的实部是原来两个复数实部的和,它的虚部是原来两个复数虚部的和z1=a+bi,z2=c+di它们的和是:(a+bi)+(c+di)=(a+c)+(b+d)i2.减法z1=a+bi,z2=c+di它们的差是:(a+bi)-(c+di)=(a-c)+(b-d)i3.乘法z1=a+bi,z2=c+di它们的积是:(a+bi)(c+di)=(ac-bd)+(bc+ad)i4.除法z1=a+bi,z2=c+di它们的商是:(a+bi)/(c+di)=((ac+bd)+(bc-ad)i)/(c^2+d^2)
Complex.h
using namespace std;#include<stdlib.h>#include<iostream>class Complex{public : Complex();//默认构造函数 Complex(double real, double image);//构造函数(构造对象) //赋值操作符重载需要注意:1.自己给自己赋值;2.返回值是引用的形式:3.返回当前对象;4.参数,传引用 Complex& operator=(const Complex&d);//赋值操作符重载 bool operator==(const Complex&c);//重载== bool operator!=(const Complex&c);//重载!= Complex(const Complex&c);//拷贝构造函数 Complex operator+(const Complex&c);//重载+ Complex operator*(const Complex&c);//重载* Complex operator-(const Complex&c);//重载- Complex operator/(const Complex&c);//重载/ Complex operator+=(const Complex&c);//重载+= Complex operator-=(const Complex&c);//重载-= Complex operator*=(const Complex&c);//重载*= Complex operator/=(const Complex&c);//重载/= Complex operator++(int c);//后置++ Complex& operator++();//前置++ Complex operator--(int c);//后置-- Complex& operator--();//前置-- void Print();private: double _real; double _image; friend ostream&operator<<(ostream&_cout, Complex&c);//输出运算符重载};
Complex.cpp
#include"Complex.h"void Complex::Print(){ cout << _real << "+" << _image << "i" << endl;}ostream&operator<<(ostream&_cout, Complex&c)//输出运算符重载{ _cout << c._real << "+" <<c._image << "i"; return _cout;}Complex::Complex()//默认构造函数{ _real = 1.3; _image = 1.4;}Complex::Complex(double real=0.1, double image=0.2):_real(real), _image(image){}Complex::Complex(const Complex&c)//拷贝构造函数{ _real = c._real; _image = c._image;}Complex& Complex::operator=(const Complex&c)//赋值运算符重载{ if (*this != c) { _real = c._real; _image = c._image; } return *this;}bool Complex::operator==(const Complex&c)//重载=={ if ((_real == c._real) && (_image = c._image)) return true; return false;}bool Complex::operator!=(const Complex&c)//重载!={ return!(*this == c);}Complex Complex::operator+(const Complex&c)//重载+{ return Complex(_real + c._real, _image + c._image);}Complex Complex::operator*(const Complex&c)//重载*{ return Complex( ( (_real*c._real) - (_image*c._image)), ((_image*c._real) + (_real*c._image) ) );}Complex Complex::operator-(const Complex&c)//重载—{ return Complex(_real - c._real, _image - c._image);}Complex Complex::operator+=(const Complex&c)//重载+={ _real = _real+c._real; _image = _image+c._image; return *this;}Complex Complex::operator-=(const Complex&c)//重载-={ _real = _real - c._real; _image = _image -c._image; return *this;}Complex Complex::operator++(int c)//后置++{ Complex temp(*this); _real += 1; _image += 1; return temp;}Complex& Complex::operator++()//前置++{ _real += 1; _image += 1; return *this;}Complex Complex::operator--(int c)//后置——{ Complex temp(*this); _real -= 1; _image -= 1; return temp;}Complex& Complex::operator--()//前置--{ _real -= 1; _image -= 1; return *this;}
test.cpp
#include"Complex.h"int main(){ Complex c1(1.1, 2.2); c1.Print(); Complex c2(3.3, 4.4); c2.Print(); Complex c3; c3.Print(); c3 = c2; cout << c3 << endl; cout << (c3 != c1) << endl; cout << (c3 == c1) << endl; cout << c1 + c2 << endl; cout << c1 - c2 << endl; cout << c1 * c2 << endl; cout << endl; Complex c4(5.5, 6.6); c4.Print(); cout << (c1 += c4) << endl; cout << (c1 -= c4) << endl; cout << endl; cout << c1++ << endl; cout << c2-- << endl; cout << ++c3 << endl; cout << --c4 << endl; system("pause"); return 0;}
运行结果:
阅读全文
0 0
- 运算符重载(复数类(Complex))
- Complex(复数运算)
- 定义一个复数类Complex 重载运算符 第二题
- 定义一个复数类complex重载运算符+ - * /使之能用于复数的加减乘除。
- Complex类运算符重载
- 面向对象复数类(class Complex) 的重载运算符 一元二元的实现与使用
- C++实现输入输出运算符重载、友元函数和成员函数实现复数类Complex
- 复数运算符的重载与模版类complex的对比
- 复数(重载运算符、重写ToString)
- C++复数运算符重载(+与<<)
- C++复数运算符重载(+与<<)
- C++复数运算符重载(+与<<)
- C++复数运算符重载(+与《)
- 复数中的运算符重载(续)
- 复数中的运算符重载(续)
- 复数运算符重载(3)
- Complex运算符重载
- 实现复数类中的运算符重载(扩充类)
- Python3.x 函数实例
- 数据结构实验之图论六:村村通公路
- Qt状态机场景模拟
- lambda表达式
- Android TextView使用HTML处理图片文字混合显示
- 运算符重载(复数类(Complex))
- 哇!Django 2.0 发布,不再支持 Python 2.x
- 除了人工智能工程师,还有一种工程师或许更抢手
- bzoj3503 [Cqoi2014]和谐矩阵(高斯消元)
- 深圳无人驾驶公交车试运行,刷爆朋友圈!
- ubuntu 14.04+ GTX 1070+cuda 8.0 + cudnn 5.1+opencv3.0+caffe-SSD配置一步到位
- LVS调度器之间的高可用(脚本)
- UART配置调试指南
- 通信网络编程第三次作业