实验三
实验1代码:
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 28 29 30 #include "window.hpp" 31 #include <iostream> 32 33 using std::cout; 34 using std::cin; 35 36 void test() { 37 Window w1("new window"); 38 w1.add_button("maximize"); 39 w1.display(); 40 w1.close(); 41 } 42 43 int main() { 44 cout << "用组合类模拟简单GUI:\n"; 45 test(); 46 } 47 48 49 50 51 #pragma once 52 #include "button.hpp" 53 #include <vector> 54 #include <iostream> 55 56 using std::vector; 57 using std::cout; 58 using std::endl; 59 60 // 窗口类 61 class Window{ 62 public: 63 Window(const string &win_title); 64 void display() const; 65 void close(); 66 void add_button(const string &label); 67 68 private: 69 string title; 70 vector<Button> buttons; 71 }; 72 73 Window::Window(const string &win_title): title{win_title} { 74 buttons.push_back(Button("close")); 75 } 76 77 inline void Window::display() const { 78 string s(40, '*'); 79 80 cout << s << endl; 81 cout << "window title: " << title << endl; 82 cout << "It has " << buttons.size() << " buttons: " << endl; 83 for(const auto &i: buttons) 84 cout << i.get_label() << " button" << endl; 85 cout << s << endl; 86 } 87 88 void Window::close() { 89 cout << "close window '" << title << "'" << endl; 90 buttons.at(0).click(); 91 } 92 93 void Window::add_button(const string &label) { 94 buttons.push_back(Button(label)); 95 }
运行截图:
问题1:自定义两个类,按钮类和窗口类,标准库使用了vector类,窗口类和vector类进行了组合
问题2:不适合,const类型函数为常量函数,无法对其值进行改变,而inline为内联函数,强行将
其他函数转化为内联函数可能会导致运行时间增加
问题3:将字符串s赋值为40个*
实验2代码:
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:创建一个含有5个int类型的元素的动态数组v1,值为42,将v1复制给v2,将v1的第一个元素赋值为-999
问题2:创建一个含有两个元素类型为int的动态数组的动态数组v1,将v1复制给v2,v1的第一个数组末尾插入-999
问题3:创建一个动态数组t1,将v1的第一个数组赋值给他,输出t1中第四个元素,创建一个动态数组t2,将v2的第一个数组赋值给他,输出t2中第三个元素
问题4:深复制,不需要
实验3代码:
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 58 59 60 61 #include "vectorInt.hpp" 62 #include <iostream> 63 64 using std::cin; 65 using std::cout; 66 67 void output(const vectorInt &vi) { 68 for(auto i = 0; i < vi.get_size(); ++i) 69 cout << vi.at(i) << ", "; 70 cout << "\b\b \n"; 71 } 72 73 74 void test1() { 75 int n; 76 cout << "Enter n: "; 77 cin >> n; 78 79 vectorInt x1(n); 80 for(auto i = 0; i < n; ++i) 81 x1.at(i) = i*i; 82 cout << "x1: "; output(x1); 83 84 vectorInt x2(n, 42); 85 vectorInt x3(x2); 86 x2.at(0) = -999; 87 cout << "x2: "; output(x2); 88 cout << "x3: "; output(x3); 89 } 90 91 void test2() { 92 const vectorInt x(5, 42); 93 vectorInt y(10, 0); 94 95 cout << "y: "; output(y); 96 y.assign(x); 97 cout << "y: "; output(y); 98 99 cout << "x.at(0) = " << x.at(0) << endl; 100 cout << "y.at(0) = " << y.at(0) << endl; 101 } 102 103 int main() { 104 cout << "测试1: \n"; 105 test1(); 106 107 cout << "\n测试2: \n"; 108 test2(); 109 }
运行截图:
问题1:深复制
问题2:不能正确运行,有,如果返回值是引用变量可能会改变数组中元素的值
实验4代码:
1 #pragma once 2 3 #include <cassert> 4 #include <iostream> 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 Matrix::Matrix(int n, int m) : lines(n), cols(m) { 36 ptr = new double[n * m]; 37 clear(); 38 } 39 40 Matrix::Matrix(int n) : Matrix(n, n) {} 41 42 Matrix::Matrix(const Matrix &x) : lines(x.lines), cols(x.cols) { 43 ptr = new double[lines * cols]; 44 for (int i = 0; i < lines * cols; i++) 45 ptr[i] = x.ptr[i]; 46 } 47 48 Matrix::~Matrix() { 49 delete[] ptr; 50 } 51 52 void Matrix::set(const double *pvalue) { 53 for (int i = 0; i < lines * cols; i++) 54 ptr[i] = pvalue[i]; 55 } 56 57 void Matrix::clear() { 58 for (int i = 0; i < lines * cols; i++) 59 ptr[i] = 0; 60 } 61 62 const double &Matrix::at(int i, int j) const { 63 if(i >= 0 && i < lines && j >= 0 && j < cols) 64 return ptr[i * cols + j]; 65 } 66 67 double &Matrix::at(int i, int j) { 68 if(i >= 0 && i < lines && j >= 0 && j < cols) 69 return ptr[i * cols + j]; 70 } 71 72 int Matrix::get_lines() const { 73 return lines; 74 } 75 76 int Matrix::get_cols() const { 77 return cols; 78 } 79 80 void Matrix::display() const { 81 for (int i = 0; i < lines; i++) { 82 for (int j = 0; j < cols; j++) { 83 cout << ptr[i * cols + j] << " "; 84 } 85 cout << endl; 86 } 87 } 88 89 90 91 92 93 #include "matrix.hpp" 94 #include <cassert> 95 #include <iostream> 96 #include <numeric> 97 98 using std::cin; 99 using std::cout; 100 using std::endl; 101 102 const int N = 1000; 103 104 // 输出矩阵对象索引为index所在行的所有元素 105 void output(const Matrix &m, int index) { 106 assert(index >= 0 && index < m.get_lines()); 107 108 for (auto j = 0; j < m.get_cols(); ++j) 109 cout << m.at(index, j) << ", "; 110 cout << "\b\b \n"; 111 } 112 113 void test1() { 114 double x[1000]; 115 116 std::iota(x, x + N, 1); // 用1到N初始化数组x 117 118 int n, m; 119 cout << "Enter n and m: "; 120 cin >> n >> m; 121 122 Matrix m1(n, m); // 创建矩阵对象m1, 大小n×m 123 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 124 125 Matrix m2(m, n); // 创建矩阵对象m1, 大小m×n 126 m2.set(x); // 用一维数组x的值按行为矩阵m1赋值 127 128 Matrix m3(2); // 创建一个2×2矩阵对象 129 m3.set(x); // 用一维数组x的值按行为矩阵m4赋值 130 131 cout << "矩阵对象m1: \n"; 132 m1.display(); 133 cout << endl; 134 cout << "矩阵对象m2: \n"; 135 m2.display(); 136 cout << endl; 137 cout << "矩阵对象m3: \n"; 138 m3.display(); 139 cout << endl; 140 } 141 142 void test2() { 143 Matrix m1(2, 3); 144 m1.clear(); 145 146 const Matrix m2(m1); 147 m1.at(0, 0) = -999; 148 149 cout << "m1.at(0, 0) = " << m1.at(0, 0) << endl; 150 cout << "m2.at(0, 0) = " << m2.at(0, 0) << endl; 151 cout << "矩阵对象m1第0行: "; 152 output(m1, 0); 153 cout << "矩阵对象m2第0行: "; 154 output(m2, 0); 155 } 156 157 int main() { 158 cout << "测试1: \n"; 159 test1(); 160 161 cout << "测试2: \n"; 162 test2(); 163 }
运行截图:
实验5代码:
1 #pragma once 2 3 #include<iostream> 4 #include<string> 5 6 using namespace std; 7 8 9 class User{ 10 public: 11 User(string Name); 12 User(string Name,string Password,string Email); 13 ~User(); 14 void set_email(); 15 void change_password(); 16 void display(); 17 private: 18 string name,password="123456",email="xyz@gmail.com"; 19 }; 20 User::User(string Name){ 21 name=Name; 22 } 23 24 User::~User()=default; 25 26 User::User(string Name,string Password,string Email){ 27 name=Name; 28 password=Password; 29 email=Email; 30 } 31 32 33 34 void User::set_email(){ 35 string v; 36 int x=0; 37 cout<<"请输入邮箱地址:"; 38 cin>>v; 39 for(auto i:v){ 40 if(i=='@'){ 41 x=1; 42 break; 43 } 44 } 45 while(!x){ 46 cout<<"请输入有效邮箱地址:"; 47 cin>>v; 48 for(auto i:v){ 49 if(i=='@'){ 50 x=1; 51 break; 52 } 53 } 54 } 55 email=v; 56 cout<<"邮箱已成功设置!"<<endl<<endl; 57 } 58 59 60 void User::change_password(){ 61 string v1,v2; 62 int t=0; 63 cout<<"请输入旧密码:"; 64 cin>>v1; 65 if(v1==password){ 66 cout<<"请输入新密码:"; 67 cin>>v2; 68 password=v2; 69 } 70 else{ 71 cout<<"密码错误!"<<endl<<endl; 72 t++; 73 while(t<3){ 74 cout<<"请输入旧密码:"; 75 cin>>v1; 76 if(v1==password) 77 break; 78 t++; 79 cout<<"密码错误!"<<endl<<endl; 80 } 81 if(t>=3){ 82 cout<<"请稍后再试"<<endl<<endl; 83 return; 84 } 85 else{ 86 cout<<"请输入新密码:"; 87 cin>>v2; 88 password=v2; 89 } 90 } 91 cout<<"密码已修改完成!"<<endl<<endl; 92 } 93 94 95 void User::display(){ 96 cout<<name<<endl; 97 for(auto i:password) 98 cout<<'*'; 99 cout<<endl; 100 cout<<email<<endl; 101 } 102 103 104 105 106 #include "user.hpp" 107 #include <iostream> 108 #include <string> 109 #include <vector> 110 111 using std::cin; 112 using std::cout; 113 using std::endl; 114 using std::string; 115 using std::vector; 116 117 void test() { 118 vector<User> user_lst; 119 120 User u1("Alice", "2024113", "Alice@hotmail.com"); 121 user_lst.push_back(u1); 122 cout << endl; 123 124 User u2("Bob"); 125 u2.set_email(); 126 u2.change_password(); 127 user_lst.push_back(u2); 128 cout << endl; 129 130 User u3("Hellen"); 131 u3.set_email(); 132 u3.change_password(); 133 user_lst.push_back(u3); 134 cout << endl; 135 136 cout << "There are " << user_lst.size() << " users. they are: " << endl; 137 for (auto &i : user_lst) { 138 i.display(); 139 cout << endl; 140 } 141 } 142 143 int main() { test(); }
运行截图:
实验6代码:
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 //namespace使下面的定义只在当前文件中有效 31 const int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 356 }; 32 } 33 Date::Date(int year, int month, int day) :year(year), month(month), day(day) { 34 if (day <= 0 || day > getMaxDay()) { 35 cout << "Invalid date: "; 36 show(); 37 cout << endl; 38 exit(1); 39 } 40 int years = year - 1; 41 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day; 42 if (isLeapYear() && month > 2) totalDays++; 43 } 44 int Date::getMaxDay() const { 45 if (isLeapYear() && month == 2) return 29; 46 else return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; 47 } 48 void Date::show() const { 49 cout << getYear() << "-" << getMonth() << "-" << getDay(); 50 } 51 52 53 54 #ifndef __ACCOUNT_H__ 55 #define __ACCOUNT_H__ 56 #include "date.h" 57 #include <string> 58 class SavingsAccount {//储蓄账户类 59 private: 60 std::string id;//账号 61 double balance;//余额 62 double rate;//存款的年利率 63 Date lastDate;//上次变更余额的时期 64 double accumulation;//按日累加以和 65 static double total;//所有账户的总金额 66 //记录一笔账,date为日期,amount为金额,desc为说明 67 void record(const Date & date, double amount, const std::string & desc); 68 //报告错误信息 69 void error(const std::string & msg) const; 70 //获得指定日期为止的存款金额按日累增值 71 double accumulate(const Date & date) const { 72 return accumulation + balance * date.distance(lastDate); 73 } 74 public: 75 //构造函数 76 SavingsAccount(const Date & date, const std::string & id, double rate);//获得账号 77 const std::string & getId() const { return id; } //获得余额 78 double getBalance() const { return balance; } //获得年利率 79 double getRate() const { return rate; } 80 static double getTotal() { return total; }//存入现金 81 void deposit(const Date & date, double amount, const std::string & desc);//取出现金 82 void withdraw(const Date & date, double amount, const std::string & desc);//结算利息,每年1月1日调用一次该函数 83 void settle(const Date & date); 84 //显示账户信息 85 void show() const; 86 }; 87 #endif //__ACCOUNT_H__#pragma once 88 89 90 91 92 #include "account.h" 93 #include <cmath> 94 #include <iostream> 95 using namespace std; 96 double SavingsAccount::total = 0; 97 //SavingsAccount类相关成员函数的实现 98 SavingsAccount::SavingsAccount(const Date &date,const string &id,double rate):id(id),balance(0),rate(rate),lastDate(date),accumulation(0){ 99 date.show(); 100 cout << "\t#" << id << " created" << endl; 101 } 102 void SavingsAccount::record(const Date & date, double amount, const string & desc) { 103 accumulation = accumulate(date); 104 lastDate = date; 105 amount = floor(amount * 100 + 0.5) / 100; 106 //保留小数点后两位 107 balance += amount; 108 total += amount; 109 date.show(); 110 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 111 } 112 void SavingsAccount::error(const string & msg) const { 113 cout << "Error(#" << id << "): " << msg << endl; 114 } 115 void SavingsAccount::deposit(const Date & date, double amount, const string & desc) { 116 record(date, amount, desc); 117 } 118 void SavingsAccount::withdraw(const Date & date, double amount, const string & desc) { 119 if (amount > getBalance()) 120 error("not enough money"); 121 else 122 record(date, -amount, desc); 123 } 124 void SavingsAccount::settle(const Date & date) { 125 double interest = accumulate(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1)); 126 if (interest != 0) 127 record(date, interest, "interest"); 128 accumulation = 0; 129 } 130 void SavingsAccount::show() const { 131 cout << "ID: " << id << "\tBalance: " << balance; 132 } 133 134 135 136 137 #include "account.h" 138 #include <iostream> 139 using namespace std; 140 int main() { 141 Date date(2008, 11, 1); 142 //起始日期 143 //建立几个账户 144 SavingsAccount accounts[] = { 145 SavingsAccount(date, "03755217", 0.015), 146 SavingsAccount(date, "02342342", 0.015) 147 }; 148 //几笔账目 149 accounts[0].deposit(Date(2008, 11, 5), 5000, "salary"); 150 accounts[1].deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 151 accounts[0].deposit(Date(2008, 12, 5), 5500, "salary"); 152 accounts[1].withdraw(Date(2008, 12, 20), 4000, "buy a laptop"); 153 //结算所有账户并输出各个账户信息 154 cout << endl; 155 for (int i = 0; i < sizeof(accounts) / sizeof(SavingsAccount); i++) { 156 accounts[i].settle(Date(2009, 1, 1)); 157 accounts[i].show(); 158 cout << endl; 159 } 160 cout << "Total: " << SavingsAccount::getTotal() << endl; 161 return 0; 162 }
运行截图: