实验2 类和对象

实验任务3
代码结构
    task3
     ├─  Complex.hpp
     └─  main.cpp
程序源码
    Complex.hpp

 1 #include <iostream>
 2 #include<cmath>
 3 
 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 add(const Complex &c) {
13             real += c.get_real();
14             imag += c.get_imag();
15         }
16         void show() {
17             std::cout << real;
18             if (imag > 0) std::cout << " + " << abs(imag) << 'i';
19             if (imag < 0) std::cout << " - " << fabs(imag) << 'i';
20         }
21         void show() const {
22             std::cout << real;
23             if (imag > 0) std::cout << " + " << abs(imag) << 'i';
24             if (imag < 0) std::cout << " - " << fabs(imag) << 'i';
25         }
26     public:
27         friend Complex add(const Complex&, const Complex&);
28         friend bool is_equal(const Complex&, const Complex&);
29         friend double abs(const Complex&);
30 };
31 Complex add(const Complex &c1, const Complex &c2) {
32     double r, i;
33     r = c1.get_real() + c2.get_real();
34     i = c1.get_imag() + c2.get_imag();
35     Complex c(r, i);
36     return c;
37 }
38 bool is_equal(const Complex &c1, const Complex &c2) {
39     bool r, i;
40     r = c1.get_real() == c2.get_real();
41     i = c1.get_imag() == c2.get_imag();
42     return r && i;
43 }
44 double abs(const Complex &c) {
45     return sqrt(pow(c.get_real(), 2) + pow(c.get_imag(), 2));
46 }
View Code

    task3.cpp

 1 #include <iostream>
 2 #include <cmath>
 3 #include "Complex.hpp"
 4 
 5 // 复数类Complex: 测试
 6 void test() {
 7     using namespace std;
 8     Complex c1(3, -4);
 9     const Complex c2(4.5);
10     Complex c3(c1);
11     cout << "c1 = ";
12     c1.show();
13     cout << endl;
14     cout << "c2 = ";
15     c2.show();
16     cout << endl;
17     cout << "c2.imag = " << c2.get_imag() << endl;
18     cout << "c3 = ";
19     c3.show();
20     cout << endl;
21     cout << "abs(c1) = ";
22     cout << abs(c1) << endl;
23     cout << boolalpha;
24     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
25     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
26     Complex c4;
27     c4 = add(c1, c2);
28     cout << "c4 = c1 + c2 = ";
29     c4.show();
30     cout << endl;
31     c1.add(c2);
32     cout << "c1 += c2, " << "c1 = ";
33     c1.show();
34     cout << endl;
35 }
36 
37 int main() {
38     test();
39 }
View Code

运行测试结果


实验任务4
程序源码
    User.hpp

 1 #include <iostream>
 2 #include <string>
 3 #include <limits>
 4 class User {
 5     private:
 6         static int n;
 7     public:
 8         static void print_n();
 9     private:
10         std::string name, password, email;
11     public:
12         User(
13             std::string nm,
14             std::string p = "111111",
15             std::string e = "")
16             : name{nm}, password{p}, email{e}
17             {n += 1;};
18         void set_email();
19         void change_passwd();
20         void print_info();
21 };
22 int User::n = 0;
23 
24 void User::print_n() {
25     std::cout << "there are " << n << " users.\n";
26 };
27 
28 void User::set_email() {
29     using namespace std;
30     cout << "Enter emaill address: ";
31     string e;
32     cin >> e;
33     cin.ignore((std::numeric_limits<streamsize>::max)(),'\n');
34     cout << "emaill is set successfully...\n";
35     email = e;
36 };
37 
38 void User::change_passwd() {
39     using namespace std;
40     string o, n;
41     for (int i = 0; i <= 3; i++) {
42         if (i == 3) {
43             cout << "Please try after a while. \n";
44             break;
45         }
46         if (i == 0)
47             cout << "Enter old password: ";
48         else
49             cout << "Please re-enter again: ";
50         cin >> o;
51         cin.ignore((std::numeric_limits<streamsize>::max)(),'\n');
52         if (o != password) {
53             cout << "password input error. ";
54             continue;
55         } else {
56             cout << "Enter new passwd: ";
57             cin >> n;
58             cin.ignore((std::numeric_limits<streamsize>::max)(),'\n');
59             password = n;
60             cout << "new passwd is set successfully...\n";
61             break;
62         };
63     };
64 };
65 
66 void User::print_info() {
67     using namespace std;
68     cout << "name: \t" << name << endl;
69     string s(password.size(),'*');
70     cout << "password: \t" << s << endl;
71     cout << "email: \t" << email << endl;
72 };
View Code

    task4.cpp

 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("Alice", "pw123456", "abc@gmail.com");
 9     user1.print_info();
10     cout << endl
11          << "testing 2......\n\n";
12     User user2("Bob");
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 }
View Code

运行测试结果

 
实验任务5
程序源码
account.h

 1 //account.h
 2 #ifndef __ACCOUNT_H__
 3 #define __ACCOUNT_H__
 4 class SavingsAccount {            //储蓄账户类
 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     public:
19         //构造函数
20         SavingsAccount(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 amount);        //取出现金
27         //结算利息,每年1月1日调用一次该函数
28         void settle(int date);
29         //显示账户信息
30         void show() const;
31 };
32 #endif //__ACCOUNT _H__
View Code

account.cpp

 1 //account.cpp
 2 #include "account.h"
 3 #include <cmath>
 4 #include <iostream>
 5 using namespace std;
 6 
 7 double SavingsAccount::total = 0;
 8 //SavingsAccount类相关成员函数的实现
 9 SavingsAccount::SavingsAccount(int date, int id, double rate)
10     : id(id), balance(0), rate(rate), lastDate (date), accumulation(0) {
11     cout << date << "\t#" << id << " is created" << endl;
12 }
13 void SavingsAccount::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 void SavingsAccount::deposit(int date, double amount) {
22     record(date, amount);
23 }
24 void SavingsAccount::withdraw(int date, double amount) {
25     if ( amount > getBalance())
26         cout << "Error: not enough money" << endl;
27     else
28         record(date, - amount);
29 }
30 void SavingsAccount::settle(int date) {
31     double interest = accumulate(date) * rate / 365;
32 //计算年息
33     if (interest != 0)
34         record(date, interest);
35     accumulation = 0;
36 }
37 void SavingsAccount::show() const {
38     cout << "#" << id << "\tBalance: " << balance;
39 }
View Code

5_11.cpp

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

运行测试结果

posted @ 2023-10-17 22:58  E5rgeXcJV1x5J  阅读(74)  评论(0)    收藏  举报