實驗任務三
// Complex.hpp
1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 5 class Complex 6 { 7 public: 8 Complex(double r = 0, double m = 0) : real(r), imag(m) {} ; 9 Complex(Complex &c) : real(c.real), imag(c.imag) {} ; 10 double get_real() const 11 { 12 return real; 13 } 14 double get_imag() const 15 { 16 return imag; 17 } 18 void show() const 19 { 20 cout<<real<<" + "<<imag<<"i"<<endl; 21 } 22 void add(Complex const &c) 23 { 24 real += c.real; 25 imag += c.imag; 26 } 27 friend Complex add(Complex const &c1, Complex const &c2); 28 friend bool is_equal(Complex const &c1, Complex const &c2); 29 friend double abs(Complex const &c); 30 private: 31 double real; 32 double imag; 33 }; 34 35 Complex add(Complex const &c1, Complex const &c2) 36 { 37 Complex c; 38 c.real = c1.real + c2.real; 39 c.imag = c1.imag + c2.imag; 40 return c; 41 } 42 43 bool is_equal(Complex const &c1, Complex const &c2) 44 { 45 if(c1.real == c2.real && c1.imag == c2.imag) 46 return true; 47 return false; 48 } 49 50 double abs(Complex const &c) 51 { 52 return sqrt(c.real * c.real + c.imag *c.imag); 53 }
// task3.cpp
1 #include "Complex.hpp" 2 #include <iostream> 3 4 int main() 5 { 6 using namespace std; 7 8 Complex c1(2, -4); 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(c3) = "; 26 cout << abs(c3) << 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 }
// 實驗任務四
User.hpp
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 class User 6 { 7 public: 8 User(string a, string p = "111111", string e = "") : name(a), password(p), email(e) { n++; } 9 User(User &u) : name(u.name), password(u.password), email(u.email) { n++; } 10 void set_email() 11 { 12 cout<<"Enter email address:"; 13 cin>>email; 14 while(email.find('@') == -1) 15 { 16 cout<<"The @ should be included. Plesae input again:"; 17 cin>>email; 18 } 19 cout<<"email is set sucessfully..."<<endl; 20 } 21 void change_passwd(); 22 void print_info() 23 { 24 cout<<"name: "<<name<<endl; 25 cout<<"passwd:******"<<endl; 26 cout<<"email :"<<email<<endl; 27 } 28 static void print_n() 29 { 30 cout<<"there are "<<n<<" users."<<endl; 31 } 32 ~User() { n--; } 33 private: 34 string name; 35 string password; 36 string email; 37 static int n; 38 }; 39 40 int User::n = 0; 41 42 void User::change_passwd() 43 { 44 int i = 0; 45 string temp; 46 while(i != 3) 47 { 48 if(i == 0) 49 cout<<"Enter old password:"; 50 else 51 cout<<"Please re_enter again:"; 52 cin>>temp; 53 if(temp == password) 54 { 55 cout<<"Enter new password:"; 56 cin>>password; 57 while(password.length() != 6) 58 { 59 cout<<"The length of the new password should be six."; 60 cout<<"Please input again:"; 61 cin>>password; 62 } 63 cout<<"new password is set sucessfully..."<<endl; 64 return; 65 } 66 else 67 { 68 cout<<"password input error."; 69 i++; 70 } 71 } 72 if(i == 3) 73 cout<<"Please try after a while."<<endl; 74 }
//task4.cpp
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("Jonny", "273274", "xyz@hotmail.com"); 10 user1.print_info(); 11 cout << endl 12 << "testing 2......" << endl 13 << endl; 14 User user2("Leonard"); 15 user2.change_passwd(); 16 user2.set_email(); 17 user2.print_info(); 18 19 User::print_n(); 20 }