cpp 实验3
task1
task1
问题一:定义了两个类 Button和window.使用了标准的两个类 string和vector。
问题二: 剩下的函数主要是一些负责输出结果的函数,它们在输出的过程中不会改变对象的值,所以我觉得没有必要加const,当然它们属于简单的输出语句可以设计成内联函数.
问题三:定义一个长度为40且元素都是'*'的string对象s.
1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 void output1(const vector<int>& v) { 5 for (auto& i : v) 6 cout << i << ", "; 7 cout << "\b\b \n"; 8 } 9 void output2(const vector<vector<int>> v) { 10 for (auto& i : v) 11 for (auto& j : i) 12 cout << j << ", "; 13 cout << "\b\b \n"; 14 15 } 16 void test1() { 17 vector<int> v1(5, 42); 18 const vector<int> v2(v1); 19 v1.at(0) = -999; 20 cout << "v1: "; output1(v1); 21 cout << "v2: "; output1(v2); 22 cout << "v1.at(0)=" << v1.at(0) << endl; 23 cout << "v2.at(0)=" << v2.at(0) << endl; 24 } 25 void test2() { 26 vector<vector<int>> v1{ {1,2,3},{4,5,6,7} }; 27 const vector<vector<int>> v2(v1); 28 v1.at(0).push_back(-999); 29 cout << "v1: \n"; output2(v1); 30 cout << "v2: \n"; output2(v2); 31 vector<int> t1 = v1.at(0); 32 cout << t1.at(t1.size() - 1) << endl; 33 const vector<int> t2 = v2.at(0); 34 cout << t2.at(t2.size() - 1) << endl; 35 } 36 int main() { 37 cout << "测试1:\n"; 38 test1(); 39 cout << "测试2:\n"; 40 test2(); 41 }
task2
问题一:第一行定义一个长度为5值全为42的整形vector对象v1,第二行使用v1复制构造了一个vector对象v2,第三行对v1的第一个值进行修改.
问题二:第一行定义一个vetcor<int> 类型的vector对象v1,第二行使用复制构造函数定义了一个vector<int> 类型的vetcor对象v2.
问题三:分别使用问题二定义的v1,v2的第一个元素,复制定义两个整形的vector数组,并输出它们的最后一个元素.
问题四:实现的机制是深复制,是.
1 #pragma once 2 #include<iostream> 3 #include<cassert> 4 using namespace std; 5 class vectorInt { 6 public: 7 vectorInt(int n); 8 vectorInt(int n, int value); 9 vectorInt(const vectorInt& vi); 10 ~vectorInt(); 11 int& at(int index); 12 const int& at(int index) const; 13 vectorInt& assign(const vectorInt& v); 14 int get_size() const; 15 private: 16 int size; 17 int* ptr; 18 }; 19 vectorInt::vectorInt(int n) :size(n), ptr{ new int[size] } { 20 } 21 vectorInt::vectorInt(int n, int value) :size{ n }, ptr{ new int[size] } { 22 for (auto i = 0; i < size; i++) 23 ptr[i] = value; 24 } 25 vectorInt::vectorInt(const vectorInt& vi) :size{ vi.size }, ptr{ new int[size] } { 26 for (auto i = 0; i < size; i++) 27 ptr[i] = vi.ptr[i]; 28 } 29 vectorInt::~vectorInt() { 30 delete[] ptr; 31 } 32 const int& vectorInt::at(int index) const { 33 assert(index >= 0 && index < size); 34 return ptr[index]; 35 } 36 int& vectorInt::at(int index) { 37 assert(index >= 0 && index < size); 38 return ptr[index]; 39 } 40 vectorInt& vectorInt::assign(const vectorInt& v) { 41 delete[] ptr; 42 size = v.size; 43 ptr = new int[size]; 44 for (int i = 0; i < size; i++) 45 ptr[i] = v.ptr[i]; 46 return *this; 47 } 48 int vectorInt::get_size() const { 49 return size; 50 } 51 52 53 54 #include "标头.h" 55 #include<iostream> 56 using namespace std; 57 void output(const vectorInt& vi) { 58 for (auto i = 0; i < vi.get_size(); i++) 59 cout << vi.at(i) << ", "; 60 cout << "\b\b \n"; 61 } 62 void test1() { 63 int n; 64 cout << "Enter n:"; 65 cin >> n; 66 vectorInt x1(n); 67 for (auto i = 0; i < n; i++) x1.at(i) = i * i; 68 cout << "x1: "; output(x1); 69 vectorInt x2(n, 42); 70 vectorInt x3(x2); 71 x2.at(0) = -999; 72 cout << "x2: "; output(x2); 73 cout << "x3: "; output(x3); 74 75 } 76 void test2() { 77 const vectorInt x(5, 42); 78 vectorInt y(10, 0); 79 cout << "y: "; output(y); 80 y.assign(x); 81 cout << "y: "; output(y); 82 cout << "x.at(0)=" << x.at(0) << endl; 83 cout << "t.at(0)=" << y.at(0) << endl; 84 } 85 int main() { 86 cout << "测试1: \n"; 87 test1(); 88 cout << "测试2:\n"; 89 test2(); 90 }
task3
问题一:深复制。
问题二:不能正常运行.是,const int& at(int index) const;返回的是引用类型,也就是可以通过它返回值对新的引用变量进行赋值,新的变量在后续操作的过程可能会改变它的值,如果是const对象,它是不允许值发生改变,所以因该加上const.
问题三:可以改成,这段代码的本质就是利用一个const vectorInt进行重新赋值,使不使用引用,对这个过程没有影响,只不过在调用这个函数的时候会有额外内存开销.
1 #pragma once 2 #include<iostream> 3 #include<cassert> 4 const double n = 0.0; 5 using namespace std; 6 class Matrix { 7 public: 8 Matrix(int n, int m); 9 Matrix(int n); 10 Matrix(const Matrix& x); 11 ~Matrix(); 12 void set(const double* pvalue); 13 void clear(); 14 const double& at(int i, int j) const; 15 double& at(int i, int j); 16 int get_lines() const; 17 int get_cols() const; 18 void display() const; 19 private: 20 int lines; 21 int clos; 22 double* ptr; 23 }; 24 Matrix::Matrix(int n,int m):lines{n},clos{m}{ 25 ptr = new double[n*m]; 26 } 27 Matrix::Matrix(int n) :lines{ n }, clos{ n } { 28 ptr = new double[n * n]; 29 } 30 Matrix::Matrix(const Matrix& x) :lines{ x.lines }, clos{x.clos}{ 31 ptr = new double[lines * clos]; 32 for (int i = 0; i < lines * clos; i++) ptr[i] = x.ptr[i]; 33 } 34 Matrix::~Matrix() { 35 delete[] ptr; 36 } 37 void Matrix::set(const double* pvalue) { 38 for (int i = 0; i < lines * clos; i++) 39 ptr[i] = pvalue[i]; 40 } 41 void Matrix::clear() { 42 for (int i = 0; i < lines * clos; i++) 43 ptr[i] = 0; 44 } 45 const double& Matrix::at(int i, int j) const { 46 assert(i >= 0 && i < lines && j >= 0 && j < clos); 47 return ptr[i * clos + j]; 48 } 49 double& Matrix::at(int i, int j) { 50 assert(i >= 0 && i < lines && j >= 0 && j < clos); 51 return ptr[i * clos + j]; 52 } 53 int Matrix::get_lines()const { 54 return lines; 55 } 56 int Matrix::get_cols()const { 57 return clos; 58 } 59 void Matrix::display() const { 60 for (int i = 0; i < lines; i++) { 61 for (int j = 0; j < clos; j++) 62 cout << ptr[i * clos + j] << ", "; 63 cout << "\b\b \n"; 64 } 65 } 66 67 68 69 #include "matrix.h" 70 #include <iostream> 71 #include <cassert> 72 73 using std::cin; 74 using std::cout; 75 using std::endl; 76 77 78 const int N = 1000; 79 80 // 输出矩阵对象索引为index所在行的所有元素 81 void output(const Matrix& m, int index) { 82 assert(index >= 0 && index < m.get_lines()); 83 84 for (auto j = 0; j < m.get_cols(); ++j) 85 cout << m.at(index, j) << ", "; 86 cout << "\b\b \n"; 87 } 88 89 90 void test1() { 91 double x[1000] = { 9,8,7,6,5,4,3,2,1 }; 92 93 int n, m; 94 cout << "Enter n and m: "; 95 cin >> n >> m; 96 97 Matrix m1(n, m); // 创建矩阵对象m1, 大小n×m 98 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 99 100 Matrix m2(m, n); // 创建矩阵对象m1, 大小m×n 101 m2.set(x); // 用一维数组x的值按行为矩阵m1赋值 102 103 Matrix m3(2); // 创建一个2×2矩阵对象 104 m3.set(x); // 用一维数组x的值按行为矩阵m4赋值 105 106 cout << "矩阵对象m1: \n"; m1.display(); cout << endl; 107 cout << "矩阵对象m2: \n"; m2.display(); cout << endl; 108 cout << "矩阵对象m3: \n"; m3.display(); cout << endl; 109 } 110 111 void test2() { 112 Matrix m1(2, 3); 113 m1.clear(); 114 115 const Matrix m2(m1); 116 m1.at(0, 0) = -999; 117 118 cout << "m1.at(0, 0) = " << m1.at(0, 0) << endl; 119 cout << "m2.at(0, 0) = " << m2.at(0, 0) << endl; 120 cout << "矩阵对象m1第0行: "; output(m1, 0); 121 cout << "矩阵对象m2第0行: "; output(m2, 0); 122 } 123 124 int main() { 125 cout << "测试1: \n"; 126 test1(); 127 128 cout << "测试2: \n"; 129 test2(); 130 }
1 #pragma once 2 #include<iostream> 3 #include<vector> 4 using namespace std; 5 class User { 6 public: 7 User(const string& name1, const string& password1="123456", const string& email1=""); 8 void set_email(); 9 void change_password(); 10 void display() const; 11 private: 12 string name; 13 string password; 14 string email; 15 }; 16 User::User(const string& name1, const string& password1, const string& email1) :name{ name1 }, password{ password1 }, email{ email1 } { 17 } 18 void User::set_email() { 19 cout << "Enter email address: "; 20 string emaill; 21 cin >> emaill; 22 while (1) { 23 if (emaill.find('@') != string::npos) { 24 email = emaill; 25 cout << "email is set successfully..." << endl; 26 break; 27 } 28 else { 29 cout << "illegal email.Please re-enter email: "; 30 cin >> emaill; 31 cout; 32 } 33 } 34 } 35 void User::change_password() { 36 cout << "Enter old password: "; 37 string password1; 38 cin >> password1; 39 int cnts = 1; 40 while (cnts < 3) { 41 if (password1 == password) { 42 cout << "new password: "; 43 cin >> password1; 44 password = password1; 45 cout << "new password is set successfully..." ; 46 break; 47 } 48 else { 49 cout << "password input error. Please re-enter agagin"; 50 cin >> password1; 51 cnts++; 52 } 53 } 54 if (cnts == 3) cout << "password input error.Please try after a while" << endl;; 55 } 56 void User::display()const { 57 cout << "name: " << name << endl; 58 cout << "pass: " << password << endl; 59 cout << "email: " << email << endl; 60 } 61 62 63 64 65 #include "user.hpp" 66 #include <iostream> 67 #include <vector> 68 #include <string> 69 70 using std::cin; 71 using std::cout; 72 using std::endl; 73 using std::vector; 74 using std::string; 75 76 void test() { 77 vector<User> user_lst; 78 79 User u1("Alice", "2024113", "Alice@hotmail.com"); 80 user_lst.push_back(u1); 81 cout << endl; 82 83 User u2("Bob"); 84 u2.set_email(); 85 u2.change_password(); 86 user_lst.push_back(u2); 87 cout << endl; 88 89 User u3("Hellen"); 90 u3.set_email(); 91 u3.change_password(); 92 user_lst.push_back(u3); 93 cout << endl; 94 95 cout << "There are " << user_lst.size() << " users. they are: " << endl; 96 for (auto& i : user_lst) { 97 i.display(); 98 cout << endl; 99 } 100 } 101 102 int main() { 103 test(); 104 }
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 }