From 7b786b9b925c3af77167e17c6643026df8daaa66 Mon Sep 17 00:00:00 2001 From: flykhan Date: Thu, 27 Jul 2023 21:04:06 +0800 Subject: [PATCH] day4: homework --- day4/homework/h1.cpp | 24 ++++++++ day4/homework/h2.cpp | 67 ++++++++++++++++++++ day4/homework/h3.cpp | 35 +++++++++++ day4/homework/h4.cpp | 45 ++++++++++++++ day4/homework/h5.cpp | 79 ++++++++++++++++++++++++ day4/homework/h6.cpp | 91 +++++++++++++++++++++++++++ day4/homework/h6_2.cpp | 136 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 477 insertions(+) create mode 100644 day4/homework/h1.cpp create mode 100644 day4/homework/h2.cpp create mode 100644 day4/homework/h3.cpp create mode 100644 day4/homework/h4.cpp create mode 100644 day4/homework/h5.cpp create mode 100644 day4/homework/h6.cpp create mode 100644 day4/homework/h6_2.cpp diff --git a/day4/homework/h1.cpp b/day4/homework/h1.cpp new file mode 100644 index 0000000..7536793 --- /dev/null +++ b/day4/homework/h1.cpp @@ -0,0 +1,24 @@ +// 如下程序运行的结果是什么 ? 是否存在错误,如果有错请指出并分析原因。 +#include + +using namespace std; + +class Abc +{ +private: + int rN; + +public: + Abc(int x) : rN(x) {} + static void print() + { + Abc abc(20); + cout << abc.rN << endl; + } +}; + +int main() +{ + Abc::print(); + return 0; +} \ No newline at end of file diff --git a/day4/homework/h2.cpp b/day4/homework/h2.cpp new file mode 100644 index 0000000..33160aa --- /dev/null +++ b/day4/homework/h2.cpp @@ -0,0 +1,67 @@ +// 设已经有A,B,C,D 4个类的定义,程序中A,B,C,D析构函数调用顺序为( ) +#include +#include +#include + +using namespace std; + +class A +{ +public: + A() { cout << "A()" << endl; } + ~A() + { + cout << "~A()" << endl; + } +}; + +class B +{ +public: + B() { cout << "B()" << endl; } + ~B() + { + cout << "~B()" << endl; + } +}; + +class C +{ +public: + C() { cout << "C()" << endl; } + ~C() + { + cout << "~C()" << endl; + } +}; + +class D +{ +public: + D() { cout << "D()" << endl; } + ~D() + { + cout << "~D()" << endl; + } +}; + +C c; + +int main() +{ + A *pa = new A(); + B b; + static D d; + delete pa; +} + +/* +C() +A() +B() +D() +~A() +~B() +~D() +~C() + */ \ No newline at end of file diff --git a/day4/homework/h3.cpp b/day4/homework/h3.cpp new file mode 100644 index 0000000..7228c09 --- /dev/null +++ b/day4/homework/h3.cpp @@ -0,0 +1,35 @@ +// 运行以下程序,写出其输出的结果? +#include + +using namespace std; + +class Test +{ +public: + Test(int i = 0) + { + cout << i; + } + Test(const Test &x) + { + cout << 2; + } + Test &operator=(const Test &x) + { + cout << 3; + return *this; + } + ~Test() + { + cout << 4; + } +}; + +int main() +{ + Test obj1(1), obj2(2); + Test obj3 = obj1; + return 0; +} + +/* 122444 */ \ No newline at end of file diff --git a/day4/homework/h4.cpp b/day4/homework/h4.cpp new file mode 100644 index 0000000..d7fffe1 --- /dev/null +++ b/day4/homework/h4.cpp @@ -0,0 +1,45 @@ +// 设计Car类,包含color(颜色)、weight(重量) 两个属性和一个带color和weight两个参数的构造函数、以及driver() 驾车、start() 启动、stop() 停车的函数。 +// 【提示】设计的类,可以在main() 自行测试 +#include +#include +#include + +using namespace std; + +class Car +{ +public: + Car(const string &color, int weight) : color(color), weight(weight) {} + +public: + void driver() + { + cout << "驾车 " << this->weight << " 车重" << endl; + } + void start() + { + cout << "启动 " << this->color << " 颜色的车" << endl; + } + void stop() + { + cout << "停车" << endl; + } + +private: + string color; // 颜色 + int weight; // 重量 +}; + +void test() +{ + Car car("blue", 1200); + car.start(); + car.driver(); + car.stop(); +} + +int main() +{ + test(); + return 0; +} diff --git a/day4/homework/h5.cpp b/day4/homework/h5.cpp new file mode 100644 index 0000000..c90b053 --- /dev/null +++ b/day4/homework/h5.cpp @@ -0,0 +1,79 @@ +// 继上面的Car类, 设计Person类,并设置Car类为Person类的友元类,使得在Person类的成员函数直接访问 Car对象的所有成员。 +// 【提示】Person类的构造函数传入 Car类的对象, 增加Person的成员函数用于操作Car类的成员函数或访问Car对象的属性。 +#include +#include +#include + +using namespace std; + +class Car +{ + friend class Person; + +public: + Car(const string &color, int weight) : color(color), weight(weight) {} + +public: + void driver() + { + cout << "驾车 " << this->weight << " 车重" << endl; + } + void start() + { + cout << "启动 " << this->color << " 颜色的车" << endl; + } + void stop() + { + cout << "停车" << endl; + } + +private: + string color; // 颜色 + int weight; // 重量 +}; + +class Person +{ + // friend class Car; + +public: + Person(Car &car) : tcar(car) {} + +public: + void setCarColor(const string &color) + { + // strcpy(tcar.color, color); + tcar.color = color; + } + void setCarWeight(int weight) + { + tcar.weight = weight; + } + void show() + { + cout << "驾驶着一辆" << tcar.color << ", 重" << tcar.weight << "千克的车辆" << endl; + } + +private: + Car &tcar; +}; + +void test() +{ + Car car("红色", 1000); + Person person(car); + person.setCarColor("蓝色"); + person.setCarWeight(1230); + + car.start(); + car.driver(); + car.stop(); + + person.show(); +} + +int main() +{ + test(); + return 0; +} diff --git a/day4/homework/h6.cpp b/day4/homework/h6.cpp new file mode 100644 index 0000000..a910725 --- /dev/null +++ b/day4/homework/h6.cpp @@ -0,0 +1,91 @@ +// 自定义ArrayList类,实现线性列表(类似数组)结构的数据元素操作。操作方法: +// 1. add(int) 添加int整型的数据元素 +// 2. remove(int) 删除数据元素 +// 3. int get(int index) 获取指定位置的元素 +// 4. void print() 打印列表中所有元素 +// 5. int size() 返回列表的当前大小 +// 6. void sort(bool reversed = false) 元素排序,reversed为true时,表示从大小到小,反之,表示从小到到。 +#include +#include +#include +#include +#include + +using namespace std; + +class ArrayList +{ +private: + vector elements; // 动态数组元素 + +public: + void add(int value) // 添加 int 整型的数据元素 + { + elements.push_back(value); + } + void remove(int index) // 删除数据元素 + { + int size = elements.size(); + if (index >= 0 && index < size) + elements.erase(elements.begin() + index); + } + int get(int index) // 获取指定位置的元素 + { + int size = elements.size(); + if (index >= 0 && index < size) + return elements[index]; + return -1; // 返回无效索引,表示未找到元素 + } + void print() // 打印列表中的所有元素 + { + for (int i = 0; i < elements.size(); i++) + cout << elements[i] << " "; + } + int size() // 返回列表的当前大小 + { + return elements.size(); + } + void sort(bool reversed = false) // 默认从小到大排序,当reversed 为 true 时,从大到小排序 + { + if (reversed == true) + std::sort(elements.rbegin(), elements.rend()); + else + std::sort(elements.begin(), elements.end()); + } +}; + +int main() +{ + ArrayList list; + + list.add(5); + list.add(2); + list.add(8); + list.add(9); + list.add(13); + + cout << "原始列表: "; + list.print(); + cout << endl; + + cout << "列表大小为: " << list.size() << endl; + + cout << "删除第二位的元素后显示为: "; + list.remove(2); + list.print(); + cout << endl; + + cout << "此时第二位的元素为: " << list.get(2) << endl; + + cout << "从大到小排序后列表为: "; + list.sort(true); + list.print(); + cout << endl; + + cout << "从小到大排序后列表为: "; + list.sort(); + list.print(); + cout << endl; + + return 0; +} diff --git a/day4/homework/h6_2.cpp b/day4/homework/h6_2.cpp new file mode 100644 index 0000000..e1008ab --- /dev/null +++ b/day4/homework/h6_2.cpp @@ -0,0 +1,136 @@ +#include + +class ArrayList +{ +private: + int *elements; // 存储元素的数组 + int capacity; // 数组的容量 + int count; // 当前元素的数量 + +public: + ArrayList() + { + capacity = 10; // 初始容量为10 + elements = new int[capacity]; + count = 0; + } + + ~ArrayList() + { + delete[] elements; + } + + void add(int value) + { + if (count == capacity) + { + // 如果数组已满,扩展容量为原来的两倍 + int newCapacity = capacity * 2; + int *newElements = new int[newCapacity]; + for (int i = 0; i < count; i++) + { + newElements[i] = elements[i]; + } + delete[] elements; + elements = newElements; + capacity = newCapacity; + } + elements[count] = value; + count++; + } + + void remove(int index) + { + if (index >= 0 && index < count) + { + for (int i = index; i < count - 1; i++) + { + elements[i] = elements[i + 1]; + } + count--; + } + } + + int get(int index) + { + if (index >= 0 && index < count) + { + return elements[index]; + } + return -1; // 返回一个特殊值表示索引无效 + } + + void print() + { + for (int i = 0; i < count; i++) + { + std::cout << elements[i] << " "; + } + std::cout << std::endl; + } + + int size() + { + return count; + } + + void sort(bool reversed = false) + { + for (int i = 0; i < count - 1; i++) + { + for (int j = 0; j < count - i - 1; j++) + { + if (reversed) + { + if (elements[j] < elements[j + 1]) + { + int temp = elements[j]; + elements[j] = elements[j + 1]; + elements[j + 1] = temp; + } + } + else + { + if (elements[j] > elements[j + 1]) + { + int temp = elements[j]; + elements[j] = elements[j + 1]; + elements[j + 1] = temp; + } + } + } + } + } +}; + +int main() +{ + ArrayList list; + + list.add(5); + list.add(2); + list.add(8); + list.add(1); + + std::cout << "Original list: "; + list.print(); + + std::cout << "Size: " << list.size() << std::endl; + + std::cout << "Element at index 2: " << list.get(2) << std::endl; + + list.remove(1); + + std::cout << "After removing element at index 1: "; + list.print(); + + std::cout << "Sorted list in ascending order: "; + list.sort(); + list.print(); + + std::cout << "Sorted list in descending order: "; + list.sort(true); + list.print(); + + return 0; +}