首页 > 其他分享 >4/18打卡 复数的输入输出和加减乘除重载

4/18打卡 复数的输入输出和加减乘除重载

时间:2023-04-18 20:23:41浏览次数:36  
标签:real cout 18 Complex istream 打卡 加减乘除

class Complex {
    double real;
    double imag;
public:
    friend istream& operator>>(istream& is, Complex& c);
    friend ostream& operator <<(ostream& os, const Complex& a);
    Complex() {
        real = 0;
        imag = 0;
    }
    Complex(double a, double b) :real(a), imag(b) {}
    //+
    Complex operator +(const Complex& a)
    {
        Complex b;
        b.imag = imag + a.imag;
        b.real = real + a.real;
        return b;
    }
    //-
    Complex operator -(const Complex& a)
    {
        Complex b;
        b.imag = imag - a.imag;
        b.real = real - a.real;
        return b;
    }
    //*
    Complex operator*(const Complex& other) const {
        return Complex(real * other.real - imag * other.imag,
            real * other.imag + imag * other.real);
    }
    // /
    Complex operator/(const Complex& other) const {
        double denominator = other.real * other.real + other.imag * other.imag;
        return Complex((real * other.real + imag * other.imag) / denominator,
            (imag * other.real - real * other.imag) / denominator);
    }
};

ostream& operator <<(ostream& os, const Complex& a)
{
    cout << a.real << "+" << a.imag << "i" << endl;
    return os;
}

istream& operator>>(istream& is, Complex& c)
{
    cout << "请输入实部和虚部:" << endl;
    is >> c.real >> c.imag;
    return is;
}
int main()
{
    Complex a = { 4,5 };
    Complex b = { 2,3 };
    Complex c = a + b;
    Complex d;
    cin >> d;
    Complex e = c / d;
    cout << "c\t" << c << endl;
    cout << "d\t" << d << endl;
    cout << "e\t" << e << endl;
    return 0;
}

 

标签:real,cout,18,Complex,istream,打卡,加减乘除
From: https://www.cnblogs.com/wlxdaydayup/p/17330950.html

相关文章

  • 建民の每日打卡7
    一、问题描述银行月利息0.63%,某人在第一年存了一笔钱,每年年底取出1000,五年后正好取完,求第一年存了多少钱?二、流程设计1.从最后一年向前递推,年末加上1000,除以年利率求出去年余款2.循环四次完成递推3.输出答案三、流程图设计 四、代码实现#include<iostream>usingnamesp......
  • 天天打卡一小时——5
    一.问题描述读入一系列整数,统计出正整数和负整数的个数,读到0结束二.设计思路1.输入一组整数2.包含正整数和负整数3.在每次读完一个数后需要进行判断4.非0接着读,为0则结束程序5.使用while语句三.程序流程图 四.代码实现#include<iostream>usingnamespacestd;int......
  • c++打卡第八天
    一、问题描述。   我国古代有一种说话叫三天打鱼两天晒网,如果一个人从1990年1月1日开始,开始三天打鱼两天晒网,问输入一个年月日,此时他是在打鱼还是晒网。二、设计思路。①、我们可以通过计算从输入日期到1990年1月1日总共有多少天,总天数除余周期五,如果结果为1.2.3则此人此......
  • 建民打卡日记4.18
    一、问题描述二、设计思路三、程序流程图四、代码实现#include<iostream>#include<math.h>usingnamespacestd;intmain(){ doublenewt(floata,floatb,floatc,floatd); floata,b,c,d,x; cin>>a>>b>>c>>d; x=newt(a,b,......
  • 2023-4-18查漏pair
    pair是将2个数据组合成一组数据,当需要这样的需求时就可以使用pair,如stl中的map就是将key和value放在一起来保存。另一个应用是,当一个函数需要返回2个数据的时候,可以选择pair。pair的实现是一个结构体,主要的两个成员变量是firstsecond因为是使用struct不是class,所以可以直接使......
  • 2023-4-18补缺map
    map是STL的一个关联容器,它提供一对一的hash。第一个可以称为关键字(key),每个关键字只能在map中出现一次;第二个可能称为该关键字的值(value);map以模板(泛型)方式实现,可以存储任意类型的数据,包括使用者自定义的数据类型。Map主要用于资料一对一映射(one-to-one)的情況,map內部的实......
  • 2023-4-18补缺for(auto i : v)遍历容器元素
    for(autoi:v)遍历容器元素1.auto2.auto&3.constauto&4.constautoC++11新增了一种循环:基于范围(range-based)的for循环。这简化了一种常见的循环任务:对数组(或容器类,如vector和array)的每个元素执行相同的操作,如下例所示:doubleprices[5]={4.99,10.99,6.87,6.47......
  • 每天打卡一小时 第八天 编译四部曲
     第一部曲自然语言可以直接套用for循环用选择语句进行选择输出可以运用剩余定理求出公式第二部曲代码一代码二  第三部曲代码代码一 #include<iostream>usingnamespacestd;intmain(){intN;cin>>N;for(inti=7;i<N;i++){......
  • 2023.4.18-人月神话-4月份读后感1
    最近,我阅读了人月神话的一部分,有了一些感受。过去,我对于编程的乐趣不是很了解。编程为什么有趣?首先是一种创建事务的纯粹快乐,其次快乐来自于开发对其他人有用的东西,第三是整个过程体现出魔术般的力量,第四是学习的乐趣,最后乐趣还来自于工作在如此易于驾驭的介质上。编程非常有趣,在......
  • Codeforces 1810G - The Maximum Prefix(DP)
    挺小清新的一道计数题。首先先分析下这个“最大前缀和”,按照最朴素的思路就是扫一遍每个前缀,然后记录一下当前的\(sum\)与前面的\(mx\),但是如果你一直陷在这个思路上你就似了,因为按照这个思路做,你DP状态里需要记录\(sum\)和\(mx\)两个维度,算上下标一维总共是\(n^3\),并......