实验任务4
h.pp
1 #pragma once 2 #include<iostream> 3 #include<cmath> 4 using std::cout; 5 using std::endl; 6 7 class Complex { 8 private: 9 double real; 10 double imag; 11 12 13 public: 14 Complex(double m = 0) :real{ m }, imag{ 0 } {} 15 Complex(double m, double n) :real{ m }, imag{ n } {} 16 Complex(const Complex& obj) { real = obj.real; imag = obj.imag; } 17 double get_real() const { return real; } 18 double get_imag() const { return imag; } 19 void show() { 20 if (imag > 0 && imag != 1) cout << real << "+" << imag << "i"; 21 else if (imag < 0 && imag != -1) cout << real << imag << "i"; 22 else if (imag == 1) cout << real << "+" << "i"; 23 else if (imag == -1) cout << real << "-" << "i"; 24 else if (imag == 0)cout << real; 25 } 26 void show() const { 27 if (imag > 0 && imag != 1) cout << real << "+" << imag << "i"; 28 else if (imag < 0 && imag != -1) cout << real << imag << "i"; 29 else if (imag == 1) cout << real << "+" << "i"; 30 else if (imag == -1) cout << real << "-" << "i"; 31 else if (imag == 0)cout << real; 32 } 33 void add(const Complex& obj) { real += obj.real; imag += obj.imag; } 34 friend Complex add(const Complex& c1, const Complex& c2); 35 friend bool is_equal(const Complex &c1,const Complex &c2); 36 friend double abs(const Complex& c1); 37 }; 38 Complex add(const Complex& c1, const Complex& c2) { 39 Complex c3; 40 c3.real = c1.real + c2.real; 41 c3.imag = c1.imag + c2.imag; 42 return c3; 43 } 44 bool is_equal(const Complex& c1, const Complex& c2){ 45 bool a1 = false; 46 if (c1.real == c2.real) 47 if (c1.imag == c2.imag) 48 a1 = true; 49 return a1; 50 } 51 double abs(const Complex& c1) { 52 double s = 0.0; 53 s = sqrt(c1.real * c1.real + c1.imag * c1.imag); 54 return s; 55 }
.cpp
1 #include"Complex.hpp" 2 #include<iostream> 3 //类测试 4 void test() { 5 using namespace std; 6 7 Complex c1(3, -4); 8 const Complex c2(4.5); 9 Complex c3(c1); 10 11 cout << "c1="; 12 c1.show(); 13 cout << endl; 14 15 cout << "c2="; 16 c2.show(); 17 cout << endl; 18 cout << "c2.imag=" << c2.get_imag() << endl; 19 cout << "c3="; 20 21 c3.show(); 22 cout << endl; 23 24 cout << "abs(c1)="; 25 cout << abs(c1) << endl; 26 27 cout << boolalpha; 28 cout << "c1==c3:" << is_equal(c1, c3) << endl; 29 cout << "c1==c2:" << is_equal(c1, c2) << endl; 30 31 Complex c4; 32 c4 = add(c1, c2); 33 cout << "c4=c1+c2="; 34 c4.show(); 35 cout << endl; 36 37 c1.add(c2); 38 cout << "c1+=c2," << "c1="; 39 c1.show(); 40 cout << endl; 41 } 42 43 int main() 44 { 45 test(); 46 }
实验任务5
.hpp
#pragma once #include<iostream> //#include<iomanip> using std::cout; using std::cin; using std::endl; using std::string; class User { private: string name; string passwd; string email; public: static int n; static void print_n() { cout <<"there are " <<n<<" users"; } User(string a, string b = "111111", string c ={}) :name{a}, passwd{b}, email{c} { ++n; } void set_email() { cout << "Enter email address:"; cin >> email; cout << "email is set successfully..."<<endl; } void change_passwd() { string c; cout << "Enter old password:"; cin >> c; for(auto i=1;i<=3;i++) { if (c != passwd&&i!=3) { cout << "password input error.Please re-enter again:"; cin >> c; } if (c != passwd && i == 3) cout << "password input error.Please try after a while."<<endl; if (c == passwd) { cout << "Enter new passwd:"; cin >> passwd; cout << "new passwd is settd successfully..." << endl; break; } } } void print_info() { cout << "name: "<< name << endl; cout << "passwd: "; for (auto i = 1; i <= passwd.size(); i++) cout << "*"; cout << endl; cout << "email: " << email << endl; } }; int User::n = 0;
.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", "[email protected]"); 11 user1.print_info(); 12 13 cout << endl 14 << "testing 2......\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 }
标签:std,cout,对象,imag,Complex,实验,using,include From: https://www.cnblogs.com/aomijia/p/16804489.html