其他分享
首页 > 其他分享> > 课堂练习

课堂练习

作者:互联网

#include<iostream>
#include<cmath>
using namespace std;
class Complex {
public:
    Complex(double a= 0, double b= 0);
    Complex(Complex &t);
    void add(Complex x);
    void show();
    double mod();
private:
    double real, imaginary;
};

Complex::Complex(double a, double b) {
    real = a;
    imaginary = b;
    if (imaginary == 0) cout << real << endl;
    else cout << real << "+" << imaginary << "i" << endl;
}

Complex::Complex(Complex &t)
{
    real = t.real;
    imaginary = t.imaginary;
}
void Complex::add(Complex x)
{
    real = x.real + real;
    imaginary = x.imaginary + imaginary;
}
void Complex::show() {
    cout << real << "+" << imaginary<< "i" << endl;
}

double Complex::mod() {
    return sqrt(pow(real, 2) + pow(imaginary, 2));
}

int main()
{
    Complex c1(3, 5);
    Complex c2(4.5);
    Complex c3(c1);
    c1.add(c2);
    c1.show();
    cout << c1.mod();
    system("pause");
    return 0;
}

总结:在课堂上没有写void add(Complex x);void show();double mod();说明对程序还是不熟悉,做的时候还是要请教同学才能写出来。

标签:课堂练习,show,double,void,add,Complex,imaginary
来源: https://www.cnblogs.com/wmy0621/p/10632758.html