实验二

task.4

#include<iostream>
#include<math.h>
using namespace std;
class Complex {
private:
    float real, imag;
public:
    Complex(float r, float i);//带两个参数的构造函数
    Complex();//默认构造函数
    Complex(float r);//带一个参数的构造函数
    Complex(Complex &c);//复制构造函数
    float get_real()const { return real; }
    float get_imag()const { return imag; }
    void show()const {
        cout << real;
        if (imag > 0)cout << "+";
        if(imag!=0)cout << imag << "i";
    }
    void add(const Complex &c) {
        real += c.real;
        imag += c.imag;
    }
    friend Complex add(const Complex &c1,const Complex &c2);//youyuan
    friend bool is_equal(const Complex &c1,const Complex &c2);
    friend float abs(const Complex &c);
};

Complex::Complex(float r, float i) :real(r), imag(i) {}//带两个参数的构造函数的实现
Complex::Complex(float r) : real(r), imag(0) {}//带一个参数的构造函数的实现
Complex::Complex() : real(0), imag(0) {}//默认构造函数的实现
Complex::Complex(Complex &c) {
    real = c.real;
    imag = c.imag;
}//复制构造函数的实现
Complex add(const Complex &c1, const Complex &c2) {
    float real = c1.real + c2.real;
    float imag = c1.imag + c2.imag;
    Complex a(real, imag);
    return a;
}
bool is_equal(const Complex &c1, const Complex &c2) {
    bool a = false;
    if (c1.real == c2.real&&c1.imag == c2.imag)a = true;
    return a;
}
float abs(const Complex &c) {
    return sqrt(c.real*c.real + c.imag*c.imag);
}

task.4.cpp

#include "Complex.hpp"
#include <iostream>
#include <math.h>
// 类测试
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();
}

 

task.5

#include<iostream>
#include<math.h>
#include<string>
using namespace std;
class User {
private:
    string name, passwd, email;
    static int n;//静态数据成员
public:
    User(string na, string p, string e);//带3个参数的构造函数
    User(string na);//带一个参数的构造函数
    void change_passwd();
    void set_email();
    void print_info();
    
    static void print_n();//静态成员函数
};
int User::n = 0;//静态初始化
User::User(string na, string p, string e) :name(na), passwd(p), email(e) { n++; }//带三个参数的构造函数
User::User(string na) : name(na), passwd("111111"), email("") { n++; }//带一个参数的构造函数
void User::change_passwd() {
    string ps;
    int t = 2;
    cout << "Enter old password:";//输出Enter old password:
    cin >> ps;//输入旧密码保存到ps
    cout << endl;
    if (ps == passwd) {//判断是否与password一致
        cout << "Enter new password:";
        cin >> ps;
        passwd = ps;
        cout <<"new passwd is set successfully..."<<endl;//若一致,输出Enter new password:,输入新密码保存到ps,再赋值给passwd
    }
    else {
        while (t) {
            cout << "password input error.Please re-enter again:";
            cin >> ps;
            if (ps == passwd) {
                cout << "Enter new password:";
                cin >> ps;
                passwd = ps;
                cout << "new passwd is set successfully..." << endl;//若一致,输出Enter new password:,输入新密码保存到ps,再赋值给passwd
                break;
            }
            cout << endl;
            t--;
       }
        if (t == 0) { cout << "password input error.Please try after a while." << endl; }
    }
}//改密码
void User::set_email() {
    string ne;
    cout << "Enter email address:";
    cin >> ne;
    email = ne;
    cout << endl;
    cout << "email is set successfully..." << endl;
}//设邮箱
void User::print_info() {
    string s1{ sizeof(passwd) , '*' };
    // 构造一个字符串对象
    cout << "name:" << name << endl;
    cout << "passwd:" <<s1<< endl;
    cout << "email:" << email << endl;
}
void User::print_n() {
    cout << "there are " << n << " users." << endl;
}

task.5.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-19 12:15  贾睿  阅读(4)  评论(0编辑  收藏  举报