complex类
Posted hongzai1206
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了complex类相关的知识,希望对你有一定的参考价值。
#include <iostream> #include <cmath> using namespace std; class Complex{ public: Complex(double real0,double imaginary0); Complex(double real0); Complex(Complex &a); void add(Complex b); void show(); double mod(); private: double real; double imaginary; }; Complex::Complex(double real0,double imaginary0) { real=real0;imaginary=imaginary0;} Complex::Complex(double real0):real(real0){ } Complex::Complex(Complex &a) { real=a.real;imaginary=a.imaginary;} void Complex::add(Complex b){ real=real+b.real; imaginary=imaginary+b.imaginary; } void Complex::show() { cout<<real<<"+"<<imaginary<<"i"<<endl; } double Complex::mod() { double m; m=sqrt(real*real+imaginary*imaginary); return m; } int main() { Complex c1(3,5); Complex c2(4.5); Complex c3(c1); c1.add(c2); c1.show(); cout<<c1.mod(); return 0; }
以上是关于complex类的主要内容,如果未能解决你的问题,请参考以下文章
用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算