1、实验任务3
1 #ifndef COMPLEX_HPP
2 #define COMPLEX_HPP
3 #include <iostream>
4 #include <iomanip>
5 #include <string>
6 #include <math.h>
7 using namespace std;
8 class Complex
9 {
10 public:
11 Complex(){
12 };
13 Complex(double r,double i);
14 Complex(double r);
15 Complex(const Complex &c);
16 ~Complex(){};
17 double get_real() const;
18 double get_imag() const;
19 void show() const;
20 void add(const Complex &c);
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 &c);
24 private:
25 double real;
26 double imag;
27 };
28 Complex::Complex(double r,double i):real(r),imag(i){}
29 Complex::Complex(double r):real(r),imag(0.0){}
30 Complex::Complex(const Complex &c):real(c.real),imag(c.imag){}
31 double Complex::get_real() const{
32 return real;
33 }
34 double Complex::get_imag() const{
35 return imag;
36 }
37 void Complex::show() const{
38 if(imag>0)
39 cout<<real<<"+"<<imag<<"i";
40 else if(imag==0)
41 cout<<real;
42 else
43 cout<<real<<imag<<"i";
44 }
45 void Complex::add(const Complex &c){
46 real+=c.real;
47 imag+=c.imag;
48 }
49 Complex add(const Complex &c1,const Complex &c2){
50 Complex c;
51 c.imag=c1.imag+c2.imag;
52 c.real=c1.real+c2.real;
53 return c;
54 }
55 bool is_equal(const Complex &c1,const Complex &c2){
56 if (c1.imag==c2.imag)
57 return true;
58 else
59 return false;
60 }
61 double abs(const Complex &c){
62 return sqrt(c.imag*c.imag+c.real*c.real);
63 }
64 #endif
1 #include "Complex.hpp"
2 #include <iostream>
3
4 int main()
5 {
6 using namespace std;
7
8 Complex c1(6, -8);
9 const Complex c2(5.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 }
![]()
2、实验任务4
1 #ifndef USER_HPP
2 #define USER_HPP
3 #include <iostream>
4 #include <iomanip>
5 #include <string>
6 #include <cstring>
7 using namespace std;
8 class User
9 {
10 public:
11 User(string n);
12 User(string n,string p,string e);
13 ~User(){};
14 void set_email();
15 void change_passwd ();
16 void print_info();
17 static void print_n();
18 private:
19 string name;
20 string passwd;
21 string email;
22 static int num;
23 };
24 int User::num=0;
25 User::User(string n):name(n),passwd("111111"),email(" "){
26 ++num;
27 }
28 User::User(string n,string p,string e):name(n),passwd(p),email(e){
29 ++num;
30 }
31 void User::set_email(){
32 string input;
33 cout<<"Enter email address:";
34 cin>>input;
35 email=input;
36 cout<<"email is set successfully..."<<endl;
37 }
38 void User::change_passwd() {
39 cout<<"Enter old password:";
40 string oldpsw;
41 cin>>oldpsw;
42 if(strcmp(oldpsw.c_str(),passwd.c_str())==0)//第一次
43 {
44 cout<<"Enter new password:";
45 string newpsw;
46 cin>>newpsw;
47 passwd=newpsw;
48 cout<<"new passwd is set successfully..."<<endl;
49 }
50 else
51 {
52 cout<<"password input error. Please re-enter again:";
53 string sec;
54 cin>>sec;
55 if(strcmp(sec.c_str(),passwd.c_str())==0)//第二次
56 {
57 cout<<"Enter new password:";
58 string newpsw;
59 cin>>newpsw;
60 passwd=newpsw;
61 cout<<"new passwd is set successfully..."<<endl;
62 }
63 else
64 {
65 cout<<"password input error. Please re-enter again:";
66 string thir;
67 cin>>thir;
68 if(strcmp(thir.c_str(),passwd.c_str())==0)//第三次
69 {
70 string newpsw;
71 cin>>newpsw;
72 passwd=newpsw;
73 cout<<"new passwd is set successfully..."<<endl;
74 }
75 else
76 {
77 cout<<"password input error. Please try for a while." <<endl;
78 }
79 }
80 }
81 }
82 void User::print_info(){
83 cout<<"name:\t"<<name<<endl;
84 cout<<"passwd:\t"<<"******"<<endl;
85 cout<<"email:\t"<<email<<endl;
86 }
87 void User::print_n(){
88
89 if(num==1){
90 cout<<"there is 1 user."<<endl;
91 }
92 else{
93 cout<<"there are "<<num<<" users."<<endl;
94 }
95 }
96 #endif
1 #include "User.hpp"
2 #include <iostream>
3
4 int main()
5 {
6 using namespace std;
7
8 cout << "testing 1......" << endl;
9 User user1("Jane", "113030", "zrh@nuist.com");
10 user1.print_info();
11
12 cout << endl
13 << "testing 2......" << endl
14 << endl;
15 User user2("Author");
16 user2.change_passwd();
17 user2.set_email();
18 user2.print_info();
19
20 User::print_n();
21 }
![]()
![]()
3、实验总结
- 在编写代码的时候遇到了一些细节方面的问题,现记录如下:
- 在task4中,验证用户旧密码是否输入正确时,使用到了strcmp函数(头文件<string>),但是忘记了返回值代表的意义。并不是返回ture代表两个字符串匹配,返回false代表两个字符串不匹配。而是:
| return value |
indicates |
| <0 |
the first character that does not match has a lower value in ptr1 than in ptr2 |
| 0 |
the contents of both strings are equal |
| >0 |
the first character that does not match has a greater value in ptr1 than in ptr2 |
- 在task4中,用于输出类的属性(static int n)的函数print_n(),在声明的时候必须也加上static关键字(后面定义的时候就不用了),因为静态成员只能由静态函数访问!!!(ps.如果静态函数想访问非静态成员的话可以通过引用对象来实现)