复数的四则运算——C++实现
复数的四则运算法则:
- 加法:(a+bi)+(c+di)=(a+c)+(b+d)i
- 减法:(a+b)-(c+di)=(a-c)+(b-d)i
- 乘法:(a+bi)*(c+di)=(ac-bd)+(bc+ad)i
- 除法:(a+bi)/(c+di)=((ac+bd)+(bc-ad)i)/(c2+d2)
注意:在复数除法运算中,若c、d均为0,则不可计算(除数不能为0)。
1、代码中直接输入参数:
C++代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;class Complex {public:Complex(double real, double imag) {this->real = real;this->imag = imag;}friend Complex operator+(Complex c1, Complex c2);friend Complex operator-(Complex c1, Complex c2);friend Complex operator*(Complex c1, Complex c2);friend Complex operator/(Complex c1, Complex c2);void Print_Complex();
private:double real;double imag;
};Complex operator+(Complex c1, Complex c2) {return Complex(c1.real + c2.real, c1.imag + c2.imag);
}Complex operator-(Complex c1, Complex c2) {return Complex(c1.real - c2.real, c1.imag - c2.imag);
}Complex operator*(Complex c1, Complex c2) {return Complex(c1.real*c2.real - c1.imag*c2.imag, c1.real*c2.imag + c1.imag*c2.real);
}Complex operator/(Complex c1, Complex c2) {if (c2.real == 0 && c2.imag == 0) {cout << "Error ";return c2;}else {return Complex((c1.real*c2.real + c1.imag*c2.imag) / (c2.real*c2.real + c2.imag*c2.imag), (c1.imag*c2.real - c1.real*c2.imag) / (c2.real*c2.real + c2.imag*c2.imag));}
}void Complex::Print_Complex() {if (real == 0) {cout << imag << "i" << endl;}else {if (imag > 0) {cout << real << "+" << imag << "i" << endl;}else {cout << real << imag << "i" << endl;}}
}int main() {Complex c1(1, 3);Complex c2(1, 1);Complex c3 = c1 + c2;cout << "Add:";c3.Print_Complex();Complex c4 = c1 - c2;cout << "Minus:";c4.Print_Complex();Complex c5 = c1 * c2;cout << "Mul:";c5.Print_Complex();Complex c6 = c1 / c2;cout << "Div:";c6.Print_Complex();return 0;
}
C++运行结果:
说明:若c2的实部和虚部均为0,输出结果为:
2 、如果想自己输入参数,代码如下:
C++代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;class Complex {public:Complex() {this->real = 0;this->imag = 0;}Complex(double real, double imag) {this->real = real;this->imag = imag;}void setRI(double real, double imag) {this->real = real;this->imag = imag;}friend Complex operator+(Complex c1, Complex c2);friend Complex operator-(Complex c1, Complex c2);friend Complex operator*(Complex c1, Complex c2);friend Complex operator/(Complex c1, Complex c2);void Print_Complex();
private:double real;double imag;
};Complex operator+(Complex c1, Complex c2) {return Complex(c1.real + c2.real, c1.imag + c2.imag);
}Complex operator-(Complex c1, Complex c2) {return Complex(c1.real - c2.real, c1.imag - c2.imag);
}Complex operator*(Complex c1, Complex c2) {return Complex(c1.real*c2.real - c1.imag*c2.imag, c1.real*c2.imag + c1.imag*c2.real);
}Complex operator/(Complex c1, Complex c2) {if (c2.real == 0 && c2.imag == 0) {cout << "c2 Error ";return c2;}else {return Complex((c1.real*c2.real + c1.imag*c2.imag) / (c2.real*c2.real + c2.imag*c2.imag), (c1.imag*c2.real - c1.real*c2.imag) / (c2.real*c2.real + c2.imag*c2.imag));}
}void Complex::Print_Complex() {if (real == 0) {cout << imag << "i" << endl;}else {if (imag > 0) {cout << real << "+" << imag << "i" << endl;}else {cout << real << imag << "i" << endl;}}
}int main() {Complex c1, c2;double real, imag;cout << "Please input c1 real:";cin >> real;cout << "Please input c1 imag:";cin >> imag;c1.setRI(real, imag);cout << "Please input c2 real:";cin >> real;cout << "Please input c2 imag:";cin >> imag;c2.setRI(real, imag);Complex c3 = c1 + c2;cout << "Add:";c3.Print_Complex();Complex c4 = c1 - c2;cout << "Minus:";c4.Print_Complex();Complex c5 = c1 * c2;cout << "Mul:";c5.Print_Complex();Complex c6 = c1 / c2;cout << "Div:";c6.Print_Complex();return 0;
}
C++运行结果:
复数的四则运算——C++实现相关推荐
- 【C语言】复数的四则运算与复数运算的头文件
目录 一.复数的四则运算 二.复数的头文件#include<complex.h> 三.一些碎碎念 一.复数的四则运算 (1)计算方法 加法:(a+bi)+(c+di)=(a+c)+(c+d ...
- c语言进行复数的四则运算
c语言进行复数的四则运算 额..好像没什么好说的直接上代码吧,注意下结构体的用法 #include<stdio.h> struct complex {double re;double im ...
- 用C++实现复数的四则运算
文章目录 前言 功能介绍 结果展示 代码区 前言 第一次写C++,用户界面感觉还行,内部实现不太高级,搞得能看... 想发出来留作纪念 功能介绍 这个代码包括一个复数类和四个运算函数,一个用户界面函数 ...
- 复数的四则运算(+,-,*,\)
此程序实现了对复数的加减乘除,输入的是实部和虚部.我分别用了c=a+b和a+=b;两个方法共同实现,代码如下,如果逻辑及代码有遗漏,欢迎随时评论,如果输入3,1则进行3+i和3+i的和为6+2i pu ...
- 抽象数据类型的实现(复数的四则运算)
简单的说一下,我是在DEV C++上实现的复数四则运算.如果学习过JAVA.C++等面向对象高级程序设计语言,那么对抽象数据类型是非常容易理解的.抽象数据类型就类似于java中的类. 废话不多说了直接 ...
- 复数类四则运算及插入/提取操作zrx
在C++中,标准库本身已经对左移运算符<<和右移运算符>>分别进行了重载,使其能够用于不同数据的输入输出,但是输入输出的对象只能是 C++ 内置的数据类型(例如 bool.in ...
- c语言复数相加的程序,运用C语言编写复数的四则运算.docx
.. 运用C语言编写复数的四则运算 需求分析 设计一个可进行复数运算的演示程序 实现下列六种基本运算: (1)由于输入的实部和虚部生成一个复数:(2)两个复数求和:(3)两个复数求差:(4)两个复数求 ...
- 复数的四则运算(C语言实现)
复数的四则运算 问题描述:设计一个可进行复数运算的演示程序. 基本要求:实现下列七种基本运算:1)由输入的实部和虚部生成一个复数:2)复数的显示:3)两个复数求和:4)两个复数求差:5)两个复数求积: ...
- 用Python实现复数的四则运算
推荐一个刷题网站,体验过一段时间了,真的建议去试试. 对于Python很少有基础题库,牛客针对基础专项出题,目前还出炉了数据分析和机器学习 . 不仅是Python,还有Java.C语言C++.数据库S ...
最新文章
- Eclipse MAT插件下载地址
- 请求外部图片的时候,遇到403的错误的解决方案
- java date dateformat_java中Date与DateFormat的格式输出
- java%4d_java积累
- SQLServer 维护脚本分享(08)临时数据库(tempdb)
- java之yield(),sleep(),wait()区别详解-备忘笔记
- Spring(16)——使用注解进行bean定义
- 公开课视频-《第01章 规划》-大企业云桌面部署实战-在线培训-视频(奉献)
- linux系统中的软件管理(软件仓库的搭建:本地+网络源+第三方)
- 用python 创建英语自定义词典
- 2017年工作回顾及2018年新展望
- 2020线上夏令营感受
- n*n蛇形方阵的输出
- IDEA 2021的下载与安装
- iPhone 游戏 Dungeon Hunter2 地牢猎手 今日通关
- 前端开发培训机构哪家好
- 如何下载 spring 官方文档 pdf
- 通过OpenNI2获取数据并转至OpenCV格式及点云生成
- 计算机系迎新晚会策划,迎新晚会策划方案
- 5G NR QC-LDPC简介(一)
热门文章
- python爬虫课程笔记-续
- 1.特定领域知识图谱知识融合方案(实体对齐):基于图神经网络的商品异构实体表征探索
- 【GDOI2018模拟7.6】吃干饭
- 网页分享到微信、微博、QQ,QQ空间,豆瓣等等函数的封装
- OEUF麻雀婴儿床 - 家长寻求新设计的幼儿园
- 手机服务器版本是什么系统版本,查看系统版本-如何查看windows系统的详细版本号...
- 网络安全、夺旗赛(CTF)技能汇总
- 选购二手房的流程和注意事项
- CMake Tutorial Step1
- 【Python面向对象编程】第12篇 特殊方法之__eq__