实验二

Task4:

Complex.hpp

#pragma once

#include<iostream>
#include<cmath>
using namespace std;
class Complex {
public:
    Complex(double r = 0.0, double i = 0.0) :real(r), imag(i) {}
    Complex(const Complex& c) {
        real = c.real;
        imag = c.imag;
    };

    double get_real() const { return real; }
    double get_imag() const { return imag; }
    
    void show() const;
    void add(const Complex& c){
        real += c.real;
        imag += c.imag;
    }

private:
    double real, imag;
    friend Complex add(const Complex& c1, const  Complex& c2);
    friend bool is_equal(const Complex& p1, const Complex& p2);
    friend double abs(const Complex& p);
};
void Complex::show() const {
    if (imag > 0)
        cout <<real<< "+" << imag << "i";
    if (imag == 0)
        cout << real;
    if (imag < 0)
        cout << real << imag << "i";
}
 Complex add(const Complex& c1, const  Complex& c2) {
    Complex c;
    c.real = c1.real + c2.real;
    c.imag = c1.imag + c2.imag;
    return c;
}
bool is_equal(const Complex& c1, const  Complex& c2) {
    if (c1.real == c2.real && c1.imag == c2.imag)
        return true;
    else 
        return false;
}
double abs(const Complex& p) {
    return(sqrt(p.real * p.real + p.imag * p.imag));
}

task4.cpp

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

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

    Complex c1(3, -4); 
    const Complex c2(4.5);
    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(); 
}

测试结果:

Task5:

User.hpp

#pragma once

#include<iostream>
#include<string>
using namespace std;

class User {
public:
    User(string name, string passwd = "111111", string email = " ") :na{ name }, pa{ passwd }, em{ email } { n++; }
    void set_email(); 
    void change_passwd(); 
    void print_info();
    static void print_n();

private:
    string na, pa, em;
    static int n;
};
int User::n= 0;
void User::set_email() {
        cout << "Enter email adress:";
        cin >> em;
        cout << "email is set successfully..." << endl;
}

void User::change_passwd() {
    cout << "Enter old password:";
    string password, newpasswd;
    int i;
    cin >> password;
    for (i = 1; i <= 3; ++i) {
        if (password != pa)
        {
            if (i < 3)
            {
                cout << "password input error. Please re-enter again: ";
                cin >> password;
            }
            else
                cout << "password input error. Please try after a whike." << endl;

        }
        else
        {
            cout << "Enter new passwd: ";
            cin >> newpasswd;
            pa = newpasswd;
            cout << "new passwd is set successfully..." << endl;
            break;
        }
    }
}

void User::print_info() {
    cout << "name:" << na << endl;
    cout << "passwd:";
    for (int i = 1; pa[i]; i++) 
        cout << "*";
    cout<< endl;
    cout << "email:" << em<< endl;
}
void User::print_n()
{
    cout << "there are " << n << " users." << endl;
}

task5.cpp

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

// 测试User类
void test() {
    using std::cout;
    using std::endl; 
    cout << "testing 1......\n"; 
    User user1("Jonny", "92197", "xyz@hotmail.com"); 
    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(); 
}

测试结果1:

测试结果2:

 

posted @ 2022-10-19 01:39  只是LL  阅读(8)  评论(0)    收藏  举报