实验任务三
Complex.hpp
1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 class Complex{ 5 private: 6 double real, imag; 7 public: 8 Complex(double r = 0, double i = 0):real(r), imag(i){} 9 Complex(const Complex & c){real = c.real; imag = c.imag;} 10 double get_real()const{return real;} 11 double get_imag()const{return imag;} 12 void show()const; 13 void add(const Complex &c); 14 friend Complex add(const Complex &c1, const Complex &c2); 15 friend bool is_equal(const Complex &c1, const Complex &c2); 16 friend double abs(const Complex &c); 17 }; 18 19 void Complex::show()const{ 20 if(imag > 0) 21 cout << real << " + " <<imag << "i"; 22 else if(imag < 0) 23 cout << real <<" - " << -1*imag << "i"; 24 else 25 cout << real; 26 } 27 void Complex::add(const Complex &c){ 28 real += c.real; 29 imag += c.imag; 30 } 31 Complex add(const Complex &c1, const Complex &c2){ 32 Complex c; 33 c.real = c1.real + c2.real; 34 c.imag = c1.imag + c2.imag; 35 return c; 36 } 37 bool is_equal(const Complex &c1, const Complex &c2){ 38 if(c1.real==c2.real && c1.imag==c2.imag) 39 return true; 40 return false; 41 } 42 double abs(const Complex &c){ 43 double s = sqrt(c.real * c.real + c.imag * c.imag); 44 return s; 45 }
task3.cpp
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(-9, 2); const Complex c2(6.3); Complex c3(c1); cout << "c1 = "; c1.show(); cout << endl; cout << "c2 = "; c2.show(); cout << endl; cout << "c2.imag = " << c2.get_imag() << endl; cout << "c3 = "; c3.show(); cout << endl; cout << "abs(c1) = "; cout << abs(c1) << endl; cout << boolalpha; cout << "c1 == c3 : " << is_equal(c1, c3) << endl; cout << "c1 == c2 : " << is_equal(c1, c2) << endl; Complex c4; c4 = add(c1, c2); cout << "c4 = c1 + c2 = "; c4.show(); cout << endl; c1.add(c2); cout << "c1 += c2, " << "c1 = "; c1.show(); cout << endl; }
实验结果
实验任务4
User.hpp
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 class User{ 6 private: 7 string name; 8 string passwd; 9 string email; 10 static int n; 11 public: 12 User(string name0, string passwd0 = "111111", string email0 = "