首页 > 其他分享 >第9周项目1-复数类中的运算符重载(续)

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

时间:2023-08-14 11:06:44浏览次数:36  
标签:const 运算符 operator Complex friend 重载 c2 c1 类中


问题描述:

在复数类中的运算符重载基础上
(1)再定义一目运算符 -,-c相当于0-c。
(2)定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然。

代码:

#include <iostream>

using namespace std;

class Complex
{
public:
    Complex(){real=0;imag=0;}
    Complex(double r,double i){real=r; imag=i;}
    friend Complex operator+(const Complex &c1,const Complex &c2);
    friend Complex operator-(const Complex &c1,const Complex &c2);
    friend Complex operator*(const Complex &c1,const Complex &c2);
    friend Complex operator/(const Complex &c1,const Complex &c2);
    Complex operator+(const double d);
    Complex operator-(const double d);
    Complex operator-();
    friend istream& operator>>(istream& input,Complex& c);
    friend ostream& operator<<(ostream& output,Complex& c);
    void display();
private:
    double real;
    double imag;
};
//下面定义成员函数
istream& operator>>(istream& input,Complex& c)
{
    char i;
    cin>>c.real>>c.imag>>i;
    return input;
}
ostream& operator<<(ostream& output,Complex& c)
{
    cout<<c.real;
    if(c.imag>0) cout<<"=";
    cout<<c.imag<<"i";
    return output;
}
Complex Complex::operator-()
{
    return Complex(-real,-imag);
}
void Complex::display()
{
    cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
Complex operator+(const Complex &c1,const Complex &c2)
{
    return Complex(c1.real+c2.real,c1.imag+c2.imag);
}
Complex operator-(const Complex &c1,const Complex &c2)
{
    return Complex(c1.real-c2.real,c1.imag-c2.imag);
}
Complex operator*(const Complex &c1,const Complex &c2)
{
    return Complex(c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real);
}
Complex operator/(const Complex &c1,const Complex &c2)
{
    Complex c;
    c.real=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
    c.imag=(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
    return c;
}
Complex Complex::operator+(const double d)
{
    return Complex(real+d,imag);
}
Complex Complex::operator-(const double d)
{
    return Complex(real-d,imag);
}

//下面定义用于测试的main()函数
int main()
{
    Complex c1,c2,c3;
    cin>>c1>>c2;
    cout<<"c1="<<c1<<endl;
    cout<<"c2="<<c2<<endl;
    c3=-c1;
    cout<<c3<<endl;
    return 0;
}

 

运行结果:

第9周项目1-复数类中的运算符重载(续)_ci

标签:const,运算符,operator,Complex,friend,重载,c2,c1,类中
From: https://blog.51cto.com/u_16225007/7074025

相关文章

  • 二、运算符
    !>顺便在这里提一句,在编程下你的输入法是要英文的,所有符号也要是英文符号,例如中文符号是。而英文符号是.当然编程就和数学一样,存在有算数运算符。运算符号描述示例+两数相加a+b-两数相减a-b*两数相乘a*b/两数相除a/b%取模-返回除......
  • php运算符
    加、减、乘、除、负数$a=10;$b=1;echo$a+$b."</br>";echo$a-$b."</br>";echo$a*$b."</br>";echo$a/$b."</br>";echo-$a;输出:1191010-10逻辑运算(与、或、非、异或)var_dump(1and1);var_dump(true&am......
  • 第二章 运算符和数学函数
    第二章运算符和数学函数2.1数学运算符:创建序列(两头都会包含)>x<-2:4>x[1]234+加>1+1[1]2-减>2-1[1]1*乘>1*2[1]2/浮点数除法>3/2[1]1.5%/%整数除法>3%/%2[1]1%%余数>3%%2[1]1^或**求幂>2^2[1]4>2**2[1]4......
  • 创建元组的三种方式、字典中的setdefault和get妙用、类中的重载方法__add__()
    创建元组的三种方式#print(tuple([input(),input()]))#print((input(),input()))t=input(),input()print(t)#可以将列表转换成tuple,也可以直接()创建tuple,或者将多个变量赋值给一个值会自动转换成tuple字典中的setdefault和get妙用setdefault类似get方法w=input()......
  • 经纬恒润第三代重载自动驾驶运输车批量交付唐山港客户
        2023年7月下旬,唐山港京唐港区集装箱码头25#泊位智能水平运输项目的关键组成部分——22辆重载自动驾驶运输车(HAV)陆续分批交付,这标志着该项目迈入了全面系统联调联测阶段。    此次交付的经纬恒润自研第三代HAV车型,在底盘动力性、经济性、整车电子电气架构、通信......
  • 运算符
    运算符%运算符满足公式a%b=a-a/b*b-10%-3=-1++,--运算intj=8;intk=++j;//输出结果都是9Sysout.out.println("j="+j+"k="+k);intj=8;intk=j++;//j等于9,k等于8Sysout.out.println("j="+j+"k="+k);关系运算符......
  • C++ 字符串拼接技巧(stringstream、字符串迭代器、字符串的加法运算符、std::accumulat
    在C++中,经常需要将多个字符串拼接成一个大字符串。这个过程很容易出错,但有一些技巧可以帮助我们轻松地实现这个目标。本文将介绍一些C++中join字符串的技巧。一、使用stringstreamstringstream是一个流。使用它可以将多个字符串连接起来,然后将它们转换为一个字符串。可......
  • 方法的重载
    1,重载就是在一个类中,有相同的函数名称,但形参不同的函数2,方法的重载规则:    1.方法名称必须相同    2.参数列表必须不同,(个数不同,或者类型不同,参数排列顺序不同等)    3.方法的返回类型return可以相同,也可以不相同    4.仅仅返回类型不同不足以成为方......
  • JavaSE--运算符
    一、运算符运算符:用于指明对于操作数的运算方式1、运算符分类  1)按照操作数的数目分  单目操作符、双目操作符、三目操作符  2)按照运算符的功能来分  算术运算符、赋值运算符、关系运算符、逻辑运算符2、运算符详解  1)算数运算符+加法-减法*乘法/除法......
  • C++ | 运算符重载
    运算符重载在类中的函数进行重载(成员函数)运算符重载用于重新定义运算符的作用,使用函数名称operatorOP作为函数名,其中OP为具体的运算符(如operator+)classTime{Timeoperator+(constTime&t);};Timea,b;Timec=a+b;在成员函数中重载的运算符,如+-等,默认左边......