实验三
任务一
代码
1 button.hpp 2 3 #pragma once 4 5 #include <iostream> 6 #include <string> 7 8 using std::string; 9 using std::cout; 10 11 // 按钮类 12 class Button { 13 public: 14 Button(const string &text); 15 string get_label() const; 16 void click(); 17 18 private: 19 string label; 20 }; 21 22 Button::Button(const string &text): label{text} { 23 } 24 25 inline string Button::get_label() const { 26 return label; 27 } 28 29 void Button::click() { 30 cout << "Button '" << label << "' clicked\n"; 31 } 32 33 window.hpp 34 35 #pragma once 36 #include "button.hpp" 37 #include <vector> 38 #include <iostream> 39 40 using std::vector; 41 using std::cout; 42 using std::endl; 43 44 // 窗口类 45 class Window{ 46 public: 47 Window(const string &win_title); 48 void display() const; 49 void close(); 50 void add_button(const string &label); 51 52 private: 53 string title; 54 vector<Button> buttons; 55 }; 56 57 Window::Window(const string &win_title): title{win_title} { 58 buttons.push_back(Button("close")); 59 } 60 61 inline void Window::display() const { 62 string s(40, '*'); 63 64 cout << s << endl; 65 cout << "window title: " << title << endl; 66 cout << "It has " << buttons.size() << " buttons: " << endl; 67 for(const auto &i: buttons) 68 cout << i.get_label() << " button" << endl; 69 cout << s << endl; 70 } 71 72 void Window::close() { 73 cout << "close window '" << title << "'" << endl; 74 buttons.at(0).click(); 75 } 76 77 void Window::add_button(const string &label) { 78 buttons.push_back(Button(label)); 79 } 80 81 task.cpp 82 83 #include "window.hpp" 84 #include <iostream> 85 86 using std::cout; 87 using std::cin; 88 89 void test() { 90 Window w1("new window"); 91 w1.add_button("maximize"); 92 w1.display(); 93 w1.close(); 94 } 95 96 int main() { 97 cout << "用组合类模拟简单GUI:\n"; 98 test(); 99 }
运行结果
问题一:自定义了两个类,使用了标准库的vector和string两个类,window类中使用了Button类,button集合是window类的属性之一
问题二:不适合,对于const,加了const之后对于调用的对象就是不可以更改的,对于inline是在编译之后把代码函数代码放到执行函数的地方可以提高效率,但是对于代码复杂的函数如果加了inline,可能会给程序带来其他问题
问题三:让输出更有条理,作用是输出40个*
任务二
代码
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 #pragma once 2 3 #include <iostream> 4 #include <cassert> 5 6 using std::cout; 7 using std::endl; 8 9 // 动态int数组对象类 10 class vectorInt{ 11 public: 12 vectorInt(int n); 13 vectorInt(int n, int value); 14 vectorInt(const vectorInt &vi); 15 ~vectorInt(); 16 17 int& at(int index); 18 const int& at(int index) const; 19 20 vectorInt& assign(const vectorInt &v); 21 int get_size() const; 22 23 private: 24 int size; 25 int *ptr; // ptr指向包含size个int的数组 26 }; 27 28 vectorInt::vectorInt(int n): size{n}, ptr{new int[size]} { 29 } 30 31 vectorInt::vectorInt(int n, int value): size{n}, ptr{new int[size]} { 32 for(auto i = 0; i < size; ++i) 33 ptr[i] = value; 34 } 35 36 vectorInt::vectorInt(const vectorInt &vi): size{vi.size}, ptr{new int[size]} { 37 for(auto i = 0; i < size; ++i) 38 ptr[i] = vi.ptr[i]; 39 } 40 41 vectorInt::~vectorInt() { 42 delete [] ptr; 43 } 44 45 const int& vectorInt::at(int index) const { 46 assert(index >= 0 && index < size); 47 48 return ptr[index]; 49 } 50 51 int& vectorInt::at(int index) { 52 assert(index >= 0 && index < size); 53 54 return ptr[index]; 55 } 56 57 vectorInt& vectorInt::assign(const vectorInt &v) { 58 delete[] ptr; // 释放对象中ptr原来指向的资源 59 60 size = v.size; 61 ptr = new int[size]; 62 63 for(int i = 0; i < size; ++i) 64 ptr[i] = v.ptr[i]; 65 66 return *this; 67 } 68 69 int vectorInt::get_size() const { 70 return size; 71 } 72 73 74 #include "vectorInt.hpp" 75 #include <iostream> 76 77 using std::cin; 78 using std::cout; 79 80 void output(const vectorInt &vi) { 81 for(auto i = 0; i < vi.get_size(); ++i) 82 cout << vi.at(i) << ", "; 83 cout << "\b\b \n"; 84 } 85 86 87 void test1() { 88 int n; 89 cout << "Enter n: "; 90 cin >> n; 91 92 vectorInt x1(n); 93 for(auto i = 0; i < n; ++i) 94 x1.at(i) = i*i; 95 cout << "x1: "; output(x1); 96 97 vectorInt x2(n, 42); 98 vectorInt x3(x2); 99 x2.at(0) = -999; 100 cout << "x2: "; output(x2); 101 cout << "x3: "; output(x3); 102 } 103 104 void test2() { 105 const vectorInt x(5, 42); 106 vectorInt y(10, 0); 107 108 cout << "y: "; output(y); 109 y.assign(x); 110 cout << "y: "; output(y); 111 112 cout << "x.at(0) = " << x.at(0) << endl; 113 cout << "y.at(0) = " << y.at(0) << endl; 114 } 115 116 int main() { 117 cout << "测试1: \n"; 118 test1(); 119 120 cout << "\n测试2: \n"; 121 test2(); 122 }
运行结果
任务四
代码
1 #pragma once 2 3 #include <iostream> 4 #include <cassert> 5 6 using std::cout; 7 using std::endl; 8 9 // 类Matrix的声明 10 class Matrix { 11 public: 12 Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵, 初始值为value 13 Matrix(int n); // 构造函数,构造一个n*n的矩阵, 初始值为value 14 Matrix(const Matrix &x); // 复制构造函数, 使用已有的矩阵X构造 15 ~Matrix(); 16 17 void set(const double *pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值 18 void clear(); // 把矩阵对象的值置0 19 20 const double& at(int i, int j) const; // 返回矩阵对象索引(i,j)的元素const引用 21 double& at(int i, int j); // 返回矩阵对象索引(i,j)的元素引用 22 23 int get_lines() const; // 返回矩阵对象行数 24 int get_cols() const; // 返回矩阵对象列数 25 26 void display() const; // 按行显示矩阵对象元素值 27 28 private: 29 int lines; // 矩阵对象内元素行数 30 int cols; // 矩阵对象内元素列数 31 double *ptr; 32 }; 33 34 // 类Matrix的实现:待补足 35 // xxx 36 Matrix::Matrix(const Matrix &x): lines{x.get_lines()}, cols{x.get_cols()}, ptr{new double[lines*cols]}{ 37 for(int i = 0;i < lines;i++){ 38 for(int j = 0;j < cols;j++){ 39 *(ptr+cols*i+j) = x.ptr[cols*i + j]; 40 } 41 } 42 } 43 44 Matrix::Matrix(int n, int m): lines{n}, cols{m}, ptr{new double[lines*cols]} {} 45 46 Matrix::Matrix(int n): lines{n}, cols{n}, ptr{new double[lines*cols]} {} 47 48 Matrix::~Matrix(){ 49 delete [] ptr; 50 } 51 52 void Matrix::set(const double *pvalue){ 53 for(int i = 0;i < lines;i++){ 54 for(int j = 0;j < cols;j++){ 55 *(ptr+cols*i+j) = *(pvalue+cols*i+j); 56 } 57 } 58 } 59 60 void Matrix::clear(){ 61 for(int i = 0;i < lines;i++){ 62 for(int j = 0;j < cols;j++){ 63 *(ptr+cols*i+j) = 0; 64 } 65 } 66 } 67 68 const double& Matrix::at(int i, int j) const{ 69 assert(i >= 0 && i < lines && j >= 0 && j < cols); 70 71 return *(ptr+cols*i+j); 72 } 73 74 double& Matrix::at(int i, int j){ 75 assert(i >= 0 && i < lines && j >= 0 && j < cols); 76 77 return *(ptr+cols*i+j); 78 } 79 80 int Matrix::get_lines() const{ 81 return lines; 82 } 83 84 int Matrix::get_cols() const{ 85 return cols; 86 } 87 88 void Matrix::display() const{ 89 for(int i = 0;i < lines;i++){ 90 for(int j = 0;j < cols;j++){ 91 cout << *(ptr+cols*i+j) << " "; 92 } 93 cout << endl; 94 } 95 } 96 97 98 99 100 101 #include "matrix.hpp" 102 #include <iostream> 103 #include <cassert> 104 105 using std::cin; 106 using std::cout; 107 using std::endl; 108 109 110 const int N = 1000; 111 112 // 输出矩阵对象索引为index所在行的所有元素 113 void output(const Matrix &m, int index) { 114 assert(index >= 0 && index < m.get_lines()); 115 116 for(auto j = 0; j < m.get_cols(); ++j) 117 cout << m.at(index, j) << ", "; 118 cout << "\b\b \n"; 119 } 120 121 122 void test1() { 123 double x[1000] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 124 125 int n, m; 126 cout << "Enter n and m: "; 127 cin >> n >> m; 128 129 Matrix m1(n, m); // 创建矩阵对象m1, 大小n×m 130 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 131 132 Matrix m2(m, n); // 创建矩阵对象m1, 大小m×n 133 m2.set(x); // 用一维数组x的值按行为矩阵m1赋值 134 135 Matrix m3(2); // 创建一个2×2矩阵对象 136 m3.set(x); // 用一维数组x的值按行为矩阵m4赋值 137 138 cout << "矩阵对象m1: \n"; m1.display(); cout << endl; 139 cout << "矩阵对象m2: \n"; m2.display(); cout << endl; 140 cout << "矩阵对象m3: \n"; m3.display(); cout << endl; 141 } 142 143 void test2() { 144 Matrix m1(2, 3); 145 m1.clear(); 146 147 const Matrix m2(m1); 148 m1.at(0, 0) = -999; 149 150 cout << "m1.at(0, 0) = " << m1.at(0, 0) << endl; 151 cout << "m2.at(0, 0) = " << m2.at(0, 0) << endl; 152 cout << "矩阵对象m1第0行: "; output(m1, 0); 153 cout << "矩阵对象m2第0行: "; output(m2, 0); 154 } 155 156 int main() { 157 cout << "测试1: \n"; 158 test1(); 159 160 cout << "测试2: \n"; 161 test2(); 162 }
运行结果
任务五
代码
1 #pragma once 2 3 #include<iostream> 4 #include<cassert> 5 #include<string> 6 #include<vector> 7 8 using namespace std; 9 10 class User{ 11 public: 12 User(string username, string password = "123456", string userEmail = ""); 13 ~User(); 14 15 void set_email(); 16 void change_password(); 17 void display(); 18 19 20 21 22 private: 23 string name; 24 string email; 25 string password; 26 27 }; 28 29 User::User(string username, string password, string userEmail):name{username}, password{password}, email{userEmail}{ 30 } 31 32 User::~User(){ 33 } 34 35 36 37 void User::set_email(){ 38 const char ch = '@'; 39 cout << "请输入邮箱:" << endl; 40 while(1){ 41 cin >> this->email; 42 if(this->email.find(ch) != string::npos) { 43 cout << "email is set successfully..." << endl; 44 break; 45 }else{ 46 cout << "格式错误,请重新输入邮箱:" << endl; 47 cin.clear(); 48 } 49 } 50 } 51 52 53 54 void User::change_password(){ 55 string inputPass; 56 cout << "请输入原密码:" << endl; 57 int cont = 0; 58 while(cont < 3){ 59 cin >> inputPass; 60 if(inputPass.compare(password) == 0){ 61 break; 62 }else{ 63 cout << "原密码输入错误,请重新输入:" << endl; 64 cin.clear(); 65 } 66 ++cont; 67 } 68 69 if(cont == 3){ 70 cout << "错误三次,稍后再试"; 71 }else{ 72 cout << "请输入新密码:" << endl; 73 cin >> password; 74 cout << "密码修改成功!"; 75 } 76 } 77 78 79 void User::display(){ 80 cout << "name: " << name << endl; 81 cout << "pass: "; 82 for(int i = 0;i < password.size();i++){ 83 cout << "*"; 84 } 85 cout << endl; 86 cout << "eamil: " << email << endl; 87 } 88 89 90 91 92 #include "user.hpp" 93 #include <iostream> 94 #include <vector> 95 #include <string> 96 97 using std::cin; 98 using std::cout; 99 using std::endl; 100 using std::vector; 101 using std::string; 102 103 void test() { 104 vector<User> user_lst; 105 106 User u1("Alice", "2024113", "Alice@hotmail.com"); 107 user_lst.push_back(u1); 108 cout << endl; 109 110 User u2("Bob"); 111 u2.set_email(); 112 u2.change_password(); 113 user_lst.push_back(u2); 114 cout << endl; 115 116 User u3("Hellen"); 117 u3.set_email(); 118 u3.change_password(); 119 user_lst.push_back(u3); 120 cout << endl; 121 122 cout << "There are " << user_lst.size() << " users. they are: " << endl; 123 for(auto &i: user_lst) { 124 i.display(); 125 cout << endl; 126 } 127 } 128 129 int main() { 130 test(); 131 }
运行结果
任务六
代码
1 1 #ifndef __DATE_H__ 2 2 #define __DATE_H__ 3 3 class Date { 4 private: 5 int year; 6 int month; 7 int day; 8 int totalDays; 9 public: 10 Date(int year, int month, int day); 11 int getYear() const { return year; } 12 int getMonth() const { return month; } 13 int getDay() const { return day; } 14 int getMaxDay() const; 15 bool isLeapYear() const { 16 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 17 } 18 void show() const; 19 20 int distance(const Date& date) const { 21 return totalDays - date.totalDays; 22 } 23 }; 24 #endif //__DATE_H__ 25 data.h 26 27 28 29 #include "date.h" 30 #include <iostream> 31 #include <cstdlib> 32 using namespace std; 33 namespace { 34 const int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; 35 } 36 Date::Date(int year, int month, int day) : year(year), month(month), day(day) { 37 if (day <= 0 || day > getMaxDay()) { 38 cout << "Invalid date: "; 39 show(); 40 cout << endl; 41 exit(1); 42 } 43 int years = year - 1; 44 totalDays = years * 365 + years / 4 - years / 100 + years / 400 45 + DAYS_BEFORE_MONTH[month - 1] + day; 46 if (isLeapYear() && month > 2) totalDays++; 47 } 48 int Date::getMaxDay() const { 49 if (isLeapYear() && month == 2) 50 return 29; 51 else 52 return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1]; 53 } 54 void Date::show() const { 55 cout << getYear() << "-" << getMonth() << "-" << getDay(); 56 } 57 58 data.cpp 59 60 61 #ifndef __ACCOUNT_H__ 62 #define __ACCOUNT_H__ 63 #include "date.h" 64 #include <string> 65 class SavingsAccount { 66 private: 67 std::string id; 68 double balance; 69 double rate; 70 Date lastDate; 71 double accumulation; 72 static double total; 73 74 void record(const Date &date, double amount, const std::string &desc); 75 76 void error(const std::string &msg) const; 77 78 double accumulate(const Date& date) const { 79 return accumulation + balance * date.distance(lastDate); 80 } 81 public: 82 83 SavingsAccount(const Date &date, const std::string &id, double rate); 84 const std::string &getId() const { return id; } 85 double getBalance() const { return balance; } 86 double getRate() const { return rate; } 87 static double getTotal() { return total; } 88 89 void deposit(const Date &date, double amount, const std::string &desc); 90 91 void withdraw(const Date &date, double amount, const std::string &desc); 92 93 void settle(const Date &date); 94 95 void show() const; 96 }; 97 #endif //__ACCOUNT_H__ 98 99 account.h 100 101 102 103 #include "account.h" 104 #include <cmath> 105 #include <iostream> 106 using namespace std; 107 double SavingsAccount::total = 0; 108 SavingsAccount::SavingsAccount(const Date &date, const string &id, double rate) 109 : id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { 110 date.show(); 111 cout << "\t#" << id << " created" << endl; 112 } 113 void SavingsAccount::record(const Date &date, double amount, const string &desc) { 114 accumulation = accumulate(date); 115 lastDate = date; 116 amount = floor(amount * 100 + 0.5) / 100; 117 balance += amount; 118 total += amount; 119 date.show(); 120 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 121 } 122 void SavingsAccount::error(const string &msg) const { 123 cout << "Error(#" << id << "): " << msg << endl; 124 } 125 void SavingsAccount::deposit(const Date &date, double amount, const string &desc) { 126 record(date, amount, desc); 127 } 128 void SavingsAccount::withdraw(const Date &date, double amount, const string &desc) { 129 if (amount > getBalance()) 130 error("not enough money"); 131 else 132 record(date, -amount, desc); 133 } 134 void SavingsAccount::settle(const Date &date) { 135 double interest = accumulate(date) * rate 136 / date.distance(Date(date.getYear() - 1, 1, 1)); 137 if (interest != 0) 138 record(date, interest, "interest"); 139 accumulation = 0; 140 } 141 void SavingsAccount::show() const { 142 cout << id << "\tBalance: " << balance; 143 } 144 145 account.cpp 146 147 148 #include "account.h" 149 #include <iostream> 150 using namespace std; 151 int main() { 152 Date date(2008, 11, 1); 153 SavingsAccount accounts[] = { 154 SavingsAccount(date, "03755217", 0.015), 155 SavingsAccount(date, "02342342", 0.015) 156 }; 157 const int n = sizeof(accounts) / sizeof(SavingsAccount); 158 159 accounts[0].deposit(Date(2008, 11, 5), 5000, "salary"); 160 accounts[1].deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 161 162 accounts[0].deposit(Date(2008, 12, 5), 5500, "salary"); 163 accounts[1].withdraw(Date(2008, 12, 20), 4000, "buy a laptop"); 164 165 cout << endl; 166 for (int i = 0; i < n; i++) { 167 accounts[i].settle(Date(2009, 1, 1)); 168 accounts[i].show(); 169 cout << endl; 170 } 171 cout << "Total: " << SavingsAccount::getTotal() << endl; 172 return 0; 173 } 174 175 6_25.cpp
运行结果