VS2019 C++实测可用

#include <iostream>
#define PI acos(-1)
using namespace std;class Complex
{public:double real;double imag;//   double cabs = double(sqrt(real * real + imag * imag));//   double ctheta = atan(imag / real);Complex(){real = 0;imag = 0;}Complex(double r, double i){real = r;imag = i;}friend Complex operator+(Complex& c1,const Complex& c2);friend Complex operator-(Complex& c1,const Complex& c2);friend Complex operator*(Complex& c1, const Complex& c2);friend Complex operator/(Complex& c1, const Complex& c2);friend Complex operator+(Complex& c1, double c);friend Complex operator-(Complex& c1, double c);friend Complex operator*(Complex& c1, double c);friend Complex operator/(Complex& c1, double c);friend Complex operator+(double c, const Complex& c1);friend Complex operator-(double c, const Complex& c1);friend Complex operator*(double c, const Complex& c1);friend Complex operator/(double c, const Complex& c1);void display();Complex ckf(Complex& c1,int n);double cabs();double csqrt();
private:};
Complex operator+(Complex& c1,const Complex& c2)
{return  Complex(c2.real + c1.real, c2.imag + c1.imag);
}
Complex operator-(Complex& c1, const Complex& c2)
{return  Complex(c1.real - c2.real, c1.imag - c2.imag);
}
Complex operator*(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/(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 operator+(Complex& c1, double c)
{return  Complex(c1.real+c, c1.imag);
}
Complex operator-(Complex& c1, double c)
{return  Complex(c1.real-c, c1.imag);
}
Complex operator*(Complex& c1, double c)
{return  Complex((c * c1.real), (c1.imag * c));
}
Complex operator/(Complex& c1, double c)
{Complex c3;c3.real = c1.real / c;c3.imag = c1.imag / c;return c3;
}
Complex operator+(double c, const Complex& c1)
{return  Complex(c + c1.real, c1.imag);
}
Complex operator-(double c, const Complex& c1)
{return  Complex(c-c1.real, -c1.imag);
}
Complex operator*(double c, const Complex& c1)
{return  Complex((c * c1.real), (c1.imag * c));
}
Complex operator/(double c, const Complex& c1)
{Complex c3;c3.real = (c * c1.real) / (c1.real * c1.real + c1.imag * c1.imag);c3.imag = (-c * c1.imag) / (c1.real * c1.real + c1.imag * c1.imag);return c3;
}
void Complex::display()
{cout << "(" << real << "," << imag << "i)" << endl;
}double cabs(Complex& c1)
{return(sqrt(c1.real * c1.real + c1.imag * c1.imag));
}Complex csqrt(Complex& c1)
{Complex c3;double ctheta;if (c1.real > 0){ctheta= atan(c1.imag / c1.real);}else if (c1.real == 0 && c1.imag > 0){ctheta = PI / 2;}else if (c1.real == 0 && c1.imag < 0){ctheta = -PI / 2;}else if (c1.real < 0 && c1.imag >= 0){ctheta = atan(c1.imag / c1.real) + PI;}else if (c1.real < 0 && c1.imag < 0){ctheta = atan(c1.imag / c1.real) - PI;}c3.real = sqrt(cabs(c1)) * cos(ctheta / 2);c3.imag = sqrt(cabs(c1)) * sin(ctheta / 2);return c3;
}

检验部分

#include "complex.h"
#include <iostream>
using namespace std;int main()
{double c = 5;Complex c1(1,-2), c2(3,-4), c3;cout << "c="<<c<<endl;cout << "c1=";c1.display();cout << "c2=";c2.display();c3 = c1 + c2;cout << "复数与复数:" << endl;cout << "c1+c2=";c3.display();c3 = c1 - c2;cout << "c1-c2=";c3.display();c3 = c1 * c2;cout << "c1*c2=";c3.display();c3 = c1 / c2;cout << "c1/c2=";c3.display();cout << endl;cout << "复数与实数:" << endl;cout << "c=" << c << endl;cout << "c1=";c1.display();c3 = c1 + c;cout << "c1+c=";c3.display();c3 = c1 - c;cout << "c1-c=";c3.display();c3 = c1 * c;cout << "c1*c=";c3.display();c3 = c1 / c;cout << "c1/c=";c3.display();cout << endl;cout << "实数与复数:" << endl;cout << "c=" << c << endl;cout << "c2=";c2.display();c3 = c + c2;cout << "c + c2=";c3.display();c3 = c - c2;cout << "c - c2=";c3.display();c3 = c * c2;cout << "c * c2=";c3.display();c3 = c / c2;cout << "c / c2=";c3.display();cout << "自定义:" << endl;cout << "实部:" << endl;cout << c3.real;cout << "\n虚部:" << endl;cout << c3.imag;cout << "复数开方:" << endl;c3.real=-1;c3.imag = 0;cout << "\n" << endl;cout << "c3:" << endl;c3.display();c3 = csqrt(c3);c3.display();cout << "混合运算:" << endl;Complex c4;c1.display();c2.display();c3 = c1 + c2;c4 = c1 * c2;c4 = c + (c1 * c2);c4.display();return 0;}

C++复数运算符重载,复数开平方相关推荐

  1. C++实现的复数运算符重载

    //#include<iostream> //using namespace std; #include <iostream.h> class Complex { privat ...

  2. [YTU]_2535( C++复数运算符重载(+与))

    题目描述 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算与输出操作. (1)参加运算的两个运算量可以都是类对象,也可以其中有一个是实数,顺序任意.例如,c ...

  3. [YTU]_2535 (Problem I: C++复数运算符重载(+与))

    定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算与输出操作. (1)参加运算的两个运算量可以都是类对象,也可以其中有一个是实数,顺序任意.例如,c1+c2, ...

  4. C++编程入门--运算符重载复数类

    题目:成运算符重载员函数形式实现复数类的四则运算 上机指导2中,我们以独立函数形式(非成员函数,非友元函数)实现了Complex附属类的加减乘除四则运算,这里要求用Complex成员函数形式实现Com ...

  5. C++运算符重载(复数中除法(“/”)数学运算公式)

    假设x为实部,y为虚部. 定义复数A,复数B,复数t: t.x=((A.x*B.x)+(A.y*B.y))/((B.x*B.x)+(B.y*B.y));     t.y=((A.y*B.x)-(A.x ...

  6. C#——《C#语言程序设计》实验报告——泛型与集合——运算符重载

    一.实验目的 掌握运算符重载. 掌握索引符的编写. 掌握常用非泛型集合类和集合类的使用: 掌握可空类型的使用 二.实验内容 (实验过程中编写的程序复制到本文件中,下课整理后上交) 运算符重载 复数包含 ...

  7. C++运算符重载讲解与经典实例

    转自:http://blog.csdn.net/dingyuanpu/article/details/5852825 C++中预定义的运算符的操作对象只能是基本数据类型,实际上,对于很多用户自定义类型 ...

  8. 第十二周项目一-实现复数类中的运算符重载(1)

    /**Copyright(c)2016,烟台大学计算机与控制工程学院*All rights reserved*文件名称:123.cpp*作 者:王蕊*完成日期:2016年5月15日*版 本 号:v1. ...

  9. 4-1 复数类的运算符重载

    4-1 复数类的运算符重载 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 通过本题目的练习 ...

  10. [YTU]_2440( C++习题 复数类--重载运算符+,-,*,/)

    题目描述 定义一个复数类Complex,重载运算符"+","-","*","/",使之能用于复数的加.减.乘.除.运算符 ...

最新文章

  1. 用Python如何查快递?
  2. 霍山职业学校16届计算机学生,霍山职高(安徽霍山职业学校)
  3. python中选择结构通过什么语句实现_Python中选择结构通过什么语句实现
  4. 电信充q币短信怎么发_王者荣耀充值中心Q币充值IOS系统游戏点券的办法_云奇付Q币寄售...
  5. Linux /etc/group文件解析(超详细)
  6. 【报错笔记】sturts2程序运行出现错误:Struts has detected an unhandled exception
  7. ListView中让TextView中的文字进行单独滚动
  8. american fuzzy lop 介绍
  9. java 加法程序_使用JAVAEE编写简单的加法程序
  10. 字体在ppt中可以整体替换吗_制作PPT必备的6个技巧,个个让人相见恨晚!你确定不来学一学?...
  11. RIP基础原理与实验
  12. McAfee迈克菲杀毒软件企业版8.8.13-McAfee VirusScan Enterprise8.8 百度云
  13. 苹果7信号天线内部位置_苹果7信号天线内部位置_除了芯片还有天线!手机后壳换玻璃也是为5G准备...
  14. 3par linux多路径软件,Redhat6.X 配置HP3PAR7200存储多路径过程
  15. 2022年安全员-A证考试试题及在线模拟考试
  16. c#语言中文编程下载,C#编程自学软件
  17. 2021年,小灰都读了哪些书?
  18. python元祖封包_Python基础——解包与封包
  19. IT 行业平均薪资第一
  20. 串行进位加法器和超前进位加法器代码实现及性能对比

热门文章

  1. 【Docker Desktop】Neo4j
  2. 什么是炎症(inflammation)?抗生素?//2021-2-12
  3. 十本经典教材带你入门Python编程
  4. 微信公众号网页开发步骤
  5. B站视频下载方法(4K60帧)
  6. python-scrapy模拟登陆网站--登陆青果教务管理系统(二)
  7. Android云真机原理以及云真机平台搭建实践
  8. hdu 5145 NPY and girls 莫队
  9. 破解大众点评 css加密
  10. 第九章 SG90伺服舵机模块的使用