实验任务1
1,自定义了两个类分别是window类和button类
使用了标准库中的iostream vector string
2,不适合
3定义了一个字符串长度为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
1 vector<int> v1(5, 42); 2 const vector<int> v2(v1); 3 4 v1.at(0) = -999;
1:构造并初始化5个42
2:拷贝构造v1
3:at访问
问题2
1 vector<vector<int>> v1{{1, 2, 3}, {4, 5, 6, 7}}; 2 const vector<vector<int>> v2(v1); 3 4 v1.at(0).push_back(-999);
1:初始化v1为两个{1,2,3}和{4,5,6,7}
2: 拷贝构造v1
3:在尾端插入v1.at(0)
问题3
1 vector<int> t1 = v1.at(0); 2 cout << t1.at(t1.size()-1) << endl; 3 4 const vector<int> t2 = v2.at(0); 5 cout << t2.at(t2.size()-1) << endl;
1:初始化t1为v1.at(0) t1能够进行修改
2:输出
3:初始化t2为v2.at(0) 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 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 }
1 #include "vectorInt.hpp" 2 #include <iostream> 3 4 using std::cin; 5 using std::cout; 6 7 void output(const vectorInt &vi) { 8 for(auto i = 0; i < vi.get_size(); ++i) 9 cout << vi.at(i) << ", "; 10 cout << "\b\b \n"; 11 } 12 13 14 void test1() { 15 int n; 16 cout << "Enter n: "; 17 cin >> n; 18 19 vectorInt x1(n); 20 for(auto i = 0; i < n; ++i) 21 x1.at(i) = i*i; 22 cout << "x1: "; output(x1); 23 24 vectorInt x2(n, 42); 25 vectorInt x3(x2); 26 x2.at(0) = -999; 27 cout << "x2: "; output(x2); 28 cout << "x3: "; output(x3); 29 } 30 31 void test2() { 32 const vectorInt x(5, 42); 33 vectorInt y(10, 0); 34 35 cout << "y: "; output(y); 36 y.assign(x); 37 cout << "y: "; output(y); 38 39 cout << "x.at(0) = " << x.at(0) << endl; 40 cout << "y.at(0) = " << y.at(0) << endl; 41 } 42 43 int main() { 44 cout << "测试1: \n"; 45 test1(); 46 47 cout << "\n测试2: \n"; 48 test2(); 49 }
问题一
深复制
问题二
不可以运行,修改完之后数据有安全风险
实验任务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 assert(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 assert(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 // task4.cpp 89 90 #include "matrix.hpp" 91 #include <cassert> 92 #include <iostream> 93 #include <numeric> 94 95 using std::cin; 96 using std::cout; 97 using std::endl; 98 99 const int N = 1000; 100 101 // 输出矩阵对象索引为index所在行的所有元素 102 void output(const Matrix &m, int index) { 103 assert(index >= 0 && index < m.get_lines()); 104 105 for (auto j = 0; j < m.get_cols(); ++j) 106 cout << m.at(index, j) << ", "; 107 cout << "\b\b \n"; 108 } 109 110 void test1() { 111 double x[1000]; 112 113 std::iota(x, x + N, 1); // 用1到N初始化数组x 114 115 int n, m; 116 cout << "Enter n and m: "; 117 cin >> n >> m; 118 119 Matrix m1(n, m); // 创建矩阵对象m1, 大小n×m 120 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 121 122 Matrix m2(m, n); // 创建矩阵对象m1, 大小m×n 123 m2.set(x); // 用一维数组x的值按行为矩阵m1赋值 124 125 Matrix m3(2); // 创建一个2×2矩阵对象 126 m3.set(x); // 用一维数组x的值按行为矩阵m4赋值 127 128 cout << "矩阵对象m1: \n"; 129 m1.display(); 130 cout << endl; 131 cout << "矩阵对象m2: \n"; 132 m2.display(); 133 cout << endl; 134 cout << "矩阵对象m3: \n"; 135 m3.display(); 136 cout << endl; 137 } 138 139 void test2() { 140 Matrix m1(2, 3); 141 m1.clear(); 142 143 const Matrix m2(m1); 144 m1.at(0, 0) = -999; 145 146 cout << "m1.at(0, 0) = " << m1.at(0, 0) << endl; 147 cout << "m2.at(0, 0) = " << m2.at(0, 0) << endl; 148 cout << "矩阵对象m1第0行: "; 149 output(m1, 0); 150 cout << "矩阵对象m2第0行: "; 151 output(m2, 0); 152 } 153 154 int main() { 155 cout << "测试1: \n"; 156 test1(); 157 158 cout << "测试2: \n"; 159 test2(); 160 }
实验任务5
1 #pragma once 2 3 #include<iostream> 4 #include<vector> 5 6 using namespace std; 7 8 class User { 9 public: 10 User(const string& name1, const string& password1="123456", const string& email1=""); 11 void set_email(); 12 void change_password(); 13 void display() const; 14 private: 15 string name; 16 string password; 17 string email; 18 }; 19 User::User(const string& name1, const string& password1, const string& email1) :name { name1 }, password { password1 }, email { email1 } { 20 } 21 void User::set_email() { 22 cout << "Enter email address: "; 23 string emaill; 24 cin >> emaill; 25 while (1) { 26 if (emaill.find('@') != string::npos) { 27 email = emaill; 28 cout << "email is set successfully..." << endl; 29 break; 30 } else { 31 cout << "illegal email.Please re-enter email: "; 32 cin >> emaill; 33 cout; 34 } 35 } 36 } 37 void User::change_password() { 38 cout << "Enter old password: "; 39 string password1; 40 cin >> password1; 41 int cnts = 1; 42 while (cnts < 3) { 43 if (password1 == password) { 44 cout << "new password: "; 45 cin >> password1; 46 password = password1; 47 cout << "new password is set successfully..." ; 48 break; 49 } else { 50 cout << "password input error. Please re-enter agagin"; 51 cin >> password1; 52 cnts++; 53 } 54 } 55 if (cnts == 3) cout << "password input error.Please try after a while" << endl;; 56 } 57 void User::display()const { 58 cout << "name: " << name << endl; 59 cout << "pass: " << password << endl; 60 cout << "email: " << email << endl; 61 }
1 #include "user.hpp" 2 #include <iostream> 3 #include <vector> 4 #include <string> 5 6 using std::cin; 7 using std::cout; 8 using std::endl; 9 using std::vector; 10 using std::string; 11 12 void test() { 13 vector<User> user_lst; 14 15 User u1("Alice", "2024113", "Alice@hotmail.com"); 16 user_lst.push_back(u1); 17 cout << endl; 18 19 User u2("Bob"); 20 u2.set_email(); 21 u2.change_password(); 22 user_lst.push_back(u2); 23 cout << endl; 24 25 User u3("Hellen"); 26 u3.set_email(); 27 u3.change_password(); 28 user_lst.push_back(u3); 29 cout << endl; 30 31 cout << "There are " << user_lst.size() << " users. they are: " << endl; 32 for(auto &i: user_lst) { 33 i.display(); 34 cout << endl; 35 } 36 } 37 38 int main() { 39 test(); 40 }
实验任务6
1 1 #pragma once 2 2 class Date { 3 3 private: 4 4 int year; 5 5 int month; 6 6 int day; 7 7 int totalDays; 8 8 public: 9 9 Date(int year, int month, int day); 10 10 int getYear()const { return year; } 11 11 int getMonth()const { return month; } 12 12 int getDay()const { return day; } 13 13 int getMaxDay()const; 14 14 bool isLeapYear()const { 15 15 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 16 16 } 17 17 void show() const; 18 18 int distance(const Date& date)const { 19 19 return totalDays - date.totalDays; 20 20 } 21 21 }; 22 22 23 23 24 24 25 25 #include"date.h" 26 26 #include<iostream> 27 27 #include<cstdlib> 28 28 using namespace std; 29 29 namespace { 30 30 const int DAYS_BEFIRE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304 ,334,365 }; 31 31 } 32 32 Date::Date(int year, int month, int day) :year(year), month(month), day(day) { 33 33 if (day <= 0 || day > getMaxDay()) { 34 34 cout << "Invalid date: "; 35 35 show(); 36 36 cout << endl; 37 37 exit(1); 38 38 } 39 39 int years = year - 1; 40 40 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFIRE_MONTH[month - 1] + day; 41 41 if (isLeapYear() && month > 2) totalDays++; 42 42 } 43 43 int Date::getMaxDay()const { 44 44 if (isLeapYear() &&month == 2) 45 45 return 29; 46 46 else return DAYS_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1]; 47 47 } 48 48 void Date::show()const { 49 49 cout << getYear() << "-" << getMonth() << "-" << getDay(); 50 50 } 51 51 52 52 53 53 54 54 #pragma once 55 55 #include"date.h" 56 56 #include<string> 57 57 using namespace std; 58 58 class SavingsAccount { 59 59 private: 60 60 string id; 61 61 double balance; 62 62 double rate; 63 63 Date lastDate; 64 64 double accumulation; 65 65 static double total; 66 66 void record(const Date& date, double amount, const string& desc); 67 67 void error(const string& msg) const; 68 68 double accumulate(const Date& date)const { 69 69 return accumulation + balance * date.distance(lastDate); 70 70 } 71 71 public: 72 72 SavingsAccount(const Date& date, const string& id, double rate); 73 73 const string& getId()const { return id; } 74 74 double getBalance()const { return balance; } 75 75 double getRate()const { return rate; } 76 76 static double getTotal() { return total; } 77 77 void deposit(const Date& date, double amount, const string& desc); 78 78 void withdraw(const Date& date, double amount, const string& desc); 79 79 void settle(const Date& date); 80 80 void show() const; 81 81 }; 82 82 83 83 84 84 85 85 #include"account.h" 86 86 #include<cmath> 87 87 #include<iostream> 88 88 using namespace std; 89 89 double SavingsAccount::total = 0; 90 90 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) : 91 91 id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { 92 92 date.show(); 93 93 cout << "\t#" << id << "created" << endl; 94 94 } 95 95 void SavingsAccount::record(const Date& date, double amount, const string& desc) { 96 96 accumulation = accumulate(date); 97 97 lastDate = date; 98 98 amount = floor(amount * 100 + 0.5) / 100; 99 99 balance += amount; 100 100 total += amount; 101 101 date.show(); 102 102 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 103 103 } 104 104 void SavingsAccount::error(const string& msg) const { 105 105 cout << "Error(#" << id << "):" << msg << endl; 106 106 } 107 107 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 108 108 record(date, amount, desc); 109 109 } 110 110 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 111 111 if (amount > getBalance()) 112 112 error("not enough money"); 113 113 else 114 114 record(date, -amount, desc); 115 115 } 116 116 void SavingsAccount::settle(const Date& date) { 117 117 double interest = accumulate(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1)); 118 118 if (interest != 0) record( date,interest,"interest" ); 119 119 accumulation = 0; 120 120 } 121 121 void SavingsAccount::show()const { 122 122 cout << id << "\tBalance: " << balance; 123 123 } 124 124 125 125 126 126 #include"account.h" 127 127 #include<iostream> 128 128 using namespace std; 129 129 int main() { 130 130 Date date{ 2008,11,1 }; 131 131 SavingsAccount accounts[] = { 132 132 SavingsAccount(date,"03755217",0.015), 133 133 SavingsAccount(date,"02342342",0.015) 134 134 }; 135 135 const int n = sizeof(accounts) / sizeof(SavingsAccount); 136 136 accounts[0].deposit(Date(2008, 11, 5), 5000, "salary"); 137 137 accounts[1].deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 138 138 accounts[0].deposit(Date(2008, 12, 5), 5500, "salary"); 139 139 accounts[1].withdraw(Date(2008, 12, 20), 4000, "buy a laptop"); 140 140 cout << endl; 141 141 for (int i = 0; i < n; i++) { 142 142 accounts[i].settle(Date(2009, 1, 1)); 143 143 accounts[i].show(); 144 144 cout << endl; 145 145 } 146 146 cout << "Total: " << SavingsAccount::getTotal() << endl; 147 147 return 0; 148 148 } 149 150 task6