其他分享
首页 > 其他分享> > 课堂作业

课堂作业

作者:互联网

#include<iostream>
#include<cmath>
using namespace std;
class Complex
{
public:
Complex(double r,double i) {real=r;imaginary=i;}
Complex(double r){real=r;imaginary=0;}
Complex(Complex &m){real= m.real;imaginary= m.imaginary;}
void add(Complex &m);
void show();
double mod();
private:
double real;
double imaginary;
};
void Complex::add(Complex &m) {
real+= m.real;
imaginary+= m.imaginary;
}
void Complex::show()
{
if(imaginary>0)
cout<< real << "+" << imaginary << "i" <<endl;
else if(imaginary<0)
cout<<real<<imaginary<<"i"<<endl;
else cout<<real<<endl;
}
double Complex::mod() {
return sqrt(real * real + imaginary * imaginary);
}
int main()
{
Complex c1(3,5);
c1.show();
Complex c2(4.5);
c1.show();
Complex c3(c1);
c1.add(c2);
c1.show();
cout<<c1.mod();
return 0;
}

 

标签:real,课堂作业,show,double,Complex,c1,imaginary
来源: https://www.cnblogs.com/q-z-y-0732/p/10639563.html