From 6e18cf2118a64d3e080e32000eecbe297ee4bf97 Mon Sep 17 00:00:00 2001 From: flykhan Date: Thu, 27 Jul 2023 15:31:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=B1=BB=E7=9A=84=E5=B0=81?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day4/ec1/Makefile | 34 ++++++++++++++++++++++++++ day4/ec1/MyArray.cpp | 53 ++++++++++++++++++++++++++++++++++++++++ day4/ec1/MyArray.h | 24 ++++++++++++++++++ day4/ec1/TestMyArray.cpp | 23 +++++++++++++++++ day4/ec1/TestMyArray.h | 8 ++++++ day4/ec1/main.cpp | 8 ++++++ 6 files changed, 150 insertions(+) create mode 100644 day4/ec1/Makefile create mode 100644 day4/ec1/MyArray.cpp create mode 100644 day4/ec1/MyArray.h create mode 100644 day4/ec1/TestMyArray.cpp create mode 100644 day4/ec1/TestMyArray.h create mode 100644 day4/ec1/main.cpp diff --git a/day4/ec1/Makefile b/day4/ec1/Makefile new file mode 100644 index 0000000..3bf55d8 --- /dev/null +++ b/day4/ec1/Makefile @@ -0,0 +1,34 @@ +# 定义路径变量 +SRC_DIR = ./ +OBJ_DIR = ./ +BIN_DIR = ./ + +# 定义编译器 +CC = g++ +# 定义目标文件 +TARGET = $(BIN_DIR)/a.out +# 定义源文件 +SRCS = $(wildcard $(SRC_DIR)/*.cpp) +# OBJS = $(OBJ_DIR)/lrc.o $(OBJ_DIR)/console.o $(OBJ_DIR)/start_mplayer.o $(OBJ_DIR)/time_delay.o $(OBJ_DIR)/main.o +OBJS = $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS)) + +# 编译规则 +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp + $(CC) -c $< -o $@ -std=c++11 + +# 链接规则 +$(TARGET): $(OBJS) + $(CC) $^ -o $@ -std=c++11 + +# 默认构建目标 +all: $(TARGET) + +# 创建目录 +$(shell mkdir -p $(OBJ_DIR) $(BIN_DIR)) + +# 清理规则 +clean: + rm -rf $(OBJ_DIR)/*.o $(TARGET) + +# 伪目标 +.PHONY: all clean \ No newline at end of file diff --git a/day4/ec1/MyArray.cpp b/day4/ec1/MyArray.cpp new file mode 100644 index 0000000..d12a4a0 --- /dev/null +++ b/day4/ec1/MyArray.cpp @@ -0,0 +1,53 @@ +#include "MyArray.h" + +MyArray::MyArray() +{ + this->mCapacity = 100; + this->mSize = 0; + this->pAdress = new int[this->mCapacity]; // 在堆开辟空间 +} + +// 有参构造函数,用户指定容量初始化 +MyArray::MyArray(int capacity) +{ + this->mCapacity = capacity; + this->mSize = 0; + this->pAdress = new int[mCapacity]; // 在堆开辟空间 +} + +// 根据位置添加元素 +void MyArray::SetData(int pos, int val) +{ + if (pos < 0 || pos > mCapacity - 1) + return; // 位置超出数组范围 + pAdress[pos] = val; // 当位置合法时,在该位置传入值 val +} + +// 获得指定位置数据 +int MyArray::GetData(int pos) +{ + return pAdress[pos]; +} + +// 尾插法 +void MyArray::PushBack(int val) +{ + if (mSize >= mCapacity) + return; + + this->pAdress[mSize] = val; + this->mSize++; +} + +// 获得长度 +int MyArray::GetLength() +{ + return this->mSize; +} + +// 析构函数,释放数组空间 +MyArray::~MyArray() +{ + if (this->pAdress != nullptr) + delete[] this->pAdress; +} \ No newline at end of file diff --git a/day4/ec1/MyArray.h b/day4/ec1/MyArray.h new file mode 100644 index 0000000..d40a522 --- /dev/null +++ b/day4/ec1/MyArray.h @@ -0,0 +1,24 @@ +// 数组类封装 +#ifndef MYARRAY_H +#define MYARRAY_H + +class MyArray +{ +public: + MyArray(); // 无参构造函数,用户没有指定容量,则初始化为100 + explicit MyArray(int capacity); // 有参构造函数,用户指定容量初始化 + ~MyArray(); // 析构函数,释放数组空间 + +public: + void SetData(int pos, int val); // 根据位置添加元素 + int GetData(int pos); // 获取指定位置数据 + void PushBack(int val); // 尾插法 + int GetLength(); // 获得长度 + +private: + int mCapacity; // 数组一共可容纳多少个元素 + int mSize; // 当前有多少个元素 + int *pAdress; // 指向存储数据的空间 +}; + +#endif \ No newline at end of file diff --git a/day4/ec1/TestMyArray.cpp b/day4/ec1/TestMyArray.cpp new file mode 100644 index 0000000..0a34c46 --- /dev/null +++ b/day4/ec1/TestMyArray.cpp @@ -0,0 +1,23 @@ +#include "TestMyArray.h" +#include + +using namespace std; + +void test() // 测试方法 +{ + MyArray myarray(50); // 创建 50 个容量大小的数组 + //数组中插入元素 + for (int i = 0; i < 50; i++) + { + // 尾插法 + myarray.PushBack(i); + // myarray.SetData(i, i * 2); + } + + // 打印数组元素 + for (int i = 0; i < myarray.GetLength(); i++) + { + cout << myarray.GetData(i) << " "; + } + cout << endl; +} \ No newline at end of file diff --git a/day4/ec1/TestMyArray.h b/day4/ec1/TestMyArray.h new file mode 100644 index 0000000..482bd44 --- /dev/null +++ b/day4/ec1/TestMyArray.h @@ -0,0 +1,8 @@ +#ifndef __TEST_MY_ARRAY_H__ +#define __TEST_MY_ARRAY_H__ + +#include "MyArray.h" + +void test(); // 测试方法 + +#endif \ No newline at end of file diff --git a/day4/ec1/main.cpp b/day4/ec1/main.cpp new file mode 100644 index 0000000..5981c43 --- /dev/null +++ b/day4/ec1/main.cpp @@ -0,0 +1,8 @@ +#include "TestMyArray.h" + +int main() +{ + test(); + + return 0; +} \ No newline at end of file