实验三 c++

实验任务一

源代码

button.hpp

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<string>
 5 
 6 using std::string;
 7 using std::cout;
 8 
 9 class Button{
10 public:
11     Button(const string &text);
12     string get_label()const;
13     void click();
14     
15 private:
16     string label;
17 };
18 
19 Button::Button(const string &text): label{text}{}
20 
21 inline string Button::get_label()const{
22     return label;
23 }
24 
25 void Button::click() {
26     cout<<"Button '"<<label<<"' clicked\n";
27 }

window.hpp

 1 #pragma once
 2 #include "button.hpp"
 3 #include <vector>
 4 #include <iostream>
 5 
 6 using std::vector;
 7 using std::cout;
 8 using std::endl;
 9 
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 }
26 
27 inline void Window::display() const {
28     string s(40, '*');
29 
30     cout << s << endl;
31     cout << "window title: " << title << endl;
32     cout << "It has " << buttons.size() << " buttons: " << endl;
33     for(const auto &i: buttons)
34         cout << i.get_label() << " button" << endl;
35     cout << s << endl;
36 }
37 
38 void Window::close() {
39     cout << "close window '" << title << "'" << endl;
40     buttons.at(0).click();
41 }
42 
43 void Window::add_button(const string &label) {
44     buttons.push_back(Button(label));
45 }

test.cpp

 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 int main() {
15     cout << "用组合类模拟简单GUI:\n";
16     test();
17 }

运行结果截图

问题1.自定义了两个类,使用了标准库的vetor,string。Button和Window是组合关系,vector和Window是组合关系,string和Window是组合关系

问题2.不需要,他们在调用时,不会改变成员数据

问题3.功能是打印40个*

实验任务二

源代码

 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,元素都是42的数组v1,第二行是复制v1的数组到v2,第三行是将v1的第一个元素改为-999。

问题2.第一行创建一个二维数组v1,共有两行,前三个元素一行,后四个元素一行。第二行复制v1到数组v2。第三行在v1的第一行中添加元素-999

问题3.将v1的第一行赋值给t1,之后输出t1的最后一个元素,再将v2的第一行赋值给t2,输出t2的最后一个元素

问题4.①深复制②不需要

实验任务三

源代码

vectorInt.hpp

 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <cassert>
 5 
 6 using std::cout;
 7 using std::endl;
 8 
 9 class vectorInt {
10 public:
11     vectorInt(int n);
12     vectorInt(int n, int value);
13     vectorInt(const vectorInt& vi);
14     ~vectorInt();
15 
16     int& at(int index);
17     const int& at(int index)const;
18     vectorInt& assign(const vectorInt& v);
19     int get_size()const;
20 
21 private:
22     int size;
23     int* ptr;
24 };
25 
26 vectorInt::vectorInt(int n) :size{ n }, ptr{ new int[size] } {
27 }
28 
29 vectorInt::vectorInt(int n, int value) :size{ n }, ptr{ new int[size] } {
30     for (auto i = 0; i < size; i++) {
31         ptr[i] = value;
32     }
33 }
34 
35 vectorInt::vectorInt(const vectorInt& vi) :size{ vi.size }, ptr{ new int[size] } {
36     for (auto i = 0; i < size; i++) {
37         ptr[i] = vi.ptr[i];
38     }
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;
59 
60     size = v.size;
61     ptr = new int[size];
62     for (int i = 0; i < size; i++)
63         ptr[i] = v.ptr[i];
64 
65     return *this;
66 }
67 
68 int vectorInt::get_size()const {
69     return size;
70 
71 }

test3.cpp

 1 #include"vecterint.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 void test1() {
14     int n;
15     cout << "Enter n: ";
16     cin >> n;
17 
18     vectorInt x1(n);
19     for (auto i = 0; i < n; i++) 
20         x1.at(i) = i * i;
21     cout << "x1: "; output(x1);
22 
23     vectorInt x2(n, 42);
24     vectorInt x3(x2);
25     x2.at(0) = -999;
26     cout << "x2: "; output(x2);
27     cout << "x3: "; output(x3);
28 }
29 
30 void test2() {
31     const vectorInt x(5, 42);
32     vectorInt y(10, 0);
33 
34     cout << "y: "; output(y);
35     y.assign(x);
36     cout << "y: "; output(y);
37 
38     cout << "x.at(0)= " << x.at(0) << endl;
39     cout << "y.at(0)= " << y.at(0) << endl;
40 
41 }
42 
43 int main() {
44     cout << "test1:\n";
45     test1();
46     cout << "test2:\n";
47     test2();
48 }

运行结果截图

问题1.深复制

问题2.不能正常运行,因为改成int后,将返回元素值的形参,无法更改其值。存在。因为去掉const后,后返回int&类型,而调用at这个接口的const型变量将有可能被改变值

问题3.可以,因为调用assign接口,并未改变对象成员数据的值,仅作复制操作,所以可以更改。

实验任务四

源代码

matrix.hpp

 1 #pragma once
 2 #include <iostream>
 3 #include <cassert>
 4 using std::cout;
 5 using std::endl;
 6 // 类Matrix的声明
 7 class Matrix {
 8 public:
 9     Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵, 初始值为value
10     Matrix(int n);// 构造函数,构造一个n*n的矩阵, 初始值为value
11     Matrix(const Matrix& x);// 复制构造函数, 使用已有的矩阵X构造
12     ~Matrix();
13     void set(const double* pvalue);// 用pvalue指向的连续内存块数据按行为矩阵赋值
14         void clear();// 把矩阵对象的值置0
15 
16     const double& at(int i, int j) const;// 返回矩阵对象索引(i,j)的元素const引用
17         double& at(int i, int j);// 返回矩阵对象索引(i,j)的元素引用
18 
19     int get_lines() const;// 返回矩阵对象行数
20     int get_cols() const;// 返回矩阵对象列数
21     void display() const;// 按行显示矩阵对象元素值
22 private:
23     int lines;// 矩阵对象内元素行数
24     int cols;// 矩阵对象内元素列数
25     double* ptr;
26 };
27 // 类Matrix的实现:待补足
28 Matrix::Matrix(int n, int m) :lines{ n }, cols{ m }, ptr{ new double[n * m ]} {}
29 Matrix::Matrix(int n):lines{n},cols{n},ptr{new double [n*n]}{}
30 Matrix::Matrix(const Matrix& x) :ptr{new double [x.lines*x.cols]} {
31     lines = x.lines;
32     cols = x.cols;
33     for (auto i = 0; i < lines * cols; i++)
34         ptr[i] = x.ptr[i];
35 }
36 Matrix::~Matrix() { delete[]ptr; }
37 void Matrix::set(const double* pvalue) {
38     for (auto i = 0; i < lines * cols; i++) {
39         ptr[i] = pvalue[i];
40     }
41 }
42 void Matrix::clear() {
43     for (auto i = 0; i < lines * cols; i++) {
44         ptr[i] = 0;
45     }
46 }
47 
48 const double& Matrix::at(int i, int j)const {
49     assert(i * j >= 0 && i * j < lines * cols);
50     return ptr[i*j+j];
51 }
52 
53 double& Matrix::at(int i, int j) {
54     assert(i * j >= 0 && i * j < lines * cols);
55     return ptr[i*j+j];
56 }
57 
58 int Matrix::get_lines()const {
59     return lines;
60 }
61 
62 int Matrix::get_cols()const {
63     return cols;
64 }
65 
66 void Matrix::display()const {
67     for (auto i = 0; i < lines*cols; i++) {
68         cout << ptr[i] << ", ";
69         if((i+1)%cols==0)
70         cout << "\b\b \n";
71     }
72     cout << endl;
73 }

task4.cpp

 1 #include "matrix.hpp"
 2 #include <iostream>
 3 #include <cassert>
 4 using std::cin;
 5 using std::cout;
 6 using std::endl;
 7 const int N = 1000;
 8 // 输出矩阵对象索引为index所在行的所有元素
 9 
10 void output(const Matrix& m, int index) {
11     assert(index >= 0 && index < m.get_lines());
12     for (auto j = 0; j < m.get_cols(); ++j)
13         cout << m.at(index, j) << ", ";
14     cout << "\b\b \n";
15 }
16 
17 void test1() {
18     double x[1000] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
19     int n, m;
20     cout << "Enter n and m: ";
21     cin >> n >> m;
22     Matrix m1(n, m);// 创建矩阵对象m1, 大小n×m
23     m1.set(x);// 用一维数组x的值按行为矩阵m1赋值
24     Matrix m2(m, n);// 创建矩阵对象m1, 大小m×n
25     m2.set(x);// 用一维数组x的值按行为矩阵m1赋值
26     Matrix m3(2);// 创建一个2×2矩阵对象
27     m3.set(x);// 用一维数组x的值按行为矩阵m4赋值
28     cout << "矩阵对象m1: \n";m1.display();cout << endl;
29     cout << "矩阵对象m2: \n";m2.display();cout << endl;
30     cout << "矩阵对象m3: \n";m3.display();cout << endl;
31 }
32 
33 void test2() {
34     Matrix m1(2, 3);
35     m1.clear();
36 
37     const Matrix m2(m1);
38     m1.at(0, 0) = -999;
39     cout << "m1.at(0, 0) = " << m1.at(0, 0) << endl;
40     cout << "m2.at(0, 0) = " << m2.at(0, 0) << endl;
41     cout << "矩阵对象m1第0行: "; output(m1, 0);
42     cout << "矩阵对象m2第0行: "; output(m2, 0);
43 }
44 int main() {
45     cout << "测试1: \n";
46     test1();
47     cout << "测试2: \n";
48     test2();
49 }

运行结果截图

 

实验任务五

源代码

User.hpp

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<iomanip>
 5 #include<string>
 6 #include<algorithm>
 7 
 8 using namespace std;
 9 
10 class User {
11 private:
12     string name,password,email;
13 public:
14     User(string N, string P = "123456", string E = "");
15     ~User(){}
16 
17     void set_email();
18     void change_password();
19     void display();
20 };
21 
22 User::User(string N, string P, string E):name{ N }, password{ P }, email{ E } {}
23 
24 void User::set_email() {
25     cout << "Enter email address: ";
26     while (1) {
27         cin >> email;
28         size_t index = email.find("@");
29         if (index != string::npos) {
30             cout << "email is set successfully..." << endl;
31             break;
32         }
33         cout << "illegal email. Please re-enter email: ";
34     }
35 }
36 
37 void User::change_password() {
38     string s;
39     cout << "Enter old password: ";
40     cin >> s;
41     for (int i = 0; i < 3; i++) {
42         if (s == password) {
43             cout << "Enter new password: ";
44             cin >> password;
45             cout<<"new password is set successfully..."<<endl;
46             break;
47         }
48         if (i == 2) {
49             cout << "password input error. Please try after a while." << endl;;
50             break;
51         }
52         cout << "password input error. Please re-enter angain: ";
53         cin >> s;
54     }
55 }
56 
57 void User::display() {
58     size_t n = password.length();
59     string s(n, '*');
60     cout << "name:  " << name << endl
61         << "pass:  " << s << endl
62         << "email: " << email << endl;
63 }

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 }

运行结果截图

实验任务六

源代码

date.h

 1 #pragma once
 2 
 3 class Date {
 4 private:
 5     int year;
 6     int month;
 7     int day;
 8     int totalDays;
 9 public:
10     Date(int year, int maonth, int day);
11     int getYear()const { return year;}
12     int getMonth()const { return month; }
13     int getDay()const { return day; }
14     int getMaxDay()const;
15     bool isLeapYear()const {
16         return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
17     }
18     void show()const;
19     int diatance(const Date& date)const {
20         return totalDays - date.totalDays;
21     }
22 };

date.cpp

 1 #include"date.h"
 2 #include<iostream>
 3 #include<cstdlib>
 4 using namespace std;
 5 namespace {
 6     const int DATS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
 7 }
 8 
 9 Date::Date(int year, int month, int day) :year{ year }, month{ month }, day{ day } {
10     if (day <= 0 || day > getMaxDay()) {
11         cout << "Invalid date: ";
12         show();
13         exit(1);
14     }
15     int years = year - 1;
16     totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DATS_BEFORE_MONTH[month - 1] + day;
17     if (isLeapYear() && month > 2)totalDays++;
18 }
19 
20 int Date::getMaxDay()const {
21     if (isLeapYear() && month == 2)
22         return 29;
23     else
24         return DATS_BEFORE_MONTH[month] - DATS_BEFORE_MONTH[month - 1];
25 }
26 
27 void Date::show()const {
28     cout << getYear() << "-" << getMonth() << "-" << getDay();
29 }

account.h

 1 #pragma once
 2 #include"date.h"
 3 #include<string>
 4 
 5 class SavingAccount {
 6 private:
 7     std::string id;
 8     double balance;
 9     double rate;
10     Date lastDate;
11     double accumulation;
12     static double total;
13 
14     void record(const Date& date, double amount, const std::string& desc);
15     void error(const std::string& msg)const;
16     double accumulate(const Date& date)const {
17         return accumulation + balance * date.diatance(lastDate);
18     }
19 
20 public:
21     SavingAccount(const Date& date, const std::string& id, double rate);
22     const std::string& getId()const { return id; }
23     double getBalance()const { return balance;}
24     double getRate()const { return rate; }
25     static double getTotal() { return total; }
26     
27     void deposit(const Date& date, double amount, const std::string& desc);
28     void withdraw(const Date& date, double amount, const std::string& desc);
29 
30     void settle(const Date& date);
31     void show()const;
32 };

account.cpp

 1 #include"account.h"
 2 #include<cmath>
 3 #include<iostream>
 4 using namespace std;
 5 double SavingAccount::total = 0;
 6 
 7 SavingAccount::SavingAccount(const Date& date, const string& id, double rate) :id{ id }, balance{ 0 }, rate{ rate }, lastDate{date},accumulation(0) {
 8     date.show();
 9     cout << "\t#" << id << "created" << endl;
10 }
11 
12 void SavingAccount::record(const Date& date, double amount, const string& desc) {
13     accumulation = accumulate(date);
14     lastDate = date;
15     amount = floor(amount * 100 + 0.5) / 100;
16     balance += amount;
17     total += amount;
18     date.show();
19     cout << "\t#" << id << "\t"<<amount<<"\t"<<balance<<"\t"<<desc << endl;
20 }
21 void SavingAccount::error(const string& msg)const {
22     cout << "Error(#" << id << "): " << msg << endl;
23 }
24 
25 
26 void SavingAccount::deposit(const Date& date, double amount, const string& desc) {
27     record(date, amount, desc);
28 }
29 void SavingAccount::withdraw(const Date& date, double amount, const string& desc) {
30     if (amount > getBalance())
31         error("not enough money");
32     else
33         record(date, -amount, desc);
34 }
35 
36 void SavingAccount::settle(const Date& date) {
37     double interest = accumulate(date) * rate / date.diatance(Date(date.getYear() - 1, 1, 1));
38     if (interest != 0)
39         record(date, interest, "interest");
40     accumulation = 0;
41 
42 }
43 void SavingAccount::show()const {
44     cout << id << "\tBalance:" << balance;
45 }

6_25.cpp

 1 #include"account.h"
 2 #include<iostream>
 3 using namespace std;
 4 int main() {
 5     Date date(2008, 11, 1);
 6     SavingAccount accounts[] = {
 7         SavingAccount(date,"03755217",0.015),
 8         SavingAccount(date,"02342342",0.015)
 9     };
10     const int n = sizeof(accounts) / sizeof(SavingAccount);
11     accounts[0].deposit(Date(2008, 11, 5), 5000, "salary");
12     accounts[1].deposit(Date(2008, 11, 25), 10000, "sell stock 0323");
13 
14     accounts[0].deposit(Date(2008, 12, 5), 5500, "salary");
15     accounts[1].withdraw(Date(2008, 12, 20), 4000, "buy a lappot");
16 
17     cout << endl;
18     for (int i = 0; i < n; i++) {
19         accounts[i].settle(Date(2009, 1, 1));
20         accounts[i].show();
21         cout << endl;
22     }
23     cout << "Total: " << SavingAccount::getTotal() << endl;
24     return 0;
25 }

运行结果截图

 

posted @ 2024-11-07 21:01  笔墨书稠  阅读(3)  评论(0编辑  收藏  举报