实验一

实验结论

实验3:自定义Complex类

Complex.hpp

 

实验一
 1 // Complex.hpp
 2 #pragma once
 3 #include <iostream>
 4 #include <cmath>
 5 
 6 class Complex
 7 {
 8 private:
 9     double real, imag;
10 
11 public:
12     Complex(int r = 0, int i = 0);
13     Complex(const Complex& c);
14     double get_real() const;
15     double get_imag() const;
16     void show() const;
17     void add(Complex c);
18     friend Complex add(Complex c1, Complex c2);
19     friend bool is_equal(Complex c1, Complex c2);
20     friend double abs(Complex c);
21 };
22 
23 Complex::Complex(int r, int i) : real(r), imag(i)
24 {
25 }
26 Complex::Complex(const Complex& c)
27 {
28     real = c.real;
29     imag = c.imag;
30 }
31 
32 double Complex::get_real() const
33 {
34     return real;
35 }
36 
37 double Complex::get_imag() const
38 {
39     return imag;
40 }
41 
42 void Complex::show() const
43 {
44     using namespace std;
45     const double real_tmp = real;
46     const double imag_tmp = imag >= 0 ? imag : imag * -1;
47     cout << real_tmp;
48     imag > 0 ? cout << " + " : cout << " - ";
49     cout << imag_tmp << "i";
50 }
51 
52 void Complex::add(Complex c)
53 {
54     real += c.real;
55     imag += c.imag;
56 }
57 
58 Complex add(Complex c1, Complex c2)
59 {
60     Complex c(c1.real + c2.real, c1.imag + c2.imag);
61     return c;
62 }
63 
64 bool is_equal(Complex c1, Complex c2)
65 {
66     return c1.real == c2.real && c1.imag == c2.imag;
67 }
68 
69 double abs(Complex c)
70 {
71     return sqrt(pow(c.real, 2) + pow(c.imag, 2));
72 }
View Code

main.cpp

 

实验一
 1 // task3.cpp
 2 #include"Complex.hpp"
 3 
 4 int main()
 5 {
 6     using namespace std;
 7     Complex c1(6, -8);
 8     const Complex c2(6);
 9     Complex c3(c1);
10     cout << "c1 = ";
11     c1.show();
12     cout << endl;
13     cout << "c2 = ";
14     c2.show();
15     cout << endl;
16     cout << "c2.imag = " << c2.get_imag() << endl;
17     cout << "c3 = ";
18     c3.show();
19     cout << endl;
20     cout << "abs(c1) = ";
21     cout << abs(c1) << endl;
22     cout << boolalpha;
23     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
24     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
25     Complex c4;
26     c4 = add(c1, c2);
27     cout << "c4 = c1 + c2 = ";
28     c4.show();
29     cout << endl;
30     c1.add(c2);
31     cout << "c1 += c2, "
32         << "c1 = ";
33     c1.show();
34     cout << endl;
35 }
View Code

 

运行效果图如下:

实验一

 

实验4:设计User类

User.hpp

 

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

 

main.cpp

 

实验一
 1 #include"User.hpp"
 2 
 3 
 4 int main()
 5 {
 6     using namespace std;
 7     cout << "testing 1......" << endl;
 8     User user1("Frank", "39984", "sob@gmail.com");
 9     user1.print_info();
10     cout << endl
11         << "testing 2......" << endl
12         << endl;
13     User user2("Sobriety");
14     user2.change_passwd();
15     user2.set_email();
16     user2.print_info();
17     User::print_n();
18 }
View Code

 

运行效果如下:

 

实验一

实验一

 

 

实验总结:

 实验3心得与注意事项

关于const的使用:

1.由于main函数中c2为const类型,因此,为了保护数据,函数传参的过程中应将形参设为const类型

2.数据的修改范围只能从小到大,而不能从大到小

 

关于show函数:

1.  需要判断是否为负数,因为正数不会又“+”这个符号

2.观察输出样例,“+”或“-”号与real和imag之间有空格,所以当imag  < 0 时, 手动输出 “-”,否则无法保持一致

3.由于形参为const类型,因此应该用新变量来保存修改后的结果

4.个人使用了三元运算符,提高了代码的可读性,减少了if else 的使用

 

关于friend函数:

注意定义的格式:类型 + 函数名 + 形参

 

实验四心得与注意事项

关于输出样例的格式

应该使用<iomainip>,以控制输出样式,保证、email,name,passwd 输出的格式统一,即占据的字符宽度等长,且左对齐

手法:利用setw(字符宽度), setiosflags(ios::left)

 

userCount

因为我们需要统计当前系统的用户总数,所以需要一个类属性来记录(设为static)

如果在类似java的语言中,static类型的变量其实时不能被对象访问的,而只能使用类名,这里可以留意一下

 

user类的丰富:

由于个人最近事情确实比较多,这里为同学们提供思路

1.实现邮箱的合法性判断

通过阅读string类的API文档后,很容易的发现他有contains这样一个方法,可以在设置邮箱时,先将邮箱放在一个零时变量str中

if (str.contains("@") && str.contains(".com")) {

  // 复制给对象(当然这个检测比较粗糙)

  // 可以将字符串从后往前遍历检测".com"的存在

} else {
  //  做出提示
}

2.对passwd的宽度,合法性进行限制

可以用str.size() 来判断

太短了不安全,太长了占用空间则会比较大,应该结合实际的案例选择一个合适的区间

至于合法性,可以用ascii码判断是否为数字,大小写字母或者为"_"之类的限定符号

如果不是则给予用户警告

 

上一篇:安装Tensorflow windows10


下一篇:实验1 类与对象