diff --git a/day3/homework/h1.cpp b/day3/homework/h1.cpp new file mode 100644 index 0000000..dc1784a --- /dev/null +++ b/day3/homework/h1.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +using namespace std; + +class SLink +{ +private: + char *title; + +public: + SLink() {} + SLink(const char *title) + { + this->title = (char *)malloc(50); + strcpy(this->title, title); + } + ~SLink(); + void show() + { + cout << title << endl; + } +}; + +SLink::~SLink() {} + +int main() +{ + SLink s1; + SLink s2("disen 补课中"); + s2.show(); + return 0; +} diff --git a/day3/homework/h2.cpp b/day3/homework/h2.cpp new file mode 100644 index 0000000..3cd8bc8 --- /dev/null +++ b/day3/homework/h2.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +using namespace std; + +class Color +{ +private: + int r, g, b; + +public: + explicit Color(int r, int g = 0, int b = 0) : r(r), g(g), b(b) + { + } + void show() + { + cout << r << "," << g << "," << b << endl; + } +}; + +int main() +{ + Color c = Color(2, 3, 4); + c.show(); + return 0; +} diff --git a/day3/homework/h3.cpp b/day3/homework/h3.cpp new file mode 100644 index 0000000..7e500f6 --- /dev/null +++ b/day3/homework/h3.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +using namespace std; + +class Point +{ +private: + int x, y; + +public: + Point(int x, int y); + void show() + { + cout << x << "," << y << endl; + } +}; + +Point::Point(int x, int y) +{ + this->x = x; + this->y = y; +} + +int main() +{ + Point *p1 = new Point[3]{Point(1, 2), Point(2, 3), Point(3, 4)}; + p1[2].show(); + delete[] p1; + return 0; +} diff --git a/day3/homework/h4.cpp b/day3/homework/h4.cpp new file mode 100644 index 0000000..ab9163e --- /dev/null +++ b/day3/homework/h4.cpp @@ -0,0 +1,52 @@ +// 设计一个学生类,包括学号、姓名、成绩,并设计接口函数用来输出这些学生数据并计算平均分。并编写main函数进行测试: +// 输出如: +// 学号 姓名 成绩 1 张XX 98 2 王XX 90 3 XXX 89 平均成绩: XX +#include +#include +#include +#include + +using namespace std; + +class Student +{ +private: + int sid; + string name; + float score; + +public: + Student(int sid, const string &name, float score) : sid(sid), name(name), score(score) + { + } + ~Student() {} + +public: + static void avgAndShow(Student *stus, int n); +}; + +void Student::avgAndShow(Student *stus, int n) +{ + float avg = 0.0f; + cout << "学号\t" + << "姓名\t" + << "成绩" << endl; + for (int i = 0; i < n; i++) + { + cout << stus[i].sid << "\t" << stus[i].name << "\t" << stus[i].score << endl; + avg += stus[i].score / n; + } + cout << "平均成绩: " << setprecision(3) << avg << endl; +} + +int main() +{ + Student *stus = new Student[3]{ + Student(1, "张XX", 98), + Student(2, "王XX", 90), + Student(3, "XXX", 89)}; + + Student::avgAndShow(stus, 3); + + return 0; +} diff --git a/day3/homework/h5.cpp b/day3/homework/h5.cpp new file mode 100644 index 0000000..5d1aae4 --- /dev/null +++ b/day3/homework/h5.cpp @@ -0,0 +1,148 @@ +// 创建一个名为 LinkedList 的类,实现链表的基本操作,包括插入节点、删除节点和反转链表等。 +// 【提示】单向链表 +#include +#include +#include + +using namespace std; + +class Node +{ +public: + int data; + Node *next; + +public: + Node(int value) + { + this->data = value; + this->next = NULL; + } +}; + +class LinkedList +{ +private: + Node *head; + +public: + LinkedList() + { + head = NULL; // 初始头结点 + } + +public: + Node *insertNode(int insValue); + Node *deleteNode(int delValue); + Node *reverseLinkedList(); + void print(); +}; + +Node *LinkedList::insertNode(int insValue) +{ + Node *newNode = new Node(insValue); + + if (NULL == head) + head = newNode; + + Node *p = head; + while (p->next != NULL) + { + p = p->next; + } + p->next = newNode; + return head; +} + +Node *LinkedList::deleteNode(int delValue) +{ + if (NULL == head) + { + cout << "不存在链表,删除失败" << endl; + return head; + } + + if (head->data == delValue) // 当删除的节点是头节点时 + { + Node *temp = head; + head = head->next; + delete temp; + return head; + } + + Node *p = head; + while (p->next != NULL) + { + if (p->next->data == delValue) + { + Node *temp = p->next; + p->next = temp->next; + delete temp; + } + p = p->next; + } + + if (p != NULL && p->data == delValue) // 处理最后一个节点的情况 + { + delete p; + p = NULL; + } + return head; +} + +Node *LinkedList::reverseLinkedList() +{ + Node *p = head; + Node *new_head = NULL; + Node *p_next = NULL; + while (p != NULL) + { + p_next = p->next; + p->next = new_head; + new_head = p; + p = p_next; + } + + head = new_head; // 新头指针 + return head; +} + +void LinkedList::print() +{ + Node *p = head; + while (p != NULL) + { + cout << p->data << " "; + p = p->next; + } + cout << endl; +} + +int main() +{ + LinkedList list; + + // 插入节点 + list.insertNode(1); + list.insertNode(3); + list.insertNode(8); + list.insertNode(2); + + // 测试打印 + cout << "原始链表: "; + list.print(); + cout << endl; + + // 删除节点 + list.deleteNode(3); + cout << "删除3后的链表: "; + list.print(); + cout << endl; + + // 反转链表 + list.reverseLinkedList(); + cout << "反序链表后: "; + list.print(); + cout << endl; + return 0; +} diff --git a/day3/homework/h6.cpp b/day3/homework/h6.cpp new file mode 100644 index 0000000..3a584a5 --- /dev/null +++ b/day3/homework/h6.cpp @@ -0,0 +1,106 @@ +// 设计一个 Stack 的类,实现栈的基本操作,包括入栈、出栈和判断栈是否为空等。主要用于存放int数值或string字符串,并对其操作。 +// 【提示】 +// 1\) 使用数组实现,每一次重分数组空间的大小是原大小的2倍; + +// 2\) Stack是栈的结构(FILO先进后出的) +#include +#include +#include + +using namespace std; + +class Stack +{ +private: + int capacity; // 栈容量 + int size; // 当前栈中元素个数 + int *data; // 存放栈的数组 +public: + Stack() + { + this->capacity = 10; // 初始容量 + this->size = 0; + this->data = new int[this->capacity]; // 初始化栈数据数组 + } + ~Stack() + { + delete[] data; // 删除栈数据数组 + } + +public: + void push(int value); // 入栈 + int pop(); // 出栈 + int isEmpty(); // 判断栈空否 +}; + +void Stack::push(int value) +{ + if (size == capacity) + { + cout << "栈满,重新分配空间中" << endl; + int newCapacity = capacity * 2; + int *newData = new int[newCapacity]; + for (int i = 0; i < size; i++) + { + newData[i] = data[i]; + } + delete[] data; + data = newData; + capacity = newCapacity; + cout << "空间分配完成,数据转存完毕" << endl; + } + data[size] = value; + cout << value << " 入栈成功" << endl; + size++; +} + +int Stack::pop() +{ + if (size == 0) + { + cout << "栈空" << endl; + } + else + { + int popValue = data[size - 1]; + size--; + cout << "出栈成功" << endl; + return popValue; + } +} + +int Stack::isEmpty() +{ + if (size == 0) + { + cout << "栈空" << endl; + return 1; // 非 0 为栈空 + } + else + { + return 0; + cout << "栈不空" << endl; + } +} + +int main() +{ + Stack stk; + + stk.push(10); + stk.push(12); + stk.push(16); + stk.push(5); + + while (!stk.isEmpty()) + { + cout << stk.pop() << " "; + } + cout << endl; + + // stk.push(7); + + stk.isEmpty(); + + return 0; +}