任务三:
1 #ifndef T_HPP 2 #define T_HPP 3 4 #include <iostream> 5 #include <cmath> 6 7 class Complex { 8 private: 9 double real; // 实部 10 double imag; // 虚部 11 public: 12 Complex(); 13 Complex(double r); 14 Complex(double r, double i); 15 Complex(const Complex& c); 16 double get_imag() const; 17 void show() const; 18 friend double abs(const Complex& c); 19 friend bool is_equal(const Complex& c1, const Complex& c2); 20 friend Complex add(const Complex& c1, const Complex& c2); 21 void add(const Complex& c); 22 Complex& operator+=(const Complex& c); 23 }; 24 25 #endif
1 #include "t.hpp" 2 3 Complex::Complex() : real(0), imag(0) {} 4 5 Complex::Complex(double r) : real(r), imag(0) {} 6 7 Complex::Complex(double r, double i) : real(r), imag(i) {} 8 9 Complex::Complex(const Complex& c) : real(c.real), imag(c.imag) {} 10 11 double Complex::get_imag() const { 12 return imag; 13 } 14 15 void Complex::show() const { 16 std::cout << real; 17 if (imag >= 0) { 18 std::cout << " + " << imag << "i"; 19 } else { 20 std::cout << " - " << -imag << "i"; 21 } 22 } 23 24 double abs(const Complex& c) { 25 return std::sqrt(c.real * c.real + c.imag * c.imag); 26 } 27 28 bool is_equal(const Complex& c1, const Complex& c2) { 29 return c1.real == c2.real && c1.imag == c2.imag; 30 } 31 32 Complex add(const Complex& c1, const Complex& c2) { 33 Complex result; 34 result.real = c1.real + c2.real; 35 result.imag = c1.imag + c2.imag; 36 return result; 37 } 38 39 void Complex::add(const Complex& c) { 40 real += c.real; 41 imag += c.imag; 42 } 43 44 Complex& Complex::operator+=(const Complex& c) { 45 add(c); 46 return *this; 47 }
1 #include "t.hpp" 2 3 void test() { 4 Complex c1(3, -4); 5 const Complex c2(4.5); 6 Complex c3(c1); 7 std::cout << "c1 = "; 8 c1.show(); 9 std::cout << std::endl; 10 std::cout << "c2 = "; 11 c2.show(); 12 std::cout << std::endl; 13 std::cout << "c2.imag = " << c2.get_imag() << std::endl; 14 std::cout << "c3 = "; 15 c3.show(); 16 std::cout << std::endl; 17 std::cout << "abs(c1) = "; 18 std::cout << abs(c1) << std::endl; 19 std::cout << std::boolalpha; 20 std::cout << "c1 == c3 : " << is_equal(c1, c3) << std::endl; 21 std::cout << "c1 == c2 : " << is_equal(c1, c2) << std::endl; 22 Complex c4; 23 c4 = add(c1, c2); 24 std::cout << "c4 = c1 + c2 = "; 25 c4.show(); 26 std::cout << std::endl; 27 c1 += c2; 28 std::cout << "c1 += c2, " << "c1 = "; 29 c1.show(); 30 std::cout << std::endl; 31 } 32 33 int main() { 34 test(); 35 return 0; 36 }
运行测试截图:
任务四:
程序源码:
1 #include <iostream> 2 #include <string> 3 4 class User { 5 private: 6 std::string name; 7 std::string passwd; 8 std::string email; 9 static int n; 10 11 public: 12 User(const std::string& name, const std::string& passwd, const std::string& email) : 13 name(name), passwd(passwd), email(email) { 14 ++n; 15 } 16 17 User(const std::string& name) : name(name) { 18 ++n; 19 } 20 21 void set_email() { 22 std::string new_email; 23 std::cout << "Enter email address: "; 24 std::cin >> new_email; 25 std::cout << "Email is set successfully..." << std::endl; 26 email = new_email; 27 } 28 29 void change_passwd() { 30 std::string old_passwd; 31 std::string new_passwd1; 32 33 int try_count = 0; 34 while (try_count < 3) { 35 std::cout << "Enter old password: "; 36 std::cin >> old_passwd; 37 if (old_passwd == passwd) { 38 break; 39 } else { 40 std::cout << "Password input error. Please re-enter again: " << std::endl; 41 ++try_count; 42 } 43 } 44 if (try_count == 3) { 45 std::cout << "Password input error. Please try after a while." << std::endl; 46 return; 47 } 48 while (true) { 49 std::cout << "Enter new password: "; 50 std::cin >> new_passwd1; 51 std::cout << "new passwd is set successfully..." << std::endl; 52 passwd = new_passwd1; 53 break; 54 } 55 } 56 57 void print_info() const { 58 std::cout << "name: " << name << std::endl; 59 std::cout << "passwd: "; 60 for (size_t i = 0; i < passwd.size(); ++i) { 61 std::cout << "*"; 62 } 63 std::cout << std::endl; 64 std::cout << "email: " << email << std::endl; 65 } 66 67 static void print_n() { 68 std::cout << "there are " << n << " users." << std::endl; 69 } 70 }; 71 72 int User::n = 0;
1 #include "User.hpp" 2 #include <iostream> 3 // 测试User类 4 void test() { 5 using std::cout; 6 using std::endl; 7 cout << "testing 1......\n"; 8 User user1("Jonny", "92197", "xyz@hotmail.com"); 9 user1.print_info(); 10 cout << endl 11 << "testing 2......\n\n"; 12 User user2("Leonard"); 13 user2.change_passwd(); 14 user2.set_email(); 15 user2.print_info(); 16 cout << endl; 17 User::print_n(); 18 } 19 int main() { 20 test(); 21 }
运行测试结果截图:
任务5:
程序源码:
1 #ifndef __ACCOUNT_H__ 2 #define __ACCOUNT_H__ 3 class SavingsAccount {//储蓄账户类 4 private: 5 int id; 6 double balance; 7 double rate; 8 int lastDate; 9 double accumulation; 10 static double total; 11 void record(int date, double amount); 12 double accumulate(int date) const { 13 return accumulation+balance*(date-lastDate); 14 } 15 public: 16 SavingsAccount(int date, int id, double rate); 17 int getId() const {return id;} 18 double getBalance() const {return balance;} 19 double getRate() const {return rate;} 20 static double getTotal() {return total;} 21 void deposit(int date, double amount); 22 void withdraw(int date, double amount); 23 void settle(int date); 24 void show() const; 25 }; 26 #endif
1 #include"account.h" 2 #include<cmath> 3 #include<iostream> 4 using namespace std; 5 6 double SavingsAccount::total = 0; 7 SavingsAccount::SavingsAccount(int date, int id, double rate) 8 :id(id),balance(0),rate(rate),lastDate(date),accumulation(0){ 9 cout << date << "\t#" << id << " is created" << endl; 10 } 11 void SavingsAccount::record(int date, double amount) { 12 accumulation=accumulate(date); 13 lastDate = date; 14 amount = floor(amount * 100 + 0.5) / 100; 15 balance += amount; 16 total += amount; 17 cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 18 } 19 void SavingsAccount::deposit(int date, double amount) { 20 record(date, amount); 21 } 22 void SavingsAccount::withdraw(int date, double amount) { 23 if ( amount > getBalance()) 24 cout << "Error: not enough money" << endl; 25 else 26 record(date, - amount); 27 } 28 void SavingsAccount::settle(int date) { 29 double interest = accumulate(date) * rate / 365; 30 if (interest != 0) 31 record(date, interest); 32 accumulation = 0; 33 } 34 void SavingsAccount::show() const { 35 cout << "#" << id << "\tBalance: " << balance; 36 }
1 #include "account.h" 2 #include <iostream> 3 using namespace std; 4 int main() { 5 SavingsAccount sa0(1, 21325302, 0.015); 6 SavingsAccount sa1(1, 58320212, 0.015); 7 sa0.deposit(5, 5000) ; 8 sa1.deposit(25, 10000); 9 sa0.deposit(45, 5500); 10 sa1.withdraw(60, 4000); 11 12 sa0.settle(90); 13 sa1.settle(90); 14 15 sa0.show();cout << endl; 16 sa1.show();cout << endl; 17 cout << "Total:" << SavingsAccount::getTotal() << endl; 18 return 0; 19 }
运行测试结果截图: