实验二

实验任务四

Complex.hpp

 1 #include<iostream>
 2 #include<cmath>
 3 
 4 using std::cout;
 5 using std::endl;
 6 
 7 class Complex {
 8 public:
 9          //构造函数 
10          Complex(float r = 0, float i = 0) : real{ r }, imag{ i } {}
11          Complex(const Complex & obj) : real{ obj.real }, imag{ obj.imag } {}
12 
13 public:
14          //普通成员函数
15          float get_real() const { return real; }
16          float get_imag() const { return imag; }
17          void show() const;
18          void add(const Complex & obj);
19 
20          //友元函数
21          friend Complex add(const Complex & c1, const Complex & c2);
22          friend bool is_equal(const Complex & c1, const Complex & c2);
23          friend double abs(const Complex & obj);
24 
25 private:
26             float real; 
27          float imag;
28 };
29 
30 //成员函数的实现 
31 void Complex::show() const {
32      //各种情况复数输出 
33      if (real != 0 && imag > 0) {
34          cout << real << " + " << imag << "i";
35     }
36      else if (real != 0 && imag < 0)
37      {
38          cout << real << " - " << abs(imag) << "i";
39      }
40      else if (real == 0 && imag != 0)
41      {
42          cout << imag << "i";
43      }
44      else
45      {
46          cout << real;
47      }
48 
49 }
50 
51 void Complex::add(const Complex & obj) {
52      real += obj.real;
53      imag += obj.imag;
54 }
55 
56 
57 //友元函数的实现 
58 Complex add(const Complex & c1, const Complex & c2) {
59     return Complex(c1.real + c2.real, c1.imag + c2.imag);
60 }
61 
62 bool is_equal(const Complex & c1, const Complex & c2) {
63      return (c1.real == c2.real) && (c1.imag == c2.imag); 64     65 }
66 
67 double abs(const Complex & obj) {
68      return sqrt(obj.real * obj.real + obj.imag * obj.imag);
69 
70 }

task4.cpp

 1 #include "Complex.hpp"
 2 #include <iostream>
 3 
 4 // 类测试
 5 void test() {
 6     using namespace std;
 7 
 8     Complex c1(3, -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 #include<iostream>
 2 #include<string> 
 3  
 4 using std::cout;
 5 using std::cin;
 6 using std::endl;
 7 using std::string;
 8 
 9 class User{
10     public:
11         User(string n, string p = "111111", string e = "");
12         void set_email() ;
13         void change_passwd();
14         void print_info();
15         static void print_n() { cout << "there are " << count << " users" <<endl; }
16     
17     public:
18         static int count;
19         
20     private:
21         string name;
22         string passwd;
23         string email;
24 };
25  
26 int User::count = 0;
27  
28 User::User(string n, string p /*= "111111"*/, string e /*= "111111"*/){
29     name = n;
30     passwd = p;
31     email = e;
32     count++;
33 }
34 
35 void User::set_email() {
36     string s1;
37     cout << "Enter email address: ";
38     cin >> s1; 
39     email = s1;
40     cout << "email is set successfully..." << endl;
41 }
42 
43 void User::change_passwd() {
44     cout << "Enter old password: ";
45     int times = 0;
46     while(times < 3){
47         string s1;
48         cin >> s1;
49         if(s1 == passwd){
50             cout << "Enter new passwd: ";
51             string s1;
52             cin >> s1;
53             passwd = s1;
54             cout << "new passwd is set successfully..." << endl; 
55             break;
56         }
57         if(times < 2)
58         cout << "password input error. Please re-enter again:";
59         times++;
60     }
61     if(times >= 3) cout << "passwd input error. Please try affter a while." << endl;
62 }
63 
64 void User::print_info(){
65     cout << "name: " << name << endl;
66     cout << "passwd: ";
67     string s1(passwd.length(), '*');
68     cout << s1 << endl;
69     cout << "email: " << email << endl;
70 }

task5.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-18 20:17  Terrence-Zhao  阅读(5)  评论(0编辑  收藏  举报