题目描述

求两个复数的加减乘除。

要求使用c++ class编写程序。可以创建如下class

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>

using namespace std;

class Complex{public:    Complex(double r = 0.0, double i = 0.0): real(r), imag(i) {};    Complex operator+ (const Complex &c2) const;    Complex operator- (const Complex &c2) const;

    /*实现下面三个函数*/    Complex operator* (const Complex &c2) const;    Complex operator/ (const Complex &c2) const;    friend ostream & operator<< (ostream &out, const Complex &c);

private:    double real;    double imag;};

Complex Complex::operator+ (const Complex &c2) const {    return Complex(real + c2.real, imag + c2.imag);}

Complex Complex::operator- (const Complex &c2) const {    return Complex(real - c2.real, imag - c2.imag);}

int main() {    double real, imag;    cin >> real >> imag;    Complex c1(real, imag);    cin >> real >> imag;    Complex c2(real, imag);    cout << c1 + c2;    cout << c1 - c2;    cout << c1 * c2;    cout << c1 / c2;}

输入描述

第一行两个double类型数,表示第一个复数的实部虚部

第二行两个double类型数,表示第二个复数的实部虚部

输出描述

输出依次计算两个复数的加减乘除,一行一个结果

输出复数先输出实部,空格,然后是虚部,

样例输入

1 1
3 -1

样例输出

4 0
-2 2
4 2
0.2 0.4
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>using namespace std;class Complex{
public:Complex(double r = 0.0, double i = 0.0): real(r), imag(i) {};Complex operator+ (const Complex &c2) const;Complex operator- (const Complex &c2) const;/*实现下面三个函数*/Complex operator* (const Complex &c2) const;Complex operator/ (const Complex &c2) const;friend ostream & operator<< (ostream &out, const Complex &c);private:double real;double imag;
};Complex Complex::operator+ (const Complex &c2) const {return Complex(real + c2.real, imag + c2.imag);
}Complex Complex::operator- (const Complex &c2) const {return Complex(real - c2.real, imag - c2.imag);
}Complex Complex::operator* (const Complex &c2) const {return Complex(real * c2.real - imag * c2.imag, real * c2.imag + imag * c2.real);
}Complex Complex::operator/ (const Complex &c2) const {return Complex((real * c2.real + imag * c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag));
}ostream & operator<<(ostream &out, const Complex &c){out << c.real<<" "<< c.imag <<endl;return out;
}int main() {double real, imag;cin >> real >> imag;Complex c1(real, imag);cin >> real >> imag;Complex c2(real, imag);cout << c1 + c2;cout << c1 - c2;cout << c1 * c2;cout << c1 / c2;return 0;
}

转载于:https://www.cnblogs.com/qianxuejin/p/9079113.html

C8-1 复数加减乘除 (100/100 分数)相关推荐

  1. 为什么 Java 中“1000==1000”为false,而”100==100“为true?

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 来自: 码农网 译文链接:http://www.codeceo.c ...

  2. java cache缓存_涨姿势:为什么Java中“1000==1000”为false,而”100==100“为true?

    为什么 Java 中"1000==1000"为false,而"100==100"为true?  这是一个挺有意思的讨论话题. 如果你运行下面的代码: Integ ...

  3. java cache缓存_为什么 Java 中“1000==1000”为false,而”100==100“为true?

    阅读本文大概需要 2 分钟. 来自: 码农网 为什么 Java 中"1000==1000"为false,而"100==100"为true? 这是一个挺有意思的讨 ...

  4. 100*100的 canvas 占多少内存?

    题目 100*100的 canvas 占多少内存? 在 三年前端,面试思考 中提到了一个题目,非常有新意,这里分享一下当时面试的思考过程. 解题思路 其实真正的答案是多少我并不清楚,面试过程中面试官也 ...

  5. Integer中1000==1000为false而100==100为true

    查看Integer.java类,会发现有一个内部私有类,IntegerCache.java,它缓存了从-128到127之间的所有的整数对象.如果在这个区间内,他就会把变量当做一个变量,放到内存中:但如 ...

  6. html背景图片不完全填充,background-size为100% 100%时背景图填充不完整

    老项目中使用了绝对定位的背景图,拼接显示一张大图,由于绝对定位时使用了百分比,在全屏显示时,两图之间出现了条空白的先. 问题 原以为是百分比计算有问题,实际发现,并不是计算的问题,只是图形的宽高都是有 ...

  7. java里false是什么意思_为什么 Java 中“1000==1000”为false,而”100==100“为true?

    为什么 Java 中"1000 == 1000"为false,而"100 == 100"为true? 这是一个挺有意思的讨论话题 如果你运行下面的代码: Int ...

  8. 背景图片全屏适应的两种方法,background-size: cover; 或者(background-size: 100% 100%;)

    background: #000 url(img1.jpg) no-repeat fixed center center;background-size: cover; 或者(background-s ...

  9. 在设置背景图片全屏时background-size: 100% 100%;和background-size: 100%;的区别,一个100%和两个100%的区别

    background-size:这个属性有两个值,第一个值为x轴方向的缩放比例或者px,第二个值为y轴方向的缩放比例或者px,如果只写一个值,则第二个值默认为auto(根据图片原来的比例,以及现有的宽 ...

  10. html5,cxt.arc(100,100,30,0,Math.PI*2,true);

    cxt.arc(100,100,30,0,Math.PI*2,true); 括号内第一个和第二个参数,代表圆心坐标.第三个参数是圆的半径.第四个参数代表圆周起始位置.0 PI就是起始位置.沿顺时针路线 ...

最新文章

  1. 普渡大学李攀:好的图表示到底是什么?
  2. DirectX11 SDK下载地址:
  3. ARM汇编语言实现peek()_ARM汇编之访问C语言结构体数据
  4. Directed Roads CodeForces - 711D (基环外向树 )
  5. jquery 判断是否有类名_Day037-JS、jQuery
  6. 磁盘位置_Win10创建和附加虚拟硬盘|Win10怎么创建VHD虚拟磁盘
  7. CvtColor(转)
  8. 直线旋转动画html5,多视角3D可旋转的HTML5 Logo动画
  9. geany怎么创建文件夹_在visual studio中创建win32应用程序
  10. ubuntu下非常好用的PDF阅读器
  11. 计算机485通讯原理,用RS-485设计的多机通信接口电路
  12. python爬虫爬取强智教务系统过程
  13. 信息与计算机,科学网—信息与计算机(1) - 姜咏江的博文
  14. comsol如何定义狄利克雷边界_COMSOL中周期性边界条件的应用
  15. 大学计算机与应用软件,第5章 应用软件与常用办公软件 大学计算机基础简明教程[最新].doc...
  16. 2022年湖南省社会工作者考试综合实务(初级)练习题及答案
  17. 机器学习-数据科学库 DAY02
  18. C++之sync_with_stdio(false)
  19. 10000多套机械手毕业设计 课程设计 毕业论文 图纸 分享/上下料机械手、搬运机械手、苹果采摘机械手、液压机械手、三自由度机械手、水果采摘机械手、六自由度机械手、焊接机械手、码垛机械手、四自由……
  20. web前端基础入门开发教程之HTML5 Web 存储

热门文章

  1. python两台电脑文件传输_python实现简单socket程序在两台电脑之间传输消息的方法...
  2. mysql server 80_mysql Host 'microsof-80f25e' is not allowed to connect to this MySQL server
  3. img引Linux的绝对路径,什么是绝对路径和相对路径
  4. 【响应式Web前端设计】CSS3 :nth-of-type() 选择器
  5. c语言程序设计迷宫,C语言程序设计课程设计-迷宫.doc
  6. 量子计算机最新研究进程,谷歌在量子计算机领域已取得重大突破,国内的研究进度怎么样了...
  7. 百度网络推广介绍网站在更换老域名时都需注意哪些?
  8. 网络宣传推广教大家网站的过期页面更合理的处理方法
  9. 网络推广期间怎样的网站外链才是被网络推广优化所需要的?
  10. 网络营销——网络营销专员浅析如何合理设置网站关键词密度