diff --git a/.vscode/launch.json b/.vscode/launch.json index f2c16ca..d46503e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,8 +8,8 @@ "args": [], "stopAtEntry": false, "externalConsole": true, - "cwd": "d:/Code/C_code/数据结构", - "program": "d:/Code/C_code/数据结构/build/Debug/outDebug", + "cwd": "d:/Code/C_code/数据结构/陈越数据结构", + "program": "d:/Code/C_code/数据结构/陈越数据结构/build/Debug/outDebug", "MIMode": "gdb", "miDebuggerPath": "gdb", "setupCommands": [ diff --git a/数据结构/1.c b/数据结构/1.c new file mode 100644 index 0000000..3f97af3 --- /dev/null +++ b/数据结构/1.c @@ -0,0 +1,35 @@ +#include +#include +#include +#ifdef _WIN32 +#include +#include +#endif + +// 计算斐波那契第n项(公式法) +long long fib_binet(int n) +{ + const double phi = (1 + sqrt(5)) / 2; // 黄金分割比 + const double sqrt5 = sqrt(5); // 根号5 + double value = pow(phi, n) / sqrt5; // 核心计算项 + return llround(value); // 四舍五入取整(避免小数误差) +} + +int main() +{ + // 设置控制台编码为UTF-8,防止中文乱码 +#ifdef _WIN32 + system("chcp 65001 > nul"); // 设置控制台编码为UTF-8 + SetConsoleOutputCP(65001); // 设置控制台输出编码 + SetConsoleCP(65001); // 设置控制台输入编码 + _mkdir("records"); +#endif + + // 计算第47项及后续3项(47~50项) + for (int n = 47; n <= 50; n++) + { + long long result = fib_binet(n); + printf("第%d项: %lld\n", n, result); + } + return 0; +} \ No newline at end of file diff --git a/数据结构/未命名.c b/数据结构/未命名.c new file mode 100644 index 0000000..7161cd6 --- /dev/null +++ b/数据结构/未命名.c @@ -0,0 +1,244 @@ +#include +#include + +// 宏定义:状态码与元素类型 +typedef int Status; // 函数返回状态 +typedef int SElemType; // 栈元素类型(可根据需求修改,如char、float等) + +// 双栈数据结构定义(题目给定) +typedef struct +{ + int top[2], bot[2]; // 栈顶、栈底指针(top[0]对应0号栈,top[1]对应1号栈) + SElemType *V; // 共享的数组空间 + int m; // 数组最大容量(双栈总容量) +} DblStack; + +/** + * 初始化双栈 + * @param s 双栈指针 + * @param m 数组最大容量 + * @return 初始化成功返回OK,内存分配失败返回OVERFLOW + */ +Status InitDblStack(DblStack *s, int m) +{ + if (m <= 0) + return 0; // 容量无效 + s->V = (SElemType *)malloc(m * sizeof(SElemType)); + if (!s->V) + return -1; // 内存分配失败 + + s->m = m; + // 初始化0号栈:栈底在数组起始位置(索引0),空栈时top[0] = -1 + s->top[0] = -1; + s->bot[0] = 0; + // 初始化1号栈:栈底在数组末尾(索引m-1),空栈时top[1] = m + s->top[1] = m; + s->bot[1] = m - 1; + + return 1; +} + +/** + * 销毁双栈(释放内存) + * @param s 双栈指针 + */ +void DestroyDblStack(DblStack *s) +{ + if (s->V) + { + free(s->V); // 释放数组空间 + s->V = NULL; + } + s->m = 0; + s->top[0] = -1; + s->top[1] = 0; + s->bot[0] = s->bot[1] = 0; +} + +/** + * 判断指定栈是否为空 + * @param s 双栈 + * @param stackNum 栈号(0或1) + * @return 空返回OK,非空返回ERROR,栈号无效返回ERROR + */ +Status IsEmpty(DblStack s, int stackNum) +{ + if (stackNum != 0 && stackNum != 1) + return 0; // 栈号无效 + if (stackNum == 0) + { + return (s.top[0] == -1) ? 1 : 0; // 0号栈空:top[0] = -1 + } + else + { + return (s.top[1] == s.m) ? 1 : 0; // 1号栈空:top[1] = m + } +} + +/** + * 判断双栈是否已满(两栈顶相遇) + * @param s 双栈 + * @return 满返回OK,未满返回ERROR + */ +Status IsFull(DblStack s) +{ + // 0号栈顶的下一个位置等于1号栈顶时,无剩余空间 + return (s.top[0] + 1 == s.top[1]) ? 1 : 0; +} + +/** + * 进栈操作 + * @param s 双栈指针 + * @param stackNum 栈号(0或1) + * @param e 待入栈元素 + * @return 成功返回OK,栈满/栈号无效返回ERROR + */ +Status Push(DblStack *s, int stackNum, SElemType e) +{ + if (stackNum != 0 && stackNum != 1) + return 0; // 栈号无效 + if (IsFull(*s) == 1) + { + printf("双栈已满,无法进栈!\n"); + return 0; + } + + if (stackNum == 0) + { + // 0号栈:栈顶指针上移(+1),元素存入新栈顶 + s->top[0]++; + s->V[s->top[0]] = e; + } + else + { + // 1号栈:栈顶指针下移(-1),元素存入新栈顶 + s->top[1]--; + s->V[s->top[1]] = e; + } + return 1; +} + +/** + * 出栈操作 + * @param s 双栈指针 + * @param stackNum 栈号(0或1) + * @param e 用于接收出栈元素的指针 + * @return 成功返回OK,栈空/栈号无效返回ERROR + */ +Status Pop(DblStack *s, int stackNum, SElemType *e) +{ + if (stackNum != 0 && stackNum != 1) + return 0; // 栈号无效 + if (IsEmpty(*s, stackNum) == 1) + { + printf("栈%d为空,无法出栈!\n", stackNum); + return 0; + } + + if (stackNum == 0) + { + // 0号栈:取出当前栈顶元素,栈顶指针下移(-1) + *e = s->V[s->top[0]]; + s->top[0]--; + } + else + { + // 1号栈:取出当前栈顶元素,栈顶指针上移(+1) + *e = s->V[s->top[1]]; + s->top[1]++; + } + return 1; +} + +/** + * 获取栈顶元素(不弹出) + * @param s 双栈 + * @param stackNum 栈号(0或1) + * @param e 用于接收栈顶元素的指针 + * @return 成功返回OK,栈空/栈号无效返回ERROR + */ +Status GetTop(DblStack s, int stackNum, SElemType *e) +{ + if (stackNum != 0 && stackNum != 1) + return 0; // 栈号无效 + if (IsEmpty(s, stackNum) == 1) + { + printf("栈%d为空,无栈顶元素!\n", stackNum); + return 0; + } + + if (stackNum == 0) + { + *e = s.V[s.top[0]]; // 0号栈顶元素 + } + else + { + *e = s.V[s.top[1]]; // 1号栈顶元素 + } + return 1; +} + +/** + * 测试函数:演示双栈基本操作 + */ +void TestDblStack() +{ + DblStack s; + int m = 5; // 双栈总容量为5 + SElemType e; + + // 初始化双栈 + if (InitDblStack(&s, m) != 1) + { + printf("双栈初始化失败!\n"); + return; + } + printf("初始化双栈成功(容量:%d)\n", m); + + // 测试栈空 + printf("栈0是否为空?%s\n", IsEmpty(s, 0) == 1 ? "是" : "否"); // 是 + printf("栈1是否为空?%s\n", IsEmpty(s, 1) == 1 ? "是" : "否"); // 是 + + // 0号栈进栈 + Push(&s, 0, 10); + Push(&s, 0, 20); + Push(&s, 0, 30); + printf("0号栈进栈元素:10, 20, 30\n"); + GetTop(s, 0, &e); + printf("0号栈顶元素:%d\n", e); // 30 + + // 1号栈进栈 + Push(&s, 1, 100); + Push(&s, 1, 200); + printf("1号栈进栈元素:100, 200\n"); + GetTop(s, 1, &e); + printf("1号栈顶元素:%d\n", e); // 200 + + // 测试栈满(此时0号栈顶=2,1号栈顶=3,2+1=3 → 满) + printf("双栈是否已满?%s\n", IsFull(s) == 1 ? "是" : "否"); // 是 + + // 尝试继续进栈(应失败) + Push(&s, 0, 40); // 提示"双栈已满" + + // 出栈操作 + Pop(&s, 0, &e); + printf("0号栈出栈元素:%d\n", e); // 30 + Pop(&s, 1, &e); + printf("1号栈出栈元素:%d\n", e); // 200 + + // 出栈后栈顶 + GetTop(s, 0, &e); + printf("0号栈顶元素(出栈后):%d\n", e); // 20 + GetTop(s, 1, &e); + printf("1号栈顶元素(出栈后):%d\n", e); // 100 + + // 销毁双栈 + DestroyDblStack(&s); + printf("双栈销毁完成\n"); +} + +int main() +{ + TestDblStack(); + return 0; +} diff --git a/数据结构/课上代码练习/README.md b/数据结构/课上代码练习/README.md index 613d415..f78df47 100644 --- a/数据结构/课上代码练习/README.md +++ b/数据结构/课上代码练习/README.md @@ -10,6 +10,7 @@ - 包含插入、删除、查找等基本操作 - 实现了安全的输入验证机制 - 提供交互式命令行界面 +- `结构体 ### 可执行文件 - `array_operations.exe` - 编译后的数组操作程序 diff --git a/数据结构/课上代码练习/双栈.c b/数据结构/课上代码练习/双栈.c new file mode 100644 index 0000000..0a70f8f --- /dev/null +++ b/数据结构/课上代码练习/双栈.c @@ -0,0 +1,254 @@ +#include +#include +#include +#ifdef _WIN32 +#include +#include +#endif + +typedef int Status; // 函数返回状态 +typedef int SElemType; // 栈元素类型 + +typedef struct +{ + int top[2], bot[2]; // 栈顶和栈底指针 + SElemType *V; // 栈数组 + int m; // 栈的最大容量 +} DblStack; + +// 双栈的初始化 +Status Init(DblStack *s, int m) +{ + // 容量无效 + if (m <= 0) + { + return 0; + } + s->V = (SElemType *)malloc(m * sizeof(SElemType)); + if (!s->V) + { + return -1; // 内存分配失败 + } + + s->m = m; + // 0号栈空时栈顶指针为-1 + s->top[0] = -1; + s->bot[0] = 0; + // 1号栈空时栈顶指针为m + s->top[1] = m; + s->bot[1] = m - 1; + + return 1; +} + +// 双栈的销毁 +void Destroy(DblStack *s) +{ + if (s->V) + { + free(s->V); + s->V = NULL; + } + s->m = 0; + s->top[0] = -1; + s->top[1] = 0; + s->bot[0] = s->bot[1] = 0; +} + +// 判断栈是否为空 +Status IsEmpty(DblStack s, int Num) +{ + // 栈号无效 + if (Num != 0 && Num != 1) + { + return 0; + } + // 判断栈是否为空 + if (Num == 0) + { + // 0号栈空:top[0] = -1 + if (s.top[0] == -1) + { + return 1; + } + else + { + return 0; + } + } + else + { + // 1号栈空:top[1] = m + if (s.top[1] == s.m) + { + return 1; + } + else + { + return 0; + } + } +} + +// 判断双栈是否已满 +Status IsFull(DblStack s) +{ + // 两栈顶相遇 + if (s.top[0] + 1 == s.top[1]) + { + return 1; + } + else + { + return 0; + } +} + +// 进栈操作 +Status Push(DblStack *s, int Num, SElemType e) +{ + // 栈号无效 + if (Num != 0 && Num != 1) + { + return 0; + } + if (IsFull(*s) == 1) + { + printf("双栈已满,无法进栈!\n"); + return 0; + } + + if (Num == 0) + { + // 0号栈:栈顶指针上移(+1),元素存入新栈顶 + s->top[0]++; + s->V[s->top[0]] = e; + } + else + { + // 1号栈:栈顶指针下移(-1),元素存入新栈顶 + s->top[1]--; + s->V[s->top[1]] = e; + } + return 1; +} + +// 出栈操作 +Status Pop(DblStack *s, int Num, SElemType *e) +{ + // 栈号无效 + if (Num != 0 && Num != 1) + { + return 0; + } + if (IsEmpty(*s, Num) == 1) + { + printf("栈%d为空,无法出栈!\n", Num); + return 0; + } + + if (Num == 0) + { + // 0号栈:取出当前栈顶元素,栈顶指针下移(-1) + *e = s->V[s->top[0]]; + s->top[0]--; + } + else + { + // 1号栈:取出当前栈顶元素,栈顶指针上移(+1) + *e = s->V[s->top[1]]; + s->top[1]++; + } + return 1; +} + +// 获取栈顶元素 +Status GetTop(DblStack s, int Num, SElemType *e) +{ + // 栈号无效 + if (Num != 0 && Num != 1) + { + return 0; + } + if (IsEmpty(s, Num) == 1) + { + printf("栈%d为空,无栈顶元素!\n", Num); + return 0; + } + + if (Num == 0) + { + *e = s.V[s.top[0]]; // 0号栈顶元素 + } + else + { + *e = s.V[s.top[1]]; // 1号栈顶元素 + } + return 1; +} + +int main() +{ + // 设置控制台编码为UTF-8,防止中文乱码 +#ifdef _WIN32 + system("chcp 65001 > nul"); // 设置控制台编码为UTF-8 + SetConsoleOutputCP(65001); // 设置控制台输出编码 + SetConsoleCP(65001); // 设置控制台输入编码 + _mkdir("records"); +#endif + + DblStack s; + int m = 5; // 双栈总容量为5 + SElemType e; + + // 初始化双栈 + if (Init(&s, m) != 1) + { + printf("双栈初始化失败!\n"); + return -1; + } + printf("初始化双栈成功(容量:%d)\n", m); + + // 测试栈空 + printf("栈0是否为空?%s\n", IsEmpty(s, 0) == 1 ? "是" : "否"); // 是 + printf("栈1是否为空?%s\n", IsEmpty(s, 1) == 1 ? "是" : "否"); // 是 + + // 0号栈进栈 + Push(&s, 0, 10); + Push(&s, 0, 20); + Push(&s, 0, 30); + printf("0号栈进栈元素:10, 20, 30\n"); + GetTop(s, 0, &e); + printf("0号栈顶元素:%d\n", e); // 30 + + // 1号栈进栈 + Push(&s, 1, 100); + Push(&s, 1, 200); + printf("1号栈进栈元素:100, 200\n"); + GetTop(s, 1, &e); + printf("1号栈顶元素:%d\n", e); // 200 + + // 测试栈满(此时0号栈顶=2,1号栈顶=3,2+1=3 → 满) + printf("双栈是否已满?%s\n", IsFull(s) == 1 ? "是" : "否"); // 是 + + // 尝试继续进栈(应失败) + Push(&s, 0, 40); // 提示"双栈已满" + + // 出栈操作 + Pop(&s, 0, &e); + printf("0号栈出栈元素:%d\n", e); // 30 + Pop(&s, 1, &e); + printf("1号栈出栈元素:%d\n", e); // 200 + + // 出栈后栈顶 + GetTop(s, 0, &e); + printf("0号栈顶元素(出栈后):%d\n", e); // 20 + GetTop(s, 1, &e); + printf("1号栈顶元素(出栈后):%d\n", e); // 100 + + // 销毁双栈 + Destroy(&s); + printf("双栈销毁完成\n"); + + return 0; +} \ No newline at end of file diff --git a/数据结构/课上代码练习/转换二进制.c b/数据结构/课上代码练习/转换二进制.c new file mode 100644 index 0000000..a8600b4 --- /dev/null +++ b/数据结构/课上代码练习/转换二进制.c @@ -0,0 +1,59 @@ +#include +#include +#include +#ifdef _WIN32 +#include +#include +#endif + +int main() +{ + // 设置控制台编码为UTF-8,防止中文乱码 +#ifdef _WIN32 + system("chcp 65001 > nul"); // 设置控制台编码为UTF-8 + SetConsoleOutputCP(65001); // 设置控制台输出编码 + SetConsoleCP(65001); // 设置控制台输入编码 + _mkdir("records"); +#endif + + // 输入一个十进制整数 + unsigned int n; + printf("请输入一个正十进制整数: "); + + // 转换为二进制 + char new_num[33]; // 32位二进制数 + 1位结束符 + int index = 0; + unsigned int temp = n; + + // 从低位到高位计算二进制位 + while (n > 0) + { + if (n % 2 == 0) + { + new_num[index] = '0'; + } + else + { + new_num[index] = '1'; + } + n /= 2; + index++; + } + + // 添加字符串结束符 + new_num[index] = '\0'; + + // 反转字符串 + int len = strlen(new_num); + for (int i = 0; i < len / 2; i++) + { + char use_char = new_num[i]; + new_num[i] = new_num[len - 1 - i]; + new_num[len - 1 - i] = use_char; + } + + // 输出结果 + printf("十进制数 %u 对应的二进制表示为: %s\n", temp, new_num); + + return 0; +} \ No newline at end of file