课堂作业
#include<iostream> using namespace std; #include<cmath> class Complex{ public: Complex(double x=0,double y=0):real(x),imaginary(y){}; Complex(const Complex &p):real(p.real),imaginary(p.imaginary) {}; void add(const Complex &p) ; void show()const; double mod()const; private: double real; double imaginary; }; void Complex::add(const Complex &p){ real=p.real+real; imaginary=p.imaginary+imaginary; } void Complex::show()const{ if(real!=0&&imaginary!=0) cout<<real<<"+"<<imaginary<<"i"<<endl; else if(real==0&&imaginary!=0) cout<<imaginary<<"i"<<endl; else if(real!=0&&imaginary==0) cout<<real<<endl; } double Complex::mod()const{ return sqrt(real*real+imaginary*imaginary); } int main(){ Complex c1(3,5); Complex c2(4.5); Complex c3(c1); c1.add(c2); c1.show(); c2.show(); cout<<c1.mod()<<endl; return 0; }
虽然这只是一个简单的小程序,但是我觉得要一下子,没有任何错误写出来也是需要细心的。
https://www.cnblogs.com/Ann-88/
https://www.cnblogs.com/Tiger-Hu22/
https://www.cnblogs.com/jyf13/