实验任务四

Complex.hpp 源代码

 1 #pragma once
 2 
 3 
 4 #include<iostream>
 5 #include<cmath>
 6 using namespace std;
 7 
 8 class Complex
 9 {
10 public:
11     Complex();
12     Complex(double x);
13     Complex(double x, double y);
14     Complex(Complex& other);
15     friend Complex add(const Complex &c1, const Complex &c2);
16     Complex add(const Complex& other);
17      friend bool is_equal(const Complex &c1,const Complex &c2);
18     friend double abs(const Complex &c1);
19     double get_real() const
20     {
21         return x;
22     }
23     double get_imag() const
24     {
25         return y;
26     }
27     void show() const
28     {
29         if (y > 0)
30         {
31             cout << x << "+" << y << "i";
32         }
33         if (y == 0)
34         {
35             cout << x;
36         }
37         if (y < 0)
38         {
39             cout << x << y << "i";
40         }
41     }
42 private:
43     double x, y;
44 };
45 Complex::Complex()
46 {
47     x = 0;
48     y = 0;
49 }
50 Complex::Complex(double x)
51 {
52     this->x = x;
53     y = 0;
54 }
55 Complex::Complex(double x,double y)
56 {
57     this->x = x;
58     this->y = y;
59 
60 }
61 Complex::Complex(Complex& other)
62 {
63     this->x = other.x;
64     this->y = other.y;
65 }
66 Complex add(const Complex &c1, const Complex &c2) 
67 {
68     Complex c3;
69     c3.x = c1.x + c2.x;
70     c3.y = c2.y + c1.y;
71     return c3;
72 }
73 bool is_equal(const Complex &c1,const Complex &c2)
74 {
75     if (c1.x == c2.x && c2.y == c1.y)
76     {
77         return true;
78     }
79     return false;
80 }
81 double abs(const Complex &c1)
82 {
83     double s;
84     s = sqrt(c1.x * c1.x + c1.y * c1.y);
85     return s;
86 }
87 Complex Complex::add(const Complex& other)
88 {
89     this->x += other.x;
90     this->y += other.y;
91     return *this;
92 }

Complex.cpp源代码

 1 #include "Complex.hpp"
 2 #include <iostream>
 3 
 4 // 类测试
 5 void test() {
 6     using namespace std;
 7 
 8     Complex c1(2, 4);
 9    const   Complex c2(4.5);
10     Complex c3(c1);
11 
12     cout << "c1 = ";
13     c1.show();
14     cout << endl;
15 
16     cout << "c2 = ";
17     c2.show();
18     cout << endl;
19     cout << "c2.imag = " << c2.get_imag() << endl;
20 
21     cout << "c3 = ";
22     c3.show();
23     cout << endl;
24 
25     cout << "abs(c1) = ";
26     cout << abs(c1) << endl;
27 
28     cout << boolalpha;
29     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
30     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
31 
32     Complex c4;
33     c4 = add(c1, c2);
34     cout << "c4 = c1 + c2 = ";
35     c4.show();
36     cout << endl;
37 
38     c1.add(c2);
39     cout << "c1 += c2, " << "c1 = ";
40     c1.show();
41     cout << endl;
42 }
43 
44 int main() {
45     test();
46 }

 

 

 实验任务五

User.hpp源代码

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<string>
 5 using namespace std;
 6     
 7 class User
 8 {
 9 
10 public:
11     User(string name);
12     User(string name, string password, string email);
13     void set_email();
14     void change_passwd();
15     void print_info();
16      static int print_n();
17 private:
18     string name, password, email;
19   static int count;
20 };
21 int User::count = 0;
22 User::User(string name)
23 {
24     this->name = name;
25     this->password = "111111";
26     this->email = ' ';
27     count ++ ;
28     
29 }
30 
31 User::User(string name, string password, string email)
32 {
33     this->name = name;
34     this->password = password;
35     this->email = email;
36     count++;
37 }
38 
39 void User::set_email()
40 {
41     cout << "enter email address:";
42     cin >> email;
43     cout << "email is set successfully..." << endl;
44 
45 }
46 
47 void User::change_passwd()
48 {
49     cout << "Enter old password:";
50     string s1;
51     int count = 3;
52     while (count)
53     {
54         cin >> s1;
55         if (s1 != password)
56         {
57             if (count != 1)
58                 cout << "password input error.Please re-enter again:" << endl;
59             else
60                 cout << "password input error.Please try after a while." << endl;
61         }
62         else
63         {
64             cout << "Enter new password:";
65             cin >> password;
66             cout << "new password id set successfully..." << endl;
67             break;
68         }
69         count--;
70     }
71     
72 }
73 void User::print_info()
74 {
75     string s1(password.length(), '*');
76     cout << "name:" << name << endl;
77     cout << "password:" << s1 << endl;
78     cout << "email:" << email << endl;
79 
80 }
81 int User::print_n()
82 {
83     
84     cout << "there are  "<<count<<"  users." << endl;
85     return count;
86 }

 

User.cpp源代码

 1 #include "User.hpp"
 2 #include <iostream>
 3 
 4 // 测试User类
 5 void test() {
 6     using std::cout;
 7     using std::endl;
 8 
 9     cout << "testing 1......\n";
10     User user1("Jonny", "92197", "xyz@hotmail.com");
11     user1.print_info();
12 
13     cout << endl
14         << "testing 2......\n\n";
15 
16     User user2("Leonard");
17     user2.change_passwd();
18     user2.set_email();
19     user2.print_info();
20 
21     cout << endl;
22     User::print_n();
23 }
24 
25 int main() {
26     test();
27 }

 

 

 

 

posted on 2022-10-14 14:43  离小离  阅读(39)  评论(0)    收藏  举报