实验二

task4:

Complex.hpp

#include<iostream>
#include<math.h>

using namespace std; 

class Complex{
public:
    Complex(double c31, double c32);
    Complex(const Complex& c4);

    double get_real() const;
    double get_imag() const;
    void show() const;
    void add(const Complex& c4);
   
    friend Complex add(const Complex c1, const Complex c2);
    friend bool is_equal(const Complex c1,const Complex c2);
    friend int abs(const Complex c3); 

private:
    double real;
    double imag;
};

Complex::Complex(double c31=0, double c32=0) :real{c31},imag{c32}{} 
Complex::Complex(const Complex& c4): real{c4.real}, imag{c4.imag}{}

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

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

void Complex::show() const{
     if(imag == 0) 
         cout << real;
     else if(imag < 0) 
         cout << real << " - " << -1 * imag << "i";
     else
         cout << real << " + " << imag << "i";    
}

void Complex::add(const Complex& c4){
    real += c4.real;
    imag += c4.imag;
}

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

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

 int abs(Complex c4) {
     return sqrt(c4.real * c4.real + c4.imag * c4.imag);
 }

test4.cpp

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

using namespace std;

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

    Complex c1(5, -7);
    const Complex c2(1.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

#include <iostream>
#include <string>
  
using namespace std;
  
class User {
public:
    User(string Name, string Passwd = "111111", string Email = "") 
        : name{Name} , passwd{Passwd}, email{Email} {count++;}
    
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n() {
        cout << "there are " << count << " users."; 
    }
 
 private: 
    string name;
    string passwd;
    string email;
    static int count;
 };
 
 int User::count = 0;

 void User::set_email() {
     cout << "Enter email address: " ;
     cin >> email;
     cout << "email is set successfully..." << endl;
 }
 
 void User::change_passwd() {
    string newpasswd;
    cout << "Enter old password: ";
    cin >> newpasswd;
    for(int count = 1; count <= 2 && newpasswd != passwd; count++){
        cout <<"password input error. Please re-enter again: ";
        cin >> newpasswd;
    }
    if(newpasswd == passwd){
        cout << "Enter new passwd: ";
        cin >> passwd;
        cout << "new passwd is set successfully..." << endl;
    }
    else
        cout << "password input error. Please try after a while." << endl;    
 }
 
 void User::print_info() {
     string s1(passwd.length(), '*');
     cout << "name: " << name << endl;
     cout << "passwd: " << s1 << endl;
     cout << "email: " << email << endl;    
}

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

 

posted @ 2022-10-18 01:41  miantiaooooo  阅读(17)  评论(0编辑  收藏  举报