实验任务4

Complex.hpp

 1 #pragma once
 2 #include<iostream>
 3 #include<cmath>
 4 
 5 using namespace std;
 6 
 7 class Complex {
 8 public:
 9     
10     Complex(double real2, double imag2);
11     Complex(const Complex &obj);
12 
13 public:
14     double get_real() const{ return real; }
15     double get_imag() const { return imag; }
16     void show() const {    
17         if (imag == 0) {
18             cout << real;
19         }
20         else if (imag > 0) {
21             cout << real << " + " << imag << "i";
22         }
23         else
24             cout << real << " - " << abs(imag) << "i";
25          ;}
26     void add(const Complex &obj);
27 
28     friend bool is_equal(const Complex &obj1,const Complex &obj2);
29     friend double abs(const Complex &obj);
30     friend Complex add(const Complex &obj1, const Complex &obj2);
31 
32 private:
33     double real, imag;
34 };
35 
36 
37 Complex::Complex(double real=0.0,double imag=0.0):real{real},imag{imag}{}
38 Complex::Complex(const Complex& obj) {
39     real = obj.real;
40     imag = obj.imag;
41 }
42 void Complex::add(const Complex &obj){
43     real += obj.real;
44     imag += obj.imag;
45 }
46 bool is_equal(const Complex& obj1, const Complex& obj2){
47     if (obj1.real == obj2.real && obj1.imag == obj2.imag) {
48         return true;
49     }
50     else
51         return false;
52 }
53 double abs(const Complex& obj) {
54     return sqrt( obj.real * obj.real + obj.imag * obj.imag);
55 }
56 Complex add(const Complex &obj1,const Complex &obj2) {
57     return Complex(obj1.real + obj2.real, obj1.imag + obj2.imag);
58 }

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 }

测试截图:

 

实验任务5

User.hpp

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

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:18  摆烂青年  阅读(15)  评论(0编辑  收藏  举报