实验二

#include<iostream>
#include<cmath>
using namespace std;
class Complex {
public:
    ~Complex() = default;
    Complex(double m = 0, double n = 0);
    Complex(const Complex& obj);
    double get_real() const { return real; };
    double get_imag() const { return imag; };
    void show() const;
    Complex  add(const Complex& k);
    friend Complex add(const Complex& p1, const Complex& p2);
    friend   bool is_equal(const Complex& p1, const Complex& p2);
    friend double abs(Complex& p1);
private:
    double real, imag;
};

Complex::Complex(double m, double n) :real{ m }, imag{ n } {}
Complex::Complex(const Complex& obj) :real{ obj.real }, imag{ obj.imag } {}

void Complex::show() const {
    if (imag > 0)
        cout << real << " + " << imag << "i" << endl;
    else if (imag < 0)
        cout << real << imag << "i" << endl;
    else
        cout << real << endl;
}

Complex Complex::add(const Complex& k) {

      real =real + k.real;
      imag = imag +k.imag;
    return Complex(real,imag);
}

Complex add(const Complex& p1, const Complex& p2) {
    Complex result;
    result.real = p1.real + p2.real;
    result.imag = p1.imag + p2.imag;
    return result;
}

bool is_equal(const Complex& p1, const Complex& p2)
{
    if (p1.real == p2.real && p1.imag == p2.imag)
        return true;
    else
        return false;
}

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

修改后的task4代码:

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

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

    Complex c1(2, 3);
    const Complex c2(8.8,2.1);
    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();
}

实验5:

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class User {
public:
    User(string a, string b = "111111", string c = "");
    ~User() { n--; };
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n() { cout << "There are " << n << "  users" << endl; }
private:
    string name,email,passwd;
    static int n ;
};
User::User(string a, string b, string c) :name{ a }, passwd{ b }, email{ c } { n++; };
void User::set_email(){
    string new_email;
    cout << "Enter email address:";
    cin >> new_email;
    email = new_email;
    cout << "email is setting successfully..."<<endl;

}
void User::change_passwd() { 
    int i = 0;
    string shuru;
    cout << "Enter old password:";
    cin >> shuru; 
    cout << endl;
    for(i=0;shuru != passwd && i<2; i++)
    {
        cout << "password input error.Please re-enter again:";
        cin >> shuru;
        cout << endl;
    }
    if (i == 2)
        cout << "password input error.Please try after a while."<<endl;
    if (shuru == passwd)
    {
        cout << "enter new password:";
        cin >> passwd;
        cout << "new password is setting successful" << endl;
    }

}
void User::print_info() {
    int length = passwd.length();
    cout <<setw(6) << "name: " << name << endl;
    cout << setw(6)<<"passwd: " ;
    for (int i = 1; i <= length; i++)
            cout << "*";
    cout << endl;
    cout << setw(6) << "email: " << email << endl;    
}
int User::n = 0;

改变task5,重新测试:

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

// 测试User类
void test() {
    using std::cout;
    using std::endl;

    cout << "testing 1......\n";
    User user1("Jackerlove", "329955", "3299552619@qq.com");
    user1.print_info();

    cout << endl
        << "testing 2......\n\n";

    User user2("wayward");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    cout << endl;
    User::print_n();
}

int main() {
    test();
}

  

 

posted @ 2022-10-17 21:39  薛天驰  阅读(15)  评论(0编辑  收藏  举报