实验3
task1:
1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 6 using std::string; 7 using std::cout; 8 9 // 按钮类 10 class Button { 11 public: 12 Button(const string &text); 13 string get_label() const; 14 void click(); 15 16 private: 17 string label; 18 }; 19 20 Button::Button(const string &text): label{text} { 21 } 22 23 inline string Button::get_label() const { 24 return label; 25 } 26 27 void Button::click() { 28 cout << "Button '" << label << "' clicked\n"; 29 } 30 31 #pragma once 32 #include "button.hpp" 33 #include <vector> 34 #include <iostream> 35 36 using std::vector; 37 using std::cout; 38 using std::endl; 39 40 // 窗口类 41 class Window{ 42 public: 43 Window(const string &win_title); 44 void display() const; 45 void close(); 46 void add_button(const string &label); 47 48 private: 49 string title; 50 vector<Button> buttons; 51 }; 52 53 Window::Window(const string &win_title): title{win_title} { 54 buttons.push_back(Button("close")); 55 } 56 57 inline void Window::display() const { 58 string s(40, '*'); 59 60 cout << s << endl; 61 cout << "window title: " << title << endl; 62 cout << "It has " << buttons.size() << " buttons: " << endl; 63 for(const auto &i: buttons) 64 cout << i.get_label() << " button" << endl; 65 cout << s << endl; 66 } 67 68 void Window::close() { 69 cout << "close window '" << title << "'" << endl; 70 buttons.at(0).click(); 71 } 72 73 void Window::add_button(const string &label) { 74 buttons.push_back(Button(label)); 75 } 76 #include "window.hpp" 77 #include <iostream> 78 79 using std::cout; 80 using std::cin; 81 82 void test() { 83 Window w1("new window"); 84 w1.add_button("maximize"); 85 w1.display(); 86 w1.close(); 87 } 88 89 int main() { 90 cout << "用组合类模拟简单GUI:\n"; 91 test(); 92 }
问题1:定义了button类和window类,使用了标准库中的string类和vector类,window类和button类之间存在组合关系
问题2:如果一个成员函数不修改类的成员变量,那么它可以被声明为const
成员函数。如果Button
类和Window
类中的某些函数体较小且调用频繁,可以考虑设置为inline
。但是,过度使用inline
可能会导致代码膨胀,因此需要谨慎使用。
问题3:创建一个包含 40 个星号‘*’的string
对象s
。
task2:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 void output1(const vector<int> &v) { 7 for(auto &i: v) 8 cout << i << ", "; 9 cout << "\b\b \n"; 10 } 11 12 void output2(const vector<vector<int>> v) { 13 for(auto &i: v) { 14 for(auto &j: i) 15 cout << j << ", "; 16 cout << "\b\b \n"; 17 } 18 } 19 20 void test1() { 21 vector<int> v1(5, 42); 22 const vector<int> v2(v1); 23 24 v1.at(0) = -999; 25 cout << "v1: "; output1(v1); 26 cout << "v2: "; output1(v2); 27 cout << "v1.at(0) = " << v1.at(0) << endl; 28 cout << "v2.at(0) = " << v2.at(0) << endl; 29 } 30 31 void test2() { 32 vector<vector<int>> v1{{1, 2, 3}, {4, 5, 6, 7}}; 33 const vector<vector<int>> v2(v1); 34 35 v1.at(0).push_back(-999); 36 cout << "v1: \n"; output2(v1); 37 cout << "v2: \n"; output2(v2); 38 39 vector<int> t1 = v1.at(0); 40 cout << t1.at(t1.size()-1) << endl; 41 42 const vector<int> t2 = v2.at(0); 43 cout << t2.at(t2.size()-1) << endl; 44 } 45 46 int main() { 47 cout << "测试1:\n"; 48 test1(); 49 50 cout << "\n测试2:\n"; 51 test2(); 52 }
问题1:
问题2:
1 #pragma once 2 class Date { 3 private: 4 int year; 5 int month; 6 int day; 7 int totalDays; 8 public: 9 Date(int year, int month, int day); 10 int getYear()const { return year; } 11 int getMonth()const { return month; } 12 int getDay()const { return day; } 13 int getMaxDay()const; 14 bool isLeapYear()const { 15 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 16 } 17 void show() const; 18 int distance(const Date& date)const { 19 return totalDays - date.totalDays; 20 } 21 }; 22 23 24 25 #include"date.h" 26 #include<iostream> 27 #include<cstdlib> 28 using namespace std; 29 namespace { 30 const int DAYS_BEFIRE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304 ,334,365 }; 31 } 32 Date::Date(int year, int month, int day) :year(year), month(month), day(day) { 33 if (day <= 0 || day > getMaxDay()) { 34 cout << "Invalid date: "; 35 show(); 36 cout << endl; 37 exit(1); 38 } 39 int years = year - 1; 40 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFIRE_MONTH[month - 1] + day; 41 if (isLeapYear() && month > 2) totalDays++; 42 } 43 int Date::getMaxDay()const { 44 if (isLeapYear() &&month == 2) 45 return 29; 46 else return DAYS_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1]; 47 } 48 void Date::show()const { 49 cout << getYear() << "-" << getMonth() << "-" << getDay(); 50 } 51 52 53 54 #pragma once 55 #include"date.h" 56 #include<string> 57 using namespace std; 58 class SavingsAccount { 59 private: 60 string id; 61 double balance; 62 double rate; 63 Date lastDate; 64 double accumulation; 65 static double total; 66 void record(const Date& date, double amount, const string& desc); 67 void error(const string& msg) const; 68 double accumulate(const Date& date)const { 69 return accumulation + balance * date.distance(lastDate); 70 } 71 public: 72 SavingsAccount(const Date& date, const string& id, double rate); 73 const string& getId()const { return id; } 74 double getBalance()const { return balance; } 75 double getRate()const { return rate; } 76 static double getTotal() { return total; } 77 void deposit(const Date& date, double amount, const string& desc); 78 void withdraw(const Date& date, double amount, const string& desc); 79 void settle(const Date& date); 80 void show() const; 81 }; 82 83 84 85 #include"account.h" 86 #include<cmath> 87 #include<iostream> 88 using namespace std; 89 double SavingsAccount::total = 0; 90 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) : 91 id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { 92 date.show(); 93 cout << "\t#" << id << "created" << endl; 94 } 95 void SavingsAccount::record(const Date& date, double amount, const string& desc) { 96 accumulation = accumulate(date); 97 lastDate = date; 98 amount = floor(amount * 100 + 0.5) / 100; 99 balance += amount; 100 total += amount; 101 date.show(); 102 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 103 } 104 void SavingsAccount::error(const string& msg) const { 105 cout << "Error(#" << id << "):" << msg << endl; 106 } 107 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 108 record(date, amount, desc); 109 } 110 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 111 if (amount > getBalance()) 112 error("not enough money"); 113 else 114 record(date, -amount, desc); 115 } 116 void SavingsAccount::settle(const Date& date) { 117 double interest = accumulate(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1)); 118 if (interest != 0) record( date,interest,"interest" ); 119 accumulation = 0; 120 } 121 void SavingsAccount::show()const { 122 cout << id << "\tBalance: " << balance; 123 } 124 125 126 #include"account.h" 127 #include<iostream> 128 using namespace std; 129 int main() { 130 Date date{ 2008,11,1 }; 131 SavingsAccount accounts[] = { 132 SavingsAccount(date,"03755217",0.015), 133 SavingsAccount(date,"02342342",0.015) 134 }; 135 const int n = sizeof(accounts) / sizeof(SavingsAccount); 136 accounts[0].deposit(Date(2008, 11, 5), 5000, "salary"); 137 accounts[1].deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 138 accounts[0].deposit(Date(2008, 12, 5), 5500, "salary"); 139 accounts[1].withdraw(Date(2008, 12, 20), 4000, "buy a laptop"); 140 cout << endl; 141 for (int i = 0; i < n; i++) { 142 accounts[i].settle(Date(2009, 1, 1)); 143 accounts[i].show(); 144 cout << endl; 145 } 146 cout << "Total: " << SavingsAccount::getTotal() << endl; 147 return 0; 148 }