实验二
任务三源代码:

1 #include "Complex.h" 2 #include<iostream> 3 #include<cmath> 4 5 Complex::Complex(double real, double imag): real{real}, imag{imag} {} 6 Complex::Complex(const Complex &c): real{c.real}, imag{c.imag} {} 7 Complex::~Complex() {} 8 9 double Complex::get_real() const { 10 return real; 11 } 12 13 double Complex::get_imag() const { 14 return imag; 15 } 16 17 void Complex::show() const { 18 using namespace std; 19 20 if(imag == 0) { 21 cout << real << endl; 22 } 23 else if(imag > 0) { 24 cout << real << "+" << imag << "i" << endl; 25 } 26 else if(imag < 0) { 27 cout << real << imag << "i" << endl; 28 } 29 } 30 31 void Complex::add(const Complex &c) { 32 real += c.real; 33 imag += c.imag; 34 } 35 36 Complex add(const Complex &c1, const Complex &c2) { 37 return Complex(c1.real + c2.real, c1.imag + c2.imag); 38 } 39 40 bool is_equal(const Complex &c1, const Complex &c2) { 41 if(c1.real == c2.real && c1.imag == c2. imag) { 42 return true; 43 } 44 else { 45 return false; 46 } 47 } 48 49 double abs(const Complex &c) { 50 return sqrt(c.real * c.real + c.imag * c.imag); 51 }

1 #ifndef Complex_H 2 3 #include<iostream> 4 #include<cmath> 5 6 class Complex{ 7 public: 8 Complex(double real = 0.0, double imag = 0.0); 9 Complex(const Complex &c); 10 ~Complex(); 11 12 double get_real() const; 13 double get_imag() const; 14 15 void add(const Complex &c); 16 void show() const; 17 18 friend Complex add(const Complex &c1, const Complex &c2); 19 friend bool is_equal(const Complex &c1, const Complex &c2); 20 friend double abs(const Complex &c); 21 22 private: 23 double real, imag; 24 }; 25 26 #endif
任务三运行结果:
任务四源代码:

1 #pragma once 2 3 #include<iostream> 4 #include<string> 5 6 using namespace std; 7 8 class User { 9 public: 10 User(string name, string password = "111111", string email = ""); 11 12 void set_email(); 13 void change_password(); 14 void print_info(); 15 16 static void display_count(); // 类方法,显示当前User类对象数目 17 18 private: 19 static int count; // 类属性,用于描述当前User类对象数目 20 21 private: 22 string name, password, email; 23 }; 24 25 int User::count = 0; 26 27 User::User(string name, string password, string email): name{name}, password{password}, email{email} { 28 ++count; 29 } 30 31 void User::set_email() { 32 cout << "设置邮箱:" << endl; 33 cin >> email; 34 cout << "邮箱设置成功!" << endl; 35 } 36 37 void User::change_password() { 38 string newpassword; 39 40 for(int i = 1; i <= 3; i++) { 41 cout << "请输入旧密码:"; 42 cin >> newpassword; 43 if(newpassword == password) { 44 cout << "请输入新密码:"; 45 cin >> password; 46 cout << "新密码设置成功!"; 47 break; 48 } 49 else { 50 cout << "密码输入错误,请重新输入!"; 51 } 52 53 if(i=4) { 54 cout << "连续三次输入错误,请稍后再试!"; 55 } 56 } 57 cout << endl; 58 } 59 60 void User::print_info() { 61 string s(password.size(), '*'); 62 cout << "用户名:" << name << endl; 63 cout << "密码:" << s << endl; 64 cout << "邮箱:" << email << endl; 65 } 66 67 void User::display_count() { 68 cout << "用户总数:" << count << endl; 69 }

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", "xyz@hotmail.com"); 11 user1.print_info(); 12 13 cout << endl << "testing 2......\n\n"; 14 15 User user2("Leonard"); 16 user2.change_password(); 17 user2.set_email(); 18 user2.print_info(); 19 cout << endl; 20 User::display_count(); 21 } 22 23 int main() { 24 test(); 25 }
任务四运行结果:
任务五源代码:

1 #include "account.h" 2 #include<iostream> 3 4 using namespace std; 5 6 int main() { 7 // 建立几个账户 8 SavingAccount sa0(1, 21325302, 0.015); 9 SavingAccount sa1(1, 58320212, 0.015); 10 // 几笔账目 11 sa0.deposit(5, 5000); 12 sa1.deposit(25, 10000); 13 sa0.deposit(45, 5500); 14 sa1.withdraw(60, 4000); 15 // 开户后第90天到了银行的计息日,结算所有账户的年息 16 sa0.settle(90); 17 sa1.settle(90); 18 // 输出各个账户信息 19 sa0.show(); 20 cout << endl; 21 sa1.show(); 22 cout << endl; 23 cout << "Total: " << SavingAccount::getTotal() << endl; 24 return 0; 25 }

1 #include<cmath> 2 #include<iostream> 3 #include "account.h" 4 5 using namespace std; 6 7 double SavingAccount::total = 0; 8 // SavingAccount类相关成员函数的实现 9 SavingAccount::SavingAccount(int date, int id, double rate): id{id}, balance{0}, rate{rate}, lastDate{date}, accumulation{0} { 10 cout << date << "\t#" << id << "is created" <<endl; 11 } 12 13 void SavingAccount::record(int date, double amount) { 14 accumulation = accumulate(date); 15 lastDate = date; 16 amount = floor(amount * 100 + 0.5) / 100; // 保留小数点后两位 17 balance += amount; 18 total += amount; 19 cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 20 } 21 22 void SavingAccount::deposit(int date, double amount) { 23 record(date, amount); 24 } 25 26 void SavingAccount::withdraw(int date, double amount) { 27 if(amount>getBalance()) 28 cout << "Error: not enough money" << endl; 29 else 30 record(date, -amount); 31 } 32 33 void SavingAccount::settle(int date) { 34 double interest = accumulate(date) * rate / 365; // 计算年息 35 if(interest != 0) 36 record(date, interest); 37 accumulation = 0; 38 } 39 40 void SavingAccount::show() const { 41 cout << "#" << id << "\tBalance" << balance; 42 }

1 // account.h 2 #ifndef __ACCOUNT_H__ 3 #define __ACCOUNT_H__ 4 class SavingAccount { // 储蓄账户类 5 private: 6 int id; // 账号 7 double balance; // 余额 8 double rate; // 存款的年利率 9 int lastDate; // 上次变更余额的时期 10 double accumulation; // 余额按日累加之和 11 static double total; // 所有账户的总金额 12 // 记录一笔账,date为日期,amount为金额,desc为说明 13 void record(int date, double amount); 14 // 获得到指定日期为止的存款金额按日累计值 15 double accumulate(int date) const { 16 return accumulation + balance * (date - lastDate); 17 } 18 19 public: 20 SavingAccount(int date, int id, double rate); 21 int getId() const {return id;} 22 double getBalance() const {return balance;} 23 double getRate() const {return rate;} 24 static double getTotal() {return total;} 25 void deposit(int date, double amount); // 存入现金 26 void withdraw(int date, double amounr); // 取出现金 27 // 结算利息,每年1月1日调用一次该函数 28 void settle(int date); 29 // 显示账户信息 30 void show() const; 31 }; 32 #endif // __ACCOUNT_H__
任务五运行结果: