实验三
task 1
main.cpp
#include "window.hpp" #include <iostream> using std::cout; using std::cin; void test() { Window w1("new window"); w1.add_button("maximize"); w1.display(); w1.close(); } int main() { cout << "用组合类模拟简单GUI:\n"; test(); }
window.hpp
#pragma once #include "button.hpp" #include <vector> #include <iostream> using std::vector; using std::cout; using std::endl; // 窗口类 class Window{ public: Window(const string &win_title); void display() const; void close(); void add_button(const string &label); private: string title; vector<Button> buttons; }; Window::Window(const string &win_title): title{win_title} { buttons.push_back(Button("close")); } inline void Window::display() const { string s(40, '*'); cout << s << endl; cout << "window title: " << title << endl; cout << "It has " << buttons.size() << " buttons: " << endl; for(const auto &i: buttons) cout << i.get_label() << " button" << endl; cout << s << endl; } void Window::close() { cout << "close window '" << title << "'" << endl; buttons.at(0).click(); } void Window::add_button(const string &label) { buttons.push_back(Button(label)); }
button.hpp
#pragma once #include <iostream> #include <string> using std::string; using std::cout; // 按钮类 class Button { public: Button(const string &text); string get_label() const; void click(); private: string label; }; Button::Button(const string &text): label{text} { } inline string Button::get_label() const { return label; } void Button::click() { cout << "Button '" << label << "' clicked\n"; }
运行截图
问题1:定义了botton和window类,使用了标准库的string和vector类,button类中使用了string类,window类中使用了botton类和vector类
问题2:不适合 不应该设置成inline
,因为它可能不是频繁调用的小函数,且可能需要从外部访问。
问题3:的功能是创建一个长度为40、填充字符为*
的字符串s
。这个字符串用于在控制台输出窗口的边框。
task 2
#include <iostream> #include <vector> using namespace std; void output1(const vector<int> &v) { for(auto &i: v) cout << i << ", "; cout << "\b\b \n"; } void output2(const vector<vector<int>> v) { for(auto &i: v) { for(auto &j: i) cout << j << ", "; cout << "\b\b \n"; } } void test1() { vector<int> v1(5, 42); const vector<int> v2(v1); v1.at(0) = -999; cout << "v1: "; output1(v1); cout << "v2: "; output1(v2); cout << "v1.at(0) = " << v1.at(0) << endl; cout << "v2.at(0) = " << v2.at(0) << endl; } void test2() { vector<vector<int>> v1{{1, 2, 3}, {4, 5, 6, 7}}; const vector<vector<int>> v2(v1); v1.at(0).push_back(-999); cout << "v1: \n"; output2(v1); cout << "v2: \n"; output2(v2); vector<int> t1 = v1.at(0); cout << t1.at(t1.size()-1) << endl; const vector<int> t2 = v2.at(0); cout << t2.at(t2.size()-1) << endl; } int main() { cout << "测试1:\n"; test1(); cout << "\n测试2:\n"; test2(); }
问题一:第一行代码创建了一个包含5个元素的vector<int>并把
元素初始化为42。
第二行代码创建了另一个vector<int>
,它通过复制构造函数复制了v1
。
第三行代码将v1
的第一个元素修改为-999。
问题二:第一行代码创建了一个包含两个vector<int>
的vector
,每个vector<int>
分别包含3个元素。
第二行代码创建了一个v1
的常量副本v2
。
第三行代码在v1
的第一个vector<int>
的末尾端添加了元素-999
问题三:第一行代码创建了一个vector<int>
的副本t1
,它是v1
的第一个vector<int>
。
第二行代码打印了t1
的最后一个元素。
第三行代码创建了v2
的第一个vector<int>
的常量副本t2
。
第四行代码打印了t2
的最后一个元素。
问题四:深复制 需要接口
task 3
main.cpp
#include "vectorInt.hpp" #include <iostream> using std::cin; using std::cout; void output(const vectorInt &vi) { for(auto i = 0; i < vi.get_size(); ++i) cout << vi.at(i) << ", "; cout << "\b\b \n"; } void test1() { int n; cout << "Enter n: "; cin >> n; vectorInt x1(n); for(auto i = 0; i < n; ++i) x1.at(i) = i*i; cout << "x1: "; output(x1); vectorInt x2(n, 42); vectorInt x3(x2); x2.at(0) = -999; cout << "x2: "; output(x2); cout << "x3: "; output(x3); } void test2() { const vectorInt x(5, 42); vectorInt y(10, 0); cout << "y: "; output(y); y.assign(x); cout << "y: "; output(y); cout << "x.at(0) = " << x.at(0) << endl; cout << "y.at(0) = " << y.at(0) << endl; } int main() { cout << "测试1: \n"; test1(); cout << "\n测试2: \n"; test2(); }
vectorInt.hpp
#pragma once #include <iostream> #include <cassert> using std::cout; using std::endl; // 动态int数组对象类 class vectorInt{ public: vectorInt(int n); vectorInt(int n, int value); vectorInt(const vectorInt &vi); ~vectorInt(); int& at(int index); const int& at(int index) const; vectorInt& assign(const vectorInt &v); int get_size() const; private: int size; int *ptr; // ptr指向包含size个int的数组 }; vectorInt::vectorInt(int n): size{n}, ptr{new int[size]} { } vectorInt::vectorInt(int n, int value): size{n}, ptr{new int[size]} { for(auto i = 0; i < size; ++i) ptr[i] = value; } vectorInt::vectorInt(const vectorInt &vi): size{vi.size}, ptr{new int[size]} { for(auto i = 0; i < size; ++i) ptr[i] = vi.ptr[i]; } vectorInt::~vectorInt() { delete [] ptr; } const int& vectorInt::at(int index) const { assert(index >= 0 && index < size); return ptr[index]; } int& vectorInt::at(int index) { assert(index >= 0 && index < size); return ptr[index]; } vectorInt& vectorInt::assign(const vectorInt &v) { delete[] ptr; // 释放对象中ptr原来指向的资源 size = v.size; ptr = new int[size]; for(int i = 0; i < size; ++i) ptr[i] = v.ptr[i]; return *this; } int vectorInt::get_size() const { return size; }
运行截图
问题一:深复制
问题二:不能正确运行
有安全隐患,违反 const
对象的常量性质,可能导致程序中的其他部分代码出现未定义行为
问题三:可以。这样使代码更加简洁但也会导致混淆
task 4
matrix.hpp
#pragma once #include <iostream> #include <cassert> using std::cout; using std::endl; // 类Matrix的声明 class Matrix { public: Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵, 初始值为value Matrix(int n); // 构造函数,构造一个n*n的矩阵, 初始值为value Matrix(const Matrix &x); // 复制构造函数, 使用已有的矩阵X构造 ~Matrix(); void set(const double *pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值 void clear(); // 把矩阵对象的值置0 const double& at(int i, int j) const; // 返回矩阵对象索引(i,j)的元素const引用 double& at(int i, int j); // 返回矩阵对象索引(i,j)的元素引用 int get_lines() const; // 返回矩阵对象行数 int get_cols() const; // 返回矩阵对象列数 void display() const; // 按行显示矩阵对象元素值 private: int lines; // 矩阵对象内元素行数 int cols; // 矩阵对象内元素列数 double *ptr; }; // 类Matrix的实现:待补足 Matrix::Matrix(int n, int m) : lines(n), cols(m) { ptr = new double[lines * cols](); } Matrix::Matrix(int n) : lines(n), cols(n) { ptr = new double[lines * cols](); } Matrix::Matrix(const Matrix &x) : lines(x.lines), cols(x.cols) { ptr = new double[lines * cols]; for (int i = 0; i < lines * cols; ++i) { ptr[i] = x.ptr[i]; } } Matrix::~Matrix() { delete[] ptr; } void Matrix::set(const double *pvalue) { for (int i = 0; i < lines * cols; ++i) { ptr[i] = pvalue[i]; } } void Matrix::clear() { for (int i = 0; i < lines * cols; ++i) { ptr[i] = 0; } } const double& Matrix::at(int i, int j) const { assert(i >= 0 && i < lines && j >= 0 && j < cols); return ptr[i * cols + j]; } double& Matrix::at(int i, int j) { assert(i >= 0 && i < lines && j >= 0 && j < cols); return ptr[i * cols + j]; } int Matrix::get_lines() const { return lines; } int Matrix::get_cols() const { return cols; } void Matrix::display() const { for (int i = 0; i < lines; ++i) { for (int j = 0; j < cols; ++j) { cout << ptr[i * cols + j] << " "; } cout << endl; } }
task4.cpp
#include "matrix.hpp" #include <iostream> #include <cassert> using std::cin; using std::cout; using std::endl; const int N = 1000; // 输出矩阵对象索引为index所在行的所有元素 void output(const Matrix &m, int index) { assert(index >= 0 && index < m.get_lines()); for(int j = 0; j < m.get_cols(); ++j) cout << m.at(index, j) << ", "; cout << "\b\b \n"; } void test1() { double x[1000] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int n, m; cout << "Enter n and m: "; cin >> n >> m; Matrix m1(n, m); // 创建矩阵对象m1, 大小n×m m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 Matrix m2(m, n); // 创建矩阵对象m1, 大小m×n m2.set(x); // 用一维数组x的值按行为矩阵m1赋值 Matrix m3(2); // 创建一个2×2矩阵对象 m3.set(x); // 用一维数组x的值按行为矩阵m4赋值 cout << "矩阵对象m1: \n"; m1.display(); cout << endl; cout << "矩阵对象m2: \n"; m2.display(); cout << endl; cout << "矩阵对象m3: \n"; m3.display(); cout << endl; } void test2() { Matrix m1(2, 3); m1.clear(); const Matrix m2(m1); m1.at(0, 0) = -999; cout << "m1.at(0, 0) = " << m1.at(0, 0) << endl; cout << "m2.at(0, 0) = " << m2.at(0, 0) << endl; cout << "矩阵对象m1第0行: "; output(m1, 0); cout << "矩阵对象m2第0行: "; output(m2, 0); } int main() { cout << "测试1: \n"; test1(); cout << "测试2: \n"; test2(); }
task 5
main.cpp
#include "user.hpp" #include <iostream> #include <vector> #include <string> using std::cin; using std::cout; using std::endl; using std::vector; using std::string; void test() { vector<User> user_lst; User u1("Alice", "2024113", "Alice@hotmail.com"); user_lst.push_back(u1); cout << endl; User u2("Bob"); u2.set_email(); u2.change_password(); user_lst.push_back(u2); cout << endl; User u3("Hellen"); u3.set_email(); u3.change_password(); user_lst.push_back(u3); cout << endl; cout << "There are " << user_lst.size() << " users. they are: " << endl; for(auto &i: user_lst) { i.display(); cout << endl; } } int main() { test(); }
user.hpp
#include <iostream> #include <string> class User { public: // 构造函数 User(const std::string& name, const std::string& password = "123456", const std::string& email = ""); // 设置邮箱 void set_email(); // 修改密码 void change_password(); // 显示用户信息 void display(); private: std::string name; // 用户名 std::string password; // 密码 std::string email; // 邮箱 }; // 构造函数 User::User(const std::string& name, const std::string& password, const std::string& email) : name(name), password(password), email(email) {} // 设置邮箱 void User::set_email() { std::string input; std::cout << "Enter email for " << name << ": "; std::cin.ignore(); // 忽略之前读取的换行符 std::getline(std::cin, input); while (input.find('@') == std::string::npos) { std::cout << "Invalid email. Please enter a valid email: "; std::getline(std::cin, input); } email = input; } // 修改密码 void User::change_password() { std::string old_password, new_password, confirm_password; std::cout << "Enter old password: "; std::cin.ignore(); std::getline(std::cin, old_password); int attempts = 0; while (old_password != password && attempts < 3) { std::cout << "Incorrect password. Try again: "; std::getline(std::cin, old_password); attempts++; } if (attempts == 3) { std::cout << "Too many failed attempts. Exiting password change." << std::endl; return; } std::cout << "Enter new password: "; std::getline(std::cin, new_password); std::cout << "Confirm new password: "; std::getline(std::cin, confirm_password); if (new_password == confirm_password) { password = new_password; } else { std::cout << "Passwords do not match. Password change cancelled." << std::endl; } } // 显示用户信息 void User::display() { std::cout << "Username: " << name << ", Password: " << std::string(password.size(), '*') << ", Email: " << email << std::endl; }
运行截图
task 6
main.cpp
#include "account.h" #include <iostream> using namespace std; int main(){ SavingAccount sa0(1,21325302,0.015); SavingAccount sa1(1,58320212,0.015); sa0.deposit(5,5000); sa1.deposit(25,10000); sa0.deposit(45,5500); sa1.withdraw(60,4000); sa0.settle(90); sa1.settle(90); sa0.show();cout<<endl; sa1.show();cout<<endl; cout<<"Total:"<<SavingAccount::getTotal()<<endl; return 0; }
account.cpp
#include "account.h" #include <cmath> #include <iostream> using namespace std; double SavingAccount::total=0; SavingAccount::SavingAccount(int date,int id,double rate) :id(id),balance(0),rate(rate),lastDate(date),accumulation(0){ cout<<date<<"\t#"<<id<<"is created"<<endl; } void SavingAccount::record(int date,double amount){ accumulation=accumulate(date); lastDate=date; amount=floor(amount*100+0.5)/100; balance+=amount; total+=amount; cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl; } void SavingAccount::deposit(int date,double amount){ record(date,amount); } void SavingAccount::withdraw(int date,double amount){ if(amount>getBalance()){ cout<<"Error:not enough money"<<endl; } else{ record(date,-amount); } } void SavingAccount::settle(int date){ double interest=accumulate(date)*rate/365; if(interest!=0) record(date,interest); accumulation=0; } void SavingAccount::show() const{ cout<<"#"<<id<<"\tBalance:"<<balance; }
account.h
#include "account.h" #include <cmath> #include <iostream> using namespace std; double SavingAccount::total=0; SavingAccount::SavingAccount(int date,int id,double rate) :id(id),balance(0),rate(rate),lastDate(date),accumulation(0){ cout<<date<<"\t#"<<id<<"is created"<<endl; } void SavingAccount::record(int date,double amount){ accumulation=accumulate(date); lastDate=date; amount=floor(amount*100+0.5)/100; balance+=amount; total+=amount; cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl; } void SavingAccount::deposit(int date,double amount){ record(date,amount); } void SavingAccount::withdraw(int date,double amount){ if(amount>getBalance()){ cout<<"Error:not enough money"<<endl; } else{ record(date,-amount); } } void SavingAccount::settle(int date){ double interest=accumulate(date)*rate/365; if(interest!=0) record(date,interest); accumulation=0; } void SavingAccount::show() const{ cout<<"#"<<id<<"\tBalance:"<<balance; }