任务1:
window.cpp
1 #pragma once 2 #include"button.hpp" 3 #include<vector> 4 //vector 5 #include<iostream> 6 7 using std::vector; 8 using std::cout; 9 using std::endl; 10 11 class Window{ 12 public: 13 Window(const string &win_title); 14 void display() const; 15 void close(); 16 void add_button(const string &label); 17 18 private: 19 string title; 20 vector<Button> buttons; 21 }; 22 23 Window::Window(const string &win_title): title{win_title}{ 24 buttons.push_back(Button("close")); 25 //push_back 26 } 27 28 inline void Window::display() const{ 29 string s(40,'*'); 30 31 cout<<s<<endl; //定义一个字符串s由40个*组成,输出,分隔作用 32 cout<<"window title: "<<title<<endl; 33 cout<<"It has "<<buttons.size()<<" buttons: "<<endl; 34 //size() 35 for(const auto &i:buttons) 36 cout<<i.get_label()<<" button"<<endl; 37 cout<<s<<endl; 38 } 39 40 void Window::close(){ 41 cout<<"close window '"<<title<<"'"<<endl; 42 buttons.at(0).click(); 43 //at() 44 } 45 46 void Window::add_button(const string &label){ 47 buttons.push_back(Button(label)); 48 }
button.cpp
1 #pragma once 2 3 #include<iostream> 4 #include<string> 5 //string 6 7 using std::string; 8 using std::cout; 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 }
task1.pp
1 #include "window.hpp" 2 #include<iostream> 3 4 using std::cout; 5 using std::cin; 6 7 void test(){ 8 Window w1("new window"); 9 w1.add_button("maximize"); 10 w1.display(); 11 w1.close(); 12 13 } 14 15 int main(){ 16 cout<<"用组合类模拟简单GUI:\n"; 17 test(); 18 }
运行结果截图:
问题1:
自定义了里两个类 Window 和 Button,使用了标准库的 vector 和 string 两个标准准库类,string和button,window,string,vector之间存在组合关系。
问题2:
click和close都没有修改成员变量,可以加const提高安全性。
问题3:
功能是自定义了一个字符串s,它由40个*组成并输出,起到了分隔的作用。
任务2:
task2.cpp:
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 << "\n测试2:\n"; 40 test2(); 41 }
运行结果截图:
问题1:
line21:创建一个int型的vector容器v1,其中值为5和42。
line22:创建一个int型的vector容器v2,将v1复制给v2。
line24:v1中第一个值改为-999。
问题2:
line32:创建一个vector容器v1,其中存放两个int型的vector,他们中存放的值分别为1,2,3和4,5,6,7。
line33:创建一个const 类型的vector容器v2,其中存放int型的vector,用v1复制构造初始化。
line35:在v1的第一个vector的最后追加一个元素,值为-999。
问题3:
创建一个vector容器v1,其中值为vector v1的第一个元素。
line40:输出t1最后一个元素的值。
line42:创建一个const 类型的 vector容器t2,其中值为vector v2的第一个元素。
line43:输出t2最后一个元素的值。
问题4:
深复制。
不需要。
任务3:
task3.cpp:
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 }
vectorInt.hpp:
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:
深复制。
问题2:
不能,左边返回值类型是一个常数不能被修改。
存在安全隐患:去掉const 可能导致值的修改影响结果。
问题3:
可以,进行一个赋值操作,但会有额外内存开销。
任务4:
matrix.hpp:
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 Matrix::Matrix(int n,int m):lines(n),cols(m){ 36 ptr=new double[n*m]; 37 } 38 39 Matrix::Matrix(int n):lines(n),cols(n){ 40 ptr=new double[n*n]; 41 } 42 43 Matrix::Matrix(const Matrix &x):lines(x.lines),cols(x.cols){ 44 ptr=new double[lines*cols]; 45 for(int i=0;i<lines*cols;i++) 46 ptr[i]=x.ptr[i]; 47 } 48 49 Matrix::~Matrix(){ 50 delete[] ptr; 51 } 52 53 void Matrix::set(const double *pvalue){ 54 for(int i=0;i<lines*cols;i++) 55 ptr[i]=*(pvalue+i); 56 } 57 58 void Matrix::clear(){ 59 for(int i=0;i<lines*cols;i++) 60 ptr[i]=0; 61 } 62 63 const double& Matrix::at(int i,int j) const{ 64 assert(i>=0&&i<lines&&j>=0&&j<cols); 65 return ptr[i*cols+j]; 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 { 83 for(int j=0;j<cols;j++) 84 cout<<at(i,j)<<" "; 85 cout<<endl; 86 } 87 } 88 // xxx
task4.cpp:
1 #include "matrix.hpp" 2 #include <iostream> 3 #include <cassert> 4 5 using std::cin; 6 using std::cout; 7 using std::endl; 8 9 10 const int N = 1000; 11 12 // 输出矩阵对象索引为index所在行的所有元素 13 void output(const Matrix &m, int index) { 14 assert(index >= 0 && index < m.get_lines()); 15 16 for(auto j = 0; j < m.get_cols(); ++j) 17 cout << m.at(index, j) << ", "; 18 cout << "\b\b \n"; 19 } 20 21 22 void test1() { 23 double x[1000] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 24 25 int n, m; 26 cout << "Enter n and m: "; 27 cin >> n >> m; 28 29 Matrix m1(n, m); // 创建矩阵对象m1, 大小n×m 30 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 31 32 Matrix m2(m, n); // 创建矩阵对象m1, 大小m×n 33 m2.set(x); // 用一维数组x的值按行为矩阵m1赋值 34 35 Matrix m3(2); // 创建一个2×2矩阵对象 36 m3.set(x); // 用一维数组x的值按行为矩阵m4赋值 37 38 cout << "矩阵对象m1: \n"; m1.display(); cout << endl; 39 cout << "矩阵对象m2: \n"; m2.display(); cout << endl; 40 cout << "矩阵对象m3: \n"; m3.display(); cout << endl; 41 } 42 43 void test2() { 44 Matrix m1(2, 3); 45 m1.clear(); 46 47 const Matrix m2(m1); 48 m1.at(0, 0) = -999; 49 50 cout << "m1.at(0, 0) = " << m1.at(0, 0) << endl; 51 cout << "m2.at(0, 0) = " << m2.at(0, 0) << endl; 52 cout << "矩阵对象m1第0行: "; output(m1, 0); 53 cout << "矩阵对象m2第0行: "; output(m2, 0); 54 } 55 56 int main() { 57 cout << "测试1: \n"; 58 test1(); 59 60 cout << "测试2: \n"; 61 test2(); 62 }
运行结果截图:
任务5:
user.hpp
1 #include<bits/stdc++.h> 2 using namespace std; 3 class User{ 4 private: 5 string name; 6 string password; 7 string email; 8 9 public: 10 User(const string &name,const string &password="123456", const string &email=""):name(name),password(password),email(email){} 11 void set_email(){ 12 string _email; 13 int tag=0; 14 cout<<"Enter email address: "; 15 while(tag==0){ 16 cin>>_email; 17 int len=sizeof(_email); 18 for(int i=0;i<len;i++){ 19 if(_email[i]=='@') 20 tag=1; 21 } 22 if(tag) 23 cout<< "email is set successfully..."<<endl; 24 else 25 cout<<"illegal email. Please re-enter email:"; 26 } 27 28 } 29 30 void change_password(){ 31 int attempt=3; 32 string old_password , new_password; 33 cout<<"Enter old password: "; 34 while(attempt>0){ 35 attempt--; 36 cin>>old_password; 37 if(old_password==password){ 38 cout<<"Enter new password: "; 39 cin>>new_password; 40 password=new_password; 41 cout<<"new password is set successfully..."<<endl; 42 return ; 43 } 44 else{ 45 cout<<"password input error. Please re-enter again: "; 46 } 47 } 48 cout<<"password input error. Please try after a while. "<<endl; 49 } 50 51 void display() const { 52 cout<<"name: "<<name<<endl; 53 cout<<"pass: "; 54 int length=sizeof(password); 55 for(int i=1;i<=length;i++) 56 cout<<"*"; 57 cout<<endl; 58 cout<<"email: "<<email<<endl; 59 } 60 };
task5.cpp
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:
date.h
1 #include "account.h" 2 #include <iostream> 3 using namespace std; 4 int main() { 5 //起始日期 6 Date date(2008, 11, 1); 7 //建立几个账户 8 SavingsAccount accounts[] = { 9 SavingsAccount(date,"03755217",0.015), 10 SavingsAccount(date, "02342342", 0.015) 11 }; 12 const int n = sizeof(accounts) / sizeof(SavingsAccount); //账户总数 13 14 accounts[0].deposit(Date(2008, 11, 5), 5000, "salary"); 15 accounts[1].deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 16 17 accounts[0].deposit(Date(2008, 12, 5), 5500, "salary"); 18 accounts[1].withdraw(Date(2008, 12, 20), 4000, "buy a laptop"); 19 20 cout << endl; 21 for (int i = 0; i < n; i++) { 22 accounts[i].settle(Date(2009, 1, 1)); 23 accounts[i].show(); 24 cout << endl; 25 } 26 cout << "Total: " << SavingsAccount::getTotal() << endl; 27 return 0; 28 }
date.cpp
1 #include "date.h" 2 #include <iostream> 3 #include <cstdlib> 4 using namespace std; 5 namespace { 6 const int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; 7 } 8 Date::Date(int year, int month, int day) : year(year), month(month), day(day) { 9 if (day <= 0 || day > getMaxDay()) { 10 cout << "Invalid date:"; 11 show(); 12 cout << endl; 13 exit(1); 14 } 15 int years = year - 1; 16 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day; 17 if (isLeapYear() && month > 2) totalDays++; 18 } 19 int Date::getMaxDay()const { 20 if (isLeapYear() && month == 2) 21 return 29; 22 else 23 return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; 24 } 25 void Date::show() const { 26 cout << getYear() << " - " << getMonth() << " - " << getDay(); 27 }
account.cpp
1 #include "account.h" 2 #include <cmath> 3 #include <iostream> 4 using namespace std; 5 double SavingsAccount::total = 0; 6 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { 7 date.show(); 8 cout << "\t#" << id << "created" << endl; 9 } 10 void SavingsAccount::record(const Date& date, double amount, const string& desc) { 11 accumulation = accumulate(date); 12 lastDate = date; 13 amount = floor(amount * 100 + 0.5) / 100; 14 balance += amount; 15 total += amount; 16 date.show(); 17 cout << "t\#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 18 } 19 void SavingsAccount::error(const string& msg) const { 20 cout << "Error(# " << id << " ): " << msg << endl; 21 } 22 void SavingsAccount::deposit(const Date & date, double amount, const string & desc) { 23 record(date, amount, desc); 24 } 25 void SavingsAccount::withdraw(const Date & date, double amount, const string& desc) { 26 if (amount > getBalance()) 27 error("not enough money"); 28 else 29 record(date, -amount, desc); 30 } 31 void SavingsAccount::settle(const Date& date) { 32 double interest = accumulate(date) * rate//计算年息 33 / date.distance(Date(date.getYear() - 1, 1, 1)); 34 if (interest != 0) 35 record(date, interest, "interest"); 36 accumulation = 0; 37 } 38 void SavingsAccount::show() const { 39 cout << "ID: "<< id << "\tBalance:" << balance; 40 }
account.h
1 #ifndef __ACCOUNT_H__ 2 #define __ACCOUNT_H__ 3 #include "date.h" 4 #include <string> 5 class SavingsAccount { 6 7 private: 8 std::string id; 9 double balance,rate,accumulation; 10 Date lastDate; 11 static double total; 12 13 void record(const Date& date, double amount, const std::string& desc); 14 15 void error(const std::string& msg) const; 16 17 double accumulate(const Date& date) const { 18 return accumulation + balance * date.distance(lastDate); 19 } 20 public: 21 SavingsAccount(const Date& date, const std::string& id, double rate); 22 const std::string& getId() const { 23 return id; 24 } 25 double getBalance() const { 26 return balance; 27 } 28 double getRate() const { 29 return rate; 30 } 31 static double getTotal() { 32 return total; 33 } 34 35 void deposit(const Date& date, double amount, const std::string& desc); 36 37 void withdraw(const Date& date, double amount, const std::string& desc); 38 39 void settle(const Date& date); 40 41 void show() const; 42 }; 43 #endif
6_25.cpp
1 #include "account.h" 2 #include <iostream> 3 using namespace std; 4 int main() { 5 //起始日期 6 Date date(2008, 11, 1); 7 //建立几个账户 8 SavingsAccount accounts[] = { 9 SavingsAccount(date,"03755217",0.015), 10 SavingsAccount(date, "02342342", 0.015) 11 }; 12 const int n = sizeof(accounts) / sizeof(SavingsAccount); //账户总数 13 14 accounts[0].deposit(Date(2008, 11, 5), 5000, "salary"); 15 accounts[1].deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 16 17 accounts[0].deposit(Date(2008, 12, 5), 5500, "salary"); 18 accounts[1].withdraw(Date(2008, 12, 20), 4000, "buy a laptop"); 19 20 cout << endl; 21 for (int i = 0; i < n; i++) { 22 accounts[i].settle(Date(2009, 1, 1)); 23 accounts[i].show(); 24 cout << endl; 25 } 26 cout << "Total: " << SavingsAccount::getTotal() << endl; 27 return 0; 28 }
运行结果截图: