220011wan

导航

实验二

 1 #pragma once
 2 #include<iostream>
 3 #include<cmath>
 4 using namespace std;
 5 class Complex
 6 {
 7     friend Complex add(Complex c1, Complex c2);
 8     friend bool is_equal(Complex c1, Complex c2);
 9     friend double abs(Complex c);
10 public:
11     Complex();
12     Complex (double i);
13     Complex (double i, double j);
14     Complex (const Complex &p);
15     double get_real();
16     double get_imag();
17     void show();
18     void add(Complex &p);
19 private:
20     double real;
21     double imag;
22 };
23 Complex::Complex() :real{ 0 }, imag{ 0 }
24 {
25 }
26 Complex::Complex(double i) :real{i}, imag{ 0 }
27 {
28 }
29 Complex::Complex(double i,double j) :real{ i }, imag{ j }
30 {
31 }
32 Complex::Complex(const Complex &p) :real{ p.real }, imag{ p.imag }
33 {
34 }
35 double Complex::get_real()
36 {
37     return real;
38 }
39 double Complex::get_imag()
40 {
41     return imag;
42 }
43 void Complex::show()
44 {
45     if (imag == 0)
46     {
47         cout << real;
48     }
49     else if (imag < 0)
50     {
51         cout << real << "-" << fabs(imag)<<"i";
52     }
53     else
54     {
55         cout << real << "+" << imag<<"i";
56     }
57 }
58 void Complex::add(Complex &p)
59 {
60     real = real + p.real;
61     imag = imag + p.imag;
62 }
63 Complex add(Complex c1, Complex c2)
64 {
65     Complex c3;
66     c3.real = c1.real + c2.real;
67     c3.imag = c1.imag + c2.imag;
68     return c3;
69 }
70 bool is_equal(Complex c1, Complex c2)
71 {
72     if (c1.real == c2.real && c1.imag == c2.imag)
73         return true;
74     else
75         return false;
76 }
77 double abs(Complex c)
78 {
79     return sqrt((c.real * c.real) + (c.imag * c.imag));
80 }
#include "Complex.h"
#include <iostream>
// 类测试
void test() 
{
    using namespace std;
    Complex c1(6, -6);
    Complex c2(2.9);
    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();
}

 


#pragma once
#include<iostream>
#include<string>
using namespace std;
class User
{
public:
    User(string _name);
    User(string _name, string _password, string _email);
    void set_email();
    void change_password();
    void print_info();
    static void print_count();
private:
    string name;
    string password;
    string email;
    static int count;
};
int User::count = 0;
User::User(string _name) :name{ _name }, password{ "111111" }, email{ "" }
{
    count++;
}
User::User(string _name, string _password, string _email) :name{ _name }, password{ _password }, email{ _email }
{
    count++;
}
void User::set_email()
{
    cout << "请设置邮箱地址:";
    string s;
    cin >> s;
    email = s;
    cout << "邮箱设置成功!" << endl;
}
void User::change_password()
{
    string s1, s2;
    int k = 3;
    cout << "请输入原密码:";
    cin >> s1;
    while (k!=1)
    {
        if (s1 == password)
        {
            cout << "请输入新密码:";
            cin >> s2;
            password = s2;
            cout << "密码设置成功!" << endl;
            break;
        }
        else
        {
            cout << "密码错误,请重新输入:";
            cin >> s1;
            k--;
        }
    }
    if (k == 1)
        cout << "密码错误,请稍后再试!" << endl;
    
}
void User::print_info()
{
    int n;
    n = password.length();
    cout << "name:" << name << endl;
    cout << "password:";
    for (int i = 1; i <= n; i++)
    {
        cout << "*";
    }
    cout << endl;
    cout << "email:" << email << endl;
}
void User::print_count()
{
    cout << "总共有" << User::count << "个用户。" << endl;
}
#include "user.h"
#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_password();
    cout << endl;
    user2.set_email();
    cout << endl;
    user2.print_info();
    cout << endl;
    User::print_count();
}
int main() 
{
    test();
}

 

 

 

 


posted on 2022-10-12 18:39  或者明天  阅读(19)  评论(0编辑  收藏  举报