任务3 源代码

Complex.hpp

#ifndef COMPLEX_HPP
#define COMPLEX_HPP

#include<iostream>
#include<stdbool.h>
#include<cmath>
using namespace std;

class Complex
{
public:
    Complex(const double real0, const double imag0);
    ~Complex() = default;
    Complex(const Complex& data);

    double get_real() const;
    double get_imag() const;
    void show()const;
    void add(const Complex& c);

    friend Complex add(const Complex& c1,const  Complex& c2);
    friend bool is_equal(const Complex& c1,const  Complex& c2);
    friend double abs(const Complex& c);

private:
    double real, imag;
};

Complex::Complex(const double real0=0.0, const  double imag0=0.0) : real{ real0 }, imag{ imag0 }{}

Complex::Complex(const Complex& data)
{
    real = data.real;
    imag = data.imag;
}

double Complex::get_real()const
{
    return real;
}

double Complex::get_imag()const
{
    return imag;
}

void Complex::show()const
{
    cout << this->real;
    if (this->imag == 0)
        return;
    else if (this->imag > 0)
        cout << " + " << this->imag << "i";
    else cout <<" - "<<abs( this->imag) << "i";
}

void Complex::add(const Complex& c)
{
    this->real += c.real;
    this->imag += c.imag;
}

inline Complex add(const Complex& c1, const  Complex& c2)
{
    Complex c3;
    c3.real = c1.real + c2.real;
    c3.imag = c1.imag + c2.imag;
    return c3;
}

inline bool is_equal(const Complex& c1, const  Complex& c2)
{
    return c1.real == c2.real && c1.imag == c2.imag;
}

inline double abs(const Complex& c)
{
    return sqrt(c.real * c.real + c.imag * c.imag);
}

#endif

task3.cpp

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

int main()
{
using namespace std;

Complex c1(2.5, -3.8);
const Complex c2(1.3);
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;
}

 任务4 源代码:

User.hpp

#ifndef USER_HPP
#define USER_HPP

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

class User
{
public:
    User(const string name0, const string passwd0 = "111111", const string email0 = "") :name{ name0 }, passwd{ passwd0 }, email{ email0 }{this->number_of_user++; }
    ~User() = default;
    void set_email();
    void change_passwd();
    void print_info()const;
    static void print_n() { cout << "there are " << User::number_of_user << " users."; };
private:
    string name, passwd, email;
    static int number_of_user;
};

int User::number_of_user = 0;

void User::set_email()
{
    string my_email;
    while (true)
    {
        cout << "Enter email address: ";
        cin >> my_email;
        if (my_email.find('@') < my_email.size() && my_email.find(".com") < my_email.size())//如果找到会返回其所在位置
        {
            this->email = my_email;
            cout << "email is set succseefully..." << endl;
            return;
        }
        else cout << "email setting failed ." << endl;
    }
    
    
}

void User::change_passwd()
{
    string my_passwd,new_passwd;
    int chance = 3;
    cout << "Enter old password:";
    cin >> my_passwd;
    while (--chance)
    {
        if (my_passwd.compare(this->passwd))
        {
            cout << "password input error.Please re-enter again: ";
            cin >> my_passwd;
        }
        else
        {
            while (true)
            {
                cout << "Enter new passwd:";
                cin >> new_passwd;
                if (new_passwd.size() == 6)
                {
                    this->passwd = new_passwd;
                    cout << "new passwd is set successfully..." << endl;
                    return;
                }
                else cout << "passwd length more than six, set the password failure;" << endl;
            }
        }
    }
    if (chance == 0)
        cout << "password input error.Please try after a while."<<endl;

}
void User::print_info()const
{
    cout.width(8);
    cout << setiosflags(ios::left) << "name:";
    cout << this->name << endl;
    cout.width(8);
    cout << setiosflags(ios::left) << "passwd:";
    cout << "******" << endl;
    cout.width(8);
    cout << setiosflags(ios::left) << "email:";
    cout << this->email << endl;
}

#endif

task4.cpp

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

int main()
{
    using namespace std;

    cout << "testing 1......" << endl;
    User user1("Jonny", "92197", "xyz@hotmail.com");
    user1.print_info();

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

    cout << endl
        << "testing 3......" << endl
        << endl;
    User user3("Corgi", "200205");
    user3.set_email();
    user3.change_passwd();
    user3.print_info();

    User::print_n();
}

 posted on 2021-10-24 16:42  Popcorn_ZSJ  阅读(19)  评论(3编辑  收藏  举报