实验二



#pragma
once #include <iostream> #include<math.h> class Complex { public: double get_real() { return real; } double get_imag()const { return imag; } Complex show() const { if (imag == 0) std::cout << real ; if (real == 0 && imag != 0) std::cout << imag << "i" ; if (real != 0 && imag < 0) std::cout << real <<" - "<< abs(imag) << "i" ; if (real != 0 && imag > 0) std::cout << real << " + " << imag << "i" ; return 0; } void add(const Complex& other) { real += other.real; imag += other.imag; } friend Complex add( Complex& c1, const Complex& c2); friend bool is_equal(Complex& c1, const Complex& c2); friend double abs(Complex& obj); Complex(double a=0.0, double b=0.0) { real = a; imag = b; } Complex(const Complex&obj) { real = obj.real; imag = obj.imag; } private: double real; double imag; }; Complex add( Complex& c1, const Complex& c2) { return Complex(c1.real + c2.real, c1.imag + c2.imag); } bool is_equal(Complex& c1,const Complex& c2) { if (c1.real == c2.real && c1.imag == c2.imag) return true; else return false; } double abs(Complex& obj) { return sqrt(obj.real * obj.real + obj.imag * obj.imag); }
#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();
}

 

 

 

实验任务五:

 

#include <iostream>
#include <algorithm>
#include<iomanip>
#include <string.h>
using namespace std;
class User
{
public:
    static int count;
    User(string name_s,string passwd_s="111111", string email_s="")
    {
        name = name_s;
        passwd=passwd_s;
        email = email_s;
        count++;
    }
    void set_email()
    {
        cout << "Enter email address:";
        cin >> email;
        cout << endl;
        cout << "email is set successfully..."<<endl;
    }
    void change_passwd()
    {
        string s;
        int j = 1;
        cout << "Enter old password: ";
        cin >> s;
        while(j)
        {
            
            if (s != passwd)
            {
                cout << "password input error.";
                j++;
            }
            if (s == passwd)
            {
                cout << "Enter new passwd: ";
                cin >> passwd;
                cout << endl;
                cout << "new passwd is set successfully..." << endl;
                break;
            }
            if (j <= 3)
            {
                cout << "Please re-enter again: ";
                    cin >> s;
                cout <<endl;
            }
            if (j > 3)
            {
                cout << "Please try after a while."<<endl;
                    break;
            }
        }
    }
    static int print_n()
    {
        cout << "there are "<<count<<"users" << endl;
        return 0;
    }
    void print_info()
    {

        cout << std::left << setw(8) << "name:" << name<<endl;
        cout << std::left << setw(8)<<"passwd:";
        string obj(passwd.length(), '*');
        cout<<obj<<endl;
        cout << std::left << setw(8) << "email:" << email << endl;
    }
private:
    string name;
    string passwd;
    string email;
};
int User::count= 0;
#include "User.cpp"
#include <iostream>

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();
} }

 

 
posted @ 2022-10-18 19:34  戴鲁  阅读(24)  评论(0)    收藏  举报