首页 > 其他分享 >实验二

实验二

时间:2022-10-13 16:55:45浏览次数:41  
标签:const cout double imag Complex 实验 include

实验4

hpp

#pragma once
#include<iostream>
using namespace std;
class Complex {
public:
    Complex();
    Complex(double real0, double imag0 = 0);
    Complex(const Complex& obj);
    double get_real()const { return real; }
    double get_imag()const { return imag; }
    void show()const;
    void add(Complex temp);
    friend Complex add(Complex c1, Complex c2);
    friend bool is_equal(Complex c1, Complex c2);
    friend double abs(Complex c1);
private:
    double real;
    double imag;
};
Complex::Complex() :real{ 0 }, imag{ 0 }{}

Complex::Complex(double real0, double imag0) : real{ real0 }, imag{ imag0 }{}
Complex::Complex(const Complex& obj) : real{ obj.real }, imag{ obj.imag }{}
void Complex::show()const {
    if (imag == 0)
        cout << real;
    else
        cout << real << imag << "i";
}
void Complex::add(Complex temp) {
    real += temp.real;
    imag += temp.imag;
}
Complex add(Complex c1, Complex c2) {
    Complex c3;
    c3.real = c1.real + c2.real;
    c3.imag = c1.imag + c2.imag;
    return c3;
}
bool is_equal(Complex c1, Complex c2) {
    if (c1.imag = c2.imag && c1.real == c2.real)
        return 1;
    else
        return 0;
}
double abs(Complex c1) {
    return sqrt((c1.real * c1.real + c1.imag * c1.imag));
}

cpp

#include "Complex.hpp"
#include <iostream>

// 类测试
void test() {
    using namespace std;

    Complex c1(2, -6);
    const Complex c2(7.9);
    Complex c3(c1);

    cout << "c1 = ";
    c1.show();
    cout << endl;

    cout << "c2 = ";
    c2.show();
    cout << endl;
    cout << "c2.imag = " << c2.get_imag() << endl;

    cout << "c3 = ";
    c3.show();
    cout << endl;

    cout << "abs(c1) = ";
    cout << abs(c1) << endl;

    cout << boolalpha;
    cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
    cout << "c1 == c2 : " << is_equal(c1, c2) << endl;

    Complex c4;
    c4 = add(c1, c2);
    cout << "c4 = c1 + c2 = ";
    c4.show();
    cout << endl;

    c1.add(c2);
    cout << "c1 += c2, " << "c1 = ";
    c1.show();
    cout << endl;
}

int main() {
    test();
}

截图

实验五

 

hpp

#pragma once
#include<iostream>
#include<string>

using namespace std;

class User {
public:
    User(string n, string p = "111111", string e = "");
    void set_email() {
        cout << "Enter email address:" ;
        cin >> email;
        cout << "email is set successfully..." << endl;
    }
    void change_passwd();
    void print_info()const;
    void static print_n() {
        cout << "there are " << n << " users" << endl;
    }
private:
    string name, passwd, email;
    static int n;


};

User::User(string n1, string p, string e) :name(n1), passwd(p), email(e) {
    n++;
}
int User::n = 0;
void User::change_passwd() {
    cout << "Enter old password:";
    string c;
    int count = 1;
    cin >> c;
    while (c != passwd && count < 3) {
        cout << "password input error.please re-enter again: ";
        cin >> passwd;
        count++;
        if (count == 3) {
            cout << "password input error.please try after a while." << endl;
        }
    }

    if (count != 3) {
        cout << "enter new passwd: ";
        cin >> passwd;
        cout << "new passwd is set successfully..." << endl;
    }
}
void User::print_info()const {
    int l = passwd.length();

    cout << "name: " << name << endl;
    cout << "password: ";
    while (l--) {
        cout << "*";
    }cout << '\n';
    cout << "email: " << email << endl;
}

cpp

#include "User.hpp"
#include <iostream>

// 测试User类
void test() {
    using std::cout;
    using std::endl;

    cout << "testing 1......\n";
    User user1("Jonny", "92197", "[email protected]");
    user1.print_info();

    cout << endl
         << "testing 2......\n\n";
         
    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    cout << endl;
    User::print_n();
}

int main() {
    test();
}

 

标签:const,cout,double,imag,Complex,实验,include
From: https://www.cnblogs.com/Xl995/p/16788730.html

相关文章