实验任务4:
程序源码:
complex.hpp
1 #pragma once 2 #include <iostream> 3 #include <cmath> 4 using namespace std; 5 class Complex { 6 public: 7 Complex(double real = 0, double imag = 0) : real(real), imag(imag) {} 8 Complex(Complex& p) : real(p.real), imag(p.imag) {} 9 double get_real() const { 10 return real; 11 } 12 double get_imag() const { 13 return imag; 14 } 15 void show() const; 16 void add(Complex const& m); 17 friend Complex add(Complex const& m, Complex const& n); 18 friend bool is_equal(Complex const& a, Complex const& b); 19 friend double abs(Complex const& c); 20 private: 21 double real; 22 double imag; 23 24 }; 25 void Complex::show() const { 26 if (imag == 0) 27 cout << real << endl; 28 else 29 cout << real << imag << 'i' << endl; 30 } 31 void Complex::add(Complex const& m) { 32 real += m.real; 33 imag += m.imag; 34 } 35 Complex add(Complex const& m, Complex const& n) { 36 Complex res; 37 res.real = m.real + n.real; 38 res.imag = m.imag + n.imag; 39 return res; 40 } 41 bool is_equal(Complex const& a, Complex const& b) { 42 if (a.real == b.real && a.imag == b.imag) 43 return true; 44 else 45 return false; 46 } 47 double abs(Complex const& c) { 48 49 return (sqrt(c.real * c.real + c.imag * c.imag)); 50 51 }
task4.cpp 源码:
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include "Complex.h" 3 #include <iostream> 4 5 void test() { 6 using namespace std; 7 Complex c1(-3, -4); 8 const Complex c2(46); 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, " << "c1 = "; 32 c1.show(); 33 cout << endl; 34 } 35 int main() { 36 test(); 37 }
运行截图:
实验任务5:
user.hpp程序源码
1 #pragma once 2 #include <iostream> 3 #include <string.h> 4 #include <iomanip> 5 using namespace std; 6 7 class User { 8 public: 9 User(string name, string password = "111111", string email = "") : name(name), 10 password(password), email(email) { 11 n++; 12 } 13 void set_email(); 14 void change_passwd(); 15 void print_info(); 16 void print_n(); 17 private: 18 string name, password, email; 19 static int n; 20 }; 21 void User::set_email() { 22 cout << "ENTER YOUR EMAIL" << ":"; 23 cin >> email; 24 25 } 26 void User::change_passwd() { 27 string old_passwd; 28 cout << "ENTER FORMER PASSWD:"; 29 cin >> old_passwd; 30 int i = 2; 31 if (old_passwd != password) { 32 33 while (i) { 34 35 cout << "ENTER AGAIN:"; 36 cin >> old_passwd; 37 if (old_passwd != password) 38 i--; 39 else 40 break; 41 42 } 43 } 44 if (i > 0) 45 { 46 cout << "ENTER NEW_PASSWD:"; 47 cin >> password; 48 49 } 50 51 52 53 } 54 void User::print_info() { 55 cout << "name:" << name << endl; 56 cout << "password:" << setfill('*') << setw(password.size())<< "" << endl; 57 cout << "email:" << email << endl; 58 59 } 60 void User::print_n() { 61 cout << "total" << n << endl; 62 63 } 64 int User::n = 0;
task5.hpp程序源码
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include "User.hpp" 3 #include <iostream> 4 // 测试User类 5 void test() { 6 using std::cout; 7 using std::endl; 8 cout << "testing 1......\n"; 9 User user1("Jonny", "92197", "xyz@hotmail.com"); 10 user1.print_info(); 11 cout << endl 12 << "testing 2......\n\n"; 13 User user2("Leonard"); 14 user2.change_passwd(); 15 user2.set_email(); 16 user2.print_info(); 17 cout << endl; 18 user2.print_n(); 19 } 20 int main() { 21 test(); 22 }
程序运行截图
标签:const,cout,对象,void,Complex,实验,include,imag From: https://www.cnblogs.com/abcdefg-gaoyuan/p/16795985.html