diff --git a/CLion/CourseBook/0301_SqStack/CMakeLists.txt b/CLion/CourseBook/0301_SqStack/CMakeLists.txt new file mode 100644 index 0000000..deb74df --- /dev/null +++ b/CLion/CourseBook/0301_SqStack/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(SqStack SqStack.h SqStack.c SqStack-main.c) +# 链接公共库 +target_link_libraries(SqStack Scanf_lib) \ No newline at end of file diff --git a/CLion/CourseBook/0301_SqStack/SqStack-main.c b/CLion/CourseBook/0301_SqStack/SqStack-main.c new file mode 100644 index 0000000..5ce58a5 --- /dev/null +++ b/CLion/CourseBook/0301_SqStack/SqStack-main.c @@ -0,0 +1,94 @@ +#include +#include "SqStack.h" //**▲03 栈和队列**// + +// 测试函数,打印元素 +void PrintElem(SElemType e); + +int main(int argc, char** argv) { + SqStack S; + int i; + SElemType e; + + printf("████████ 函数 InitStack \n"); + { + printf("█ 初始化顺序栈 S ...\n"); + InitStack(&S); + } + PressEnterToContinue(); + + printf("████████ 函数 StackEmpty \n"); + { + StackEmpty(S) ? printf("█ S 为空!!\n") : printf("█ S 不为空!\n"); + } + PressEnterToContinue(); + + printf("████████ 函数 Push \n"); + { + for(i = 1; i <= 6; i++) { + Push(&S, 2 * i); + printf("█ 将 \"%2d\" 压入栈 S ...\n", 2 * i); + } + } + PressEnterToContinue(); + + printf("████████ 函数 StackTraverse \n"); + { + printf("█ S 中的元素为:S = "); + StackTraverse(S, PrintElem); + } + PressEnterToContinue(); + + printf("████████ 函数 StackLength \n"); + { + i = StackLength(S); + printf("█ S 的长度为 %d \n", i); + } + PressEnterToContinue(); + + printf("████████ 函数 Pop \n"); + { + Pop(&S, &e); + printf("█ 栈顶元素 \"%d\" 出栈...\n", e); + printf("█ S 中的元素为:S = "); + StackTraverse(S, PrintElem); + } + PressEnterToContinue(); + + printf("████████ 函数 GetTop \n"); + { + GetTop(S, &e); + printf("█ 栈顶元素的值为 \"%d\" \n", e); + } + PressEnterToContinue(); + + printf("████████ 函数 ClearStack \n"); + { + printf("█ 清空 S 前:"); + StackEmpty(S) ? printf(" S 为空!!\n") : printf(" S 不为空!\n"); + + ClearStack(&S); + + printf("█ 清空 S 后:"); + StackEmpty(S) ? printf(" S 为空!!\n") : printf(" S 不为空!\n"); + } + PressEnterToContinue(); + + printf("████████ 函数 DestroyStack \n"); + { + printf("█ 销毁 S 前:"); + S.base != NULL && S.top != NULL ? printf(" S 存在!\n") : printf(" S 不存在!!\n"); + + DestroyStack(&S); + + printf("█ 销毁 S 后:"); + S.base != NULL && S.top != NULL ? printf(" S 存在!\n") : printf(" S 不存在!!\n"); + } + PressEnterToContinue(); + + return 0; +} + +// 测试函数,打印元素 +void PrintElem(SElemType e) { + printf("%d ", e); +} diff --git a/CLion/CourseBook/0301_SqStack/SqStack.c b/CLion/CourseBook/0301_SqStack/SqStack.c new file mode 100644 index 0000000..626ffae --- /dev/null +++ b/CLion/CourseBook/0301_SqStack/SqStack.c @@ -0,0 +1,174 @@ +/*========================= + * 栈的顺序存储结构(顺序栈) + ==========================*/ + +#include "SqStack.h" //**▲03 栈和队列**// + +/* + * 初始化 + * + * 构造一个空栈。初始化成功则返回OK,否则返回ERROR。 + */ +Status InitStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + (*S).base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); + } + + (*S).top = (*S).base; + (*S).stacksize = STACK_INIT_SIZE; + + return OK; +} + +/* + * 销毁(结构) + * + * 释放顺序栈所占内存。 + */ +Status DestroyStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + free((*S).base); + + (*S).base = NULL; + (*S).top = NULL; + (*S).stacksize = 0; + + return OK; +} + +/* + * 置空(内容) + * + * 只是清理顺序栈中存储的数据,不释放顺序栈所占内存。 + */ +Status ClearStack(SqStack* S) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + (*S).top = (*S).base; + + return OK; +} + +/* + * 判空 + * + * 判断顺序栈中是否包含有效数据。 + * + * 返回值: + * TRUE : 顺序栈为空 + * FALSE: 顺序栈不为空 + */ +Status StackEmpty(SqStack S) { + if(S.top == S.base) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * 计数 + * + * 返回顺序栈包含的有效元素的数量。 + */ +int StackLength(SqStack S) { + if(S.base == NULL) { + return 0; + } + + return (int) (S.top - S.base); +} + +/* + * 取值 + * + * 返回栈顶元素,并用e接收。 + */ +Status GetTop(SqStack S, SElemType* e) { + if(S.base == NULL || S.top == S.base) { + return 0; + } + + // 不会改变栈中元素 + *e = *(S.top - 1); + + return OK; +} + +/* + * 入栈 + * + * 将元素e压入到栈顶。 + */ +Status Push(SqStack* S, SElemType e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + // 栈满时,追加存储空间 + if((*S).top - (*S).base >= (*S).stacksize) { + (*S).base = (SElemType*) realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); // 存储分配失败 + } + + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + + // 进栈先赋值,栈顶指针再自增 + *(S->top++) = e; + + return OK; +} + +/* + * 出栈 + * + * 将栈顶元素弹出,并用e接收。 + */ +Status Pop(SqStack* S, SElemType* e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + if((*S).top == (*S).base) { + return ERROR; + } + + // 出栈栈顶指针先递减,再赋值 + *e = *(--(*S).top); + + return OK; +} + +/* + * 遍历 + * + * 用visit函数访问顺序栈S + */ +Status StackTraverse(SqStack S, void(Visit)(SElemType)) { + SElemType* p = S.base; + + if(S.base == NULL) { + return ERROR; + } + + while(p < S.top) { + Visit(*p++); + } + + printf("\n"); + + return OK; +} diff --git a/CLion/CourseBook/0301_SqStack/SqStack.h b/CLion/CourseBook/0301_SqStack/SqStack.h new file mode 100644 index 0000000..bb5aaa4 --- /dev/null +++ b/CLion/CourseBook/0301_SqStack/SqStack.h @@ -0,0 +1,94 @@ +/*========================= + * 栈的顺序存储结构(顺序栈) + ==========================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +/* 宏定义 */ +#define STACK_INIT_SIZE 100 // 顺序栈存储空间的初始分配量 +#define STACKINCREMENT 10 // 顺序栈存储空间的分配增量 + +/* 顺序栈元素类型定义 */ +typedef int SElemType; + +// 顺序栈元素结构 +typedef struct { + SElemType* base; // 栈底指针 + SElemType* top; // 栈顶指针 + int stacksize; // 当前已分配的存储空间,以元素为单位 +} SqStack; + + +/* + * 初始化 + * + * 构造一个空栈。初始化成功则返回OK,否则返回ERROR。 + */ +Status InitStack(SqStack* S); + +/* + * 销毁(结构) + * + * 释放顺序栈所占内存。 + */ +Status DestroyStack(SqStack* S); + +/* + * 置空(内容) + * + * 只是清理顺序栈中存储的数据,不释放顺序栈所占内存。 + */ +Status ClearStack(SqStack* S); + +/* + * 判空 + * + * 判断顺序栈中是否包含有效数据。 + * + * 返回值: + * TRUE : 顺序栈为空 + * FALSE: 顺序栈不为空 + */ +Status StackEmpty(SqStack S); + +/* + * 计数 + * + * 返回顺序栈包含的有效元素的数量。 + */ +int StackLength(SqStack S); + +/* + * 取值 + * + * 返回栈顶元素,并用e接收。 + */ +Status GetTop(SqStack S, SElemType* e); + +/* + * 入栈 + * + * 将元素e压入到栈顶。 + */ +Status Push(SqStack* S, SElemType e); + +/* + * 出栈 + * + * 将栈顶元素弹出,并用e接收。 + */ +Status Pop(SqStack* S, SElemType* e); + +/* + * 遍历 + * + * 用visit函数访问顺序栈S + */ +Status StackTraverse(SqStack S, void(Visit)(SElemType)); + +#endif diff --git a/CLion/CourseBook/0302_Conversion/CMakeLists.txt b/CLion/CourseBook/0302_Conversion/CMakeLists.txt new file mode 100644 index 0000000..5b8be02 --- /dev/null +++ b/CLion/CourseBook/0302_Conversion/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(Conversion SqStack.h SqStack.c Conversion.h Conversion.c Conversion-main.c) +# 链接公共库 +target_link_libraries(Conversion Scanf_lib) \ No newline at end of file diff --git a/CLion/CourseBook/0302_Conversion/Conversion-main.c b/CLion/CourseBook/0302_Conversion/Conversion-main.c new file mode 100644 index 0000000..a198e3d --- /dev/null +++ b/CLion/CourseBook/0302_Conversion/Conversion-main.c @@ -0,0 +1,11 @@ +#include "Conversion.h" //**▲03 栈和队列**// + +int main(int argc, char** argv) { + int i = 342391; + + printf("将十进制数转换为八进制数...\n"); + + conversion(i); + + return 0; +} diff --git a/CLion/CourseBook/0302_Conversion/Conversion.c b/CLion/CourseBook/0302_Conversion/Conversion.c new file mode 100644 index 0000000..a3239f4 --- /dev/null +++ b/CLion/CourseBook/0302_Conversion/Conversion.c @@ -0,0 +1,37 @@ +/*============== + * 进制转换 + * + * 包含算法: 3.1 + ===============*/ + +#include "Conversion.h" //**▲03 栈和队列**// + +/* + * ████████ 算法3.1 ████████ + * + * 进制转换:将指定的非负十进制整数,转换为八进制后输出。 + * + *【注】 + * 教材使用的是控制台输入,这里为了便于测试,直接改为从形参接收参数 + */ +void conversion(int i) { + SqStack S; + SElemType e; + + InitStack(&S); + + // 八进制数前面加0 + printf("十进制数 %d 转换为八进制数后为:0", i); + + while(i!=0) { + Push(&S, i % 8); // 进栈时从低位到高位 + i = i / 8; + } + + while(StackEmpty(S)==FALSE) { + Pop(&S, &e); // 出栈时从高位到低位 + printf("%d", e); + } + + printf("\n"); +} diff --git a/CLion/CourseBook/0302_Conversion/Conversion.h b/CLion/CourseBook/0302_Conversion/Conversion.h new file mode 100644 index 0000000..24e4abb --- /dev/null +++ b/CLion/CourseBook/0302_Conversion/Conversion.h @@ -0,0 +1,20 @@ +/*============== + * 进制转换 + * + * 包含算法: 3.1 + ===============*/ + +#ifndef CONVERSION_H +#define CONVERSION_H + +#include +#include "SqStack.h" //**▲03 栈和队列**// + +/* + * ████████ 算法3.1 ████████ + * + * 进制转换:将指定的非负十进制整数,转换为八进制后输出。 + */ +void conversion(int i); + +#endif diff --git a/CLion/CourseBook/0302_Conversion/SqStack.c b/CLion/CourseBook/0302_Conversion/SqStack.c new file mode 100644 index 0000000..300e20e --- /dev/null +++ b/CLion/CourseBook/0302_Conversion/SqStack.c @@ -0,0 +1,90 @@ +/*============================= + * 栈的顺序存储结构(顺序栈) + =============================*/ + +#include "SqStack.h" //**▲03 栈和队列**// + +/* + * 初始化 + * + * 构造一个空栈。初始化成功则返回OK,否则返回ERROR。 + */ +Status InitStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + (*S).base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); + } + + (*S).top = (*S).base; + (*S).stacksize = STACK_INIT_SIZE; + + return OK; +} + +/* + * 判空 + * + * 判断顺序栈中是否包含有效数据。 + * + * 返回值: + * TRUE : 顺序栈为空 + * FALSE: 顺序栈不为空 + */ +Status StackEmpty(SqStack S) { + if(S.top == S.base) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * 入栈 + * + * 将元素e压入到栈顶。 + */ +Status Push(SqStack* S, SElemType e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + // 栈满时,追加存储空间 + if((*S).top - (*S).base >= (*S).stacksize) { + (*S).base = (SElemType*) realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); // 存储分配失败 + } + + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + + // 进栈先赋值,栈顶指针再自增 + *(S->top++) = e; + + return OK; +} + +/* + * 出栈 + * + * 将栈顶元素弹出,并用e接收。 + */ +Status Pop(SqStack* S, SElemType* e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + if((*S).top == (*S).base) { + return ERROR; + } + + // 出栈栈顶指针先递减,再赋值 + *e = *(--(*S).top); + + return OK; +} diff --git a/CLion/CourseBook/0302_Conversion/SqStack.h b/CLion/CourseBook/0302_Conversion/SqStack.h new file mode 100644 index 0000000..e697e51 --- /dev/null +++ b/CLion/CourseBook/0302_Conversion/SqStack.h @@ -0,0 +1,59 @@ +/*============================= + * 栈的顺序存储结构(顺序栈) + =============================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +/* 宏定义 */ +#define STACK_INIT_SIZE 100 // 顺序栈存储空间的初始分配量 +#define STACKINCREMENT 10 // 顺序栈存储空间的分配增量 + +/* 顺序栈元素类型定义 */ +typedef int SElemType; + +// 顺序栈元素结构 +typedef struct { + SElemType* base; // 栈底指针 + SElemType* top; // 栈顶指针 + int stacksize; // 当前已分配的存储空间,以元素为单位 +} SqStack; + + +/* + * 初始化 + * + * 构造一个空栈。初始化成功则返回OK,否则返回ERROR。 + */ +Status InitStack(SqStack* S); + +/* + * 判空 + * + * 判断顺序栈中是否包含有效数据。 + * + * 返回值: + * TRUE : 顺序栈为空 + * FALSE: 顺序栈不为空 + */ +Status StackEmpty(SqStack S); + +/* + * 入栈 + * + * 将元素e压入到栈顶。 + */ +Status Push(SqStack* S, SElemType e); + +/* + * 出栈 + * + * 将栈顶元素弹出,并用e接收。 + */ +Status Pop(SqStack* S, SElemType* e); + +#endif diff --git a/CLion/CourseBook/0303_LineEdit/CMakeLists.txt b/CLion/CourseBook/0303_LineEdit/CMakeLists.txt new file mode 100644 index 0000000..5e058d8 --- /dev/null +++ b/CLion/CourseBook/0303_LineEdit/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(LineEdit SqStack.h SqStack.c LineEdit.h LineEdit.c LineEdit-main.c) +# 链接公共库 +target_link_libraries(LineEdit Scanf_lib) \ No newline at end of file diff --git a/CLion/CourseBook/0303_LineEdit/LineEdit-main.c b/CLion/CourseBook/0303_LineEdit/LineEdit-main.c new file mode 100644 index 0000000..c2926b0 --- /dev/null +++ b/CLion/CourseBook/0303_LineEdit/LineEdit-main.c @@ -0,0 +1,18 @@ +#include +#include "LineEdit.h" //**▲03 栈和队列**// + +int main(int argc, char* argv[]) { + char* buf = "whli##ilr#e(s#*s)\noutcha@ putchar(*s=#++);"; //需要录入的内容 + + printf("作为示范,用户输入的文本内容为:\n"); + printf("%s\n\n", buf); + + printf("进入行编辑程序...\n\n"); + + printf("特殊符号:'#' 代表删除上一元素 '@' 代表删除当前输入行\n"); + printf(" '\\n'代表此行输入无误 '\\0'代表输入结束\n"); + printf("最终存储的内容为:\n"); + LineEdit(buf); + + return 0; +} diff --git a/CLion/CourseBook/0303_LineEdit/LineEdit.c b/CLion/CourseBook/0303_LineEdit/LineEdit.c new file mode 100644 index 0000000..69443e7 --- /dev/null +++ b/CLion/CourseBook/0303_LineEdit/LineEdit.c @@ -0,0 +1,71 @@ +/*============== + * 行编辑程序 + * + * 包含算法: 3.2 + ===============*/ + +#include "LineEdit.h" //**▲03 栈和队列**// + +/* + * ████████ 算法3.2 ████████ + * + * 行编辑程序,模拟编辑文本时的退格与清空行的操作。 + * + *【注】 + * 教材使用的是控制台输入,这里为了便于测试,直接改为从形参接收参数 + */ +void LineEdit(const char buffer[]) { + SqStack S; //接收输入的字符 + SElemType e; + int i; + char ch; + + // 初始化栈 + InitStack(&S); + + i = 0; + ch = buffer[i++]; + + // 如果未达文本末尾 + while(ch != EOF) { + // 如果未达文本末尾,且本行未结束(未遇到换行) + while(ch != EOF && ch != '\n') { + switch(ch) { + case '#': + Pop(&S, &e); // 遇到'#'表示删除一个字符 + break; + case '@': + ClearStack(&S); // 遇到'@'表示清空当前行 + break; + default : + Push(&S, ch); // 有效字符入栈 + } + + // 识别下一个字符 + ch = buffer[i++]; + } + + // 清空之前输出当前栈的内容,此处教材上没有 + StackTraverse(S, Print); + + // 清空改行的缓冲区 + ClearStack(&S); + + // 如果未到文本末尾,说明遇到了'\n',即该行结束了 + if(ch != EOF) { + // 进入下一行 + ch = buffer[i++]; + } + } + + // 已经到了文本末尾,输出目前栈中的元素,此处教材上没有 + StackTraverse(S, Print); + + // 销毁栈 + DestroyStack(&S); +} + +// 测试函数,打印元素 +void Print(SElemType e) { + printf("%c", e); +} diff --git a/CLion/CourseBook/0303_LineEdit/LineEdit.h b/CLion/CourseBook/0303_LineEdit/LineEdit.h new file mode 100644 index 0000000..1b9a0ae --- /dev/null +++ b/CLion/CourseBook/0303_LineEdit/LineEdit.h @@ -0,0 +1,33 @@ +/*============== + * 行编辑程序 + * + * 包含算法: 3.2 + ===============*/ + +#ifndef LINEEDIT_H +#define LINEEDIT_H + +#include +#include "SqStack.h" //**▲03 栈和队列**// +#include "LineEdit.h" + +// 模拟文件中的文本结束标记,需要覆盖已有的定义 +#ifdef EOF +#undef EOF +#define EOF '\0' +#endif + +/* + * ████████ 算法3.2 ████████ + * + * 行编辑程序,模拟编辑文本时的退格与清空行的操作。 + * + *【注】 + * 教材使用的是控制台输入,这里为了便于测试,直接改为从形参接收参数 + */ +void LineEdit(const char buffer[]); + +// 测试函数,打印元素 +void Print(SElemType e); + +#endif diff --git a/CLion/CourseBook/0303_LineEdit/SqStack.c b/CLion/CourseBook/0303_LineEdit/SqStack.c new file mode 100644 index 0000000..691e49c --- /dev/null +++ b/CLion/CourseBook/0303_LineEdit/SqStack.c @@ -0,0 +1,128 @@ +/*============================= + * 栈的顺序存储结构(顺序栈) + =============================*/ + +#include "SqStack.h" //**▲03 栈和队列**// + +/* + * 初始化 + * + * 构造一个空栈。初始化成功则返回OK,否则返回ERROR。 + */ +Status InitStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + (*S).base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); + } + + (*S).top = (*S).base; + (*S).stacksize = STACK_INIT_SIZE; + + return OK; +} + +/* + * 销毁(结构) + * + * 释放顺序栈所占内存。 + */ +Status DestroyStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + free((*S).base); + + (*S).base = NULL; + (*S).top = NULL; + (*S).stacksize = 0; + + return OK; +} + +/* + * 置空(内容) + * + * 只是清理顺序栈中存储的数据,不释放顺序栈所占内存。 + */ +Status ClearStack(SqStack* S) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + (*S).top = (*S).base; + + return OK; +} + +/* + * 入栈 + * + * 将元素e压入到栈顶。 + */ +Status Push(SqStack* S, SElemType e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + // 栈满时,追加存储空间 + if((*S).top - (*S).base >= (*S).stacksize) { + (*S).base = (SElemType*) realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); // 存储分配失败 + } + + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + + // 进栈先赋值,栈顶指针再自增 + *(S->top++) = e; + + return OK; +} + +/* + * 出栈 + * + * 将栈顶元素弹出,并用e接收。 + */ +Status Pop(SqStack* S, SElemType* e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + if((*S).top == (*S).base) { + return ERROR; + } + + // 出栈栈顶指针先递减,再赋值 + *e = *(--(*S).top); + + return OK; +} + +/* + * 遍历 + * + * 用visit函数访问顺序栈S + */ +Status StackTraverse(SqStack S, void(Visit)(SElemType)) { + SElemType* p = S.base; + + if(S.base == NULL) { + return ERROR; + } + + while(p < S.top) { + Visit(*p++); + } + + printf("\n"); + + return OK; +} diff --git a/CLion/CourseBook/0303_LineEdit/SqStack.h b/CLion/CourseBook/0303_LineEdit/SqStack.h new file mode 100644 index 0000000..3f00fc1 --- /dev/null +++ b/CLion/CourseBook/0303_LineEdit/SqStack.h @@ -0,0 +1,69 @@ +/*============================= + * 栈的顺序存储结构(顺序栈) + =============================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +/* 宏定义 */ +#define STACK_INIT_SIZE 100 // 顺序栈存储空间的初始分配量 +#define STACKINCREMENT 10 // 顺序栈存储空间的分配增量 + +/* 顺序栈元素类型定义 */ +typedef int SElemType; + +// 顺序栈元素结构 +typedef struct { + SElemType* base; // 栈底指针 + SElemType* top; // 栈顶指针 + int stacksize; // 当前已分配的存储空间,以元素为单位 +} SqStack; + + +/* + * 初始化 + * + * 构造一个空栈。初始化成功则返回OK,否则返回ERROR。 + */ +Status InitStack(SqStack* S); + +/* + * 销毁(结构) + * + * 释放顺序栈所占内存。 + */ +Status DestroyStack(SqStack* S); + +/* + * 置空(内容) + * + * 只是清理顺序栈中存储的数据,不释放顺序栈所占内存。 + */ +Status ClearStack(SqStack* S); + +/* + * 入栈 + * + * 将元素e压入到栈顶。 + */ +Status Push(SqStack* S, SElemType e); + +/* + * 出栈 + * + * 将栈顶元素弹出,并用e接收。 + */ +Status Pop(SqStack* S, SElemType* e); + +/* + * 遍历 + * + * 用visit函数访问顺序栈S + */ +Status StackTraverse(SqStack S, void(Visit)(SElemType)); + +#endif diff --git a/CLion/CourseBook/0304_Maze/CMakeLists.txt b/CLion/CourseBook/0304_Maze/CMakeLists.txt new file mode 100644 index 0000000..c21061f --- /dev/null +++ b/CLion/CourseBook/0304_Maze/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(Maze SqStack.h SqStack.c Maze.h Maze.c Maze-main.c) +# 链接公共库 +target_link_libraries(Maze Scanf_lib) \ No newline at end of file diff --git a/CLion/CourseBook/0304_Maze/Maze-main.c b/CLion/CourseBook/0304_Maze/Maze-main.c new file mode 100644 index 0000000..d77c7fc --- /dev/null +++ b/CLion/CourseBook/0304_Maze/Maze-main.c @@ -0,0 +1,21 @@ +#include "Maze.h" //**▲03 栈和队列**// + +int main(int argc, char* argv[]) { + MazeType maze; + PosType start, end; + char n, Re = 'Y'; + + while(Re == 'Y' || Re == 'y') { + InitMaze(maze, &start, &end); // 初始化迷宫,包括出入口 + + MazePath(maze, start, end); // 迷宫寻路 + + printf("重置?(Y/N):"); + scanf("%c%c", &Re, &n); + + printf("\n"); + } + + return 0; +} + diff --git a/CLion/CourseBook/0304_Maze/Maze.c b/CLion/CourseBook/0304_Maze/Maze.c new file mode 100644 index 0000000..90b89a6 --- /dev/null +++ b/CLion/CourseBook/0304_Maze/Maze.c @@ -0,0 +1,298 @@ +/*============== + * 迷宫寻路程序 + * + * 包含算法: 3.3 + ===============*/ + +#ifndef MAZE_C +#define MAZE_C + +#include "Maze.h" //**▲03 栈和队列**// + +/* + * ████████ 算法3.3 ████████ + * + * 迷宫寻路 + * + * 使用穷举法,找到一条可行通路即返回 + */ +Status MazePath(MazeType maze, PosType start, PosType end) { + SqStack S; // 存储探索过的通道块 + SElemType e; // e存储当前通道块信息 + PosType curPos; // 当前位置 + int curStep; // 当前通道块序号 + + // 初始化轨迹栈 + InitStack(&S); + + curPos = start; // 设定当前位置为"出口位置" + curStep = 1; // 探索第一步 + + do { + // 如果当前位置可通过(要求改位置是从未曾探索的通道块) + if(Pass(maze, curPos)) { + // 留下初始足迹,即留下向东访问的标记 + FootPrint(maze, curPos); + + // 构造一个通道块信息并返回 + e = Construct(curStep, curPos, East); + + // 加入路径 + Push(&S, e); + + // 如果到达终点 + if(Equals(curPos, end) == TRUE) { + printf("\n寻路成功!!\n\n"); + return TRUE; + } + + // 获取下一个应当探索的位置,即当前位置的东邻 + curPos = NextPos(curPos, East); + + // 探索下一步 + curStep++; + + // 如果当前位置已经探索过了,则考虑修改探索方向 + } else { + // 如果栈不为空(存在探索的必要) + if(!StackEmpty(S)) { + // 回退到上一个位置 + Pop(&S, &e); + + // 如果待探索位置的4个方向都探索过,则需要做标记 + while(e.di == North && !StackEmpty(S)) { + // 留下"死胡同"标记,即从该位置出发的路径都没有通路 + MarkPrint(maze, e.seat, Impasse); + + // 继续回退 + Pop(&S, &e); + } + + // 如果待探索位置还有剩余可探索的方向 + if(e.di < North) { + // 改变探索方向,按东南西北的方向轮询 + ++e.di; + + // 在迷宫中留下访问标记,用来观察迷宫状态(教材中没有该步骤) + MarkPrint(maze, e.seat, e.di); + + // 重新将该位置加入到路径中 + Push(&S, e); + + // 获取下一个应当探索的位置 + curPos = NextPos(e.seat, e.di); + } + } + } + + // 栈不为空,意味着还有探索的必要 + } while(!StackEmpty(S)); + + printf("\n寻路失败!!\n\n"); + + return FALSE; +} + +/* + * 初始化一个规模为N×N迷宫 + * start和end分别为迷宫的入口坐标和出口坐标 + * + *【注】 + * 教材中无此操作,但该操作是必须存在的 + */ +void InitMaze(MazeType maze, PosType* start, PosType* end) { + int i, j, tmp; + + srand((unsigned) time(NULL)); // 用系统时间做随机数种子 + + for(i = 0; i < M; i++) { + for(j = 0; j < N; j++) { + + // 在迷宫最外层生成外墙 + if(i == 0 || j == 0 || i == M - 1 || j == N - 1) { + maze[i][j] = Wall; + + // 迷宫内部的物件 + } else { + tmp = rand() % X; // 生成随机数[0, X-1]填充迷宫 + + if(tmp == 0) { + // 1/X的概率生成障碍 + maze[i][j] = Obstacle; + } else { + // 其它地方作为可遍历的通路 + maze[i][j] = Way; + } + } + } + } + + // 迷宫入口坐标 + (*start).x = 1; + (*start).y = 0; + + // 迷宫出口坐标 + (*end).x = M - 2; + (*end).y = N - 1; + + // 开放入口和出口 + maze[1][0] = maze[M - 2][N - 1] = Way; + + // 为了提高寻路成功率,将入口处和出口处临近的结点设为通路(非必须操作) + maze[1][1] = maze[M - 2][N - 2] = Way; + + // 显示迷宫的初始状态 + PaintMaze(maze); +} + +/* + * 判断当前位置是否可通过:要求该位置是从未曾探索的通道块 + * + *【注】 + * 可理解为判断当前位置是否为首次探索 + */ +Status Pass(MazeType maze, PosType seat) { + int x = seat.x; + int y = seat.y; + + // 首先检查是否越界,如果是越界了,当前位置肯定无法通过 + if(x < 0 || y < 0 || x > M - 1 || y > N - 1) { + return FALSE; //越界 + } + + // 要求该位置必须是从未曾探索的通道块 + if(maze[x][y] != Way) { + return FALSE; + } + + return TRUE; +} + +/* + * 获取下一个应当探索的位置 + * di指示当前所处位置的探索方向,包括East, South, West, North + */ +PosType NextPos(PosType seat, int di) { + PosType tmp = seat; + + switch(di) { + case East: + tmp.y++; //向东 + break; + case South: + tmp.x++; //向南 + break; + case West: + tmp.y--; //向西 + break; + case North: + tmp.x--; //向北 + break; + } + + return tmp; +} + +/* + * 留下初始访问足迹 + * + * 初始访问足迹即向东访问 + */ +void FootPrint(MazeType maze, PosType seat) { + //初始设置向东探索 + MarkPrint(maze, seat, East); +} + +/* + * 在迷宫的seat处留下mark标记 + * + *【注】 + * 该函数与教材上的函数有区别 + * 教材上只是用此函数留下"不可探索"的标记 + * 而此处的函数改进为可以留下任意标记,包括探索方向的标记 + */ +void MarkPrint(MazeType maze, PosType seat, int mark) { + int x = seat.x; + int y = seat.y; + + maze[x][y] = mark; //留下不能通过的标记 + + // 绘制迷宫 + PaintMaze(maze); +} + +/* + * 构造一个通道块信息并返回 + * + *【注】 + * 教材中有此操作,但无此函数 + */ +SElemType Construct(int ord, PosType seat, int di) { + SElemType e; + + e.ord = ord; + e.seat = seat; + e.di = di; + + return e; +} + +/* + * 判断两个坐标是否相等 + * + *【注】 + * 教材中有此操作,但无此函数 + * 因为这里需要比较两个结构体,所以不能直接用"=="符号 + */ +Status Equals(PosType seat1, PosType seat2) { + if(seat1.x == seat2.x && seat1.y == seat2.y) { + return TRUE; + } else { + return ERROR; + } +} + +/* + * 绘制迷宫 + * 以图形的方式呈现迷宫当前的状态 + * + *【注】 + * 1.教材中无此操作,此处增加该操作的目的是观察寻路过程的每一步 + * 2.该实现适用于CLion的控制台 + */ +void PaintMaze(MazeType maze) { + int i, j; + + Wait(SleepTime); // 暂停一下 + + for(i = 0; i < M; i++) { + for(j = 0; j < N; j++) { + if(maze[i][j] == Wall) { // 外墙 + printf("*"); + } else if(maze[i][j] == Obstacle) { // 迷宫内部的障碍 + printf("●"); + } else if(maze[i][j] == East) { // 正在朝东探索 + printf("▷"); + } else if(maze[i][j] == South) { // 正在朝南探索 + printf("▽"); + } else if(maze[i][j] == West) { // 正在朝西探索 + printf("◁"); + } else if(maze[i][j] == North) { // 正在朝北探索 + printf("△"); + } else if(maze[i][j] == Impasse) { // 死胡同,即四个方向都探索过,但无法通过的位置 + printf("x"); + } else { // 还未探索过的路径结点 + printf(" "); + } + + if(j != 0 && j % (N - 1) == 0) { // 每隔N个结点换行 + printf("\n"); + } + } + } + + printf("\n"); +} + +#endif + diff --git a/CLion/CourseBook/0304_Maze/Maze.h b/CLion/CourseBook/0304_Maze/Maze.h new file mode 100644 index 0000000..00cf757 --- /dev/null +++ b/CLion/CourseBook/0304_Maze/Maze.h @@ -0,0 +1,112 @@ +/*============== + * 迷宫寻路程序 + * + * 包含算法: 3.3 + ===============*/ + +#ifndef MAZE_H +#define MAZE_H + +#include +#include // 提供system、rand、srand原型 +#include // 提供time原型 +#include "Status.h" //**▲01 绪论**// +#include "SqStack.h" //**▲03 栈和队列**// + +/* 宏定义 */ +#define M 15 // 迷宫的行数 +#define N 30 // 迷宫的列数 + +#define X 4 // X指示迷宫障碍出现的概率。例如,X=4,意味着遍历迷宫时遇到障碍的概率是1/4=25% + +#define SleepTime 3 //SleepTime代表打印地图时的时间间隔 + +/* 迷宫类型定义 */ +typedef enum { + Wall, // 外墙 + Obstacle, // 迷宫内部的障碍 + Way, // 通路 + Impasse, // “死胡同” + East, South, West, North // 当前探索方向:东南西北 +} MazeNode; + +typedef int MazeType[M][N]; // 迷宫类型 + + +/* + * ████████ 算法3.3 ████████ + * + * 迷宫寻路 + * + * 使用穷举法,找到一条可行通路即返回 + */ +Status MazePath(MazeType maze, PosType start, PosType end); + +/* + * 初始化一个规模为N×N迷宫 + * start和end分别为迷宫的入口坐标和出口坐标 + * + *【注】 + * 教材中无此操作,但该操作是必须存在的 + */ +void InitMaze(MazeType maze, PosType* start, PosType* end); + +/* + * 判断当前位置是否可通过:要求该位置是从未曾探索的通道块 + * + *【注】 + * 可理解为判断当前位置是否为首次探索 + */ +Status Pass(MazeType maze, PosType seat); + +/* + * 获取下一个应当探索的位置 + * di指示当前所处位置的探索方向,包括East, South, West, North + */ +PosType NextPos(PosType seat, int di); + +/* + * 留下初始访问足迹 + * + * 初始访问足迹即向东访问 + */ +void FootPrint(MazeType maze, PosType seat); + +/* + * 在迷宫的seat处留下mark标记 + * + *【注】 + * 该函数与教材上的函数有区别 + * 教材上只是用此函数留下"不可探索"的标记 + * 而此处的函数改进为可以留下任意标记,包括探索方向的标记 + */ +void MarkPrint(MazeType maze, PosType seat, int mark); + +/* + * 构造一个通道块信息并返回 + * + *【注】 + * 教材中有此操作,但无此函数 + */ +SElemType Construct(int ord, PosType seat, int di); + +/* + * 判断两个坐标是否相等 + * + *【注】 + * 教材中有此操作,但无此函数 + * 因为这里需要比较两个结构体,所以不能直接用"=="符号 + */ +Status Equals(PosType a, PosType b); + +/* + * 绘制迷宫 + * 以图形的方式呈现迷宫当前的状态 + * + *【注】 + * 教材中无此操作 + * 此处增加该操作的目的是观察寻路过程的每一步 + */ +void PaintMaze(MazeType maze); + +#endif diff --git a/CLion/CourseBook/0304_Maze/SqStack.c b/CLion/CourseBook/0304_Maze/SqStack.c new file mode 100644 index 0000000..300e20e --- /dev/null +++ b/CLion/CourseBook/0304_Maze/SqStack.c @@ -0,0 +1,90 @@ +/*============================= + * 栈的顺序存储结构(顺序栈) + =============================*/ + +#include "SqStack.h" //**▲03 栈和队列**// + +/* + * 初始化 + * + * 构造一个空栈。初始化成功则返回OK,否则返回ERROR。 + */ +Status InitStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + (*S).base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); + } + + (*S).top = (*S).base; + (*S).stacksize = STACK_INIT_SIZE; + + return OK; +} + +/* + * 判空 + * + * 判断顺序栈中是否包含有效数据。 + * + * 返回值: + * TRUE : 顺序栈为空 + * FALSE: 顺序栈不为空 + */ +Status StackEmpty(SqStack S) { + if(S.top == S.base) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * 入栈 + * + * 将元素e压入到栈顶。 + */ +Status Push(SqStack* S, SElemType e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + // 栈满时,追加存储空间 + if((*S).top - (*S).base >= (*S).stacksize) { + (*S).base = (SElemType*) realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); // 存储分配失败 + } + + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + + // 进栈先赋值,栈顶指针再自增 + *(S->top++) = e; + + return OK; +} + +/* + * 出栈 + * + * 将栈顶元素弹出,并用e接收。 + */ +Status Pop(SqStack* S, SElemType* e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + if((*S).top == (*S).base) { + return ERROR; + } + + // 出栈栈顶指针先递减,再赋值 + *e = *(--(*S).top); + + return OK; +} diff --git a/CLion/CourseBook/0304_Maze/SqStack.h b/CLion/CourseBook/0304_Maze/SqStack.h new file mode 100644 index 0000000..43ec0a9 --- /dev/null +++ b/CLion/CourseBook/0304_Maze/SqStack.h @@ -0,0 +1,69 @@ +/*============================= + * 栈的顺序存储结构(顺序栈) + =============================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +/* 宏定义 */ +#define STACK_INIT_SIZE 100 // 顺序栈存储空间的初始分配量 +#define STACKINCREMENT 10 // 顺序栈存储空间的分配增量 + +// 迷宫通道块的坐标 +typedef struct { + int x; // 通道块的横、纵坐标定义 + int y; +} PosType; + +/* 通道块信息,用于迷宫算法 */ +typedef struct { + int ord; // 通道块的“序号” + PosType seat; // 通道块的“坐标位置” + int di; // 下一个应当访问的“方向” +} SElemType; + +// 顺序栈元素结构 +typedef struct { + SElemType* base; // 栈底指针 + SElemType* top; // 栈顶指针 + int stacksize; // 当前已分配的存储空间,以元素为单位 +} SqStack; + + +/* + * 初始化 + * + * 构造一个空栈。初始化成功则返回OK,否则返回ERROR。 + */ +Status InitStack(SqStack* S); + +/* + * 判空 + * + * 判断顺序栈中是否包含有效数据。 + * + * 返回值: + * TRUE : 顺序栈为空 + * FALSE: 顺序栈不为空 + */ +Status StackEmpty(SqStack S); + +/* + * 入栈 + * + * 将元素e压入到栈顶。 + */ +Status Push(SqStack* S, SElemType e); + +/* + * 出栈 + * + * 将栈顶元素弹出,并用e接收。 + */ +Status Pop(SqStack* S, SElemType* e); + +#endif diff --git a/CLion/CourseBook/0305_Expression/CMakeLists.txt b/CLion/CourseBook/0305_Expression/CMakeLists.txt new file mode 100644 index 0000000..bdfb2f4 --- /dev/null +++ b/CLion/CourseBook/0305_Expression/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(Expression SqStack.h SqStack.c Expression.h Expression.c Expression-main.c) +# 链接公共库 +target_link_libraries(Expression Scanf_lib) \ No newline at end of file diff --git a/CLion/CourseBook/0305_Expression/Expression-main.c b/CLion/CourseBook/0305_Expression/Expression-main.c new file mode 100644 index 0000000..acaf3f6 --- /dev/null +++ b/CLion/CourseBook/0305_Expression/Expression-main.c @@ -0,0 +1,12 @@ +#include "Expression.h" //**▲03 栈和队列**// + +int main(int argc, char** argv) { + char opnd; + char* exp = "(1+3)*2/4#"; + + opnd = EvaluateExpression(exp); + + printf("作为示例, %s 的计算结果为:%d\n", exp, opnd - '0'); + + return 0; +} diff --git a/CLion/CourseBook/0305_Expression/Expression.c b/CLion/CourseBook/0305_Expression/Expression.c new file mode 100644 index 0000000..dd4e025 --- /dev/null +++ b/CLion/CourseBook/0305_Expression/Expression.c @@ -0,0 +1,139 @@ +/*============== + * 表达式计算 + * + * 包含算法: 3.4 + ===============*/ + +#include "Expression.h" //**▲03 栈和队列**// + +/* + * ████████ 算法3.4 ████████ + * + * 从exp读入表达式,并计算表达式的运算结果 + * + *【注】 + * 1.教材使用的是控制台输入,这里为了便于测试,直接改为从形参接收参数 + * 2.该计算功能有限,理论上仅支持对个位数运算,且要求每一步的运算结果也是个位数。 + * 教材提供此算法的目的是验证栈的使用,如果想扩展对运算符的支持,并扩大对操作数的支持, + * 则可以顺着此思路进行改版 + */ +OperandType EvaluateExpression(const char exp[]) { + SElemType c; // 输入序列 + + SqStack OPTR; // 运算符栈 + SqStack OPND; // 操作数栈 + + OperatorType theta, x; // 运算符 + OperandType a, b; // 操作数 + + int i = 0; + + // 初始化运算符栈,并将一个界限符'#'入栈 + InitStack(&OPTR); + Push(&OPTR, '#'); + + // 初始化操作数栈,并开始读取输入 + InitStack(&OPND); + c = exp[i++]; + + // 当输入中遇到界限符'#',且运算符栈的栈顶元素也是界限符'#'时,则表示读取结束,且运算结束 + while(c != '#' || GetTop(OPTR) != '#') { + // 如果ch不是运算符,则视其为操作数,将其入栈 + if(!In(c, OP)) { + Push(&OPND, c); // 将操作数入栈 + c = exp[i++]; // 获取下一个输入字符 + } else { + switch(Precede(GetTop(OPTR), c)) { + // 栈中运算符优先级低,继续进栈 + case '<': + Push(&OPTR, c); + c = exp[i++]; + break; + + // 优先级相等时,说明这里遇到括号,需要脱括号 + case '=': + Pop(&OPTR, &x); + c = exp[i++]; + break; + + /* + * 栈中运算符优先级高时,先计算,再将计算结果压入栈 + * + * 注:这儿没有读字符,c保留的还是刚才读到的字符 + */ + case '>': + Pop(&OPTR, &theta); // 弹出运算符 + Pop(&OPND, &b); // 弹出右边的操作数 + Pop(&OPND, &a); // 弹出左边的操作数 + Push(&OPND, Operate(a, theta, b)); + break; + } + } + } + + return GetTop(OPND); +} + +// 判断指定的运算符是否合规 +Status In(SElemType c, const char OP[]) { + + SElemType* e = strchr(OP, c); + + // 如果运算符c不在合规范围内,说明指定的运算符不合规 + if(e == NULL) { + return FALSE; + } else { + return TRUE; + } +} + +/* + * 判断运算符栈中操作符o1与表达式中的操作符o2的优先级。 + * + * 返回'>'、'<'、'=',以指示o1和o2的优先级 + */ +OperatorType Precede(OperatorType o1, OperatorType o2) { + int x, y; + + // 获取指定的运算符在运算符表中的位置 + char* p1 = strchr(OP, o1); + char* p2 = strchr(OP, o2); + + // 计算出一个运算符优先级表坐标 + x = p1 - OP; + y = p2 - OP; + + return PrecedeTable[x][y]; +} + +/* + * 对操作数进行运算 + * + * a、b是操作数,theta是运算符。 + * 对于操作数和运算结果,仅保证对个位数的支持 + */ +OperandType Operate(OperandType a, OperatorType theta, OperandType b) { + int x, y, z = CHAR_MAX - 48; + + // 先从字符型转为整型 + x = a - '0'; + y = b - '0'; + + switch(theta) { + case '+': + z = x + y; + break; + case '-': + z = x - y; + break; + case '*': + z = x * y; + break; + case '/': + z = x / y; + break; + } + + // 计算完成后,将整型转换为字符型返回 + return z + 48; +} diff --git a/CLion/CourseBook/0305_Expression/Expression.h b/CLion/CourseBook/0305_Expression/Expression.h new file mode 100644 index 0000000..b374fcb --- /dev/null +++ b/CLion/CourseBook/0305_Expression/Expression.h @@ -0,0 +1,70 @@ +/*============== + * 表达式计算 + * + * 包含算法: 3.4 + ===============*/ + +#ifndef EXPRESSION_H +#define EXPRESSION_H + +#include +#include +#include +#include "SqStack.h" //**▲03 栈和队列**// + +typedef SElemType OperatorType; // 运算符类型 +typedef SElemType OperandType; // 操作数类型 + +// 运算符表,即表达式中允许出现的符号(包括界限符'#') +static const char OP[] = {'+', '-', '*', '/', '(', ')', '#'}; + +/* + * 运算符优先级表(包括界限符'#'),与上面的OP表是呼应的。 + * 可参见教材中的"算符间的优先关系"表 + */ +static const char PrecedeTable[7][7] = {{'>', '>', '<', '<', '<', '>', '>'}, + {'>', '>', '<', '<', '<', '>', '>'}, + {'>', '>', '>', '>', '<', '>', '>'}, + {'>', '>', '>', '>', '<', '>', '>'}, + {'<', '<', '<', '<', '<', '=', ' '}, + {'>', '>', '>', '>', ' ', '>', '>'}, + {'<', '<', '<', '<', '<', ' ', '='}}; + + +/* + * ████████ 算法3.4 ████████ + * + * 从exp读入表达式,并计算表达式的运算结果 + * + *【注】 + * 1.教材使用的是控制台输入,这里为了便于测试,直接改为从形参接收参数 + * 2.该计算功能有限,理论上仅支持对个位数运算,且要求每一步的运算结果也是个位数。 + * 教材提供此算法的目的是验证栈的使用,如果想扩展对运算符的支持,并扩大对操作数的支持, + * 则可以顺着此思路进行改版 + */ +OperandType EvaluateExpression(const char exp[]); + +/* + * 判断指定的运算符是否合规 + * + * OP中存储了合规的运算符,包括界限符'#' + */ +Status In(SElemType c, const char OP[]); + +/* + * 判断运算符栈中操作符o1与表达式中的操作符o2的优先级。 + * + * 返回'>'、'<'、'=',以指示o1和o2的优先级 + */ +OperatorType Precede(OperatorType o1, OperatorType o2); + +/* + * 对操作数进行运算 + * + * a、b是操作数,theta是运算符。 + * 对于操作数和运算结果,仅保持对个位数的支持 + */ +OperandType Operate(OperandType a, OperatorType theta, OperandType b); + + +#endif diff --git a/CLion/CourseBook/0305_Expression/SqStack.c b/CLion/CourseBook/0305_Expression/SqStack.c new file mode 100644 index 0000000..d758426 --- /dev/null +++ b/CLion/CourseBook/0305_Expression/SqStack.c @@ -0,0 +1,111 @@ +/*============================= + * 栈的顺序存储结构(顺序栈) + =============================*/ + +#include "SqStack.h" //**▲03 栈和队列**// + +/* + * 初始化 + * + * 构造一个空栈。初始化成功则返回OK,否则返回ERROR。 + */ +Status InitStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + (*S).base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); + } + + (*S).top = (*S).base; + (*S).stacksize = STACK_INIT_SIZE; + + return OK; +} + +/* + * 判空 + * + * 判断顺序栈中是否包含有效数据。 + * + * 返回值: + * TRUE : 顺序栈为空 + * FALSE: 顺序栈不为空 + */ +Status StackEmpty(SqStack S) { + if(S.top == S.base) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * 取值 + * + * 获取操作符栈的栈顶元素。 + * + *【注】 + * 该操作的实现与传统的顺序栈的取值操作有些不同,但核心作用一致 + */ +SElemType GetTop(SqStack S) { + SElemType e; + + if(S.base == NULL || S.top == S.base) { + return '\0'; + } + + // 不会改变栈中元素 + e = *(S.top - 1); + + return e; +} + +/* + * 入栈 + * + * 将元素e压入到栈顶。 + */ +Status Push(SqStack* S, SElemType e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + // 栈满时,追加存储空间 + if((*S).top - (*S).base >= (*S).stacksize) { + (*S).base = (SElemType*) realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); // 存储分配失败 + } + + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + + // 进栈先赋值,栈顶指针再自增 + *(S->top++) = e; + + return OK; +} + +/* + * 出栈 + * + * 将栈顶元素弹出,并用e接收。 + */ +Status Pop(SqStack* S, SElemType* e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + if((*S).top == (*S).base) { + return ERROR; + } + + // 出栈栈顶指针先递减,再赋值 + *e = *(--(*S).top); + + return OK; +} diff --git a/CLion/CourseBook/0305_Expression/SqStack.h b/CLion/CourseBook/0305_Expression/SqStack.h new file mode 100644 index 0000000..21d66eb --- /dev/null +++ b/CLion/CourseBook/0305_Expression/SqStack.h @@ -0,0 +1,69 @@ +/*============================= + * 栈的顺序存储结构(顺序栈) + =============================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +/* 宏定义 */ +#define STACK_INIT_SIZE 100 // 顺序栈存储空间的初始分配量 +#define STACKINCREMENT 10 // 顺序栈存储空间的分配增量 + +/* 表达式元素类型定义 */ +typedef char SElemType; + +// 顺序栈元素结构 +typedef struct { + SElemType* base; // 栈底指针 + SElemType* top; // 栈顶指针 + int stacksize; // 当前已分配的存储空间,以元素为单位 +} SqStack; + + +/* + * 初始化 + * + * 构造一个空栈。初始化成功则返回OK,否则返回ERROR。 + */ +Status InitStack(SqStack* S); + +/* + * 判空 + * + * 判断顺序栈中是否包含有效数据。 + * + * 返回值: + * TRUE : 顺序栈为空 + * FALSE: 顺序栈不为空 + */ +Status StackEmpty(SqStack S); + +/* + * 取值 + * + * 获取操作符栈的栈顶元素。 + * + *【注】 + * 该操作的实现与传统的顺序栈的取值操作有些不同,但核心作用一致 + */ +SElemType GetTop(SqStack S); + +/* + * 入栈 + * + * 将元素e压入到栈顶。 + */ +Status Push(SqStack* S, SElemType e); + +/* + * 出栈 + * + * 将栈顶元素弹出,并用e接收。 + */ +Status Pop(SqStack* S, SElemType* e); + +#endif diff --git a/CLion/CourseBook/0306_Hanoi/CMakeLists.txt b/CLion/CourseBook/0306_Hanoi/CMakeLists.txt new file mode 100644 index 0000000..025b1b5 --- /dev/null +++ b/CLion/CourseBook/0306_Hanoi/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(Hanoi Hanoi.h Hanoi.c Hanoi-main.c) +# 链接公共库 +target_link_libraries(Hanoi Scanf_lib) \ No newline at end of file diff --git a/CLion/CourseBook/0306_Hanoi/Hanoi-main.c b/CLion/CourseBook/0306_Hanoi/Hanoi-main.c new file mode 100644 index 0000000..9ff3a32 --- /dev/null +++ b/CLion/CourseBook/0306_Hanoi/Hanoi-main.c @@ -0,0 +1,15 @@ +#include "Hanoi.h" //**03 栈和队列**// + +int main(int argc, char** argv) { + char x = 'x'; + char y = 'y'; + char z = 'z'; + + printf("作为示例,假设圆盘个数为 %d ,操作步骤如下...\n", N); + + init(N); + + hanoi(N, x, y, z); + + return 0; +} diff --git a/CLion/CourseBook/0306_Hanoi/Hanoi.c b/CLion/CourseBook/0306_Hanoi/Hanoi.c new file mode 100644 index 0000000..0f4f83e --- /dev/null +++ b/CLion/CourseBook/0306_Hanoi/Hanoi.c @@ -0,0 +1,133 @@ +/*============== + * 汉诺塔 + * + * 包含算法: 3.5 + ===============*/ + +#include "Hanoi.h" //**03 栈和队列**// + +/* + * ████████ 算法3.5 ████████ + * + * 汉诺塔求解:以y为辅助,将x上前n个圆盘移动到z + */ +void hanoi(int n, char x, char y, char z) { + if(n == 1) { // 欲移动n个圆盘,需先移动其上的n-1个圆盘 + move(x, 1, z); // 将编号为1的圆盘从x移到z + } else { + hanoi(n - 1, x, z, y); // 将x上编号为1至n-1的圆盘移到y,z作辅助塔 + move(x, n, z); // 将编号为n的圆盘从x移到z + hanoi(n - 1, y, x, z); // 将y上编号为1至n-1的圆盘移动到z,x作辅助塔 + } +} + +void move(char x, int n, char z) { + // step为全局变量,在main函数之外定义 + gStep++; + printf("第%2d步:将第 %d 个圆盘从 %c 移到 %c \n", gStep, n, x, z); + + // 汉诺塔移动的图形表示 + PrintGraph(x, n, z); +} + +/* + * 汉诺塔图形信息初始化 + * + *【注】 + * 教材中无此操作。 + * 增加此操作的目的是为了便于观察汉诺塔圆盘的移动过程 + */ +void init(int n) { + int i; + int* towerX, * towerY, * towerZ; + + T.plates = (int**) malloc(3 * sizeof(int*)); + + towerX = (int*) malloc(n * sizeof(int)); + towerY = (int*) malloc(n * sizeof(int)); + towerZ = (int*) malloc(n * sizeof(int)); + + for(i = 0; i < n; ++i) { + towerX[i] = n - i; + towerY[i] = 0; + towerZ[i] = 0; + } + + T.plates[0] = towerX; + T.plates[1] = towerY; + T.plates[2] = towerZ; + + T.high[0] = n; + T.high[1] = 0; + T.high[2] = 0; + + // 汉诺塔移动的图形表示 + PrintGraph('\0', 0, '\0'); +} + +/* + * 汉诺塔移动的图形表示 + * + *【注】 + * 教材中无此操作。 + * 增加此操作的目的是为了便于观察汉诺塔圆盘的移动过程 + */ +void PrintGraph(char t1, int n, char t2) { + int i, j; + char** s; + + // 将n号盘子从t1中移除 + if(t1 == 'x') { + T.plates[0][T.high[0] - 1] = 0; + T.high[0]--; + } else if(t1 == 'y') { + T.plates[1][T.high[1] - 1] = 0; + T.high[1]--; + } else if(t1 == 'z') { + T.plates[2][T.high[2] - 1] = 0; + T.high[2]--; + } else { + // t1上的圆盘不需要移动 + } + + // 将n号盘子添加到t2中 + if(t2 == 'x') { + T.plates[0][T.high[0]] = n; + T.high[0]++; + } else if(t2 == 'y') { + T.plates[1][T.high[1]] = n; + T.high[1]++; + } else if(t2 == 'z') { + T.plates[2][T.high[2]] = n; + T.high[2]++; + } else { + // t2上的圆盘不需要移动 + } + + s = (char**) malloc((N + 2) * sizeof(char*)); + for(i = 0; i < N + 2; i++) { + s[i] = (char*) malloc(N * sizeof(char)); + + for(j = 0; j < i; j++) { + if(i == N + 1) { + s[i][j] = '-'; // 将最后一行初始化为托盘 + } else { + s[i][j] = '*'; + } + } + + if(i == N + 1) { + s[i][j - 1] = '\0'; + } else { + s[i][j] = '\0'; + } + } + + for(i = N - 1; i >= 0; i--) { + printf("%-*s | %-*s | %-*s\n", N, s[T.plates[0][i]], N, s[T.plates[1][i]], N, s[T.plates[2][i]]); + } + printf("%-*s + %-*s + %-*s\n", N, s[N + 1], N, s[N + 1], N, s[N + 1]); + printf("%-*s %-*s %-*s\n", N + 2, "x", N + 2, "y", N + 2, "z"); + + printf("\n"); +} diff --git a/CLion/CourseBook/0306_Hanoi/Hanoi.h b/CLion/CourseBook/0306_Hanoi/Hanoi.h new file mode 100644 index 0000000..7ebdaec --- /dev/null +++ b/CLion/CourseBook/0306_Hanoi/Hanoi.h @@ -0,0 +1,59 @@ +/*============== + * 汉诺塔 + * + * 包含算法: 3.5 + ===============*/ + +#ifndef HANOI_H +#define HANOI_H + +#include +#include +#include "Status.h" + +#define N 5 // 汉诺塔中盘子总数 + +// 汉诺塔图形信息 +typedef struct { + int** plates; // 汉诺塔中的圆盘信息 + int high[3]; // 三座塔的高度(持有的盘子数量) +} Tower; + +// 汉诺塔 +Tower T; + +// 统计移动步数 +int gStep; + + +/* + * ████████ 算法3.5 ████████ + * + * 汉诺塔求解:以y塔为辅助,将x塔上前n个圆盘移动到z塔 + */ +void hanoi(int n, char x, char y, char z); + +/* + * 移动汉诺塔圆盘:将第n个圆盘从x塔移到z塔。 + */ +void move(char x, int n, char z); + +/* + * 汉诺塔图形信息初始化 + * + *【注】 + * 教材中无此操作。 + * 增加此操作的目的是为了便于观察汉诺塔圆盘的移动过程 + */ +void init(int n); + +/* + * 汉诺塔移动的图形表示,参数含义参见move()函数 + * + *【注】 + * 教材中无此操作。 + * 增加此操作的目的是为了便于观察汉诺塔圆盘的移动过程 + */ +void PrintGraph(char x, int n, char z); + +#endif diff --git a/CLion/CourseBook/0307_LinkQueue/CMakeLists.txt b/CLion/CourseBook/0307_LinkQueue/CMakeLists.txt new file mode 100644 index 0000000..4189a47 --- /dev/null +++ b/CLion/CourseBook/0307_LinkQueue/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(LinkQueue LinkQueue.h LinkQueue.c LinkQueue-main.c) +# 链接公共库 +target_link_libraries(LinkQueue Scanf_lib) \ No newline at end of file diff --git a/CLion/CourseBook/0307_LinkQueue/LinkQueue-main.c b/CLion/CourseBook/0307_LinkQueue/LinkQueue-main.c new file mode 100644 index 0000000..c1c6838 --- /dev/null +++ b/CLion/CourseBook/0307_LinkQueue/LinkQueue-main.c @@ -0,0 +1,94 @@ +#include +#include "LinkQueue.h" //**03 栈和队列**// + +// 测试函数,打印整型 +void PrintElem(QElemType e); + +int main(int argc, char** argv) { + LinkQueue Q; + int i; + QElemType e; + + printf("████████ 函数 InitQueue \n"); + { + printf("█ 初始化链队 Q ...\n"); + InitQueue(&Q); + } + PressEnterToContinue(); + + printf("████████ 函数 QueueEmpty \n"); + { + QueueEmpty(Q) ? printf("█ Q 为空!!\n") : printf("█ Q 不为空!\n"); + } + PressEnterToContinue(); + + printf("████████ 函数 EnQueue \n"); + { + for(i = 1; i <= 6; i++) { + EnQueue(&Q, 2 * i); + printf("█ 元素 \"%2d\" 入队...\n", 2 * i); + } + } + PressEnterToContinue(); + + printf("████████ 函数 QueueTraverse \n"); + { + printf("█ Q 中的元素为:Q = "); + QueueTraverse(Q, PrintElem); + } + PressEnterToContinue(); + + printf("████████ 函数 QueueLength \n"); + { + i = QueueLength(Q); + printf("█ Q 的长度为 %d \n", i); + } + PressEnterToContinue(); + + printf("████████ 函数 DeQueue \n"); + { + DeQueue(&Q, &e); + printf("█ 队头元素 \"%d\" 出队...\n", e); + printf("█ Q 中的元素为:Q = "); + QueueTraverse(Q, PrintElem); + } + PressEnterToContinue(); + + printf("████████ 函数 GetHead \n"); + { + GetHead(Q, &e); + printf("█ 队头元素的值为 \"%d\" \n", e); + } + PressEnterToContinue(); + + printf("████████ 函数 ClearQueue \n"); + { + printf("█ 清空 Q 前:"); + QueueEmpty(Q) ? printf(" Q 为空!!\n") : printf(" Q 不为空!\n"); + + ClearQueue(&Q); + + printf("█ 清空 Q 后:"); + QueueEmpty(Q) ? printf(" Q 为空!!\n") : printf(" Q 不为空!\n"); + } + PressEnterToContinue(); + + printf("████████ 函数 DestroyQueue \n"); + { + printf("█ 销毁 Q 前:"); + Q.front != NULL && Q.rear != NULL ? printf(" Q 存在!\n") : printf(" Q 不存在!!\n"); + + DestroyQueue(&Q); + + printf("█ 销毁 Q 后:"); + Q.front != NULL && Q.rear != NULL ? printf(" Q 存在!\n") : printf(" Q 不存在!!\n"); + } + PressEnterToContinue(); + + return 0; +} + +// 测试函数,打印整型 +void PrintElem(QElemType e) { + printf("%d ", e); +} diff --git a/CLion/CourseBook/0307_LinkQueue/LinkQueue.c b/CLion/CourseBook/0307_LinkQueue/LinkQueue.c new file mode 100644 index 0000000..bdca3a5 --- /dev/null +++ b/CLion/CourseBook/0307_LinkQueue/LinkQueue.c @@ -0,0 +1,204 @@ +/*========================= + * 队列的链式存储结构(链队) + ==========================*/ + +#ifndef LINKQUEUE_C +#define LINKQUEUE_C + +#include "LinkQueue.h" //**▲03 栈和队列**// + +/* + * 初始化 + * + * 构造一个空的链队。 + * 初始化成功则返回OK,否则返回ERROR。 + * + *【注】 + * 这里的队列带有头结点 + */ +Status InitQueue(LinkQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).front = (*Q).rear = (QueuePtr) malloc(sizeof(QNode)); + if(!(*Q).front) { + exit(OVERFLOW); + } + + (*Q).front->next = NULL; + + return OK; +} + +/* + * 销毁(结构) + * + * 释放链队所占内存。 + */ +Status DestroyQueue(LinkQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + while((*Q).front) { + (*Q).rear = (*Q).front->next; + free((*Q).front); + (*Q).front = (*Q).rear; + } + + return OK; +} + +/* + * 置空(内容) + * + * 这里需要释放链队中非头结点处的空间。 + */ +Status ClearQueue(LinkQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).rear = (*Q).front->next; + + while((*Q).rear) { + (*Q).front->next = (*Q).rear->next; + free((*Q).rear); + (*Q).rear = (*Q).front->next; + } + + (*Q).rear = (*Q).front; + + return OK; +} + +/* + * 判空 + * + * 判断链队中是否包含有效数据。 + * + * 返回值: + * TRUE : 链队为空 + * FALSE: 链队不为空 + */ +Status QueueEmpty(LinkQueue Q) { + if(Q.front == Q.rear) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * 计数 + * + * 返回链队包含的有效元素的数量。 + */ +int QueueLength(LinkQueue Q) { + int count = 0; + QueuePtr p = Q.front; + + while(p != Q.rear) { + count++; + p = p->next; + } + + return count; +} + +/* + * 取值 + * + * 获取队头元素,将其存储到e中。 + * 如果可以找到,返回OK,否则,返回ERROR。 + */ +Status GetHead(LinkQueue Q, QElemType* e) { + QueuePtr p; + + if(Q.front == NULL || Q.front == Q.rear) { + return ERROR; + } + + p = Q.front->next; + *e = p->data; + + return OK; +} + +/* + * 入队 + * + * 将元素e添加到队列尾部。 + */ +Status EnQueue(LinkQueue* Q, QElemType e) { + QueuePtr p; + + if(Q == NULL || (*Q).front == NULL) { + return ERROR; + } + + p = (QueuePtr) malloc(sizeof(QNode)); + if(!p) { + exit(OVERFLOW); + } + + p->data = e; + p->next = NULL; + + (*Q).rear->next = p; + (*Q).rear = p; + + return OK; +} + +/* + * 出队 + * + * 移除队列头部的元素,将其存储到e中。 + */ +Status DeQueue(LinkQueue* Q, QElemType* e) { + QueuePtr p; + + if(Q == NULL || (*Q).front == NULL || (*Q).front == (*Q).rear) { + return ERROR; + } + + p = (*Q).front->next; + *e = p->data; + + (*Q).front->next = p->next; + if((*Q).rear == p) { + (*Q).rear = (*Q).front; + } + + free(p); + + return OK; +} + +/* + * 遍历 + * + * 用visit函数访问队列Q + */ +Status QueueTraverse(LinkQueue Q, void (Visit)(QElemType)) { + QueuePtr p; + + if(Q.front == NULL) { + return ERROR; + } + + p = Q.front->next; + + while(p != NULL) { + Visit(p->data); + p = p->next; + } + + printf("\n"); + + return OK; +} + +#endif diff --git a/CLion/CourseBook/0307_LinkQueue/LinkQueue.h b/CLion/CourseBook/0307_LinkQueue/LinkQueue.h new file mode 100644 index 0000000..512ea96 --- /dev/null +++ b/CLion/CourseBook/0307_LinkQueue/LinkQueue.h @@ -0,0 +1,100 @@ +/*========================= + * 队列的链式存储结构(链队) + ==========================*/ + +#ifndef LINKQUEUE_H +#define LINKQUEUE_H + +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +/* 链队元素类型定义 */ +typedef int QElemType; + +// 队列元素结构 +typedef struct QNode { + QElemType data; + struct QNode* next; +} QNode, * QueuePtr; + +// 队列结构 +typedef struct { + QueuePtr front; // 队头指针 + QueuePtr rear; // 队尾指针 +} LinkQueue; // 队列的链式存储表示 + + +/* + * 初始化 + * + * 构造一个空的链队。 + * 初始化成功则返回OK,否则返回ERROR。 + * + *【注】 + * 这里的队列带有头结点 + */ +Status InitQueue(LinkQueue* Q); + +/* + * 销毁(结构) + * + * 释放链队所占内存。 + */ +Status DestroyQueue(LinkQueue* Q); + +/* + * 置空(内容) + * + * 这里需要释放链队中非头结点处的空间。 + */ +Status ClearQueue(LinkQueue* Q); + +/* + * 判空 + * + * 判断链队中是否包含有效数据。 + * + * 返回值: + * TRUE : 链队为空 + * FALSE: 链队不为空 + */ +Status QueueEmpty(LinkQueue Q); + +/* + * 计数 + * + * 返回链队包含的有效元素的数量。 + */ +int QueueLength(LinkQueue Q); + +/* + * 取值 + * + * 获取队头元素,将其存储到e中。 + * 如果可以找到,返回OK,否则,返回ERROR。 + */ +Status GetHead(LinkQueue Q, QElemType* e); + +/* + * 入队 + * + * 将元素e添加到队列尾部。 + */ +Status EnQueue(LinkQueue* Q, QElemType e); + +/* + * 出队 + * + * 移除队列头部的元素,将其存储到e中。 + */ +Status DeQueue(LinkQueue* Q, QElemType* e); + +/* + * 遍历 + * + * 用visit函数访问队列Q + */ +Status QueueTraverse(LinkQueue Q, void(Visit)(QElemType)); + +#endif diff --git a/CLion/CourseBook/0308_SqQueue/CMakeLists.txt b/CLion/CourseBook/0308_SqQueue/CMakeLists.txt new file mode 100644 index 0000000..ede8486 --- /dev/null +++ b/CLion/CourseBook/0308_SqQueue/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(SqQueue SqQueue.h SqQueue.c SqQueue-main.c) +# 链接公共库 +target_link_libraries(SqQueue Scanf_lib) \ No newline at end of file diff --git a/CLion/CourseBook/0308_SqQueue/SqQueue-main.c b/CLion/CourseBook/0308_SqQueue/SqQueue-main.c new file mode 100644 index 0000000..1975d2a --- /dev/null +++ b/CLion/CourseBook/0308_SqQueue/SqQueue-main.c @@ -0,0 +1,94 @@ +#include +#include "SqQueue.h" //**▲03 栈和队列**// + +// 测试函数,打印整型 +void PrintElem(QElemType e); + +int main(int argc, char** argv) { + SqQueue Q; + int i; + QElemType e; + + printf("████████ 函数 InitQueue 测试...\n"); + { + printf("█ 初始化循环顺序队列 Q ...\n"); + InitQueue(&Q); + } + PressEnterToContinue(); + + printf("████████ 函数 QueueEmpty 测试...\n"); + { + QueueEmpty(Q) ? printf("█ Q 为空!!\n") : printf("█ Q 不为空!\n"); + } + PressEnterToContinue(); + + printf("████████ 函数 EnQueue 测试...\n"); + { + for(i = 1; i <= 6; i++) { + EnQueue(&Q, 2 * i); + printf("█ 元素 \"%2d\" 入队Q...\n", 2 * i); + } + } + PressEnterToContinue(); + + printf("████████ 函数 QueueTraverse 测试...\n"); + { + printf("█ Q 中的元素为:Q = "); + QueueTraverse(Q, PrintElem); + } + PressEnterToContinue(); + + printf("████████ 函数 QueueLength 测试...\n"); + { + i = QueueLength(Q); + printf("█ Q 的长度为 %d \n", i); + } + PressEnterToContinue(); + + printf("████████ 函数 DeQueue 测试...\n"); + { + DeQueue(&Q, &e); + printf("█ 队头元素 \"%d\" 出队...\n", e); + printf("█ Q 中的元素为:Q = "); + QueueTraverse(Q, PrintElem); + } + PressEnterToContinue(); + + printf("████████ 函数 GetHead 测试...\n"); + { + GetHead(Q, &e); + printf("█ 队头元素的值为 \"%d\" \n", e); + } + PressEnterToContinue(); + + printf("████████ 函数 ClearQueue 测试...\n"); + { + printf("█ 清空 Q 前:"); + QueueEmpty(Q) ? printf(" Q 为空!!\n") : printf(" Q 不为空!\n"); + + ClearQueue(&Q); + + printf("█ 清空 Q 后:"); + QueueEmpty(Q) ? printf(" Q 为空!!\n") : printf(" Q 不为空!\n"); + } + PressEnterToContinue(); + + printf("████████ 函数 DestroyQueue 测试...\n"); + { + printf("█ 销毁 Q 前:"); + Q.base != NULL ? printf(" Q 存在!\n") : printf(" Q 不存在!!\n"); + + DestroyQueue(&Q); + + printf("█ 销毁 Q 后:"); + Q.base != NULL ? printf(" Q 存在!\n") : printf(" Q 不存在!!\n"); + } + PressEnterToContinue(); + + return 0; +} + +// 测试函数,打印整型 +void PrintElem(QElemType e) { + printf("%d ", e); +} diff --git a/CLion/CourseBook/0308_SqQueue/SqQueue.c b/CLion/CourseBook/0308_SqQueue/SqQueue.c new file mode 100644 index 0000000..5e3eadf --- /dev/null +++ b/CLion/CourseBook/0308_SqQueue/SqQueue.c @@ -0,0 +1,182 @@ +/*============================= + * 队列的顺序存储结构(顺序队列) + ==============================*/ + +#include "SqQueue.h" //**▲03 栈和队列**// + +/* + * 初始化 + * + * 构造一个空的顺序队列。 + * 初始化成功则返回OK,否则返回ERROR。 + * + *【注】 + * 这里的队列是循环队列 + */ +Status InitQueue(SqQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).base = (QElemType*) malloc(MAXQSIZE * sizeof(QElemType)); + if(!(*Q).base) { + exit(OVERFLOW); + } + + (*Q).front = (*Q).rear = 0; + + return OK; +} + +/* + * 销毁(结构) + * + * 释放循环顺序队列所占内存。 + */ +Status DestroyQueue(SqQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + if((*Q).base) { + free((*Q).base); + } + + (*Q).base = NULL; + (*Q).front = (*Q).rear = 0; + + return ERROR; +} + +/* + * 置空(内容) + * + * 只是清理循环顺序队列中存储的数据,不释放顺序栈所占内存。 + */ +Status ClearQueue(SqQueue* Q) { + if(Q == NULL || (*Q).base == NULL) { + return ERROR; + } + + (*Q).front = (*Q).rear = 0; + + return OK; +} + +/* + * 判空 + * + * 判断循环顺序队列中是否包含有效数据。 + * + * 返回值: + * TRUE : 循环顺序队列为空 + * FALSE: 循环顺序队列不为空 + */ +Status QueueEmpty(SqQueue Q) { + // 队列空的标志 + if(Q.front == Q.rear) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * 计数 + * + * 返回循环顺序队列包含的有效元素的数量。 + */ +int QueueLength(SqQueue Q) { + if(Q.base == NULL) { + return 0; + } + + // 队列长度 + return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE; +} + +/* + * 取值 + * + * 获取队头元素,将其存储到e中。 + * 如果可以找到,返回OK,否则,返回ERROR。 + */ +Status GetHead(SqQueue Q, QElemType* e) { + // 队列空的标志 + if(Q.base == NULL || Q.front == Q.rear) { + return ERROR; + } + + *e = Q.base[Q.front]; + + return OK; +} + +/* + * 入队 + * + * 将元素e添加到队列尾部。 + */ +Status EnQueue(SqQueue* Q, QElemType e) { + if(Q == NULL || (*Q).base == NULL) { + return ERROR; + } + + // 队列满的标志(会浪费一个空间来区分队列空和队列满) + if(((*Q).rear + 1) % MAXQSIZE == (*Q).front) { + return ERROR; + } + + // 入队 + (*Q).base[(*Q).rear] = e; + + // 尾指针前进 + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + + return OK; +} + +/* + * 出队 + * + * 移除队列头部的元素,将其存储到e中。 + */ +Status DeQueue(SqQueue* Q, QElemType* e) { + if(Q == NULL || (*Q).base == NULL) { + return ERROR; + } + + // 队列空的标志 + if((*Q).front == (*Q).rear) { + return ERROR; + } + + // 出队 + *e = (*Q).base[(*Q).front]; + + // 头指针前进 + (*Q).front = ((*Q).front + 1) % MAXQSIZE; + + return OK; +} + +/* + * 遍历 + * + * 用visit函数访问队列Q + */ +Status QueueTraverse(SqQueue Q, void(Visit)(QElemType)) { + int i; + + if(Q.base == NULL) { + return ERROR; + } + + for(i = Q.front; i != Q.rear; i = (i + 1) % MAXQSIZE) { + Visit(Q.base[i]); + } + + printf("\n"); + + return OK; +} diff --git a/CLion/CourseBook/0308_SqQueue/SqQueue.h b/CLion/CourseBook/0308_SqQueue/SqQueue.h new file mode 100644 index 0000000..a37cd43 --- /dev/null +++ b/CLion/CourseBook/0308_SqQueue/SqQueue.h @@ -0,0 +1,98 @@ +/*============================= + * 队列的顺序存储结构(顺序队列) + ==============================*/ + +#ifndef SQQUEUE_H +#define SQQUEUE_H + +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +/* 宏定义 */ +#define MAXQSIZE 1000 //最大队列长度 + +/* 循环队列元素类型定义 */ +typedef int QElemType; + +// 循环队列的顺序存储结构 +typedef struct { + QElemType* base; // 动态分配存储空间 + int front; // 头指针,若队列不空,指向队头元素 + int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置 +} SqQueue; + + +/* + * 初始化 + * + * 构造一个空的顺序队列。 + * 初始化成功则返回OK,否则返回ERROR。 + * + *【注】 + * 这里的队列是循环队列 + */ +Status InitQueue(SqQueue* Q); + +/* + * 销毁(结构) + * + * 释放循环顺序队列所占内存。 + */ +Status ClearQueue(SqQueue* Q); + +/* + * 置空(内容) + * + * 只是清理循环顺序队列中存储的数据,不释放顺序栈所占内存。 + */ +Status DestroyQueue(SqQueue* Q); + +/* + * 判空 + * + * 判断循环顺序队列中是否包含有效数据。 + * + * 返回值: + * TRUE : 循环顺序队列为空 + * FALSE: 循环顺序队列不为空 + */ +Status QueueEmpty(SqQueue Q); + +/* + * 计数 + * + * 返回循环顺序队列包含的有效元素的数量。 + */ +int QueueLength(SqQueue Q); + +/* + * 取值 + * + * 获取队头元素,将其存储到e中。 + * 如果可以找到,返回OK,否则,返回ERROR。 + */ +Status GetHead(SqQueue Q, QElemType* e); + +/* + * 入队 + * + * 将元素e添加到队列尾部。 + */ +Status EnQueue(SqQueue* Q, QElemType e); + +/* + * 出队 + * + * 移除队列头部的元素,将其存储到e中。 + */ +Status DeQueue(SqQueue* Q, QElemType* e); + +/* + * 遍历 + * + * 用visit函数访问队列Q + */ +Status QueueTraverse(SqQueue Q, void(Visit)(QElemType)); + +#endif diff --git a/CLion/CourseBook/0309_BankQueuing/BankQueuing-main.c b/CLion/CourseBook/0309_BankQueuing/BankQueuing-main.c new file mode 100644 index 0000000..9b96bf2 --- /dev/null +++ b/CLion/CourseBook/0309_BankQueuing/BankQueuing-main.c @@ -0,0 +1,10 @@ +#include "BankQueuing.h" //**▲03 栈和队列**// + +int main(int argc, char** argv) { + + Bank_Simulation_1(); //算法3.6 + +// Bank_Simulation_2(); //算法3.7,跟3.6类似 + + return 0; +} diff --git a/CLion/CourseBook/0309_BankQueuing/BankQueuing.c b/CLion/CourseBook/0309_BankQueuing/BankQueuing.c new file mode 100644 index 0000000..d455747 --- /dev/null +++ b/CLion/CourseBook/0309_BankQueuing/BankQueuing.c @@ -0,0 +1,338 @@ +/*================== + * 模拟银行排队 + * + * 包含算法: 3.6、3.7 + ===================*/ + +#ifndef BANKQUEUING_C +#define BANKQUEUING_C + +#include "BankQueuing.h" //**▲03 栈和队列**// + +/* + * ████████ 算法3.6 ████████ + * + * 银行业务模拟,统计一天内客户在银行逗留的平均时间 + */ +void Bank_Simulation_1() +{ + char eventType; + + OpenForDay(); // 银行开门 + + // 如果事件表不为空 + while(MoreEvent()) { + EventDrived(&eventType); // 事件驱动 + + switch(eventType) { + case 'A': + CustomerArrived(); // 处理客户到达事件 + break; + case 'D': + CustomerDeparture(); // 处理客户离开事件 + break; + default : + Invalid(); + } + } + + CloseForDay(); +} + +/* + * ████████ 算法3.7 ████████ + * + * 银行业务模拟,统计一天内客户在银行逗留的平均时间 + * 算法内容与3.7是类似的 + */ +void Bank_Simulation_2() { + OpenForDay(); // 银行开门 + + while(!ListEmpty(gEv)) { + ListDelete(gEv, 1, &gEn); + + if(gEn.NType == Arrive) { + CustomerArrived(); // 处理客户到达事件 + } else { + CustomerDeparture(); // 处理客户离开事件 + } + } + + CloseForDay(); // 银行关门 +} + +/* + * 银行开门,初始化运行环境 + */ +void OpenForDay() { + int i; + + // 关门时间,假设银行每天营业8小时,480分 + gCloseTime = 480; + + // 初始化累计时间和客户数为0 + gTotalTime = 0; + gCustomerNum = 0; + + // 初始化事件链表为空表 + InitList(&gEv); + + // 设定第一个客户到达事件 + gEn.OccurTime = 0; + gEn.NType = Arrive; + + // 插入事件表 + OrderInsert(gEv, gEn, cmp); + + // 初始化4个空队列 + for(i = 1; i <= N; ++i) { + InitQueue(&gQ[i]); + } + + Show(); +} + +/* + * 银行关门 + * + * 释放资源,打印统计信息 + */ +void CloseForDay() { + printf("当天总共有%d个客户,平均逗留时间为%d分钟。\n", gCustomerNum, gTotalTime / gCustomerNum); +} + +/* + * 判断事件表是否为空。 + * 即是否存在未处理的事件。 + */ +Status MoreEvent() { + return !ListEmpty(gEv); +} + +/* + * 将待处理事件从事件表中移除,并将该事件存储到全局变量gEn中。 + * event用来存储该事件的类型 + */ +void EventDrived(char* eventType) { + // 从事件表中获取待处理事件 + ListDelete(gEv, 1, &gEn); + + // 识别事件类型 + if(gEn.NType == Arrive) { + *eventType = 'A'; + } else { + *eventType = 'D'; + } +} + +/* + * 处理客户到达事件,gEn.NType=0 + */ +void CustomerArrived() { + Event en; // 事件 + QElemType customer; // 客户记录 + + int durtime; // 当前客户办理业务需要的时间 + + int intertime; // 下一个客户达到时间间隔 + int t; // 下一个客户到达时间 + + int i; // 队列编号 + + // 总客户数增一 + ++gCustomerNum; + + // 生成当前客户办理业务需要的时间和下一个客户达到时间间隔 + Random(&durtime, &intertime); + + // 下一个客户到达时间 + t = gEn.OccurTime + intertime; + + // 如果银行尚未关门,将下一客户"到达"事件插入事件表 + if(t < gCloseTime) { + en.OccurTime = t; // 下一客户的到达时间 + en.NType = Arrive; // "到达"事件类型 + OrderInsert(gEv, en, cmp); // "到达"事件插入事件表 + } + + // 获取当前长度最短的队列编号 + i = Minimum(gQ); + + // 记录当前客户信息 + customer.ArrivedTime = gEn.OccurTime; // 到达时间 + customer.Duration = durtime; // 办理业务所需时间 + customer.Count = gCustomerNum; // 客户编号 + + // 当前客户进入最短队列排队 + EnQueue(&gQ[i], customer); + printf("第%3d个客户到队列 %d 中排队...\n", customer.Count, i); + Show(); + + /* + * 如果当前队列中只有这一个客户排队,则需要计算其离开时间, + * 并构造一个"离开"事件插入到事件表中 + */ + if(QueueLength(gQ[i]) == 1) { + en.OccurTime = gEn.OccurTime + durtime; // 当前客户的离开时间 + en.NType = i; // "离开"事件类型,值为1-4,指示从第几个队列中离开 + OrderInsert(gEv, en, cmp); // "离开"事件插入事件表 + } +} + +/* + * 处理客户离开事件,gEn.NType>0 + */ +void CustomerDeparture() { + Event en; // 事件 + QElemType customer; // 客户记录 + int i = gEn.NType; // 队列编号 + + // 第i个队列的队头客户完成业务并出队 + DeQueue(&gQ[i], &customer); + printf("第%3d个客户从队列 %d 中离开...\n", customer.Count, i); + Show(); + + // 累计客户逗留时间 + gTotalTime += gEn.OccurTime - customer.ArrivedTime; + + /* + * 如果当前队列中仍然存在排队的客户,则需要计算该队列中队头客户的离开时间 + * 注:之所以在这里计算,是因为只有上一个客户离开了,下一个客户的离开时间才会有意义 + */ + if(!QueueEmpty(gQ[i])) { + // 获取队头客户 + GetHead(gQ[i], &customer); + en.OccurTime = gEn.OccurTime + customer.Duration; // "离开"事件发生的时间 + en.NType = i; // "离开"事件类型 + OrderInsert(gEv, en, cmp); // "离开"事件插入事件表 + } +} + +/* + * 代表遇到了无效的事件 + */ +void Invalid() { + printf("运行错误!"); + exit(OVERFLOW); +} + +/* + * 将事件en插入到事件表ev中(ev是按发生时间从早到晚排列的事件表) + * cmp用来比较两个事件发生的早晚,en会作为第二个实参传进去 + */ +Status OrderInsert(EventList ev, Event en, int(cmp)(Event, Event)) { + EventList p, pre, s; + + if(ev == NULL) { + return ERROR; + } + + for(pre = ev; pre->next != NULL && cmp(pre->next->data, en) < 0; pre = pre->next) { + // 查找 + } + + s = (LinkList) malloc(sizeof(LNode)); + if(s == NULL) { + exit(OVERFLOW); + } + s->data = en; + + s->next = pre->next; + pre->next = s; + + return OK; +} + +/* + * 比较两事件发生次序 + */ +int cmp(Event a, Event b) { + if(a.OccurTime < b.OccurTime) { + return -1; // a发生比较早 + } else if(a.OccurTime > b.OccurTime) { + return 1; // a发生比较晚 + } else { + return 0; // 同时发生 + } +} + +/* + * 生成随机数 + * + * durtime :当前客服办理业务所需时间 + * intertime:下一客户到达间隔的时间 + */ +void Random(int* durtime, int* intertime) { + srand((unsigned) time(NULL)); + *durtime = rand() % DurationTime + 1; // 办业务时间持续1到20分钟 + *intertime = rand() % IntervalTime + 1; // 下一个顾客到来的时间为间隔1到10分钟 +} + +/* + * 返回长度最短的队列的序号 + */ +int Minimum() { + int i1 = QueueLength(gQ[1]); + int i2 = QueueLength(gQ[2]); + int i3 = QueueLength(gQ[3]); + int i4 = QueueLength(gQ[4]); + + if(i1 <= i2 && i1 <= i3 && i1 <= i4) { + return 1; + } + + if(i2 < i1 && i2 <= i3 && i2 <= i4) { + return 2; + } + + if(i3 < i1 && i3 < i2 && i3 <= i4) { + return 3; + } + + if(i4 < i1 && i4 < i2 && i4 < i3) { + return 4; + } + + return 0; +} + +/* + * 显示所有客户队列的排队情况 + */ +void Show() { + int i; + QueuePtr p; // 记录到来的客户是第几个 + + // 遍历所有客户队列 + for(i = 1; i <= N; i++) { + for(p = gQ[i].front; p; p = p->next) { + if(p == gQ[i].front) { + if(i == 1) { + printf("柜台①●"); + } + if(i == 2) { + printf("柜台②●"); + } + if(i == 3) { + printf("柜台③●"); + } + if(i == 4) { + printf("柜台④●"); + } + } else { + printf("(%03d)", p->data.Count); + } + + + if(p == gQ[i].rear) { + printf("\n"); + } + } + } + + printf("\n"); + + Wait(SleepTime); +} + +#endif diff --git a/CLion/CourseBook/0309_BankQueuing/BankQueuing.h b/CLion/CourseBook/0309_BankQueuing/BankQueuing.h new file mode 100644 index 0000000..18c4bab --- /dev/null +++ b/CLion/CourseBook/0309_BankQueuing/BankQueuing.h @@ -0,0 +1,121 @@ +/*================== + * 模拟银行排队 + * + * 包含算法: 3.6、3.7 + ===================*/ + +#ifndef BANKQUEUING_H +#define BANKQUEUING_H + +#include +#include // 提供malloc、realloc、free、exit原型 +#include // 提供time原型 +#include "Status.h" //**▲01 绪论**// +#include "LinkList.h" //**▲02 线性表**// +#include "LinkQueue.h" //**▲03 栈和队列**// + +/* 宏定义 */ +#define N 4 // 客户队列数量 +#define SleepTime 1 // SleepTime代表休眠时间 +#define DurationTime 20 // 办理业务持续时间从1到DurationTime分钟不等 +#define IntervalTime 8 // 下一个客户到来时间间隔为1到IntervalTime分钟不等 + +/* 类型定义 */ +typedef LinkList EventList; //事件链表类型,定义为有序链表 + +/* 全局变量(变量名称前面都加了g标记) */ +int gTotalTime; // 累计客户数 +int gCustomerNum; // 累计客户逗留时间 + +int gCloseTime; // 关门时间,假设银行每天营业8小时,480分 + +EventList gEv; // 事件表,存储所有待处理事件 +Event gEn; // 当前正在处理的事件 + +LinkQueue gQ[N+1]; // 4个客户队列,0号单元弃用 + + +/* + * ████████ 算法3.6 ████████ + * + * 银行业务模拟,统计一天内客户在银行逗留的平均时间 + */ +void Bank_Simulation_1(); + +/* + * ████████ 算法3.7 ████████ + * + * 银行业务模拟,统计一天内客户在银行逗留的平均时间 + * 算法内容与3.7是类似的 + */ +void Bank_Simulation_2(); + +/* + * 银行开门,初始化运行环境 + */ +void OpenForDay(); + +/* + * 银行关门 + * + * 释放资源,打印统计信息 + */ +void CloseForDay(); + +/* + * 判断事件表是否为空。 + * 即是否存在未处理的事件。 + */ +Status MoreEvent(); + +/* + * 将待处理事件从事件表中移除,并将该事件存储到全局变量gEn中。 + * event用来存储该事件的类型 + */ +void EventDrived(char* event); + +/* + * 处理客户到达事件,gEn.NType=0 + */ +void CustomerArrived(); + +/* + * 处理客户离开事件,gEn.NType>0 + */ +void CustomerDeparture(); + +/* + * 代表遇到了无效的事件 + */ +void Invalid(); + +/* + * 将事件en插入到事件表ev中(ev是按发生时间从早到晚排列的事件表) + * cmp用来比较两个事件发生的早晚,en会作为第二个实参传进去 + */ +Status OrderInsert(EventList gEv, Event gEn, int(cmp)(Event, Event)); + +/* + * 比较两事件发生次序 + */ +int cmp(Event a, Event b); + +/* + * 生成随机数 + * + * durtime :当前客服办理业务所需时间 + * intertime:下一客户到达间隔的时间 + */ +void Random(int* durtime, int* intertime); + +/* + * 返回长度最短的队列的序号 + */ +int Minimum(); + +/* + * 显示所有客户队列的排队情况 + */ +void Show(); + +#endif diff --git a/CLion/CourseBook/0309_BankQueuing/CMakeLists.txt b/CLion/CourseBook/0309_BankQueuing/CMakeLists.txt new file mode 100644 index 0000000..b73815e --- /dev/null +++ b/CLion/CourseBook/0309_BankQueuing/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(BankQueuing LinkList.h LinkList.c LinkQueue.h LinkQueue.c BankQueuing.h BankQueuing.c BankQueuing-main.c) +# 链接公共库 +target_link_libraries(BankQueuing Scanf_lib) \ No newline at end of file diff --git a/CLion/CourseBook/0309_BankQueuing/LinkList.c b/CLion/CourseBook/0309_BankQueuing/LinkList.c new file mode 100644 index 0000000..ec4c4f6 --- /dev/null +++ b/CLion/CourseBook/0309_BankQueuing/LinkList.c @@ -0,0 +1,130 @@ +/*=============================== + * 线性表的链式存储结构(链表) + * + * 包含算法: 2.8、2.9、2.10、2.11 + ================================*/ + +#include "LinkList.h" //**▲02 线性表**// + +/* + * 初始化 + * + * 只是初始化一个头结点。 + * 初始化成功则返回OK,否则返回ERROR。 + */ +Status InitList(LinkList* L) { + (*L) = (LinkList) malloc(sizeof(LNode)); + if(*L == NULL) { + exit(OVERFLOW); + } + + (*L)->next = NULL; + + return OK; +} + +/* + * 判空 + * + * 判断链表中是否包含有效数据。 + * + * 返回值: + * TRUE : 链表为空 + * FALSE: 链表不为空 + */ +Status ListEmpty(LinkList L) { + // 链表只有头结点时,认为该链表为空 + if(L != NULL && L->next == NULL) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * ████████ 算法2.9 ████████ + * + * 插入 + * + * 向链表第i个位置上插入e,插入成功则返回OK,否则返回ERROR。 + * + *【备注】 + * 教材中i的含义是元素位置,从1开始计数 + */ +Status ListInsert(LinkList L, int i, ElemType e) { + LinkList p, s; + int j; + + // 确保链表存 + if(L == NULL) { + return ERROR; + } + + p = L; + j = 0; + + // 寻找第i-1个结点,且保证该结点本身不为NULL + while(p != NULL && j < i - 1) { + p = p->next; + ++j; + } + + // 如果遍历到头了,或者i的值不合规(比如i<=0),说明没找到合乎目标的结点 + if(p == NULL || j > i - 1) { + return ERROR; + } + + // 生成新结点 + s = (LinkList) malloc(sizeof(LNode)); + if(s == NULL) { + exit(OVERFLOW); + } + s->data = e; + s->next = p->next; + p->next = s; + + return OK; +} + +/* + * ████████ 算法2.10 ████████ + * + * 删除 + * + * 删除链表第i个位置上的元素,并将被删除元素存储到e中。 + * 删除成功则返回OK,否则返回ERROR。 + * + *【备注】 + * 教材中i的含义是元素位置,从1开始计数 + */ +Status ListDelete(LinkList L, int i, ElemType* e) { + LinkList p, q; + int j; + + // 确保链表存在且不为空表 + if(L == NULL || L->next == NULL) { + return ERROR; + } + + p = L; + j = 0; + + // 寻找第i-1个结点,且保证该结点的后继不为NULL + while(p->next != NULL && j < i - 1) { + p = p->next; + ++j; + } + + // 如果遍历到头了,或者i的值不合规(比如i<=0),说明没找到合乎目标的结点 + if(p->next == NULL || j > i - 1) { + return ERROR; + } + + // 删除第i个结点 + q = p->next; + p->next = q->next; + *e = q->data; + free(q); + + return OK; +} diff --git a/CLion/CourseBook/0309_BankQueuing/LinkList.h b/CLion/CourseBook/0309_BankQueuing/LinkList.h new file mode 100644 index 0000000..2019135 --- /dev/null +++ b/CLion/CourseBook/0309_BankQueuing/LinkList.h @@ -0,0 +1,83 @@ +/*=============================== + * 线性表的链式存储结构(链表) + * + * 包含算法: 2.8、2.9、2.10、2.11 + ================================*/ + +#ifndef LINKLIST_H +#define LINKLIST_H + +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +// 事件类型枚举常量,0代表到达事件,1至4表示四个窗口的离开事件 +typedef enum { + Arrive, Leave_1, Leave_2, Leave_3, Leave_4 +} EventType; + +/* 事件链表元素类型定义 */ +typedef struct +{ + int OccurTime; // 事件发生时刻 + EventType NType; // 事件类型 +} Event, ElemType; // 事件链表元素 + +/* + * 单链表结构 + * + * 注:这里的单链表存在头结点 + */ +typedef struct LNode { + ElemType data; // 数据结点 + struct LNode* next; // 指向下一个结点的指针 +} LNode; + +// 指向单链表结点的指针 +typedef LNode* LinkList; + + +/* + * 初始化 + * + * 初始化成功则返回OK,否则返回ERROR。 + */ +Status InitList(LinkList* L); + +/* + * 判空 + * + * 判断链表中是否包含有效数据。 + * + * 返回值: + * TRUE : 链表为空 + * FALSE: 链表不为空 + */ +Status ListEmpty(LinkList L); + +/* + * ████████ 算法2.9 ████████ + * + * 插入 + * + * 向链表第i个位置上插入e,插入成功则返回OK,否则返回ERROR。 + * + *【备注】 + * 教材中i的含义是元素位置,从1开始计数 + */ +Status ListInsert(LinkList L, int i, ElemType e); + +/* + * ████████ 算法2.10 ████████ + * + * 删除 + * + * 删除链表第i个位置上的元素,并将被删除元素存储到e中。 + * 删除成功则返回OK,否则返回ERROR。 + * + *【备注】 + * 教材中i的含义是元素位置,从1开始计数 + */ +Status ListDelete(LinkList L, int i, ElemType* e); + +#endif diff --git a/CLion/CourseBook/0309_BankQueuing/LinkQueue.c b/CLion/CourseBook/0309_BankQueuing/LinkQueue.c new file mode 100644 index 0000000..1b526de --- /dev/null +++ b/CLion/CourseBook/0309_BankQueuing/LinkQueue.c @@ -0,0 +1,138 @@ +/*========================= + * 队列的链式存储结构(链队) + ==========================*/ + +#ifndef LINKQUEUE_C +#define LINKQUEUE_C + +#include "LinkQueue.h" //**▲03 栈和队列**// + +/* + * 初始化 + * + * 构造一个空的链队。 + * 初始化成功则返回OK,否则返回ERROR。 + * + *【注】 + * 这里的队列带有头结点 + */ +Status InitQueue(LinkQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).front = (*Q).rear = (QueuePtr) malloc(sizeof(QNode)); + if(!(*Q).front) { + exit(OVERFLOW); + } + + (*Q).front->next = NULL; + + return OK; +} + +/* + * 判空 + * + * 判断链队中是否包含有效数据。 + * + * 返回值: + * TRUE : 链队为空 + * FALSE: 链队不为空 + */ +Status QueueEmpty(LinkQueue Q) { + if(Q.front == Q.rear) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * 计数 + * + * 返回链队包含的有效元素的数量。 + */ +int QueueLength(LinkQueue Q) { + int count = 0; + QueuePtr p = Q.front; + + while(p != Q.rear) { + count++; + p = p->next; + } + + return count; +} + +/* + * 取值 + * + * 获取队头元素,将其存储到e中。 + * 如果可以找到,返回OK,否则,返回ERROR。 + */ +Status GetHead(LinkQueue Q, QElemType* e) { + QueuePtr p; + + if(Q.front == NULL || Q.front == Q.rear) { + return ERROR; + } + + p = Q.front->next; + *e = p->data; + + return OK; +} + +/* + * 入队 + * + * 将元素e添加到队列尾部。 + */ +Status EnQueue(LinkQueue* Q, QElemType e) { + QueuePtr p; + + if(Q == NULL || (*Q).front == NULL) { + return ERROR; + } + + p = (QueuePtr) malloc(sizeof(QNode)); + if(!p) { + exit(OVERFLOW); + } + + p->data = e; + p->next = NULL; + + (*Q).rear->next = p; + (*Q).rear = p; + + return OK; +} + +/* + * 出队 + * + * 移除队列头部的元素,将其存储到e中。 + */ +Status DeQueue(LinkQueue* Q, QElemType* e) { + QueuePtr p; + + if(Q == NULL || (*Q).front == NULL || (*Q).front == (*Q).rear) { + return ERROR; + } + + p = (*Q).front->next; + *e = p->data; + + (*Q).front->next = p->next; + if((*Q).rear == p) { + (*Q).rear = (*Q).front; + } + + free(p); + + return OK; +} + +#endif diff --git a/CLion/CourseBook/0309_BankQueuing/LinkQueue.h b/CLion/CourseBook/0309_BankQueuing/LinkQueue.h new file mode 100644 index 0000000..c69dc47 --- /dev/null +++ b/CLion/CourseBook/0309_BankQueuing/LinkQueue.h @@ -0,0 +1,83 @@ +/*========================= + * 队列的链式存储结构(链队) + ==========================*/ + +#ifndef LINKQUEUE_H +#define LINKQUEUE_H + +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +/* 链队元素类型定义,这里记录客户信息 */ +typedef struct { + int ArrivedTime; // 客户到达时间 + int Duration; // 办理业务所需的时间 + int Count; // 此变量记录来到每个队列的客户是第几个【教材中无此变量,增加此变量的目的是观察排队状况。】 +} QElemType; //队列的数据元素类型 + +// 队列元素结构 +typedef struct QNode { + QElemType data; + struct QNode* next; +} QNode, * QueuePtr; + +// 队列结构 +typedef struct { + QueuePtr front; // 队头指针 + QueuePtr rear; // 队尾指针 +} LinkQueue; // 队列的链式存储表示 + + +/* + * 初始化 + * + * 构造一个空的链队。 + * 初始化成功则返回OK,否则返回ERROR。 + * + *【注】 + * 这里的队列带有头结点 + */ +Status InitQueue(LinkQueue* Q); + +/* + * 判空 + * + * 判断链队中是否包含有效数据。 + * + * 返回值: + * TRUE : 链队为空 + * FALSE: 链队不为空 + */ +Status QueueEmpty(LinkQueue Q); + +/* + * 计数 + * + * 返回链队包含的有效元素的数量。 + */ +int QueueLength(LinkQueue Q); + +/* + * 取值 + * + * 获取队头元素,将其存储到e中。 + * 如果可以找到,返回OK,否则,返回ERROR。 + */ +Status GetHead(LinkQueue Q, QElemType* e); + +/* + * 入队 + * + * 将元素e添加到队列尾部。 + */ +Status EnQueue(LinkQueue* Q, QElemType e); + +/* + * 出队 + * + * 移除队列头部的元素,将其存储到e中。 + */ +Status DeQueue(LinkQueue* Q, QElemType* e); + +#endif diff --git a/CLion/CourseBook/CMakeLists.txt b/CLion/CourseBook/CMakeLists.txt index b08f9c3..37ea433 100644 --- a/CLion/CourseBook/CMakeLists.txt +++ b/CLion/CourseBook/CMakeLists.txt @@ -9,3 +9,13 @@ add_subdirectory(0208_DuLinkList) add_subdirectory(0209_ELinkList) add_subdirectory(0210_MergeEList) add_subdirectory(0211_Polynomial) + +add_subdirectory(0301_SqStack) +add_subdirectory(0302_Conversion) +add_subdirectory(0303_LineEdit) +add_subdirectory(0304_Maze) +add_subdirectory(0305_Expression) +add_subdirectory(0306_Hanoi) +add_subdirectory(0307_LinkQueue) +add_subdirectory(0308_SqQueue) +add_subdirectory(0309_BankQueuing) diff --git a/Dev-C++/CourseBook/0301_SqStack/SqStack-main.cpp b/Dev-C++/CourseBook/0301_SqStack/SqStack-main.cpp new file mode 100644 index 0000000..ae7df91 --- /dev/null +++ b/Dev-C++/CourseBook/0301_SqStack/SqStack-main.cpp @@ -0,0 +1,94 @@ +#include +#include "SqStack.h" //**03 ջͶ**// + +// ԺӡԪ +void PrintElem(SElemType e); + +int main(int argc, char** argv) { + SqStack S; + int i; + SElemType e; + + printf(" InitStack \n"); + { + printf(" ʼ˳ջ S ...\n"); + InitStack(&S); + } + PressEnterToContinue(); + + printf(" StackEmpty \n"); + { + StackEmpty(S) ? printf(" S Ϊգ\n") : printf(" S Ϊգ\n"); + } + PressEnterToContinue(); + + printf(" Push \n"); + { + for(i = 1; i <= 6; i++) { + Push(&S, 2 * i); + printf(" \"%2d\" ѹջ S ...\n", 2 * i); + } + } + PressEnterToContinue(); + + printf(" StackTraverse \n"); + { + printf(" S еԪΪS = "); + StackTraverse(S, PrintElem); + } + PressEnterToContinue(); + + printf(" StackLength \n"); + { + i = StackLength(S); + printf(" S ijΪ %d \n", i); + } + PressEnterToContinue(); + + printf(" Pop \n"); + { + Pop(&S, &e); + printf(" ջԪ \"%d\" ջ...\n", e); + printf(" S еԪΪS = "); + StackTraverse(S, PrintElem); + } + PressEnterToContinue(); + + printf(" GetTop \n"); + { + GetTop(S, &e); + printf(" ջԪصֵΪ \"%d\" \n", e); + } + PressEnterToContinue(); + + printf(" ClearStack \n"); + { + printf(" S ǰ"); + StackEmpty(S) ? printf(" S Ϊգ\n") : printf(" S Ϊգ\n"); + + ClearStack(&S); + + printf(" S "); + StackEmpty(S) ? printf(" S Ϊգ\n") : printf(" S Ϊգ\n"); + } + PressEnterToContinue(); + + printf(" DestroyStack \n"); + { + printf(" S ǰ"); + S.base != NULL && S.top != NULL ? printf(" S ڣ\n") : printf(" S ڣ\n"); + + DestroyStack(&S); + + printf(" S "); + S.base != NULL && S.top != NULL ? printf(" S ڣ\n") : printf(" S ڣ\n"); + } + PressEnterToContinue(); + + return 0; +} + +// ԺӡԪ +void PrintElem(SElemType e) { + printf("%d ", e); +} diff --git a/Dev-C++/CourseBook/0301_SqStack/SqStack.cpp b/Dev-C++/CourseBook/0301_SqStack/SqStack.cpp new file mode 100644 index 0000000..6ae0e8b --- /dev/null +++ b/Dev-C++/CourseBook/0301_SqStack/SqStack.cpp @@ -0,0 +1,174 @@ +/*========================= + * ջ˳洢ṹ˳ջ + ==========================*/ + +#include "SqStack.h" //**03 ջͶ**// + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + (*S).base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); + } + + (*S).top = (*S).base; + (*S).stacksize = STACK_INIT_SIZE; + + return OK; +} + +/* + * (ṹ) + * + * ͷ˳ջռڴ档 + */ +Status DestroyStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + free((*S).base); + + (*S).base = NULL; + (*S).top = NULL; + (*S).stacksize = 0; + + return OK; +} + +/* + * ÿ() + * + * ֻ˳ջд洢ݣͷ˳ջռڴ档 + */ +Status ClearStack(SqStack* S) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + (*S).top = (*S).base; + + return OK; +} + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S) { + if(S.top == S.base) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * + * + * ˳ջЧԪص + */ +int StackLength(SqStack S) { + if(S.base == NULL) { + return 0; + } + + return (int) (S.top - S.base); +} + +/* + * ȡֵ + * + * ջԪأeա + */ +Status GetTop(SqStack S, SElemType* e) { + if(S.base == NULL || S.top == S.base) { + return 0; + } + + // ıջԪ + *e = *(S.top - 1); + + return OK; +} + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + // ջʱ׷Ӵ洢ռ + if((*S).top - (*S).base >= (*S).stacksize) { + (*S).base = (SElemType*) realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); // 洢ʧ + } + + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + + // ջȸֵջָ + *(S->top++) = e; + + return OK; +} + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + if((*S).top == (*S).base) { + return ERROR; + } + + // ջջָȵݼٸֵ + *e = *(--(*S).top); + + return OK; +} + +/* + * + * + * visit˳ջS + */ +Status StackTraverse(SqStack S, void(Visit)(SElemType)) { + SElemType* p = S.base; + + if(S.base == NULL) { + return ERROR; + } + + while(p < S.top) { + Visit(*p++); + } + + printf("\n"); + + return OK; +} diff --git a/Dev-C++/CourseBook/0301_SqStack/SqStack.dev b/Dev-C++/CourseBook/0301_SqStack/SqStack.dev new file mode 100644 index 0000000..76b38c8 --- /dev/null +++ b/Dev-C++/CourseBook/0301_SqStack/SqStack.dev @@ -0,0 +1,82 @@ +[Project] +FileName=SqStack.dev +Name=SqStack +Type=1 +Ver=2 +ObjFiles= +Includes= +Libs= +PrivateResource= +ResourceIncludes= +MakeIncludes= +Compiler= +CppCompiler= +Linker= +IsCpp=0 +Icon= +ExeOutput= +ObjectOutput= +LogOutput= +LogOutputEnabled=0 +OverrideOutput=0 +OverrideOutputName= +HostApplication= +UseCustomMakefile=0 +CustomMakefile= +CommandLine= +Folders= +IncludeVersionInfo=0 +SupportXPThemes=0 +CompilerSet=1 +CompilerSettings=0000000000000000001000000 +UnitCount=3 + +[VersionInfo] +Major=1 +Minor=0 +Release=0 +Build=0 +LanguageID=1033 +CharsetID=1252 +CompanyName= +FileVersion= +FileDescription=Developed using the Dev-C++ IDE +InternalName= +LegalCopyright= +LegalTrademarks= +OriginalFilename= +ProductName= +ProductVersion= +AutoIncBuildNr=0 +SyncProduct=1 + +[Unit1] +FileName=SqStack.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit2] +FileName=SqStack.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit3] +FileName=SqStack-main.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/CourseBook/0301_SqStack/SqStack.h b/Dev-C++/CourseBook/0301_SqStack/SqStack.h new file mode 100644 index 0000000..3a0d1a8 --- /dev/null +++ b/Dev-C++/CourseBook/0301_SqStack/SqStack.h @@ -0,0 +1,94 @@ +/*========================= + * ջ˳洢ṹ˳ջ + ==========================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define STACK_INIT_SIZE 100 // ˳ջ洢ռijʼ +#define STACKINCREMENT 10 // ˳ջ洢ռķ + +/* ˳ջԪͶ */ +typedef int SElemType; + +// ˳ջԪؽṹ +typedef struct { + SElemType* base; // ջָ + SElemType* top; // ջָ + int stacksize; // ǰѷĴ洢ռ䣬ԪΪλ +} SqStack; + + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S); + +/* + * (ṹ) + * + * ͷ˳ջռڴ档 + */ +Status DestroyStack(SqStack* S); + +/* + * ÿ() + * + * ֻ˳ջд洢ݣͷ˳ջռڴ档 + */ +Status ClearStack(SqStack* S); + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S); + +/* + * + * + * ˳ջЧԪص + */ +int StackLength(SqStack S); + +/* + * ȡֵ + * + * ջԪأeա + */ +Status GetTop(SqStack S, SElemType* e); + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e); + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e); + +/* + * + * + * visit˳ջS + */ +Status StackTraverse(SqStack S, void(Visit)(SElemType)); + +#endif diff --git a/Dev-C++/CourseBook/0302_Conversion/Conversion-main.cpp b/Dev-C++/CourseBook/0302_Conversion/Conversion-main.cpp new file mode 100644 index 0000000..2d99ac5 --- /dev/null +++ b/Dev-C++/CourseBook/0302_Conversion/Conversion-main.cpp @@ -0,0 +1,11 @@ +#include "Conversion.h" //**03 ջͶ**// + +int main(int argc, char** argv) { + int i = 342391; + + printf("ʮתΪ˽...\n"); + + conversion(i); + + return 0; +} diff --git a/Dev-C++/CourseBook/0302_Conversion/Conversion.cpp b/Dev-C++/CourseBook/0302_Conversion/Conversion.cpp new file mode 100644 index 0000000..4df0cfa --- /dev/null +++ b/Dev-C++/CourseBook/0302_Conversion/Conversion.cpp @@ -0,0 +1,37 @@ +/*============== + * ת + * + * 㷨: 3.1 + ===============*/ + +#include "Conversion.h" //**03 ջͶ**// + +/* + * 㷨3.1 + * + * תָķǸʮתΪ˽ƺ + * + *ע + * ̲ʹõǿ̨룬Ϊ˱ڲԣֱӸΪβνղ + */ +void conversion(int i) { + SqStack S; + SElemType e; + + InitStack(&S); + + // ˽ǰ0 + printf("ʮ %d תΪ˽Ϊ0", i); + + while(i!=0) { + Push(&S, i % 8); // ջʱӵλλ + i = i / 8; + } + + while(StackEmpty(S)==FALSE) { + Pop(&S, &e); // ջʱӸλλ + printf("%d", e); + } + + printf("\n"); +} diff --git a/Dev-C++/CourseBook/0302_Conversion/Conversion.dev b/Dev-C++/CourseBook/0302_Conversion/Conversion.dev new file mode 100644 index 0000000..99e5a05 --- /dev/null +++ b/Dev-C++/CourseBook/0302_Conversion/Conversion.dev @@ -0,0 +1,102 @@ +[Project] +FileName=Conversion.dev +Name=Conversion +Type=1 +Ver=2 +ObjFiles= +Includes= +Libs= +PrivateResource= +ResourceIncludes= +MakeIncludes= +Compiler= +CppCompiler= +Linker= +IsCpp=0 +Icon= +ExeOutput= +ObjectOutput= +LogOutput= +LogOutputEnabled=0 +OverrideOutput=0 +OverrideOutputName= +HostApplication= +UseCustomMakefile=0 +CustomMakefile= +CommandLine= +Folders= +IncludeVersionInfo=0 +SupportXPThemes=0 +CompilerSet=1 +CompilerSettings=0000000000000000001000000 +UnitCount=5 + +[VersionInfo] +Major=1 +Minor=0 +Release=0 +Build=0 +LanguageID=1033 +CharsetID=1252 +CompanyName= +FileVersion= +FileDescription=Developed using the Dev-C++ IDE +InternalName= +LegalCopyright= +LegalTrademarks= +OriginalFilename= +ProductName= +ProductVersion= +AutoIncBuildNr=0 +SyncProduct=1 + +[Unit3] +FileName=Conversion-main.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit1] +FileName=Conversion.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit4] +FileName=SqStack.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit2] +FileName=Conversion.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit5] +FileName=SqStack.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/CourseBook/0302_Conversion/Conversion.h b/Dev-C++/CourseBook/0302_Conversion/Conversion.h new file mode 100644 index 0000000..0371acf --- /dev/null +++ b/Dev-C++/CourseBook/0302_Conversion/Conversion.h @@ -0,0 +1,20 @@ +/*============== + * ת + * + * 㷨: 3.1 + ===============*/ + +#ifndef CONVERSION_H +#define CONVERSION_H + +#include +#include "SqStack.h" //**03 ջͶ**// + +/* + * 㷨3.1 + * + * תָķǸʮתΪ˽ƺ + */ +void conversion(int i); + +#endif diff --git a/Dev-C++/CourseBook/0302_Conversion/SqStack.cpp b/Dev-C++/CourseBook/0302_Conversion/SqStack.cpp new file mode 100644 index 0000000..7e16904 --- /dev/null +++ b/Dev-C++/CourseBook/0302_Conversion/SqStack.cpp @@ -0,0 +1,90 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#include "SqStack.h" //**03 ջͶ**// + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + (*S).base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); + } + + (*S).top = (*S).base; + (*S).stacksize = STACK_INIT_SIZE; + + return OK; +} + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S) { + if(S.top == S.base) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + // ջʱ׷Ӵ洢ռ + if((*S).top - (*S).base >= (*S).stacksize) { + (*S).base = (SElemType*) realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); // 洢ʧ + } + + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + + // ջȸֵջָ + *(S->top++) = e; + + return OK; +} + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + if((*S).top == (*S).base) { + return ERROR; + } + + // ջջָȵݼٸֵ + *e = *(--(*S).top); + + return OK; +} diff --git a/Dev-C++/CourseBook/0302_Conversion/SqStack.h b/Dev-C++/CourseBook/0302_Conversion/SqStack.h new file mode 100644 index 0000000..19e6cc1 --- /dev/null +++ b/Dev-C++/CourseBook/0302_Conversion/SqStack.h @@ -0,0 +1,59 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define STACK_INIT_SIZE 100 // ˳ջ洢ռijʼ +#define STACKINCREMENT 10 // ˳ջ洢ռķ + +/* ˳ջԪͶ */ +typedef int SElemType; + +// ˳ջԪؽṹ +typedef struct { + SElemType* base; // ջָ + SElemType* top; // ջָ + int stacksize; // ǰѷĴ洢ռ䣬ԪΪλ +} SqStack; + + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S); + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S); + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e); + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e); + +#endif diff --git a/Dev-C++/CourseBook/0303_LineEdit/LineEdit-main.cpp b/Dev-C++/CourseBook/0303_LineEdit/LineEdit-main.cpp new file mode 100644 index 0000000..56235b7 --- /dev/null +++ b/Dev-C++/CourseBook/0303_LineEdit/LineEdit-main.cpp @@ -0,0 +1,18 @@ +#include +#include "LineEdit.h" //**03 ջͶ**// + +int main(int argc, char* argv[]) { + char* buf = "whli##ilr#e(s#*s)\noutcha@ putchar(*s=#++);"; //Ҫ¼ + + printf("ΪʾûıΪ\n"); + printf("%s\n\n", buf); + + printf("б༭...\n\n"); + + printf("ţ'#' ɾһԪء'@' ɾǰ\n"); + printf(" '\\n''\\0'\n"); + printf("մ洢Ϊ\n"); + LineEdit(buf); + + return 0; +} diff --git a/Dev-C++/CourseBook/0303_LineEdit/LineEdit.cpp b/Dev-C++/CourseBook/0303_LineEdit/LineEdit.cpp new file mode 100644 index 0000000..cf00861 --- /dev/null +++ b/Dev-C++/CourseBook/0303_LineEdit/LineEdit.cpp @@ -0,0 +1,71 @@ +/*============== + * б༭ + * + * 㷨: 3.2 + ===============*/ + +#include "LineEdit.h" //**03 ջͶ**// + +/* + * 㷨3.2 + * + * б༭ģ༭ıʱ˸еIJ + * + *ע + * ̲ʹõǿ̨룬Ϊ˱ڲԣֱӸΪβνղ + */ +void LineEdit(const char buffer[]) { + SqStack S; //ַ + SElemType e; + int i; + char ch; + + // ʼջ + InitStack(&S); + + i = 0; + ch = buffer[i++]; + + // δıĩβ + while(ch != EOF) { + // δıĩβұδδУ + while(ch != EOF && ch != '\n') { + switch(ch) { + case '#': + Pop(&S, &e); // '#'ʾɾһַ + break; + case '@': + ClearStack(&S); // '@'ʾյǰ + break; + default : + Push(&S, ch); // Чַջ + } + + // ʶһַ + ch = buffer[i++]; + } + + // ֮ǰǰջݣ˴̲û + StackTraverse(S, Print); + + // ոеĻ + ClearStack(&S); + + // δıĩβ˵'\n'н + if(ch != EOF) { + // һ + ch = buffer[i++]; + } + } + + // ѾıĩβĿǰջеԪأ˴̲û + StackTraverse(S, Print); + + // ջ + DestroyStack(&S); +} + +// ԺӡԪ +void Print(SElemType e) { + printf("%c", e); +} diff --git a/Dev-C++/CourseBook/0303_LineEdit/LineEdit.dev b/Dev-C++/CourseBook/0303_LineEdit/LineEdit.dev new file mode 100644 index 0000000..4f419e3 --- /dev/null +++ b/Dev-C++/CourseBook/0303_LineEdit/LineEdit.dev @@ -0,0 +1,102 @@ +[Project] +FileName=LineEdit.dev +Name=LineEdit +Type=1 +Ver=2 +ObjFiles= +Includes= +Libs= +PrivateResource= +ResourceIncludes= +MakeIncludes= +Compiler= +CppCompiler= +Linker= +IsCpp=0 +Icon= +ExeOutput= +ObjectOutput= +LogOutput= +LogOutputEnabled=0 +OverrideOutput=0 +OverrideOutputName= +HostApplication= +UseCustomMakefile=0 +CustomMakefile= +CommandLine= +Folders= +IncludeVersionInfo=0 +SupportXPThemes=0 +CompilerSet=1 +CompilerSettings=0000000000000000001000000 +UnitCount=5 + +[VersionInfo] +Major=1 +Minor=0 +Release=0 +Build=0 +LanguageID=1033 +CharsetID=1252 +CompanyName= +FileVersion= +FileDescription=Developed using the Dev-C++ IDE +InternalName= +LegalCopyright= +LegalTrademarks= +OriginalFilename= +ProductName= +ProductVersion= +AutoIncBuildNr=0 +SyncProduct=1 + +[Unit3] +FileName=LineEdit-main.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit1] +FileName=LineEdit.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit4] +FileName=SqStack.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit2] +FileName=LineEdit.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit5] +FileName=SqStack.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/CourseBook/0303_LineEdit/LineEdit.h b/Dev-C++/CourseBook/0303_LineEdit/LineEdit.h new file mode 100644 index 0000000..528960b --- /dev/null +++ b/Dev-C++/CourseBook/0303_LineEdit/LineEdit.h @@ -0,0 +1,33 @@ +/*============== + * б༭ + * + * 㷨: 3.2 + ===============*/ + +#ifndef LINEEDIT_H +#define LINEEDIT_H + +#include +#include "SqStack.h" //**03 ջͶ**// +#include "LineEdit.h" + +// ģļеıǣҪеĶ +#ifdef EOF +#undef EOF +#define EOF '\0' +#endif + +/* + * 㷨3.2 + * + * б༭ģ༭ıʱ˸еIJ + * + *ע + * ̲ʹõǿ̨룬Ϊ˱ڲԣֱӸΪβνղ + */ +void LineEdit(const char buffer[]); + +// ԺӡԪ +void Print(SElemType e); + +#endif diff --git a/Dev-C++/CourseBook/0303_LineEdit/SqStack.cpp b/Dev-C++/CourseBook/0303_LineEdit/SqStack.cpp new file mode 100644 index 0000000..d089f26 --- /dev/null +++ b/Dev-C++/CourseBook/0303_LineEdit/SqStack.cpp @@ -0,0 +1,128 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#include "SqStack.h" //**03 ջͶ**// + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + (*S).base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); + } + + (*S).top = (*S).base; + (*S).stacksize = STACK_INIT_SIZE; + + return OK; +} + +/* + * (ṹ) + * + * ͷ˳ջռڴ档 + */ +Status DestroyStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + free((*S).base); + + (*S).base = NULL; + (*S).top = NULL; + (*S).stacksize = 0; + + return OK; +} + +/* + * ÿ() + * + * ֻ˳ջд洢ݣͷ˳ջռڴ档 + */ +Status ClearStack(SqStack* S) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + (*S).top = (*S).base; + + return OK; +} + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + // ջʱ׷Ӵ洢ռ + if((*S).top - (*S).base >= (*S).stacksize) { + (*S).base = (SElemType*) realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); // 洢ʧ + } + + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + + // ջȸֵջָ + *(S->top++) = e; + + return OK; +} + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + if((*S).top == (*S).base) { + return ERROR; + } + + // ջջָȵݼٸֵ + *e = *(--(*S).top); + + return OK; +} + +/* + * + * + * visit˳ջS + */ +Status StackTraverse(SqStack S, void(Visit)(SElemType)) { + SElemType* p = S.base; + + if(S.base == NULL) { + return ERROR; + } + + while(p < S.top) { + Visit(*p++); + } + + printf("\n"); + + return OK; +} diff --git a/Dev-C++/CourseBook/0303_LineEdit/SqStack.h b/Dev-C++/CourseBook/0303_LineEdit/SqStack.h new file mode 100644 index 0000000..7382c02 --- /dev/null +++ b/Dev-C++/CourseBook/0303_LineEdit/SqStack.h @@ -0,0 +1,69 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define STACK_INIT_SIZE 100 // ˳ջ洢ռijʼ +#define STACKINCREMENT 10 // ˳ջ洢ռķ + +/* ˳ջԪͶ */ +typedef int SElemType; + +// ˳ջԪؽṹ +typedef struct { + SElemType* base; // ջָ + SElemType* top; // ջָ + int stacksize; // ǰѷĴ洢ռ䣬ԪΪλ +} SqStack; + + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S); + +/* + * (ṹ) + * + * ͷ˳ջռڴ档 + */ +Status DestroyStack(SqStack* S); + +/* + * ÿ() + * + * ֻ˳ջд洢ݣͷ˳ջռڴ档 + */ +Status ClearStack(SqStack* S); + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e); + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e); + +/* + * + * + * visit˳ջS + */ +Status StackTraverse(SqStack S, void(Visit)(SElemType)); + +#endif diff --git a/Dev-C++/CourseBook/0304_Maze/Maze-main.cpp b/Dev-C++/CourseBook/0304_Maze/Maze-main.cpp new file mode 100644 index 0000000..1e2246a --- /dev/null +++ b/Dev-C++/CourseBook/0304_Maze/Maze-main.cpp @@ -0,0 +1,20 @@ +#include "Maze.h" //**03 ջͶ**// + +int main(int argc, char* argv[]) { + MazeType maze; + PosType start, end; + char n, Re = 'Y'; + + while(Re == 'Y' || Re == 'y') { + InitMaze(maze, &start, &end); // ʼԹ + + MazePath(maze, start, end); // ԹѰ· + + printf("ãY/N"); + scanf("%c%c", &Re, &n); + + printf("\n"); + } + + return 0; +} diff --git a/Dev-C++/CourseBook/0304_Maze/Maze.cpp b/Dev-C++/CourseBook/0304_Maze/Maze.cpp new file mode 100644 index 0000000..cb2dca1 --- /dev/null +++ b/Dev-C++/CourseBook/0304_Maze/Maze.cpp @@ -0,0 +1,294 @@ +/*============== + * ԹѰ· + * + * 㷨: 3.3 + ===============*/ + +#include "Maze.h" //**03 ջͶ**// + +/* + * 㷨3.3 + * + * ԹѰ· + * + * ʹٷҵһͨ· + */ +Status MazePath(MazeType maze, PosType start, PosType end) { + SqStack S; // 洢̽ͨ + SElemType e; // e洢ǰͨϢ + PosType curPos; // ǰλ + int curStep; // ǰͨ + + // ʼ켣ջ + InitStack(&S); + + curPos = start; // 趨ǰλΪ"λ" + curStep = 1; // ̽һ + + do { + // ǰλÿͨҪλǴδ̽ͨ飩 + if(Pass(maze, curPos)) { + // ³ʼ㼣򶫷ʵı + FootPrint(maze, curPos); + + // һͨϢ + e = Construct(curStep, curPos, East); + + // · + Push(&S, e); + + // յ + if(Equals(curPos, end) == TRUE) { + printf("\nѰ·ɹ\n\n"); + return TRUE; + } + + // ȡһӦ̽λãǰλõĶ + curPos = NextPos(curPos, East); + + // ̽һ + curStep++; + + // ǰλѾ̽ˣ޸̽ + } else { + // ջΪգ̽ıҪ + if(!StackEmpty(S)) { + // ˵һλ + Pop(&S, &e); + + // ̽λõ4̽Ҫ + while(e.di == North && !StackEmpty(S)) { + // "ͬ"ǣӸλó·ûͨ· + MarkPrint(maze, e.seat, Impasse); + + // + Pop(&S, &e); + } + + // ̽λûʣ̽ķ + if(e.di < North) { + // ı̽򣬰ķѯ + ++e.di; + + // Թ·ʱǣ۲Թ״̬̲ûиò裩 + MarkPrint(maze, e.seat, e.di); + + // ½λü뵽· + Push(&S, e); + + // ȡһӦ̽λ + curPos = NextPos(e.seat, e.di); + } + } + } + + // ջΪգζŻ̽ıҪ + } while(!StackEmpty(S)); + + printf("\nѰ·ʧܣ\n\n"); + + return FALSE; +} + +/* + * ʼһģΪNNԹ + * startendֱΪԹͳ + * + *ע + * ̲޴˲òDZڵ + */ +void InitMaze(MazeType maze, PosType* start, PosType* end) { + int i, j, tmp; + + srand((unsigned) time(NULL)); // ϵͳʱ + + for(i = 0; i < M; i++) { + for(j = 0; j < N; j++) { + + // Թǽ + if(i == 0 || j == 0 || i == M - 1 || j == N - 1) { + maze[i][j] = Wall; + + // Թڲ + } else { + tmp = rand() % X; // [0, X-1]Թ + + if(tmp == 0) { + // 1/Xĸϰ + maze[i][j] = Obstacle; + } else { + // طΪɱͨ· + maze[i][j] = Way; + } + } + } + } + + // Թ + (*start).x = 1; + (*start).y = 0; + + // Թ + (*end).x = M - 2; + (*end).y = N - 1; + + // ںͳ + maze[1][0] = maze[M - 2][N - 1] = Way; + + // ΪѰ·ɹʣڴͳڴٽĽΪͨ·DZ + maze[1][1] = maze[M - 2][N - 2] = Way; + + // ʾԹijʼ״̬ + PaintMaze(maze); +} + +/* + * жϵǰλǷͨҪλǴδ̽ͨ + * + *ע + * ΪжϵǰλǷΪ״̽ + */ +Status Pass(MazeType maze, PosType seat) { + int x = seat.x; + int y = seat.y; + + // ȼǷԽ磬Խˣǰλÿ϶޷ͨ + if(x < 0 || y < 0 || x > M - 1 || y > N - 1) { + return FALSE; //Խ + } + + // ҪλñǴδ̽ͨ + if(maze[x][y] != Way) { + return FALSE; + } + + return TRUE; +} + +/* + * ȡһӦ̽λ + * diָʾǰλõ̽򣬰East, South, West, North + */ +PosType NextPos(PosType seat, int di) { + PosType tmp = seat; + + switch(di) { + case East: + tmp.y++; // + break; + case South: + tmp.x++; // + break; + case West: + tmp.y--; // + break; + case North: + tmp.x--; // + break; + } + + return tmp; +} + +/* + * ³ʼ㼣 + * + * ʼ㼣򶫷 + */ +void FootPrint(MazeType maze, PosType seat) { + //ʼ̽ + MarkPrint(maze, seat, East); +} + +/* + * Թseatmark + * + *ע + * ú̲ϵĺ + * ֻ̲ô˺"̽"ı + * ˴ĺĽΪǣ̽ı + */ +void MarkPrint(MazeType maze, PosType seat, int mark) { + int x = seat.x; + int y = seat.y; + + maze[x][y] = mark; //²ͨı + + // Թ + PaintMaze(maze); +} + +/* + * һͨϢ + * + *ע + * ̲д˲޴˺ + */ +SElemType Construct(int ord, PosType seat, int di) { + SElemType e; + + e.ord = ord; + e.seat = seat; + e.di = di; + + return e; +} + +/* + * жǷ + * + *ע + * ̲д˲޴˺ + * ΪҪȽṹ壬Բֱ"==" + */ +Status Equals(PosType seat1, PosType seat2) { + if(seat1.x == seat2.x && seat1.y == seat2.y) { + return TRUE; + } else { + return ERROR; + } +} + +/* + * Թ + * ͼεķʽԹǰ״̬ + * + *ע + * 1.̲޴˲˴ӸòĿǹ۲Ѱ·̵ÿһ + * 2.ʵCLionĿ̨ + */ +void PaintMaze(MazeType maze) { + int i, j; + + Wait(SleepTime); // ͣһ + + system("cls"); // Ļ + + for(i = 0; i < M; i++) { + for(j = 0; j < N; j++) { + if(maze[i][j] == Wall) { // ǽ + printf("~"); + } else if(maze[i][j] == Obstacle) { // Թڲϰ + printf(""); + } else if(maze[i][j] == East) { // ڳ̽ + printf(""); + } else if(maze[i][j] == South) { // ڳ̽ + printf(""); + } else if(maze[i][j] == West) { // ڳ̽ + printf(""); + } else if(maze[i][j] == North) { // ڳ̽ + printf(""); + } else if(maze[i][j] == Impasse) { // ͬĸ̽޷ͨλ + printf(""); + } else { // δ̽· + printf(""); + } + + if(j != 0 && j % (N - 1) == 0) { // ÿN㻻 + printf("\n"); + } + } + } + + printf("\n"); +} diff --git a/Dev-C++/CourseBook/0304_Maze/Maze.dev b/Dev-C++/CourseBook/0304_Maze/Maze.dev new file mode 100644 index 0000000..dfad10c --- /dev/null +++ b/Dev-C++/CourseBook/0304_Maze/Maze.dev @@ -0,0 +1,102 @@ +[Project] +FileName=Maze.dev +Name=Maze +Type=1 +Ver=2 +ObjFiles= +Includes= +Libs= +PrivateResource= +ResourceIncludes= +MakeIncludes= +Compiler= +CppCompiler= +Linker= +IsCpp=0 +Icon= +ExeOutput= +ObjectOutput= +LogOutput= +LogOutputEnabled=0 +OverrideOutput=0 +OverrideOutputName= +HostApplication= +UseCustomMakefile=0 +CustomMakefile= +CommandLine= +Folders= +IncludeVersionInfo=0 +SupportXPThemes=0 +CompilerSet=1 +CompilerSettings=0000000000000000001000000 +UnitCount=5 + +[VersionInfo] +Major=1 +Minor=0 +Release=0 +Build=0 +LanguageID=1033 +CharsetID=1252 +CompanyName= +FileVersion= +FileDescription=Developed using the Dev-C++ IDE +InternalName= +LegalCopyright= +LegalTrademarks= +OriginalFilename= +ProductName= +ProductVersion= +AutoIncBuildNr=0 +SyncProduct=1 + +[Unit3] +FileName=Maze-main.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit1] +FileName=Maze.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit4] +FileName=SqStack.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit2] +FileName=Maze.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit5] +FileName=SqStack.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/CourseBook/0304_Maze/Maze.h b/Dev-C++/CourseBook/0304_Maze/Maze.h new file mode 100644 index 0000000..d18a67a --- /dev/null +++ b/Dev-C++/CourseBook/0304_Maze/Maze.h @@ -0,0 +1,112 @@ +/*============== + * ԹѰ· + * + * 㷨: 3.3 + ===============*/ + +#ifndef MAZE_H +#define MAZE_H + +#include +#include // ṩsystemrandsrandԭ +#include // ṩtimeԭ +#include "Status.h" //**01 **// +#include "SqStack.h" //**03 ջͶ**// + +/* 궨 */ +#define M 15 // Թ +#define N 15 // Թ + +#define X 4 // XָʾԹϰֵĸʡ磬X=4ζűԹʱϰĸ1/4=25% + +#define SleepTime 3 //SleepTimeӡͼʱʱ + +/* ԹͶ */ +typedef enum { + Wall, // ǽ + Obstacle, // Թڲϰ + Way, // ͨ· + Impasse, // ͬ + East, South, West, North // ǰ̽򣺶 +} MazeNode; + +typedef int MazeType[M][N]; // Թ + + +/* + * 㷨3.3 + * + * ԹѰ· + * + * ʹٷҵһͨ· + */ +Status MazePath(MazeType maze, PosType start, PosType end); + +/* + * ʼһģΪNNԹ + * startendֱΪԹͳ + * + *ע + * ̲޴˲òDZڵ + */ +void InitMaze(MazeType maze, PosType* start, PosType* end); + +/* + * жϵǰλǷͨҪλǴδ̽ͨ + * + *ע + * ΪжϵǰλǷΪ״̽ + */ +Status Pass(MazeType maze, PosType seat); + +/* + * ȡһӦ̽λ + * diָʾǰλõ̽򣬰East, South, West, North + */ +PosType NextPos(PosType seat, int di); + +/* + * ³ʼ㼣 + * + * ʼ㼣򶫷 + */ +void FootPrint(MazeType maze, PosType seat); + +/* + * Թseatmark + * + *ע + * ú̲ϵĺ + * ֻ̲ô˺"̽"ı + * ˴ĺĽΪǣ̽ı + */ +void MarkPrint(MazeType maze, PosType seat, int mark); + +/* + * һͨϢ + * + *ע + * ̲д˲޴˺ + */ +SElemType Construct(int ord, PosType seat, int di); + +/* + * жǷ + * + *ע + * ̲д˲޴˺ + * ΪҪȽṹ壬Բֱ"==" + */ +Status Equals(PosType a, PosType b); + +/* + * Թ + * ͼεķʽԹǰ״̬ + * + *ע + * ̲޴˲ + * ˴ӸòĿǹ۲Ѱ·̵ÿһ + */ +void PaintMaze(MazeType maze); + +#endif diff --git a/Dev-C++/CourseBook/0304_Maze/SqStack.cpp b/Dev-C++/CourseBook/0304_Maze/SqStack.cpp new file mode 100644 index 0000000..7e16904 --- /dev/null +++ b/Dev-C++/CourseBook/0304_Maze/SqStack.cpp @@ -0,0 +1,90 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#include "SqStack.h" //**03 ջͶ**// + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + (*S).base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); + } + + (*S).top = (*S).base; + (*S).stacksize = STACK_INIT_SIZE; + + return OK; +} + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S) { + if(S.top == S.base) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + // ջʱ׷Ӵ洢ռ + if((*S).top - (*S).base >= (*S).stacksize) { + (*S).base = (SElemType*) realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); // 洢ʧ + } + + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + + // ջȸֵջָ + *(S->top++) = e; + + return OK; +} + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + if((*S).top == (*S).base) { + return ERROR; + } + + // ջջָȵݼٸֵ + *e = *(--(*S).top); + + return OK; +} diff --git a/Dev-C++/CourseBook/0304_Maze/SqStack.h b/Dev-C++/CourseBook/0304_Maze/SqStack.h new file mode 100644 index 0000000..bbd553e --- /dev/null +++ b/Dev-C++/CourseBook/0304_Maze/SqStack.h @@ -0,0 +1,69 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define STACK_INIT_SIZE 100 // ˳ջ洢ռijʼ +#define STACKINCREMENT 10 // ˳ջ洢ռķ + +// Թͨ +typedef struct { + int x; // ͨĺᡢ궨 + int y; +} PosType; + +/* ͨϢԹ㷨 */ +typedef struct { + int ord; // ͨġš + PosType seat; // ͨġλá + int di; // һӦʵġ +} SElemType; + +// ˳ջԪؽṹ +typedef struct { + SElemType* base; // ջָ + SElemType* top; // ջָ + int stacksize; // ǰѷĴ洢ռ䣬ԪΪλ +} SqStack; + + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S); + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S); + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e); + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e); + +#endif diff --git a/Dev-C++/CourseBook/0305_Expression/Expression-main.cpp b/Dev-C++/CourseBook/0305_Expression/Expression-main.cpp new file mode 100644 index 0000000..9186009 --- /dev/null +++ b/Dev-C++/CourseBook/0305_Expression/Expression-main.cpp @@ -0,0 +1,12 @@ +#include "Expression.h" //**03 ջͶ**// + +int main(int argc, char** argv) { + char opnd; + char* exp = "(1+3)*2/4#"; + + opnd = EvaluateExpression(exp); + + printf("Ϊʾ %s ļΪ%d\n", exp, opnd - '0'); + + return 0; +} diff --git a/Dev-C++/CourseBook/0305_Expression/Expression.cpp b/Dev-C++/CourseBook/0305_Expression/Expression.cpp new file mode 100644 index 0000000..0bfa743 --- /dev/null +++ b/Dev-C++/CourseBook/0305_Expression/Expression.cpp @@ -0,0 +1,139 @@ +/*============== + * ʽ + * + * 㷨: 3.4 + ===============*/ + +#include "Expression.h" //**03 ջͶ**// + +/* + * 㷨3.4 + * + * expʽʽ + * + *ע + * 1.̲ʹõǿ̨룬Ϊ˱ڲԣֱӸΪβνղ + * 2.ü㹦ޣϽֶ֧Ըλ㣬ҪÿһҲǸλ + * ̲ṩ㷨Ŀ֤ջʹãչ֧֣Բ֧֣ + * ˳Ŵ˼·иİ + */ +OperandType EvaluateExpression(const char exp[]) { + SElemType c; // + + SqStack OPTR; // ջ + SqStack OPND; // ջ + + OperatorType theta, x; // + OperandType a, b; // + + int i = 0; + + // ʼջһ޷'#'ջ + InitStack(&OPTR); + Push(&OPTR, '#'); + + // ʼջʼȡ + InitStack(&OPND); + c = exp[i++]; + + // ޷'#'ջջԪҲǽ޷'#'ʱʾȡ + while(c != '#' || GetTop(OPTR) != '#') { + // chΪջ + if(!In(c, OP)) { + Push(&OPND, c); // ջ + c = exp[i++]; // ȡһַ + } else { + switch(Precede(GetTop(OPTR), c)) { + // ջȼͣջ + case '<': + Push(&OPTR, c); + c = exp[i++]; + break; + + // ȼʱ˵ţҪ + case '=': + Pop(&OPTR, &x); + c = exp[i++]; + break; + + /* + * ջȼʱȼ㣬ٽѹջ + * + * עûжַcĻǸղŶַ + */ + case '>': + Pop(&OPTR, &theta); // + Pop(&OPND, &b); // ұߵIJ + Pop(&OPND, &a); // ߵIJ + Push(&OPND, Operate(a, theta, b)); + break; + } + } + } + + return GetTop(OPND); +} + +// жָǷϹ +Status In(SElemType c, const char OP[]) { + + SElemType* e = strchr(OP, c); + + // cںϹ淶Χڣ˵ָϹ + if(e == NULL) { + return FALSE; + } else { + return TRUE; + } +} + +/* + * жջвo1ʽеIJo2ȼ + * + * '>''<''='ָʾo1o2ȼ + */ +OperatorType Precede(OperatorType o1, OperatorType o2) { + int x, y; + + // ȡָеλ + char* p1 = strchr(OP, o1); + char* p2 = strchr(OP, o2); + + // һȼ + x = p1 - OP; + y = p2 - OP; + + return PrecedeTable[x][y]; +} + +/* + * Բ + * + * abDztheta + * ڲ֤Ըλ֧ + */ +OperandType Operate(OperandType a, OperatorType theta, OperandType b) { + int x, y, z = CHAR_MAX - 48; + + // ȴַתΪ + x = a - '0'; + y = b - '0'; + + switch(theta) { + case '+': + z = x + y; + break; + case '-': + z = x - y; + break; + case '*': + z = x * y; + break; + case '/': + z = x / y; + break; + } + + // ɺ󣬽תΪַͷ + return z + 48; +} diff --git a/Dev-C++/CourseBook/0305_Expression/Expression.dev b/Dev-C++/CourseBook/0305_Expression/Expression.dev new file mode 100644 index 0000000..d4f0e73 --- /dev/null +++ b/Dev-C++/CourseBook/0305_Expression/Expression.dev @@ -0,0 +1,102 @@ +[Project] +FileName=Expression.dev +Name=Expression +Type=1 +Ver=2 +ObjFiles= +Includes= +Libs= +PrivateResource= +ResourceIncludes= +MakeIncludes= +Compiler= +CppCompiler= +Linker= +IsCpp=0 +Icon= +ExeOutput= +ObjectOutput= +LogOutput= +LogOutputEnabled=0 +OverrideOutput=0 +OverrideOutputName= +HostApplication= +UseCustomMakefile=0 +CustomMakefile= +CommandLine= +Folders= +IncludeVersionInfo=0 +SupportXPThemes=0 +CompilerSet=1 +CompilerSettings=0000000000000000001000000 +UnitCount=5 + +[VersionInfo] +Major=1 +Minor=0 +Release=0 +Build=0 +LanguageID=1033 +CharsetID=1252 +CompanyName= +FileVersion= +FileDescription=Developed using the Dev-C++ IDE +InternalName= +LegalCopyright= +LegalTrademarks= +OriginalFilename= +ProductName= +ProductVersion= +AutoIncBuildNr=0 +SyncProduct=1 + +[Unit3] +FileName=Expression-main.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit1] +FileName=Expression.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit4] +FileName=SqStack.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit2] +FileName=Expression.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit5] +FileName=SqStack.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/CourseBook/0305_Expression/Expression.h b/Dev-C++/CourseBook/0305_Expression/Expression.h new file mode 100644 index 0000000..0250fa6 --- /dev/null +++ b/Dev-C++/CourseBook/0305_Expression/Expression.h @@ -0,0 +1,70 @@ +/*============== + * ʽ + * + * 㷨: 3.4 + ===============*/ + +#ifndef EXPRESSION_H +#define EXPRESSION_H + +#include +#include +#include +#include "SqStack.h" //**03 ջͶ**// + +typedef SElemType OperatorType; // +typedef SElemType OperandType; // + +// ʽֵķţ޷'#' +static const char OP[] = {'+', '-', '*', '/', '(', ')', '#'}; + +/* + * ȼ޷'#'OPǺӦġ + * ɲμ̲е"ȹϵ" + */ +static const char PrecedeTable[7][7] = {{'>', '>', '<', '<', '<', '>', '>'}, + {'>', '>', '<', '<', '<', '>', '>'}, + {'>', '>', '>', '>', '<', '>', '>'}, + {'>', '>', '>', '>', '<', '>', '>'}, + {'<', '<', '<', '<', '<', '=', ' '}, + {'>', '>', '>', '>', ' ', '>', '>'}, + {'<', '<', '<', '<', '<', ' ', '='}}; + + +/* + * 㷨3.4 + * + * expʽʽ + * + *ע + * 1.̲ʹõǿ̨룬Ϊ˱ڲԣֱӸΪβνղ + * 2.ü㹦ޣϽֶ֧Ըλ㣬ҪÿһҲǸλ + * ̲ṩ㷨Ŀ֤ջʹãչ֧֣Բ֧֣ + * ˳Ŵ˼·иİ + */ +OperandType EvaluateExpression(const char exp[]); + +/* + * жָǷϹ + * + * OPд洢˺Ϲ޷'#' + */ +Status In(SElemType c, const char OP[]); + +/* + * жջвo1ʽеIJo2ȼ + * + * '>''<''='ָʾo1o2ȼ + */ +OperatorType Precede(OperatorType o1, OperatorType o2); + +/* + * Բ + * + * abDztheta + * ڲֶԸλ֧ + */ +OperandType Operate(OperandType a, OperatorType theta, OperandType b); + + +#endif diff --git a/Dev-C++/CourseBook/0305_Expression/SqStack.cpp b/Dev-C++/CourseBook/0305_Expression/SqStack.cpp new file mode 100644 index 0000000..30e35ab --- /dev/null +++ b/Dev-C++/CourseBook/0305_Expression/SqStack.cpp @@ -0,0 +1,111 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#include "SqStack.h" //**03 ջͶ**// + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + (*S).base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); + } + + (*S).top = (*S).base; + (*S).stacksize = STACK_INIT_SIZE; + + return OK; +} + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S) { + if(S.top == S.base) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * ȡֵ + * + * ȡջջԪء + * + *ע + * òʵ봫ͳ˳ջȡֵЩͬһ + */ +SElemType GetTop(SqStack S) { + SElemType e; + + if(S.base == NULL || S.top == S.base) { + return '\0'; + } + + // ıջԪ + e = *(S.top - 1); + + return e; +} + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + // ջʱ׷Ӵ洢ռ + if((*S).top - (*S).base >= (*S).stacksize) { + (*S).base = (SElemType*) realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); // 洢ʧ + } + + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + + // ջȸֵջָ + *(S->top++) = e; + + return OK; +} + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + if((*S).top == (*S).base) { + return ERROR; + } + + // ջջָȵݼٸֵ + *e = *(--(*S).top); + + return OK; +} diff --git a/Dev-C++/CourseBook/0305_Expression/SqStack.h b/Dev-C++/CourseBook/0305_Expression/SqStack.h new file mode 100644 index 0000000..74a388a --- /dev/null +++ b/Dev-C++/CourseBook/0305_Expression/SqStack.h @@ -0,0 +1,69 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define STACK_INIT_SIZE 100 // ˳ջ洢ռijʼ +#define STACKINCREMENT 10 // ˳ջ洢ռķ + +/* ʽԪͶ */ +typedef char SElemType; + +// ˳ջԪؽṹ +typedef struct { + SElemType* base; // ջָ + SElemType* top; // ջָ + int stacksize; // ǰѷĴ洢ռ䣬ԪΪλ +} SqStack; + + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S); + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S); + +/* + * ȡֵ + * + * ȡջջԪء + * + *ע + * òʵ봫ͳ˳ջȡֵЩͬһ + */ +SElemType GetTop(SqStack S); + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e); + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e); + +#endif diff --git a/Dev-C++/CourseBook/0306_Hanoi/Hanoi-main.cpp b/Dev-C++/CourseBook/0306_Hanoi/Hanoi-main.cpp new file mode 100644 index 0000000..b05e34f --- /dev/null +++ b/Dev-C++/CourseBook/0306_Hanoi/Hanoi-main.cpp @@ -0,0 +1,15 @@ +#include "Hanoi.h" //**03 ջͶ**// + +int main(int argc, char** argv) { + char x = 'x'; + char y = 'y'; + char z = 'z'; + + printf("ΪʾԲ̸Ϊ %d ...\n", N); + + init(N); + + hanoi(N, x, y, z); + + return 0; +} diff --git a/Dev-C++/CourseBook/0306_Hanoi/Hanoi.cpp b/Dev-C++/CourseBook/0306_Hanoi/Hanoi.cpp new file mode 100644 index 0000000..c772b44 --- /dev/null +++ b/Dev-C++/CourseBook/0306_Hanoi/Hanoi.cpp @@ -0,0 +1,133 @@ +/*============== + * ŵ + * + * 㷨: 3.5 + ===============*/ + +#include "Hanoi.h" //**03 ջͶ**// + +/* + * 㷨3.5 + * + * ŵ⣺yΪxǰnԲƶz + */ +void hanoi(int n, char x, char y, char z) { + if(n == 1) { // ƶnԲ̣ƶϵn-1Բ + move(x, 1, z); // Ϊ1Բ̴xƵz + } else { + hanoi(n - 1, x, z, y); // xϱΪ1n-1ԲƵyz + move(x, n, z); // ΪnԲ̴xƵz + hanoi(n - 1, y, x, z); // yϱΪ1n-1Բƶzx + } +} + +void move(char x, int n, char z) { + // stepΪȫֱmain֮ⶨ + gStep++; + printf("%2d %d Բ̴ %c Ƶ %c \n", gStep, n, x, z); + + // ŵƶͼαʾ + PrintGraph(x, n, z); +} + +/* + * ŵͼϢʼ + * + *ע + * ̲޴˲ + * Ӵ˲ĿΪ˱ڹ۲캺ŵԲ̵ƶ + */ +void init(int n) { + int i; + int* towerX, * towerY, * towerZ; + + T.plates = (int**) malloc(3 * sizeof(int*)); + + towerX = (int*) malloc(n * sizeof(int)); + towerY = (int*) malloc(n * sizeof(int)); + towerZ = (int*) malloc(n * sizeof(int)); + + for(i = 0; i < n; ++i) { + towerX[i] = n-i; + towerY[i] = 0; + towerZ[i] = 0; + } + + T.plates[0] = towerX; + T.plates[1] = towerY; + T.plates[2] = towerZ; + + T.high[0] = n; + T.high[1] = 0; + T.high[2] = 0; + + // ŵƶͼαʾ + PrintGraph('\0', 0, '\0'); +} + +/* + * ŵƶͼαʾ + * + *ע + * ̲޴˲ + * Ӵ˲ĿΪ˱ڹ۲캺ŵԲ̵ƶ + */ +void PrintGraph(char t1, int n, char t2){ + int i, j; + char** s; + + // nӴt1Ƴ + if(t1=='x') { + T.plates[0][T.high[0]-1] = 0; + T.high[0]--; + } else if(t1=='y') { + T.plates[1][T.high[1]-1] = 0; + T.high[1]--; + } else if(t1=='z') { + T.plates[2][T.high[2]-1] = 0; + T.high[2]--; + } else { + // t1ϵԲ̲Ҫƶ + } + + // nӵt2 + if(t2=='x') { + T.plates[0][T.high[0]] = n; + T.high[0]++; + } else if(t2=='y') { + T.plates[1][T.high[1]] = n; + T.high[1]++; + } else if(t2=='z') { + T.plates[2][T.high[2]] = n; + T.high[2]++; + } else { + // t2ϵԲ̲Ҫƶ + } + + s = (char**)malloc((N+2)*sizeof(char*)); + for(i = 0; i= 0; i--) { + printf("%-*s | %-*s | %-*s\n", N, s[T.plates[0][i]], N, s[T.plates[1][i]], N, s[T.plates[2][i]]); + } + printf("%-*s + %-*s + %-*s\n", N, s[N+1], N, s[N+1], N, s[N+1]); + printf("%-*s %-*s %-*s\n", N+2, "x", N+2, "y", N+2, "z"); + + printf("\n"); +} diff --git a/Dev-C++/CourseBook/0306_Hanoi/Hanoi.dev b/Dev-C++/CourseBook/0306_Hanoi/Hanoi.dev new file mode 100644 index 0000000..bc829ef --- /dev/null +++ b/Dev-C++/CourseBook/0306_Hanoi/Hanoi.dev @@ -0,0 +1,82 @@ +[Project] +FileName=Hanoi.dev +Name=Hanoi +Type=1 +Ver=2 +ObjFiles= +Includes= +Libs= +PrivateResource= +ResourceIncludes= +MakeIncludes= +Compiler= +CppCompiler= +Linker= +IsCpp=0 +Icon= +ExeOutput= +ObjectOutput= +LogOutput= +LogOutputEnabled=0 +OverrideOutput=0 +OverrideOutputName= +HostApplication= +UseCustomMakefile=0 +CustomMakefile= +CommandLine= +Folders= +IncludeVersionInfo=0 +SupportXPThemes=0 +CompilerSet=1 +CompilerSettings=0000000000000000001000000 +UnitCount=3 + +[VersionInfo] +Major=1 +Minor=0 +Release=0 +Build=0 +LanguageID=1033 +CharsetID=1252 +CompanyName= +FileVersion= +FileDescription=Developed using the Dev-C++ IDE +InternalName= +LegalCopyright= +LegalTrademarks= +OriginalFilename= +ProductName= +ProductVersion= +AutoIncBuildNr=0 +SyncProduct=1 + +[Unit3] +FileName=Hanoi-main.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit1] +FileName=Hanoi.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit2] +FileName=Hanoi.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/CourseBook/0306_Hanoi/Hanoi.h b/Dev-C++/CourseBook/0306_Hanoi/Hanoi.h new file mode 100644 index 0000000..9ff9730 --- /dev/null +++ b/Dev-C++/CourseBook/0306_Hanoi/Hanoi.h @@ -0,0 +1,59 @@ +/*============== + * ŵ + * + * 㷨: 3.5 + ===============*/ + +#ifndef HANOI_H +#define HANOI_H + +#include +#include +#include "Status.h" + +#define N 5 // ŵ + +// ŵͼϢ +typedef struct { + int** plates; // ŵеԲϢ + int high[3]; // ĸ߶ȣе +} Tower; + +// ŵ +static Tower T; + +// ͳƶ +static int gStep; + + +/* + * 㷨3.5 + * + * ŵ⣺yΪxǰnԲƶz + */ +void hanoi(int n, char x, char y, char z); + +/* + * ƶŵԲ̣nԲ̴xƵz + */ +void move(char x, int n, char z); + +/* + * ŵͼϢʼ + * + *ע + * ̲޴˲ + * Ӵ˲ĿΪ˱ڹ۲캺ŵԲ̵ƶ + */ +void init(int n); + +/* + * ŵƶͼαʾμmove() + * + *ע + * ̲޴˲ + * Ӵ˲ĿΪ˱ڹ۲캺ŵԲ̵ƶ + */ +void PrintGraph(char x, int n, char z); + +#endif diff --git a/Dev-C++/CourseBook/0307_LinkQueue/LinkQueue-main.cpp b/Dev-C++/CourseBook/0307_LinkQueue/LinkQueue-main.cpp new file mode 100644 index 0000000..1e71482 --- /dev/null +++ b/Dev-C++/CourseBook/0307_LinkQueue/LinkQueue-main.cpp @@ -0,0 +1,94 @@ +#include +#include "LinkQueue.h" //**03 ջͶ**// + +// Ժӡ +void PrintElem(QElemType e); + +int main(int argc, char** argv) { + LinkQueue Q; + int i; + QElemType e; + + printf(" InitQueue \n"); + { + printf(" ʼ Q ...\n"); + InitQueue(&Q); + } + PressEnterToContinue(); + + printf(" QueueEmpty \n"); + { + QueueEmpty(Q) ? printf(" Q Ϊգ\n") : printf(" Q Ϊգ\n"); + } + PressEnterToContinue(); + + printf(" EnQueue \n"); + { + for(i = 1; i <= 6; i++) { + EnQueue(&Q, 2 * i); + printf(" Ԫ \"%2d\" ...\n", 2 * i); + } + } + PressEnterToContinue(); + + printf(" QueueTraverse \n"); + { + printf(" Q еԪΪQ = "); + QueueTraverse(Q, PrintElem); + } + PressEnterToContinue(); + + printf(" QueueLength \n"); + { + i = QueueLength(Q); + printf(" Q ijΪ %d \n", i); + } + PressEnterToContinue(); + + printf(" DeQueue \n"); + { + DeQueue(&Q, &e); + printf(" ͷԪ \"%d\" ...\n", e); + printf(" Q еԪΪQ = "); + QueueTraverse(Q, PrintElem); + } + PressEnterToContinue(); + + printf(" GetHead \n"); + { + GetHead(Q, &e); + printf(" ͷԪصֵΪ \"%d\" \n", e); + } + PressEnterToContinue(); + + printf(" ClearQueue \n"); + { + printf(" Q ǰ"); + QueueEmpty(Q) ? printf(" Q Ϊգ\n") : printf(" Q Ϊգ\n"); + + ClearQueue(&Q); + + printf(" Q "); + QueueEmpty(Q) ? printf(" Q Ϊգ\n") : printf(" Q Ϊգ\n"); + } + PressEnterToContinue(); + + printf(" DestroyQueue \n"); + { + printf(" Q ǰ"); + Q.front != NULL && Q.rear != NULL ? printf(" Q ڣ\n") : printf(" Q ڣ\n"); + + DestroyQueue(&Q); + + printf(" Q "); + Q.front != NULL && Q.rear != NULL ? printf(" Q ڣ\n") : printf(" Q ڣ\n"); + } + PressEnterToContinue(); + + return 0; +} + +// Ժӡ +void PrintElem(QElemType e) { + printf("%d ", e); +} diff --git a/Dev-C++/CourseBook/0307_LinkQueue/LinkQueue.cpp b/Dev-C++/CourseBook/0307_LinkQueue/LinkQueue.cpp new file mode 100644 index 0000000..ba578f3 --- /dev/null +++ b/Dev-C++/CourseBook/0307_LinkQueue/LinkQueue.cpp @@ -0,0 +1,204 @@ +/*========================= + * еʽ洢ṹӣ + ==========================*/ + +#ifndef LINKQUEUE_C +#define LINKQUEUE_C + +#include "LinkQueue.h" //**03 ջͶ**// + +/* + * ʼ + * + * һյӡ + * ʼɹ򷵻OK򷵻ERROR + * + *ע + * Ķдͷ + */ +Status InitQueue(LinkQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).front = (*Q).rear = (QueuePtr) malloc(sizeof(QNode)); + if(!(*Q).front) { + exit(OVERFLOW); + } + + (*Q).front->next = NULL; + + return OK; +} + +/* + * (ṹ) + * + * ͷռڴ档 + */ +Status DestroyQueue(LinkQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + while((*Q).front) { + (*Q).rear = (*Q).front->next; + free((*Q).front); + (*Q).front = (*Q).rear; + } + + return OK; +} + +/* + * ÿ() + * + * Ҫͷзͷ㴦Ŀռ䡣 + */ +Status ClearQueue(LinkQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).rear = (*Q).front->next; + + while((*Q).rear) { + (*Q).front->next = (*Q).rear->next; + free((*Q).rear); + (*Q).rear = (*Q).front->next; + } + + (*Q).rear = (*Q).front; + + return OK; +} + +/* + * п + * + * жǷЧݡ + * + * ֵ + * TRUE : Ϊ + * FALSE: ӲΪ + */ +Status QueueEmpty(LinkQueue Q) { + if(Q.front == Q.rear) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * + * + * ӰЧԪص + */ +int QueueLength(LinkQueue Q) { + int count = 0; + QueuePtr p = Q.front; + + while(p != Q.rear) { + count++; + p = p->next; + } + + return count; +} + +/* + * ȡֵ + * + * ȡͷԪأ洢eС + * ҵOK򣬷ERROR + */ +Status GetHead(LinkQueue Q, QElemType* e) { + QueuePtr p; + + if(Q.front == NULL || Q.front == Q.rear) { + return ERROR; + } + + p = Q.front->next; + *e = p->data; + + return OK; +} + +/* + * + * + * Ԫeӵβ + */ +Status EnQueue(LinkQueue* Q, QElemType e) { + QueuePtr p; + + if(Q == NULL || (*Q).front == NULL) { + return ERROR; + } + + p = (QueuePtr) malloc(sizeof(QNode)); + if(!p) { + exit(OVERFLOW); + } + + p->data = e; + p->next = NULL; + + (*Q).rear->next = p; + (*Q).rear = p; + + return OK; +} + +/* + * + * + * ƳͷԪأ洢eС + */ +Status DeQueue(LinkQueue* Q, QElemType* e) { + QueuePtr p; + + if(Q == NULL || (*Q).front == NULL || (*Q).front == (*Q).rear) { + return ERROR; + } + + p = (*Q).front->next; + *e = p->data; + + (*Q).front->next = p->next; + if((*Q).rear == p) { + (*Q).rear = (*Q).front; + } + + free(p); + + return OK; +} + +/* + * + * + * visitʶQ + */ +Status QueueTraverse(LinkQueue Q, void (Visit)(QElemType)) { + QueuePtr p; + + if(Q.front == NULL) { + return ERROR; + } + + p = Q.front->next; + + while(p != NULL) { + Visit(p->data); + p = p->next; + } + + printf("\n"); + + return OK; +} + +#endif diff --git a/Dev-C++/CourseBook/0307_LinkQueue/LinkQueue.dev b/Dev-C++/CourseBook/0307_LinkQueue/LinkQueue.dev new file mode 100644 index 0000000..e39d0e7 --- /dev/null +++ b/Dev-C++/CourseBook/0307_LinkQueue/LinkQueue.dev @@ -0,0 +1,82 @@ +[Project] +FileName=LinkQueue.dev +Name=LinkQueue +Type=1 +Ver=2 +ObjFiles= +Includes= +Libs= +PrivateResource= +ResourceIncludes= +MakeIncludes= +Compiler= +CppCompiler= +Linker= +IsCpp=0 +Icon= +ExeOutput= +ObjectOutput= +LogOutput= +LogOutputEnabled=0 +OverrideOutput=0 +OverrideOutputName= +HostApplication= +UseCustomMakefile=0 +CustomMakefile= +CommandLine= +Folders= +IncludeVersionInfo=0 +SupportXPThemes=0 +CompilerSet=1 +CompilerSettings=0000000000000000001000000 +UnitCount=3 + +[VersionInfo] +Major=1 +Minor=0 +Release=0 +Build=0 +LanguageID=1033 +CharsetID=1252 +CompanyName= +FileVersion= +FileDescription=Developed using the Dev-C++ IDE +InternalName= +LegalCopyright= +LegalTrademarks= +OriginalFilename= +ProductName= +ProductVersion= +AutoIncBuildNr=0 +SyncProduct=1 + +[Unit3] +FileName=LinkQueue-main.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit1] +FileName=LinkQueue.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit2] +FileName=LinkQueue.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/CourseBook/0307_LinkQueue/LinkQueue.h b/Dev-C++/CourseBook/0307_LinkQueue/LinkQueue.h new file mode 100644 index 0000000..42cc95b --- /dev/null +++ b/Dev-C++/CourseBook/0307_LinkQueue/LinkQueue.h @@ -0,0 +1,100 @@ +/*========================= + * еʽ洢ṹӣ + ==========================*/ + +#ifndef LINKQUEUE_H +#define LINKQUEUE_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* ԪͶ */ +typedef int QElemType; + +// Ԫؽṹ +typedef struct QNode { + QElemType data; + struct QNode* next; +} QNode, * QueuePtr; + +// нṹ +typedef struct { + QueuePtr front; // ͷָ + QueuePtr rear; // βָ +} LinkQueue; // еʽ洢ʾ + + +/* + * ʼ + * + * һյӡ + * ʼɹ򷵻OK򷵻ERROR + * + *ע + * Ķдͷ + */ +Status InitQueue(LinkQueue* Q); + +/* + * (ṹ) + * + * ͷռڴ档 + */ +Status DestroyQueue(LinkQueue* Q); + +/* + * ÿ() + * + * Ҫͷзͷ㴦Ŀռ䡣 + */ +Status ClearQueue(LinkQueue* Q); + +/* + * п + * + * жǷЧݡ + * + * ֵ + * TRUE : Ϊ + * FALSE: ӲΪ + */ +Status QueueEmpty(LinkQueue Q); + +/* + * + * + * ӰЧԪص + */ +int QueueLength(LinkQueue Q); + +/* + * ȡֵ + * + * ȡͷԪأ洢eС + * ҵOK򣬷ERROR + */ +Status GetHead(LinkQueue Q, QElemType* e); + +/* + * + * + * Ԫeӵβ + */ +Status EnQueue(LinkQueue* Q, QElemType e); + +/* + * + * + * ƳͷԪأ洢eС + */ +Status DeQueue(LinkQueue* Q, QElemType* e); + +/* + * + * + * visitʶQ + */ +Status QueueTraverse(LinkQueue Q, void(Visit)(QElemType)); + +#endif diff --git a/Dev-C++/CourseBook/0308_SqQueue/SqQueue-main.cpp b/Dev-C++/CourseBook/0308_SqQueue/SqQueue-main.cpp new file mode 100644 index 0000000..c4a59d3 --- /dev/null +++ b/Dev-C++/CourseBook/0308_SqQueue/SqQueue-main.cpp @@ -0,0 +1,94 @@ +#include +#include "SqQueue.h" //**03 ջͶ**// + +// Ժӡ +void PrintElem(QElemType e); + +int main(int argc, char** argv) { + SqQueue Q; + int i; + QElemType e; + + printf(" InitQueue ...\n"); + { + printf(" ʼѭ˳ Q ...\n"); + InitQueue(&Q); + } + PressEnterToContinue(); + + printf(" QueueEmpty ...\n"); + { + QueueEmpty(Q) ? printf(" Q Ϊգ\n") : printf(" Q Ϊգ\n"); + } + PressEnterToContinue(); + + printf(" EnQueue ...\n"); + { + for(i = 1; i <= 6; i++) { + EnQueue(&Q, 2 * i); + printf(" Ԫ \"%2d\" Q...\n", 2 * i); + } + } + PressEnterToContinue(); + + printf(" QueueTraverse ...\n"); + { + printf(" Q еԪΪQ = "); + QueueTraverse(Q, PrintElem); + } + PressEnterToContinue(); + + printf(" QueueLength ...\n"); + { + i = QueueLength(Q); + printf(" Q ijΪ %d \n", i); + } + PressEnterToContinue(); + + printf(" DeQueue ...\n"); + { + DeQueue(&Q, &e); + printf(" ͷԪ \"%d\" ...\n", e); + printf(" Q еԪΪQ = "); + QueueTraverse(Q, PrintElem); + } + PressEnterToContinue(); + + printf(" GetHead ...\n"); + { + GetHead(Q, &e); + printf(" ͷԪصֵΪ \"%d\" \n", e); + } + PressEnterToContinue(); + + printf(" ClearQueue ...\n"); + { + printf(" Q ǰ"); + QueueEmpty(Q) ? printf(" Q Ϊգ\n") : printf(" Q Ϊգ\n"); + + ClearQueue(&Q); + + printf(" Q "); + QueueEmpty(Q) ? printf(" Q Ϊգ\n") : printf(" Q Ϊգ\n"); + } + PressEnterToContinue(); + + printf(" DestroyQueue ...\n"); + { + printf(" Q ǰ"); + Q.base != NULL ? printf(" Q ڣ\n") : printf(" Q ڣ\n"); + + DestroyQueue(&Q); + + printf(" Q "); + Q.base != NULL ? printf(" Q ڣ\n") : printf(" Q ڣ\n"); + } + PressEnterToContinue(); + + return 0; +} + +// Ժӡ +void PrintElem(QElemType e) { + printf("%d ", e); +} diff --git a/Dev-C++/CourseBook/0308_SqQueue/SqQueue.cpp b/Dev-C++/CourseBook/0308_SqQueue/SqQueue.cpp new file mode 100644 index 0000000..6079495 --- /dev/null +++ b/Dev-C++/CourseBook/0308_SqQueue/SqQueue.cpp @@ -0,0 +1,182 @@ +/*============================= + * е˳洢ṹ˳У + ==============================*/ + +#include "SqQueue.h" //**03 ջͶ**// + +/* + * ʼ + * + * һյ˳С + * ʼɹ򷵻OK򷵻ERROR + * + *ע + * Ķѭ + */ +Status InitQueue(SqQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).base = (QElemType*) malloc(MAXQSIZE * sizeof(QElemType)); + if(!(*Q).base) { + exit(OVERFLOW); + } + + (*Q).front = (*Q).rear = 0; + + return OK; +} + +/* + * (ṹ) + * + * ͷѭ˳ռڴ档 + */ +Status DestroyQueue(SqQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + if((*Q).base) { + free((*Q).base); + } + + (*Q).base = NULL; + (*Q).front = (*Q).rear = 0; + + return ERROR; +} + +/* + * ÿ() + * + * ֻѭ˳д洢ݣͷ˳ջռڴ档 + */ +Status ClearQueue(SqQueue* Q) { + if(Q == NULL || (*Q).base == NULL) { + return ERROR; + } + + (*Q).front = (*Q).rear = 0; + + return OK; +} + +/* + * п + * + * жѭ˳ǷЧݡ + * + * ֵ + * TRUE : ѭ˳Ϊ + * FALSE: ѭ˳вΪ + */ +Status QueueEmpty(SqQueue Q) { + // пյı־ + if(Q.front == Q.rear) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * + * + * ѭ˳аЧԪص + */ +int QueueLength(SqQueue Q) { + if(Q.base == NULL) { + return 0; + } + + // г + return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE; +} + +/* + * ȡֵ + * + * ȡͷԪأ洢eС + * ҵOK򣬷ERROR + */ +Status GetHead(SqQueue Q, QElemType* e) { + // пյı־ + if(Q.base == NULL || Q.front == Q.rear) { + return ERROR; + } + + *e = Q.base[Q.front]; + + return OK; +} + +/* + * + * + * Ԫeӵβ + */ +Status EnQueue(SqQueue* Q, QElemType e) { + if(Q == NULL || (*Q).base == NULL) { + return ERROR; + } + + // ı־˷һռֶпպͶ + if(((*Q).rear + 1) % MAXQSIZE == (*Q).front) { + return ERROR; + } + + // + (*Q).base[(*Q).rear] = e; + + // βָǰ + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + + return OK; +} + +/* + * + * + * ƳͷԪأ洢eС + */ +Status DeQueue(SqQueue* Q, QElemType* e) { + if(Q == NULL || (*Q).base == NULL) { + return ERROR; + } + + // пյı־ + if((*Q).front == (*Q).rear) { + return ERROR; + } + + // + *e = (*Q).base[(*Q).front]; + + // ͷָǰ + (*Q).front = ((*Q).front + 1) % MAXQSIZE; + + return OK; +} + +/* + * + * + * visitʶQ + */ +Status QueueTraverse(SqQueue Q, void(Visit)(QElemType)) { + int i; + + if(Q.base == NULL) { + return ERROR; + } + + for(i = Q.front; i != Q.rear; i = (i + 1) % MAXQSIZE) { + Visit(Q.base[i]); + } + + printf("\n"); + + return OK; +} diff --git a/Dev-C++/CourseBook/0308_SqQueue/SqQueue.dev b/Dev-C++/CourseBook/0308_SqQueue/SqQueue.dev new file mode 100644 index 0000000..f5efc2d --- /dev/null +++ b/Dev-C++/CourseBook/0308_SqQueue/SqQueue.dev @@ -0,0 +1,82 @@ +[Project] +FileName=SqQueue.dev +Name=SqQueue +Type=1 +Ver=2 +ObjFiles= +Includes= +Libs= +PrivateResource= +ResourceIncludes= +MakeIncludes= +Compiler= +CppCompiler= +Linker= +IsCpp=0 +Icon= +ExeOutput= +ObjectOutput= +LogOutput= +LogOutputEnabled=0 +OverrideOutput=0 +OverrideOutputName= +HostApplication= +UseCustomMakefile=0 +CustomMakefile= +CommandLine= +Folders= +IncludeVersionInfo=0 +SupportXPThemes=0 +CompilerSet=1 +CompilerSettings=0000000000000000001000000 +UnitCount=3 + +[VersionInfo] +Major=1 +Minor=0 +Release=0 +Build=0 +LanguageID=1033 +CharsetID=1252 +CompanyName= +FileVersion= +FileDescription=Developed using the Dev-C++ IDE +InternalName= +LegalCopyright= +LegalTrademarks= +OriginalFilename= +ProductName= +ProductVersion= +AutoIncBuildNr=0 +SyncProduct=1 + +[Unit3] +FileName=SqQueue-main.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit1] +FileName=SqQueue.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit2] +FileName=SqQueue.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/CourseBook/0308_SqQueue/SqQueue.h b/Dev-C++/CourseBook/0308_SqQueue/SqQueue.h new file mode 100644 index 0000000..18c3ba7 --- /dev/null +++ b/Dev-C++/CourseBook/0308_SqQueue/SqQueue.h @@ -0,0 +1,98 @@ +/*============================= + * е˳洢ṹ˳У + ==============================*/ + +#ifndef SQQUEUE_H +#define SQQUEUE_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define MAXQSIZE 1000 //г + +/* ѭԪͶ */ +typedef int QElemType; + +// ѭе˳洢ṹ +typedef struct { + QElemType* base; // ̬洢ռ + int front; // ͷָ룬вգָͷԪ + int rear; // βָ룬вգָβԪصһλ +} SqQueue; + + +/* + * ʼ + * + * һյ˳С + * ʼɹ򷵻OK򷵻ERROR + * + *ע + * Ķѭ + */ +Status InitQueue(SqQueue* Q); + +/* + * (ṹ) + * + * ͷѭ˳ռڴ档 + */ +Status ClearQueue(SqQueue* Q); + +/* + * ÿ() + * + * ֻѭ˳д洢ݣͷ˳ջռڴ档 + */ +Status DestroyQueue(SqQueue* Q); + +/* + * п + * + * жѭ˳ǷЧݡ + * + * ֵ + * TRUE : ѭ˳Ϊ + * FALSE: ѭ˳вΪ + */ +Status QueueEmpty(SqQueue Q); + +/* + * + * + * ѭ˳аЧԪص + */ +int QueueLength(SqQueue Q); + +/* + * ȡֵ + * + * ȡͷԪأ洢eС + * ҵOK򣬷ERROR + */ +Status GetHead(SqQueue Q, QElemType* e); + +/* + * + * + * Ԫeӵβ + */ +Status EnQueue(SqQueue* Q, QElemType e); + +/* + * + * + * ƳͷԪأ洢eС + */ +Status DeQueue(SqQueue* Q, QElemType* e); + +/* + * + * + * visitʶQ + */ +Status QueueTraverse(SqQueue Q, void(Visit)(QElemType)); + +#endif diff --git a/Dev-C++/CourseBook/0309_BankQueuing/BankQueuing-main.cpp b/Dev-C++/CourseBook/0309_BankQueuing/BankQueuing-main.cpp new file mode 100644 index 0000000..7e74c26 --- /dev/null +++ b/Dev-C++/CourseBook/0309_BankQueuing/BankQueuing-main.cpp @@ -0,0 +1,10 @@ +#include "BankQueuing.h" //**03 ջͶ**// + +int main(int argc, char** argv) { + + Bank_Simulation_1(); //㷨3.6 + +// Bank_Simulation_2(); //㷨3.73.6 + + return 0; +} diff --git a/Dev-C++/CourseBook/0309_BankQueuing/BankQueuing.cpp b/Dev-C++/CourseBook/0309_BankQueuing/BankQueuing.cpp new file mode 100644 index 0000000..836db55 --- /dev/null +++ b/Dev-C++/CourseBook/0309_BankQueuing/BankQueuing.cpp @@ -0,0 +1,338 @@ +/*================== + * ģŶ + * + * 㷨: 3.63.7 + ===================*/ + +#ifndef BANKQUEUING_C +#define BANKQUEUING_C + +#include "BankQueuing.h" //**03 ջͶ**// + +/* + * 㷨3.6 + * + * ҵģ⣬ͳһڿͻжƽʱ + */ +void Bank_Simulation_1() +{ + char eventType; + + OpenForDay(); // п + + // ¼Ϊ + while(MoreEvent()) { + EventDrived(&eventType); // ¼ + + switch(eventType) { + case 'A': + CustomerArrived(); // ͻ¼ + break; + case 'D': + CustomerDeparture(); // ͻ뿪¼ + break; + default : + Invalid(); + } + } + + CloseForDay(); +} + +/* + * 㷨3.7 + * + * ҵģ⣬ͳһڿͻжƽʱ + * 㷨3.7Ƶ + */ +void Bank_Simulation_2() { + OpenForDay(); // п + + while(!ListEmpty(gEv)) { + ListDelete(gEv, 1, &gEn); + + if(gEn.NType == Arrive) { + CustomerArrived(); // ͻ¼ + } else { + CustomerDeparture(); // ͻ뿪¼ + } + } + + CloseForDay(); // й +} + +/* + * пţʼл + */ +void OpenForDay() { + int i; + + // ʱ,ÿӪҵ8Сʱ480 + gCloseTime = 480; + + // ʼۼʱͿͻΪ0 + gTotalTime = 0; + gCustomerNum = 0; + + // ʼ¼Ϊձ + InitList(&gEv); + + // 趨һͻ¼ + gEn.OccurTime = 0; + gEn.NType = Arrive; + + // ¼ + OrderInsert(gEv, gEn, cmp); + + // ʼ4ն + for(i = 1; i <= N; ++i) { + InitQueue(&gQ[i]); + } + + Show(); +} + +/* + * й + * + * ͷԴӡͳϢ + */ +void CloseForDay() { + printf("ܹ%dͻƽʱΪ%dӡ\n", gCustomerNum, gTotalTime / gCustomerNum); +} + +/* + * ж¼ǷΪա + * Ƿδ¼ + */ +Status MoreEvent() { + return !ListEmpty(gEv); +} + +/* + * ¼¼Ƴ¼洢ȫֱgEnС + * event洢¼ + */ +void EventDrived(char* eventType) { + // ¼лȡ¼ + ListDelete(gEv, 1, &gEn); + + // ʶ¼ + if(gEn.NType == Arrive) { + *eventType = 'A'; + } else { + *eventType = 'D'; + } +} + +/* + * ͻ¼gEn.NType=0 + */ +void CustomerArrived() { + Event en; // ¼ + QElemType customer; // ͻ¼ + + int durtime; // ǰͻҵҪʱ + + int intertime; // һͻﵽʱ + int t; // һͻʱ + + int i; // б + + // ܿͻһ + ++gCustomerNum; + + // ɵǰͻҵҪʱһͻﵽʱ + Random(&durtime, &intertime); + + // һͻʱ + t = gEn.OccurTime + intertime; + + // δţһͻ""¼¼ + if(t < gCloseTime) { + en.OccurTime = t; // һͻĵʱ + en.NType = Arrive; // ""¼ + OrderInsert(gEv, en, cmp); // ""¼¼ + } + + // ȡǰ̵Ķб + i = Minimum(); + + // ¼ǰͻϢ + customer.ArrivedTime = gEn.OccurTime; // ʱ + customer.Duration = durtime; // ҵʱ + customer.Count = gCustomerNum; // ͻ + + // ǰͻ̶Ŷ + EnQueue(&gQ[i], customer); + printf("%3dͻ %d Ŷ...\n", customer.Count, i); + Show(); + + /* + * ǰֻһͻŶӣҪ뿪ʱ䣬 + * һ"뿪"¼뵽¼ + */ + if(QueueLength(gQ[i]) == 1) { + en.OccurTime = gEn.OccurTime + durtime; // ǰͻ뿪ʱ + en.NType = EventType(i); // "뿪"¼ֵͣΪ1-4ָʾӵڼ뿪 + OrderInsert(gEv, en, cmp); // "뿪"¼¼ + } +} + +/* + * ͻ뿪¼gEn.NType>0 + */ +void CustomerDeparture() { + Event en; // ¼ + QElemType customer; // ͻ¼ + int i = gEn.NType; // б + + // iеĶͷͻҵ񲢳 + DeQueue(&gQ[i], &customer); + printf("%3dͻӶ %d 뿪...\n", customer.Count, i); + Show(); + + // ۼƿͻʱ + gTotalTime += gEn.OccurTime - customer.ArrivedTime; + + /* + * ǰȻŶӵĿͻҪöжͷͻ뿪ʱ + * ע֮㣬Ϊֻһͻ뿪ˣһͻ뿪ʱŻ + */ + if(!QueueEmpty(gQ[i])) { + // ȡͷͻ + GetHead(gQ[i], &customer); + en.OccurTime = gEn.OccurTime + customer.Duration; // "뿪"¼ʱ + en.NType = EventType(i); // "뿪"¼ + OrderInsert(gEv, en, cmp); // "뿪"¼¼ + } +} + +/* + * Ч¼ + */ +void Invalid() { + printf("д"); + exit(OVERFLOW); +} + +/* + * ¼en뵽¼evУevǰʱ絽е¼ + * cmpȽ¼enΪڶʵδȥ + */ +Status OrderInsert(EventList ev, Event en, int(cmp)(Event, Event)) { + EventList p, pre, s; + + if(ev == NULL) { + return ERROR; + } + + for(pre = ev; pre->next != NULL && cmp(pre->next->data, en) < 0; pre = pre->next) { + // + } + + s = (LinkList) malloc(sizeof(LNode)); + if(s == NULL) { + exit(OVERFLOW); + } + s->data = en; + + s->next = pre->next; + pre->next = s; + + return OK; +} + +/* + * Ƚ¼ + */ +int cmp(Event a, Event b) { + if(a.OccurTime < b.OccurTime) { + return -1; // aȽ + } else if(a.OccurTime > b.OccurTime) { + return 1; // aȽ + } else { + return 0; // ͬʱ + } +} + +/* + * + * + * durtime ǰͷҵʱ + * intertimeһͻʱ + */ +void Random(int* durtime, int* intertime) { + srand((unsigned) time(NULL)); + *durtime = rand() % DurationTime + 1; // ҵʱ120 + *intertime = rand() % IntervalTime + 1; // һ˿͵ʱΪ110 +} + +/* + * س̵Ķе + */ +int Minimum() { + int i1 = QueueLength(gQ[1]); + int i2 = QueueLength(gQ[2]); + int i3 = QueueLength(gQ[3]); + int i4 = QueueLength(gQ[4]); + + if(i1 <= i2 && i1 <= i3 && i1 <= i4) { + return 1; + } + + if(i2 < i1 && i2 <= i3 && i2 <= i4) { + return 2; + } + + if(i3 < i1 && i3 < i2 && i3 <= i4) { + return 3; + } + + if(i4 < i1 && i4 < i2 && i4 < i3) { + return 4; + } + + return 0; +} + +/* + * ʾпͻеŶ + */ +void Show() { + int i; + QueuePtr p; // ¼Ŀͻǵڼ + + // пͻ + for(i = 1; i <= N; i++) { + for(p = gQ[i].front; p; p = p->next) { + if(p == gQ[i].front) { + if(i == 1) { + printf("̨١"); + } + if(i == 2) { + printf("̨ڡ"); + } + if(i == 3) { + printf("̨ۡ"); + } + if(i == 4) { + printf("̨ܡ"); + } + } else { + printf("%03d", p->data.Count); + } + + + if(p == gQ[i].rear) { + printf("\n"); + } + } + } + + printf("\n"); + + Wait(SleepTime); +} + +#endif diff --git a/Dev-C++/CourseBook/0309_BankQueuing/BankQueuing.dev b/Dev-C++/CourseBook/0309_BankQueuing/BankQueuing.dev new file mode 100644 index 0000000..2514642 --- /dev/null +++ b/Dev-C++/CourseBook/0309_BankQueuing/BankQueuing.dev @@ -0,0 +1,122 @@ +[Project] +FileName=BankQueuing.dev +Name=BankQueuing +Type=1 +Ver=2 +ObjFiles= +Includes= +Libs= +PrivateResource= +ResourceIncludes= +MakeIncludes= +Compiler= +CppCompiler= +Linker= +IsCpp=0 +Icon= +ExeOutput= +ObjectOutput= +LogOutput= +LogOutputEnabled=0 +OverrideOutput=0 +OverrideOutputName= +HostApplication= +UseCustomMakefile=0 +CustomMakefile= +CommandLine= +Folders= +IncludeVersionInfo=0 +SupportXPThemes=0 +CompilerSet=1 +CompilerSettings=0000000000000000001000000 +UnitCount=7 + +[VersionInfo] +Major=1 +Minor=0 +Release=0 +Build=0 +LanguageID=1033 +CharsetID=1252 +CompanyName= +FileVersion= +FileDescription=Developed using the Dev-C++ IDE +InternalName= +LegalCopyright= +LegalTrademarks= +OriginalFilename= +ProductName= +ProductVersion= +AutoIncBuildNr=0 +SyncProduct=1 + +[Unit3] +FileName=BankQueuing-main.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit1] +FileName=BankQueuing.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit4] +FileName=LinkList.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit6] +FileName=LinkQueue.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit2] +FileName=BankQueuing.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit5] +FileName=LinkList.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit7] +FileName=LinkQueue.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/CourseBook/0309_BankQueuing/BankQueuing.h b/Dev-C++/CourseBook/0309_BankQueuing/BankQueuing.h new file mode 100644 index 0000000..008483c --- /dev/null +++ b/Dev-C++/CourseBook/0309_BankQueuing/BankQueuing.h @@ -0,0 +1,121 @@ +/*================== + * ģŶ + * + * 㷨: 3.63.7 + ===================*/ + +#ifndef BANKQUEUING_H +#define BANKQUEUING_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include // ṩtimeԭ +#include "Status.h" //**01 **// +#include "LinkList.h" //**02 Ա**// +#include "LinkQueue.h" //**03 ջͶ**// + +/* 궨 */ +#define N 4 // ͻ +#define SleepTime 1 // SleepTimeʱ +#define DurationTime 20 // ҵʱ1DurationTimeӲ +#define IntervalTime 8 // һͻʱΪ1IntervalTimeӲ + +/* Ͷ */ +typedef LinkList EventList; //¼ͣΪ + +/* ȫֱǰ涼gǣ */ +static int gTotalTime; // ۼƿͻ +static int gCustomerNum; // ۼƿͻʱ + +static int gCloseTime; // ʱ,ÿӪҵ8Сʱ480 + +static EventList gEv; // ¼洢д¼ +static Event gEn; // ǰڴ¼ + +static LinkQueue gQ[N+1]; // 4ͻ,0ŵԪ + + +/* + * 㷨3.6 + * + * ҵģ⣬ͳһڿͻжƽʱ + */ +void Bank_Simulation_1(); + +/* + * 㷨3.7 + * + * ҵģ⣬ͳһڿͻжƽʱ + * 㷨3.7Ƶ + */ +void Bank_Simulation_2(); + +/* + * пţʼл + */ +void OpenForDay(); + +/* + * й + * + * ͷԴӡͳϢ + */ +void CloseForDay(); + +/* + * ж¼ǷΪա + * Ƿδ¼ + */ +Status MoreEvent(); + +/* + * ¼¼Ƴ¼洢ȫֱgEnС + * event洢¼ + */ +void EventDrived(char* event); + +/* + * ͻ¼gEn.NType=0 + */ +void CustomerArrived(); + +/* + * ͻ뿪¼gEn.NType>0 + */ +void CustomerDeparture(); + +/* + * Ч¼ + */ +void Invalid(); + +/* + * ¼en뵽¼evУevǰʱ絽е¼ + * cmpȽ¼enΪڶʵδȥ + */ +Status OrderInsert(EventList gEv, Event gEn, int(cmp)(Event, Event)); + +/* + * Ƚ¼ + */ +int cmp(Event a, Event b); + +/* + * + * + * durtime ǰͷҵʱ + * intertimeһͻʱ + */ +void Random(int* durtime, int* intertime); + +/* + * س̵Ķе + */ +int Minimum(); + +/* + * ʾпͻеŶ + */ +void Show(); + +#endif diff --git a/Dev-C++/CourseBook/0309_BankQueuing/LinkList.cpp b/Dev-C++/CourseBook/0309_BankQueuing/LinkList.cpp new file mode 100644 index 0000000..ecc119c --- /dev/null +++ b/Dev-C++/CourseBook/0309_BankQueuing/LinkList.cpp @@ -0,0 +1,130 @@ +/*=============================== + * Աʽ洢ṹ + * + * 㷨: 2.82.92.102.11 + ================================*/ + +#include "LinkList.h" //**02 Ա**// + +/* + * ʼ + * + * ֻdzʼһͷ㡣 + * ʼɹ򷵻OK򷵻ERROR + */ +Status InitList(LinkList* L) { + (*L) = (LinkList) malloc(sizeof(LNode)); + if(*L == NULL) { + exit(OVERFLOW); + } + + (*L)->next = NULL; + + return OK; +} + +/* + * п + * + * жǷЧݡ + * + * ֵ + * TRUE : Ϊ + * FALSE: Ϊ + */ +Status ListEmpty(LinkList L) { + // ֻͷʱΪΪ + if(L != NULL && L->next == NULL) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * 㷨2.9 + * + * + * + * iλϲeɹ򷵻OK򷵻ERROR + * + *ע + * ̲iĺԪλã1ʼ + */ +Status ListInsert(LinkList L, int i, ElemType e) { + LinkList p, s; + int j; + + // ȷ + if(L == NULL) { + return ERROR; + } + + p = L; + j = 0; + + // Ѱҵi-1㣬ұ֤ý㱾ΪNULL + while(p != NULL && j < i - 1) { + p = p->next; + ++j; + } + + // ͷˣiֵϹ(i<=0)˵ûҵϺĿĽ + if(p == NULL || j > i - 1) { + return ERROR; + } + + // ½ + s = (LinkList) malloc(sizeof(LNode)); + if(s == NULL) { + exit(OVERFLOW); + } + s->data = e; + s->next = p->next; + p->next = s; + + return OK; +} + +/* + * 㷨2.10 + * + * ɾ + * + * ɾiλϵԪأɾԪش洢eС + * ɾɹ򷵻OK򷵻ERROR + * + *ע + * ̲iĺԪλã1ʼ + */ +Status ListDelete(LinkList L, int i, ElemType* e) { + LinkList p, q; + int j; + + // ȷҲΪձ + if(L == NULL || L->next == NULL) { + return ERROR; + } + + p = L; + j = 0; + + // Ѱҵi-1㣬ұ֤ýĺ̲ΪNULL + while(p->next != NULL && j < i - 1) { + p = p->next; + ++j; + } + + // ͷˣiֵϹ(i<=0)˵ûҵϺĿĽ + if(p->next == NULL || j > i - 1) { + return ERROR; + } + + // ɾi + q = p->next; + p->next = q->next; + *e = q->data; + free(q); + + return OK; +} diff --git a/Dev-C++/CourseBook/0309_BankQueuing/LinkList.h b/Dev-C++/CourseBook/0309_BankQueuing/LinkList.h new file mode 100644 index 0000000..53bc85f --- /dev/null +++ b/Dev-C++/CourseBook/0309_BankQueuing/LinkList.h @@ -0,0 +1,83 @@ +/*=============================== + * Աʽ洢ṹ + * + * 㷨: 2.82.92.102.11 + ================================*/ + +#ifndef LINKLIST_H +#define LINKLIST_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +// ¼öٳ0¼14ʾĸڵ뿪¼ +typedef enum { + Arrive, Leave_1, Leave_2, Leave_3, Leave_4 +} EventType; + +/* ¼ԪͶ */ +typedef struct +{ + int OccurTime; // ¼ʱ + EventType NType; // ¼ +} Event, ElemType; // ¼Ԫ + +/* + * ṹ + * + * עĵͷ + */ +typedef struct LNode { + ElemType data; // ݽ + struct LNode* next; // ָһָ +} LNode; + +// ָָ +typedef LNode* LinkList; + + +/* + * ʼ + * + * ʼɹ򷵻OK򷵻ERROR + */ +Status InitList(LinkList* L); + +/* + * п + * + * жǷЧݡ + * + * ֵ + * TRUE : Ϊ + * FALSE: Ϊ + */ +Status ListEmpty(LinkList L); + +/* + * 㷨2.9 + * + * + * + * iλϲeɹ򷵻OK򷵻ERROR + * + *ע + * ̲iĺԪλã1ʼ + */ +Status ListInsert(LinkList L, int i, ElemType e); + +/* + * 㷨2.10 + * + * ɾ + * + * ɾiλϵԪأɾԪش洢eС + * ɾɹ򷵻OK򷵻ERROR + * + *ע + * ̲iĺԪλã1ʼ + */ +Status ListDelete(LinkList L, int i, ElemType* e); + +#endif diff --git a/Dev-C++/CourseBook/0309_BankQueuing/LinkQueue.cpp b/Dev-C++/CourseBook/0309_BankQueuing/LinkQueue.cpp new file mode 100644 index 0000000..b800d2c --- /dev/null +++ b/Dev-C++/CourseBook/0309_BankQueuing/LinkQueue.cpp @@ -0,0 +1,138 @@ +/*========================= + * еʽ洢ṹӣ + ==========================*/ + +#ifndef LINKQUEUE_C +#define LINKQUEUE_C + +#include "LinkQueue.h" //**03 ջͶ**// + +/* + * ʼ + * + * һյӡ + * ʼɹ򷵻OK򷵻ERROR + * + *ע + * Ķдͷ + */ +Status InitQueue(LinkQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).front = (*Q).rear = (QueuePtr) malloc(sizeof(QNode)); + if(!(*Q).front) { + exit(OVERFLOW); + } + + (*Q).front->next = NULL; + + return OK; +} + +/* + * п + * + * жǷЧݡ + * + * ֵ + * TRUE : Ϊ + * FALSE: ӲΪ + */ +Status QueueEmpty(LinkQueue Q) { + if(Q.front == Q.rear) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * + * + * ӰЧԪص + */ +int QueueLength(LinkQueue Q) { + int count = 0; + QueuePtr p = Q.front; + + while(p != Q.rear) { + count++; + p = p->next; + } + + return count; +} + +/* + * ȡֵ + * + * ȡͷԪأ洢eС + * ҵOK򣬷ERROR + */ +Status GetHead(LinkQueue Q, QElemType* e) { + QueuePtr p; + + if(Q.front == NULL || Q.front == Q.rear) { + return ERROR; + } + + p = Q.front->next; + *e = p->data; + + return OK; +} + +/* + * + * + * Ԫeӵβ + */ +Status EnQueue(LinkQueue* Q, QElemType e) { + QueuePtr p; + + if(Q == NULL || (*Q).front == NULL) { + return ERROR; + } + + p = (QueuePtr) malloc(sizeof(QNode)); + if(!p) { + exit(OVERFLOW); + } + + p->data = e; + p->next = NULL; + + (*Q).rear->next = p; + (*Q).rear = p; + + return OK; +} + +/* + * + * + * ƳͷԪأ洢eС + */ +Status DeQueue(LinkQueue* Q, QElemType* e) { + QueuePtr p; + + if(Q == NULL || (*Q).front == NULL || (*Q).front == (*Q).rear) { + return ERROR; + } + + p = (*Q).front->next; + *e = p->data; + + (*Q).front->next = p->next; + if((*Q).rear == p) { + (*Q).rear = (*Q).front; + } + + free(p); + + return OK; +} + +#endif diff --git a/Dev-C++/CourseBook/0309_BankQueuing/LinkQueue.h b/Dev-C++/CourseBook/0309_BankQueuing/LinkQueue.h new file mode 100644 index 0000000..7600daa --- /dev/null +++ b/Dev-C++/CourseBook/0309_BankQueuing/LinkQueue.h @@ -0,0 +1,83 @@ +/*========================= + * еʽ洢ṹӣ + ==========================*/ + +#ifndef LINKQUEUE_H +#define LINKQUEUE_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* ԪͶ壬¼ͻϢ */ +typedef struct { + int ArrivedTime; // ͻʱ + int Duration; // ҵʱ + int Count; // ˱¼ÿеĿͻǵڼ̲޴˱Ӵ˱Ŀǹ۲Ŷ״ +} QElemType; //еԪ + +// Ԫؽṹ +typedef struct QNode { + QElemType data; + struct QNode* next; +} QNode, * QueuePtr; + +// нṹ +typedef struct { + QueuePtr front; // ͷָ + QueuePtr rear; // βָ +} LinkQueue; // еʽ洢ʾ + + +/* + * ʼ + * + * һյӡ + * ʼɹ򷵻OK򷵻ERROR + * + *ע + * Ķдͷ + */ +Status InitQueue(LinkQueue* Q); + +/* + * п + * + * жǷЧݡ + * + * ֵ + * TRUE : Ϊ + * FALSE: ӲΪ + */ +Status QueueEmpty(LinkQueue Q); + +/* + * + * + * ӰЧԪص + */ +int QueueLength(LinkQueue Q); + +/* + * ȡֵ + * + * ȡͷԪأ洢eС + * ҵOK򣬷ERROR + */ +Status GetHead(LinkQueue Q, QElemType* e); + +/* + * + * + * Ԫeӵβ + */ +Status EnQueue(LinkQueue* Q, QElemType e); + +/* + * + * + * ƳͷԪأ洢eС + */ +Status DeQueue(LinkQueue* Q, QElemType* e); + +#endif diff --git a/README.md b/README.md index c4f602d..245ce22 100644 --- a/README.md +++ b/README.md @@ -12,57 +12,56 @@ ## 项目结构 -本项目包含了**教材源码**跟**习题源码**,并分为4个版本,分别是:**CFree**、**Dev-C++**、**CLion**、**VisualC++**,其中: +本项目包含了**教材源码**跟**习题源码**,并分为4个版本,分别是:**CFree**、**Dev-C++**、**CLion**、**VisualC++**,其中: -- **CFree** 版本是早期上传的完整版本,该版本在CFree这个IDE下测试通过。此版本中的代码虽有瑕疵,但不会再维护,新的更新会在下面三个分支版本中呈现。 -- **Dev-C++** 版本是指在Dev-C++这个IDE下测试通过的版本。 -- **CLion** 版本是指在CLion这个IDE下测试通过的版本。 -- **VisualC++** 版本是指在Microsoft Visual C++ 2010这个IDE下测试通过的版本。 +- **CFree** 版本是早期上传的完整版本,该版本在CFree这个IDE下测试通过。此版本中的代码虽有瑕疵,但不会再维护,新的更新会在下面三个分支版本中呈现。 +- **Dev-C++** 版本是指在Dev-C++这个IDE下测试通过的版本。 +- **CLion** 版本是指在CLion这个IDE下测试通过的版本。 +- **VisualC++** 版本是指在Microsoft Visual C++ 2010这个IDE下测试通过的版本。 > IDE的选择 >> CFree是一个优秀的国产软件,麻雀虽小五脏俱全,非常适合新手使用。不过该产品早已停更,在win10上有些兼容问题,需要调教。 -> ->> Dev-C++是一个开源软件,同CFree一样小巧实用。最关键的是,可以兼容win10,推荐使用。 -> +> +>> Dev-C++是一个开源软件,同CFree一样小巧实用。最关键的是,可以兼容win10,推荐使用。 +> >> CLion需要掌握一点cmake知识,对笔记本性能要求也略高。不过JetBrains系列的产品,功能优秀没得说,强烈建议尝试。 -> +> >> Microsoft Visual C++是微软出品,该系列号称地表最强,不过复杂度也是很高,对于新手并不友好,需要耐心琢磨。如果将来不是走C/C++/C#等路线,可以先不使用。(注:从2018年开始,计算机二级C语言项目的考试中,已将VC++6换成了Microsoft Visual C++ 2010。所以如果有考级需求的同学,请自行熟悉该IDE) -**习题解析**中存储了《数据结构题集》中非代码题的解析,对于需要写代码解决的问题,参见 **Dev-C++**、**CLion**、**VisualC++** 这三个版本中的源码。 +**习题解析**中存储了《数据结构题集》中非代码题的解析,对于需要写代码解决的问题,参见 **Dev-C++**、**CLion**、**VisualC++** 这三个版本中的源码。 ``` 注: -1. "CFree"是完整版本。"Dev-C++"/"CLion"/"VisualC++"是新增的版本,这三个版本最终会取代"CFree"版本。 -2. "CFree"版本既可以用CFree直接打开,也支持用Dev-C++打开,所以当使用CFree遇到兼容问题时,可尝试用Dev-C++。 -3. 上述四个版本各自独立,没有任何依赖关系,可单独运行/测试。 -4. 对所有版本的代码均未充分测试,所以如有BUG请到Issues反馈。 +1. "CFree"是完整版本。"Dev-C++"/"CLion"/"VisualC++"是新增的版本,这三个版本最终会取代"CFree"版本。 +2. "CFree"版本既可以用CFree直接打开,也支持用Dev-C++打开,所以当使用CFree遇到兼容问题时,可尝试用Dev-C++。 +3. 上述四个版本各自独立,没有任何依赖关系,可单独运行/测试。 +4. 对所有版本的代码均未充分测试,所以如有BUG请到Issues反馈。 ``` ## 更新目标 -总的目标是保障正确性,提高可读性,降低学习难度,具体来说包含以下几点: +总的目标是保障正确性,提高可读性,降低学习难度,具体来说包含以下几点: -1. 项目工程化★★ -2. 修复一些已知/潜在的BUG -3. 简化源码之间的引用关系,争取每个模块都可以单独运行测试 -4. 修剪被引用源码中的次要内容,使得焦点更聚集,重点更突出 -5. 增加注释与帮助信息,使源码展示更友好 -6. 出自教材中的算法,会尽量使其代码与教材一致,如有改动,会在注释中提示。其它算法会视情形书写,不唯一 +1. 项目工程化★★ +2. 修复一些已知/潜在的BUG +3. 简化源码之间的引用关系,争取每个模块都可以单独运行测试 +4. 修剪被引用源码中的次要内容,使得焦点更聚集,重点更突出 +5. 增加注释与帮助信息,使源码展示更友好 +6. 出自教材中的算法,会尽量使其代码与教材一致,如有改动,会在注释中提示。其它算法会视情形书写,不唯一 ## 使用方式 -* 开箱即用 - -> 将源码克隆/下载到本地后,可以查看各分支内的README.md文件以获取帮助信息 +* 开箱即用 +> 将源码克隆/下载到本地后,可以查看各分支内的 **README** 文件以获取帮助信息 ## 注意事项 -1. **本内容仅限个人学习使用,未经作者许可,不得用于商业用途** -2. **源码仅供参考,别抄作业** -3. **鼓励在Github提交[Issues](https://github.com/kangjianwei/Data-Structure/issues)来反馈,在博客上发私信未必可以及时看到** +1. **本内容仅限个人学习使用,未经作者许可,不得用于商业用途** +2. **源码仅供参考,别抄作业** +3. **鼓励在Github提交[Issues](https://github.com/kangjianwei/Data-Structure/issues)来反馈,在博客上发私信未必可以及时看到** ## Commit图例 @@ -79,31 +78,39 @@ ## 相关链接 -[个人博客](http://www.cnblogs.com/kangjianwei101) +[个人博客](http://www.cnblogs.com/kangjianwei101) ## 脚注 - -Commit信息中的`emoji`参考来源: - + +Commit信息中的`emoji`参考来源: + * [Full Emoji List](https://unicode.org/emoji/charts/full-emoji-list.html) - -* [gitmoji](https://gitmoji.carloscuesta.me/) +* [gitmoji](https://gitmoji.carloscuesta.me/) ## 附:教材源码目录 -| 章 | 节 | 内容 | 包含算法 | 备注 | -| :------- | :---------- | :----------- | :-------------------- | :------------------- | -| 01 绪论 | | | | 定义一些共享常量和函数 | -| 02 线性表 | SqList | 顺序表 | 2.3、2.4、2.5、2.6 | | -| | Union | A=A∪B | 2.1 | | -| | MergeSqList | C=A+B | 2.2、2.7 | 归并顺序表 | -| | LinkList | 链表 | 2.8、2.9、2.10、2.11 | | -| | MergeList | C=A+B | 2.12 | 归并链表 | -| | SLinkList | 静态链表 | 2.13、2.14、2.15、2.16 | | -| | Difference | (A-B)∪(B-A) | 2.17 | | -| | DuLinkList | 双向循环链表 | 2.18、2.19 | | -| | ELinkList | 扩展的线性链表 | 2.20 | | -| | MergeEList | C=A+B | 2.21 | 归并扩展的线性链表 | -| | Polynomial | 一元多项式 | 2.22、2.23 | | +| 章 | 节 | 内容 | 包含算法 | 备注 | +| :--------- | :---------- | :----------- | :-------------------- | :------------------- | +| 01 绪论 | Status | | | 定义一些共享常量和函数 | +| 02 线性表 | SqList | 顺序表 | 2.3、2.4、2.5、2.6 | 线性表的顺序存储结构 | +| | Union | A=A∪B | 2.1 | | +| | MergeSqList | C=A+B | 2.2、2.7 | 归并顺序表 | +| | LinkList | 链表 | 2.8、2.9、2.10、2.11 | 线性表的链式存储结构 | +| | MergeList | C=A+B | 2.12 | 归并链表 | +| | SLinkList | 静态链表 | 2.13、2.14、2.15、2.16 | | +| | Difference | (A-B)∪(B-A) | 2.17 | | +| | DuLinkList | 双向循环链表 | 2.18、2.19 | | +| | ELinkList | 扩展的线性链表 | 2.20 | | +| | MergeEList | C=A+B | 2.21 | 归并扩展的线性链表 | +| | Polynomial | 一元多项式 | 2.22、2.23 | | +| 03 栈和队列 | SqStack | 栈 | | 顺序存储结构 | +| | Conversion | 进制转换 | 3.1 | 栈的应用 | +| | LineEdit | 行编辑程序 | 3.2 | 栈的应用 | +| | Maze | 迷宫寻路 | 3.3 | 栈的应用 | +| | Expression | 表达式求值 | 3.4 | 栈的应用 | +| | Hanoi | 汉诺塔 | 3.5 | 递归 | +| | LinkQueue | 链列 | | 链式存储结构 | +| | SqQueue | 顺序队列 | | 循环队列,顺序存储结构 | +| | BankQueuing | 模拟银行排队 | 3.6、3.7 | 队列的应用 | diff --git a/VisualC++/CourseBook/0301_SqStack/0301_SqStack.vcxproj b/VisualC++/CourseBook/0301_SqStack/0301_SqStack.vcxproj new file mode 100644 index 0000000..ba5e9c6 --- /dev/null +++ b/VisualC++/CourseBook/0301_SqStack/0301_SqStack.vcxproj @@ -0,0 +1,76 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {CBC87773-9686-4E77-BE7E-1335A673E0E1} + My0301_SqStack + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)\..\Status;$(IncludePath) + + + + Level3 + Disabled + + + true + $(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies) + Console + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0301_SqStack/0301_SqStack.vcxproj.filters b/VisualC++/CourseBook/0301_SqStack/0301_SqStack.vcxproj.filters new file mode 100644 index 0000000..0ec157d --- /dev/null +++ b/VisualC++/CourseBook/0301_SqStack/0301_SqStack.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + 源文件 + + + + + 头文件 + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0301_SqStack/0301_SqStack.vcxproj.user b/VisualC++/CourseBook/0301_SqStack/0301_SqStack.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/CourseBook/0301_SqStack/0301_SqStack.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0301_SqStack/SqStack-main.c b/VisualC++/CourseBook/0301_SqStack/SqStack-main.c new file mode 100644 index 0000000..ae7df91 --- /dev/null +++ b/VisualC++/CourseBook/0301_SqStack/SqStack-main.c @@ -0,0 +1,94 @@ +#include +#include "SqStack.h" //**03 ջͶ**// + +// ԺӡԪ +void PrintElem(SElemType e); + +int main(int argc, char** argv) { + SqStack S; + int i; + SElemType e; + + printf(" InitStack \n"); + { + printf(" ʼ˳ջ S ...\n"); + InitStack(&S); + } + PressEnterToContinue(); + + printf(" StackEmpty \n"); + { + StackEmpty(S) ? printf(" S Ϊգ\n") : printf(" S Ϊգ\n"); + } + PressEnterToContinue(); + + printf(" Push \n"); + { + for(i = 1; i <= 6; i++) { + Push(&S, 2 * i); + printf(" \"%2d\" ѹջ S ...\n", 2 * i); + } + } + PressEnterToContinue(); + + printf(" StackTraverse \n"); + { + printf(" S еԪΪS = "); + StackTraverse(S, PrintElem); + } + PressEnterToContinue(); + + printf(" StackLength \n"); + { + i = StackLength(S); + printf(" S ijΪ %d \n", i); + } + PressEnterToContinue(); + + printf(" Pop \n"); + { + Pop(&S, &e); + printf(" ջԪ \"%d\" ջ...\n", e); + printf(" S еԪΪS = "); + StackTraverse(S, PrintElem); + } + PressEnterToContinue(); + + printf(" GetTop \n"); + { + GetTop(S, &e); + printf(" ջԪصֵΪ \"%d\" \n", e); + } + PressEnterToContinue(); + + printf(" ClearStack \n"); + { + printf(" S ǰ"); + StackEmpty(S) ? printf(" S Ϊգ\n") : printf(" S Ϊգ\n"); + + ClearStack(&S); + + printf(" S "); + StackEmpty(S) ? printf(" S Ϊգ\n") : printf(" S Ϊգ\n"); + } + PressEnterToContinue(); + + printf(" DestroyStack \n"); + { + printf(" S ǰ"); + S.base != NULL && S.top != NULL ? printf(" S ڣ\n") : printf(" S ڣ\n"); + + DestroyStack(&S); + + printf(" S "); + S.base != NULL && S.top != NULL ? printf(" S ڣ\n") : printf(" S ڣ\n"); + } + PressEnterToContinue(); + + return 0; +} + +// ԺӡԪ +void PrintElem(SElemType e) { + printf("%d ", e); +} diff --git a/VisualC++/CourseBook/0301_SqStack/SqStack.c b/VisualC++/CourseBook/0301_SqStack/SqStack.c new file mode 100644 index 0000000..6ae0e8b --- /dev/null +++ b/VisualC++/CourseBook/0301_SqStack/SqStack.c @@ -0,0 +1,174 @@ +/*========================= + * ջ˳洢ṹ˳ջ + ==========================*/ + +#include "SqStack.h" //**03 ջͶ**// + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + (*S).base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); + } + + (*S).top = (*S).base; + (*S).stacksize = STACK_INIT_SIZE; + + return OK; +} + +/* + * (ṹ) + * + * ͷ˳ջռڴ档 + */ +Status DestroyStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + free((*S).base); + + (*S).base = NULL; + (*S).top = NULL; + (*S).stacksize = 0; + + return OK; +} + +/* + * ÿ() + * + * ֻ˳ջд洢ݣͷ˳ջռڴ档 + */ +Status ClearStack(SqStack* S) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + (*S).top = (*S).base; + + return OK; +} + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S) { + if(S.top == S.base) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * + * + * ˳ջЧԪص + */ +int StackLength(SqStack S) { + if(S.base == NULL) { + return 0; + } + + return (int) (S.top - S.base); +} + +/* + * ȡֵ + * + * ջԪأeա + */ +Status GetTop(SqStack S, SElemType* e) { + if(S.base == NULL || S.top == S.base) { + return 0; + } + + // ıջԪ + *e = *(S.top - 1); + + return OK; +} + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + // ջʱ׷Ӵ洢ռ + if((*S).top - (*S).base >= (*S).stacksize) { + (*S).base = (SElemType*) realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); // 洢ʧ + } + + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + + // ջȸֵջָ + *(S->top++) = e; + + return OK; +} + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + if((*S).top == (*S).base) { + return ERROR; + } + + // ջջָȵݼٸֵ + *e = *(--(*S).top); + + return OK; +} + +/* + * + * + * visit˳ջS + */ +Status StackTraverse(SqStack S, void(Visit)(SElemType)) { + SElemType* p = S.base; + + if(S.base == NULL) { + return ERROR; + } + + while(p < S.top) { + Visit(*p++); + } + + printf("\n"); + + return OK; +} diff --git a/VisualC++/CourseBook/0301_SqStack/SqStack.h b/VisualC++/CourseBook/0301_SqStack/SqStack.h new file mode 100644 index 0000000..3a0d1a8 --- /dev/null +++ b/VisualC++/CourseBook/0301_SqStack/SqStack.h @@ -0,0 +1,94 @@ +/*========================= + * ջ˳洢ṹ˳ջ + ==========================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define STACK_INIT_SIZE 100 // ˳ջ洢ռijʼ +#define STACKINCREMENT 10 // ˳ջ洢ռķ + +/* ˳ջԪͶ */ +typedef int SElemType; + +// ˳ջԪؽṹ +typedef struct { + SElemType* base; // ջָ + SElemType* top; // ջָ + int stacksize; // ǰѷĴ洢ռ䣬ԪΪλ +} SqStack; + + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S); + +/* + * (ṹ) + * + * ͷ˳ջռڴ档 + */ +Status DestroyStack(SqStack* S); + +/* + * ÿ() + * + * ֻ˳ջд洢ݣͷ˳ջռڴ档 + */ +Status ClearStack(SqStack* S); + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S); + +/* + * + * + * ˳ջЧԪص + */ +int StackLength(SqStack S); + +/* + * ȡֵ + * + * ջԪأeա + */ +Status GetTop(SqStack S, SElemType* e); + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e); + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e); + +/* + * + * + * visit˳ջS + */ +Status StackTraverse(SqStack S, void(Visit)(SElemType)); + +#endif diff --git a/VisualC++/CourseBook/0302_Conversion/0302_Conversion.vcxproj b/VisualC++/CourseBook/0302_Conversion/0302_Conversion.vcxproj new file mode 100644 index 0000000..dc09019 --- /dev/null +++ b/VisualC++/CourseBook/0302_Conversion/0302_Conversion.vcxproj @@ -0,0 +1,78 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {B1FD90AC-75CE-404C-907E-8D939C078D79} + My0302_Conversion + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)\..\Status;$(IncludePath) + + + + Level3 + Disabled + + + true + $(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies) + Console + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0302_Conversion/0302_Conversion.vcxproj.filters b/VisualC++/CourseBook/0302_Conversion/0302_Conversion.vcxproj.filters new file mode 100644 index 0000000..5b7dd4c --- /dev/null +++ b/VisualC++/CourseBook/0302_Conversion/0302_Conversion.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + 源文件 + + + 源文件 + + + + + 头文件 + + + 头文件 + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0302_Conversion/0302_Conversion.vcxproj.user b/VisualC++/CourseBook/0302_Conversion/0302_Conversion.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/CourseBook/0302_Conversion/0302_Conversion.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0302_Conversion/Conversion-main.c b/VisualC++/CourseBook/0302_Conversion/Conversion-main.c new file mode 100644 index 0000000..2d99ac5 --- /dev/null +++ b/VisualC++/CourseBook/0302_Conversion/Conversion-main.c @@ -0,0 +1,11 @@ +#include "Conversion.h" //**03 ջͶ**// + +int main(int argc, char** argv) { + int i = 342391; + + printf("ʮתΪ˽...\n"); + + conversion(i); + + return 0; +} diff --git a/VisualC++/CourseBook/0302_Conversion/Conversion.c b/VisualC++/CourseBook/0302_Conversion/Conversion.c new file mode 100644 index 0000000..4df0cfa --- /dev/null +++ b/VisualC++/CourseBook/0302_Conversion/Conversion.c @@ -0,0 +1,37 @@ +/*============== + * ת + * + * 㷨: 3.1 + ===============*/ + +#include "Conversion.h" //**03 ջͶ**// + +/* + * 㷨3.1 + * + * תָķǸʮתΪ˽ƺ + * + *ע + * ̲ʹõǿ̨룬Ϊ˱ڲԣֱӸΪβνղ + */ +void conversion(int i) { + SqStack S; + SElemType e; + + InitStack(&S); + + // ˽ǰ0 + printf("ʮ %d תΪ˽Ϊ0", i); + + while(i!=0) { + Push(&S, i % 8); // ջʱӵλλ + i = i / 8; + } + + while(StackEmpty(S)==FALSE) { + Pop(&S, &e); // ջʱӸλλ + printf("%d", e); + } + + printf("\n"); +} diff --git a/VisualC++/CourseBook/0302_Conversion/Conversion.h b/VisualC++/CourseBook/0302_Conversion/Conversion.h new file mode 100644 index 0000000..0371acf --- /dev/null +++ b/VisualC++/CourseBook/0302_Conversion/Conversion.h @@ -0,0 +1,20 @@ +/*============== + * ת + * + * 㷨: 3.1 + ===============*/ + +#ifndef CONVERSION_H +#define CONVERSION_H + +#include +#include "SqStack.h" //**03 ջͶ**// + +/* + * 㷨3.1 + * + * תָķǸʮתΪ˽ƺ + */ +void conversion(int i); + +#endif diff --git a/VisualC++/CourseBook/0302_Conversion/SqStack.c b/VisualC++/CourseBook/0302_Conversion/SqStack.c new file mode 100644 index 0000000..7e16904 --- /dev/null +++ b/VisualC++/CourseBook/0302_Conversion/SqStack.c @@ -0,0 +1,90 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#include "SqStack.h" //**03 ջͶ**// + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + (*S).base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); + } + + (*S).top = (*S).base; + (*S).stacksize = STACK_INIT_SIZE; + + return OK; +} + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S) { + if(S.top == S.base) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + // ջʱ׷Ӵ洢ռ + if((*S).top - (*S).base >= (*S).stacksize) { + (*S).base = (SElemType*) realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); // 洢ʧ + } + + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + + // ջȸֵջָ + *(S->top++) = e; + + return OK; +} + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + if((*S).top == (*S).base) { + return ERROR; + } + + // ջջָȵݼٸֵ + *e = *(--(*S).top); + + return OK; +} diff --git a/VisualC++/CourseBook/0302_Conversion/SqStack.h b/VisualC++/CourseBook/0302_Conversion/SqStack.h new file mode 100644 index 0000000..19e6cc1 --- /dev/null +++ b/VisualC++/CourseBook/0302_Conversion/SqStack.h @@ -0,0 +1,59 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define STACK_INIT_SIZE 100 // ˳ջ洢ռijʼ +#define STACKINCREMENT 10 // ˳ջ洢ռķ + +/* ˳ջԪͶ */ +typedef int SElemType; + +// ˳ջԪؽṹ +typedef struct { + SElemType* base; // ջָ + SElemType* top; // ջָ + int stacksize; // ǰѷĴ洢ռ䣬ԪΪλ +} SqStack; + + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S); + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S); + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e); + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e); + +#endif diff --git a/VisualC++/CourseBook/0303_LineEdit/0303_LineEdit.vcxproj b/VisualC++/CourseBook/0303_LineEdit/0303_LineEdit.vcxproj new file mode 100644 index 0000000..480a7c2 --- /dev/null +++ b/VisualC++/CourseBook/0303_LineEdit/0303_LineEdit.vcxproj @@ -0,0 +1,78 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {55BB85DE-4B42-4EF0-BBF8-F5E03A518376} + My0303_LineEdit + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)\..\Status;$(IncludePath) + + + + Level3 + Disabled + + + true + $(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies) + Console + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0303_LineEdit/0303_LineEdit.vcxproj.filters b/VisualC++/CourseBook/0303_LineEdit/0303_LineEdit.vcxproj.filters new file mode 100644 index 0000000..150f369 --- /dev/null +++ b/VisualC++/CourseBook/0303_LineEdit/0303_LineEdit.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + 源文件 + + + 源文件 + + + + + 头文件 + + + 头文件 + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0303_LineEdit/0303_LineEdit.vcxproj.user b/VisualC++/CourseBook/0303_LineEdit/0303_LineEdit.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/CourseBook/0303_LineEdit/0303_LineEdit.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0303_LineEdit/LineEdit-main.c b/VisualC++/CourseBook/0303_LineEdit/LineEdit-main.c new file mode 100644 index 0000000..56235b7 --- /dev/null +++ b/VisualC++/CourseBook/0303_LineEdit/LineEdit-main.c @@ -0,0 +1,18 @@ +#include +#include "LineEdit.h" //**03 ջͶ**// + +int main(int argc, char* argv[]) { + char* buf = "whli##ilr#e(s#*s)\noutcha@ putchar(*s=#++);"; //Ҫ¼ + + printf("ΪʾûıΪ\n"); + printf("%s\n\n", buf); + + printf("б༭...\n\n"); + + printf("ţ'#' ɾһԪء'@' ɾǰ\n"); + printf(" '\\n''\\0'\n"); + printf("մ洢Ϊ\n"); + LineEdit(buf); + + return 0; +} diff --git a/VisualC++/CourseBook/0303_LineEdit/LineEdit.c b/VisualC++/CourseBook/0303_LineEdit/LineEdit.c new file mode 100644 index 0000000..cf00861 --- /dev/null +++ b/VisualC++/CourseBook/0303_LineEdit/LineEdit.c @@ -0,0 +1,71 @@ +/*============== + * б༭ + * + * 㷨: 3.2 + ===============*/ + +#include "LineEdit.h" //**03 ջͶ**// + +/* + * 㷨3.2 + * + * б༭ģ༭ıʱ˸еIJ + * + *ע + * ̲ʹõǿ̨룬Ϊ˱ڲԣֱӸΪβνղ + */ +void LineEdit(const char buffer[]) { + SqStack S; //ַ + SElemType e; + int i; + char ch; + + // ʼջ + InitStack(&S); + + i = 0; + ch = buffer[i++]; + + // δıĩβ + while(ch != EOF) { + // δıĩβұδδУ + while(ch != EOF && ch != '\n') { + switch(ch) { + case '#': + Pop(&S, &e); // '#'ʾɾһַ + break; + case '@': + ClearStack(&S); // '@'ʾյǰ + break; + default : + Push(&S, ch); // Чַջ + } + + // ʶһַ + ch = buffer[i++]; + } + + // ֮ǰǰջݣ˴̲û + StackTraverse(S, Print); + + // ոеĻ + ClearStack(&S); + + // δıĩβ˵'\n'н + if(ch != EOF) { + // һ + ch = buffer[i++]; + } + } + + // ѾıĩβĿǰջеԪأ˴̲û + StackTraverse(S, Print); + + // ջ + DestroyStack(&S); +} + +// ԺӡԪ +void Print(SElemType e) { + printf("%c", e); +} diff --git a/VisualC++/CourseBook/0303_LineEdit/LineEdit.h b/VisualC++/CourseBook/0303_LineEdit/LineEdit.h new file mode 100644 index 0000000..528960b --- /dev/null +++ b/VisualC++/CourseBook/0303_LineEdit/LineEdit.h @@ -0,0 +1,33 @@ +/*============== + * б༭ + * + * 㷨: 3.2 + ===============*/ + +#ifndef LINEEDIT_H +#define LINEEDIT_H + +#include +#include "SqStack.h" //**03 ջͶ**// +#include "LineEdit.h" + +// ģļеıǣҪеĶ +#ifdef EOF +#undef EOF +#define EOF '\0' +#endif + +/* + * 㷨3.2 + * + * б༭ģ༭ıʱ˸еIJ + * + *ע + * ̲ʹõǿ̨룬Ϊ˱ڲԣֱӸΪβνղ + */ +void LineEdit(const char buffer[]); + +// ԺӡԪ +void Print(SElemType e); + +#endif diff --git a/VisualC++/CourseBook/0303_LineEdit/SqStack.c b/VisualC++/CourseBook/0303_LineEdit/SqStack.c new file mode 100644 index 0000000..d089f26 --- /dev/null +++ b/VisualC++/CourseBook/0303_LineEdit/SqStack.c @@ -0,0 +1,128 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#include "SqStack.h" //**03 ջͶ**// + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + (*S).base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); + } + + (*S).top = (*S).base; + (*S).stacksize = STACK_INIT_SIZE; + + return OK; +} + +/* + * (ṹ) + * + * ͷ˳ջռڴ档 + */ +Status DestroyStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + free((*S).base); + + (*S).base = NULL; + (*S).top = NULL; + (*S).stacksize = 0; + + return OK; +} + +/* + * ÿ() + * + * ֻ˳ջд洢ݣͷ˳ջռڴ档 + */ +Status ClearStack(SqStack* S) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + (*S).top = (*S).base; + + return OK; +} + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + // ջʱ׷Ӵ洢ռ + if((*S).top - (*S).base >= (*S).stacksize) { + (*S).base = (SElemType*) realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); // 洢ʧ + } + + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + + // ջȸֵջָ + *(S->top++) = e; + + return OK; +} + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + if((*S).top == (*S).base) { + return ERROR; + } + + // ջջָȵݼٸֵ + *e = *(--(*S).top); + + return OK; +} + +/* + * + * + * visit˳ջS + */ +Status StackTraverse(SqStack S, void(Visit)(SElemType)) { + SElemType* p = S.base; + + if(S.base == NULL) { + return ERROR; + } + + while(p < S.top) { + Visit(*p++); + } + + printf("\n"); + + return OK; +} diff --git a/VisualC++/CourseBook/0303_LineEdit/SqStack.h b/VisualC++/CourseBook/0303_LineEdit/SqStack.h new file mode 100644 index 0000000..7382c02 --- /dev/null +++ b/VisualC++/CourseBook/0303_LineEdit/SqStack.h @@ -0,0 +1,69 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define STACK_INIT_SIZE 100 // ˳ջ洢ռijʼ +#define STACKINCREMENT 10 // ˳ջ洢ռķ + +/* ˳ջԪͶ */ +typedef int SElemType; + +// ˳ջԪؽṹ +typedef struct { + SElemType* base; // ջָ + SElemType* top; // ջָ + int stacksize; // ǰѷĴ洢ռ䣬ԪΪλ +} SqStack; + + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S); + +/* + * (ṹ) + * + * ͷ˳ջռڴ档 + */ +Status DestroyStack(SqStack* S); + +/* + * ÿ() + * + * ֻ˳ջд洢ݣͷ˳ջռڴ档 + */ +Status ClearStack(SqStack* S); + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e); + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e); + +/* + * + * + * visit˳ջS + */ +Status StackTraverse(SqStack S, void(Visit)(SElemType)); + +#endif diff --git a/VisualC++/CourseBook/0304_Maze/0304_Maze.vcxproj b/VisualC++/CourseBook/0304_Maze/0304_Maze.vcxproj new file mode 100644 index 0000000..8295250 --- /dev/null +++ b/VisualC++/CourseBook/0304_Maze/0304_Maze.vcxproj @@ -0,0 +1,78 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {38C80F0E-757B-4E11-93F2-C2666AF3617F} + My0304_Maze + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)\..\Status;$(IncludePath) + + + + Level3 + Disabled + + + true + $(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies) + Console + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0304_Maze/0304_Maze.vcxproj.filters b/VisualC++/CourseBook/0304_Maze/0304_Maze.vcxproj.filters new file mode 100644 index 0000000..e4c78ed --- /dev/null +++ b/VisualC++/CourseBook/0304_Maze/0304_Maze.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + 源文件 + + + 源文件 + + + + + 头文件 + + + 头文件 + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0304_Maze/0304_Maze.vcxproj.user b/VisualC++/CourseBook/0304_Maze/0304_Maze.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/CourseBook/0304_Maze/0304_Maze.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0304_Maze/Maze-main.c b/VisualC++/CourseBook/0304_Maze/Maze-main.c new file mode 100644 index 0000000..1e2246a --- /dev/null +++ b/VisualC++/CourseBook/0304_Maze/Maze-main.c @@ -0,0 +1,20 @@ +#include "Maze.h" //**03 ջͶ**// + +int main(int argc, char* argv[]) { + MazeType maze; + PosType start, end; + char n, Re = 'Y'; + + while(Re == 'Y' || Re == 'y') { + InitMaze(maze, &start, &end); // ʼԹ + + MazePath(maze, start, end); // ԹѰ· + + printf("ãY/N"); + scanf("%c%c", &Re, &n); + + printf("\n"); + } + + return 0; +} diff --git a/VisualC++/CourseBook/0304_Maze/Maze.c b/VisualC++/CourseBook/0304_Maze/Maze.c new file mode 100644 index 0000000..cb2dca1 --- /dev/null +++ b/VisualC++/CourseBook/0304_Maze/Maze.c @@ -0,0 +1,294 @@ +/*============== + * ԹѰ· + * + * 㷨: 3.3 + ===============*/ + +#include "Maze.h" //**03 ջͶ**// + +/* + * 㷨3.3 + * + * ԹѰ· + * + * ʹٷҵһͨ· + */ +Status MazePath(MazeType maze, PosType start, PosType end) { + SqStack S; // 洢̽ͨ + SElemType e; // e洢ǰͨϢ + PosType curPos; // ǰλ + int curStep; // ǰͨ + + // ʼ켣ջ + InitStack(&S); + + curPos = start; // 趨ǰλΪ"λ" + curStep = 1; // ̽һ + + do { + // ǰλÿͨҪλǴδ̽ͨ飩 + if(Pass(maze, curPos)) { + // ³ʼ㼣򶫷ʵı + FootPrint(maze, curPos); + + // һͨϢ + e = Construct(curStep, curPos, East); + + // · + Push(&S, e); + + // յ + if(Equals(curPos, end) == TRUE) { + printf("\nѰ·ɹ\n\n"); + return TRUE; + } + + // ȡһӦ̽λãǰλõĶ + curPos = NextPos(curPos, East); + + // ̽һ + curStep++; + + // ǰλѾ̽ˣ޸̽ + } else { + // ջΪգ̽ıҪ + if(!StackEmpty(S)) { + // ˵һλ + Pop(&S, &e); + + // ̽λõ4̽Ҫ + while(e.di == North && !StackEmpty(S)) { + // "ͬ"ǣӸλó·ûͨ· + MarkPrint(maze, e.seat, Impasse); + + // + Pop(&S, &e); + } + + // ̽λûʣ̽ķ + if(e.di < North) { + // ı̽򣬰ķѯ + ++e.di; + + // Թ·ʱǣ۲Թ״̬̲ûиò裩 + MarkPrint(maze, e.seat, e.di); + + // ½λü뵽· + Push(&S, e); + + // ȡһӦ̽λ + curPos = NextPos(e.seat, e.di); + } + } + } + + // ջΪգζŻ̽ıҪ + } while(!StackEmpty(S)); + + printf("\nѰ·ʧܣ\n\n"); + + return FALSE; +} + +/* + * ʼһģΪNNԹ + * startendֱΪԹͳ + * + *ע + * ̲޴˲òDZڵ + */ +void InitMaze(MazeType maze, PosType* start, PosType* end) { + int i, j, tmp; + + srand((unsigned) time(NULL)); // ϵͳʱ + + for(i = 0; i < M; i++) { + for(j = 0; j < N; j++) { + + // Թǽ + if(i == 0 || j == 0 || i == M - 1 || j == N - 1) { + maze[i][j] = Wall; + + // Թڲ + } else { + tmp = rand() % X; // [0, X-1]Թ + + if(tmp == 0) { + // 1/Xĸϰ + maze[i][j] = Obstacle; + } else { + // طΪɱͨ· + maze[i][j] = Way; + } + } + } + } + + // Թ + (*start).x = 1; + (*start).y = 0; + + // Թ + (*end).x = M - 2; + (*end).y = N - 1; + + // ںͳ + maze[1][0] = maze[M - 2][N - 1] = Way; + + // ΪѰ·ɹʣڴͳڴٽĽΪͨ·DZ + maze[1][1] = maze[M - 2][N - 2] = Way; + + // ʾԹijʼ״̬ + PaintMaze(maze); +} + +/* + * жϵǰλǷͨҪλǴδ̽ͨ + * + *ע + * ΪжϵǰλǷΪ״̽ + */ +Status Pass(MazeType maze, PosType seat) { + int x = seat.x; + int y = seat.y; + + // ȼǷԽ磬Խˣǰλÿ϶޷ͨ + if(x < 0 || y < 0 || x > M - 1 || y > N - 1) { + return FALSE; //Խ + } + + // ҪλñǴδ̽ͨ + if(maze[x][y] != Way) { + return FALSE; + } + + return TRUE; +} + +/* + * ȡһӦ̽λ + * diָʾǰλõ̽򣬰East, South, West, North + */ +PosType NextPos(PosType seat, int di) { + PosType tmp = seat; + + switch(di) { + case East: + tmp.y++; // + break; + case South: + tmp.x++; // + break; + case West: + tmp.y--; // + break; + case North: + tmp.x--; // + break; + } + + return tmp; +} + +/* + * ³ʼ㼣 + * + * ʼ㼣򶫷 + */ +void FootPrint(MazeType maze, PosType seat) { + //ʼ̽ + MarkPrint(maze, seat, East); +} + +/* + * Թseatmark + * + *ע + * ú̲ϵĺ + * ֻ̲ô˺"̽"ı + * ˴ĺĽΪǣ̽ı + */ +void MarkPrint(MazeType maze, PosType seat, int mark) { + int x = seat.x; + int y = seat.y; + + maze[x][y] = mark; //²ͨı + + // Թ + PaintMaze(maze); +} + +/* + * һͨϢ + * + *ע + * ̲д˲޴˺ + */ +SElemType Construct(int ord, PosType seat, int di) { + SElemType e; + + e.ord = ord; + e.seat = seat; + e.di = di; + + return e; +} + +/* + * жǷ + * + *ע + * ̲д˲޴˺ + * ΪҪȽṹ壬Բֱ"==" + */ +Status Equals(PosType seat1, PosType seat2) { + if(seat1.x == seat2.x && seat1.y == seat2.y) { + return TRUE; + } else { + return ERROR; + } +} + +/* + * Թ + * ͼεķʽԹǰ״̬ + * + *ע + * 1.̲޴˲˴ӸòĿǹ۲Ѱ·̵ÿһ + * 2.ʵCLionĿ̨ + */ +void PaintMaze(MazeType maze) { + int i, j; + + Wait(SleepTime); // ͣһ + + system("cls"); // Ļ + + for(i = 0; i < M; i++) { + for(j = 0; j < N; j++) { + if(maze[i][j] == Wall) { // ǽ + printf("~"); + } else if(maze[i][j] == Obstacle) { // Թڲϰ + printf(""); + } else if(maze[i][j] == East) { // ڳ̽ + printf(""); + } else if(maze[i][j] == South) { // ڳ̽ + printf(""); + } else if(maze[i][j] == West) { // ڳ̽ + printf(""); + } else if(maze[i][j] == North) { // ڳ̽ + printf(""); + } else if(maze[i][j] == Impasse) { // ͬĸ̽޷ͨλ + printf(""); + } else { // δ̽· + printf(""); + } + + if(j != 0 && j % (N - 1) == 0) { // ÿN㻻 + printf("\n"); + } + } + } + + printf("\n"); +} diff --git a/VisualC++/CourseBook/0304_Maze/Maze.h b/VisualC++/CourseBook/0304_Maze/Maze.h new file mode 100644 index 0000000..d18a67a --- /dev/null +++ b/VisualC++/CourseBook/0304_Maze/Maze.h @@ -0,0 +1,112 @@ +/*============== + * ԹѰ· + * + * 㷨: 3.3 + ===============*/ + +#ifndef MAZE_H +#define MAZE_H + +#include +#include // ṩsystemrandsrandԭ +#include // ṩtimeԭ +#include "Status.h" //**01 **// +#include "SqStack.h" //**03 ջͶ**// + +/* 궨 */ +#define M 15 // Թ +#define N 15 // Թ + +#define X 4 // XָʾԹϰֵĸʡ磬X=4ζűԹʱϰĸ1/4=25% + +#define SleepTime 3 //SleepTimeӡͼʱʱ + +/* ԹͶ */ +typedef enum { + Wall, // ǽ + Obstacle, // Թڲϰ + Way, // ͨ· + Impasse, // ͬ + East, South, West, North // ǰ̽򣺶 +} MazeNode; + +typedef int MazeType[M][N]; // Թ + + +/* + * 㷨3.3 + * + * ԹѰ· + * + * ʹٷҵһͨ· + */ +Status MazePath(MazeType maze, PosType start, PosType end); + +/* + * ʼһģΪNNԹ + * startendֱΪԹͳ + * + *ע + * ̲޴˲òDZڵ + */ +void InitMaze(MazeType maze, PosType* start, PosType* end); + +/* + * жϵǰλǷͨҪλǴδ̽ͨ + * + *ע + * ΪжϵǰλǷΪ״̽ + */ +Status Pass(MazeType maze, PosType seat); + +/* + * ȡһӦ̽λ + * diָʾǰλõ̽򣬰East, South, West, North + */ +PosType NextPos(PosType seat, int di); + +/* + * ³ʼ㼣 + * + * ʼ㼣򶫷 + */ +void FootPrint(MazeType maze, PosType seat); + +/* + * Թseatmark + * + *ע + * ú̲ϵĺ + * ֻ̲ô˺"̽"ı + * ˴ĺĽΪǣ̽ı + */ +void MarkPrint(MazeType maze, PosType seat, int mark); + +/* + * һͨϢ + * + *ע + * ̲д˲޴˺ + */ +SElemType Construct(int ord, PosType seat, int di); + +/* + * жǷ + * + *ע + * ̲д˲޴˺ + * ΪҪȽṹ壬Բֱ"==" + */ +Status Equals(PosType a, PosType b); + +/* + * Թ + * ͼεķʽԹǰ״̬ + * + *ע + * ̲޴˲ + * ˴ӸòĿǹ۲Ѱ·̵ÿһ + */ +void PaintMaze(MazeType maze); + +#endif diff --git a/VisualC++/CourseBook/0304_Maze/SqStack.c b/VisualC++/CourseBook/0304_Maze/SqStack.c new file mode 100644 index 0000000..7e16904 --- /dev/null +++ b/VisualC++/CourseBook/0304_Maze/SqStack.c @@ -0,0 +1,90 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#include "SqStack.h" //**03 ջͶ**// + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + (*S).base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); + } + + (*S).top = (*S).base; + (*S).stacksize = STACK_INIT_SIZE; + + return OK; +} + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S) { + if(S.top == S.base) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + // ջʱ׷Ӵ洢ռ + if((*S).top - (*S).base >= (*S).stacksize) { + (*S).base = (SElemType*) realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); // 洢ʧ + } + + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + + // ջȸֵջָ + *(S->top++) = e; + + return OK; +} + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + if((*S).top == (*S).base) { + return ERROR; + } + + // ջջָȵݼٸֵ + *e = *(--(*S).top); + + return OK; +} diff --git a/VisualC++/CourseBook/0304_Maze/SqStack.h b/VisualC++/CourseBook/0304_Maze/SqStack.h new file mode 100644 index 0000000..bbd553e --- /dev/null +++ b/VisualC++/CourseBook/0304_Maze/SqStack.h @@ -0,0 +1,69 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define STACK_INIT_SIZE 100 // ˳ջ洢ռijʼ +#define STACKINCREMENT 10 // ˳ջ洢ռķ + +// Թͨ +typedef struct { + int x; // ͨĺᡢ궨 + int y; +} PosType; + +/* ͨϢԹ㷨 */ +typedef struct { + int ord; // ͨġš + PosType seat; // ͨġλá + int di; // һӦʵġ +} SElemType; + +// ˳ջԪؽṹ +typedef struct { + SElemType* base; // ջָ + SElemType* top; // ջָ + int stacksize; // ǰѷĴ洢ռ䣬ԪΪλ +} SqStack; + + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S); + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S); + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e); + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e); + +#endif diff --git a/VisualC++/CourseBook/0305_Expression/0305_Expression.vcxproj b/VisualC++/CourseBook/0305_Expression/0305_Expression.vcxproj new file mode 100644 index 0000000..32ce6f3 --- /dev/null +++ b/VisualC++/CourseBook/0305_Expression/0305_Expression.vcxproj @@ -0,0 +1,78 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {71331A05-F03F-41E1-A7BD-D6F9D2944D8D} + My0305_Expression + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)\..\Status;$(IncludePath) + + + + Level3 + Disabled + + + true + $(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies) + Console + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0305_Expression/0305_Expression.vcxproj.filters b/VisualC++/CourseBook/0305_Expression/0305_Expression.vcxproj.filters new file mode 100644 index 0000000..115a619 --- /dev/null +++ b/VisualC++/CourseBook/0305_Expression/0305_Expression.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + 源文件 + + + 源文件 + + + + + 头文件 + + + 头文件 + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0305_Expression/0305_Expression.vcxproj.user b/VisualC++/CourseBook/0305_Expression/0305_Expression.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/CourseBook/0305_Expression/0305_Expression.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0305_Expression/Expression-main.c b/VisualC++/CourseBook/0305_Expression/Expression-main.c new file mode 100644 index 0000000..9186009 --- /dev/null +++ b/VisualC++/CourseBook/0305_Expression/Expression-main.c @@ -0,0 +1,12 @@ +#include "Expression.h" //**03 ջͶ**// + +int main(int argc, char** argv) { + char opnd; + char* exp = "(1+3)*2/4#"; + + opnd = EvaluateExpression(exp); + + printf("Ϊʾ %s ļΪ%d\n", exp, opnd - '0'); + + return 0; +} diff --git a/VisualC++/CourseBook/0305_Expression/Expression.c b/VisualC++/CourseBook/0305_Expression/Expression.c new file mode 100644 index 0000000..0bfa743 --- /dev/null +++ b/VisualC++/CourseBook/0305_Expression/Expression.c @@ -0,0 +1,139 @@ +/*============== + * ʽ + * + * 㷨: 3.4 + ===============*/ + +#include "Expression.h" //**03 ջͶ**// + +/* + * 㷨3.4 + * + * expʽʽ + * + *ע + * 1.̲ʹõǿ̨룬Ϊ˱ڲԣֱӸΪβνղ + * 2.ü㹦ޣϽֶ֧Ըλ㣬ҪÿһҲǸλ + * ̲ṩ㷨Ŀ֤ջʹãչ֧֣Բ֧֣ + * ˳Ŵ˼·иİ + */ +OperandType EvaluateExpression(const char exp[]) { + SElemType c; // + + SqStack OPTR; // ջ + SqStack OPND; // ջ + + OperatorType theta, x; // + OperandType a, b; // + + int i = 0; + + // ʼջһ޷'#'ջ + InitStack(&OPTR); + Push(&OPTR, '#'); + + // ʼջʼȡ + InitStack(&OPND); + c = exp[i++]; + + // ޷'#'ջջԪҲǽ޷'#'ʱʾȡ + while(c != '#' || GetTop(OPTR) != '#') { + // chΪջ + if(!In(c, OP)) { + Push(&OPND, c); // ջ + c = exp[i++]; // ȡһַ + } else { + switch(Precede(GetTop(OPTR), c)) { + // ջȼͣջ + case '<': + Push(&OPTR, c); + c = exp[i++]; + break; + + // ȼʱ˵ţҪ + case '=': + Pop(&OPTR, &x); + c = exp[i++]; + break; + + /* + * ջȼʱȼ㣬ٽѹջ + * + * עûжַcĻǸղŶַ + */ + case '>': + Pop(&OPTR, &theta); // + Pop(&OPND, &b); // ұߵIJ + Pop(&OPND, &a); // ߵIJ + Push(&OPND, Operate(a, theta, b)); + break; + } + } + } + + return GetTop(OPND); +} + +// жָǷϹ +Status In(SElemType c, const char OP[]) { + + SElemType* e = strchr(OP, c); + + // cںϹ淶Χڣ˵ָϹ + if(e == NULL) { + return FALSE; + } else { + return TRUE; + } +} + +/* + * жջвo1ʽеIJo2ȼ + * + * '>''<''='ָʾo1o2ȼ + */ +OperatorType Precede(OperatorType o1, OperatorType o2) { + int x, y; + + // ȡָеλ + char* p1 = strchr(OP, o1); + char* p2 = strchr(OP, o2); + + // һȼ + x = p1 - OP; + y = p2 - OP; + + return PrecedeTable[x][y]; +} + +/* + * Բ + * + * abDztheta + * ڲ֤Ըλ֧ + */ +OperandType Operate(OperandType a, OperatorType theta, OperandType b) { + int x, y, z = CHAR_MAX - 48; + + // ȴַתΪ + x = a - '0'; + y = b - '0'; + + switch(theta) { + case '+': + z = x + y; + break; + case '-': + z = x - y; + break; + case '*': + z = x * y; + break; + case '/': + z = x / y; + break; + } + + // ɺ󣬽תΪַͷ + return z + 48; +} diff --git a/VisualC++/CourseBook/0305_Expression/Expression.h b/VisualC++/CourseBook/0305_Expression/Expression.h new file mode 100644 index 0000000..0250fa6 --- /dev/null +++ b/VisualC++/CourseBook/0305_Expression/Expression.h @@ -0,0 +1,70 @@ +/*============== + * ʽ + * + * 㷨: 3.4 + ===============*/ + +#ifndef EXPRESSION_H +#define EXPRESSION_H + +#include +#include +#include +#include "SqStack.h" //**03 ջͶ**// + +typedef SElemType OperatorType; // +typedef SElemType OperandType; // + +// ʽֵķţ޷'#' +static const char OP[] = {'+', '-', '*', '/', '(', ')', '#'}; + +/* + * ȼ޷'#'OPǺӦġ + * ɲμ̲е"ȹϵ" + */ +static const char PrecedeTable[7][7] = {{'>', '>', '<', '<', '<', '>', '>'}, + {'>', '>', '<', '<', '<', '>', '>'}, + {'>', '>', '>', '>', '<', '>', '>'}, + {'>', '>', '>', '>', '<', '>', '>'}, + {'<', '<', '<', '<', '<', '=', ' '}, + {'>', '>', '>', '>', ' ', '>', '>'}, + {'<', '<', '<', '<', '<', ' ', '='}}; + + +/* + * 㷨3.4 + * + * expʽʽ + * + *ע + * 1.̲ʹõǿ̨룬Ϊ˱ڲԣֱӸΪβνղ + * 2.ü㹦ޣϽֶ֧Ըλ㣬ҪÿһҲǸλ + * ̲ṩ㷨Ŀ֤ջʹãչ֧֣Բ֧֣ + * ˳Ŵ˼·иİ + */ +OperandType EvaluateExpression(const char exp[]); + +/* + * жָǷϹ + * + * OPд洢˺Ϲ޷'#' + */ +Status In(SElemType c, const char OP[]); + +/* + * жջвo1ʽеIJo2ȼ + * + * '>''<''='ָʾo1o2ȼ + */ +OperatorType Precede(OperatorType o1, OperatorType o2); + +/* + * Բ + * + * abDztheta + * ڲֶԸλ֧ + */ +OperandType Operate(OperandType a, OperatorType theta, OperandType b); + + +#endif diff --git a/VisualC++/CourseBook/0305_Expression/SqStack.c b/VisualC++/CourseBook/0305_Expression/SqStack.c new file mode 100644 index 0000000..30e35ab --- /dev/null +++ b/VisualC++/CourseBook/0305_Expression/SqStack.c @@ -0,0 +1,111 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#include "SqStack.h" //**03 ջͶ**// + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S) { + if(S == NULL) { + return ERROR; + } + + (*S).base = (SElemType*) malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); + } + + (*S).top = (*S).base; + (*S).stacksize = STACK_INIT_SIZE; + + return OK; +} + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S) { + if(S.top == S.base) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * ȡֵ + * + * ȡջջԪء + * + *ע + * òʵ봫ͳ˳ջȡֵЩͬһ + */ +SElemType GetTop(SqStack S) { + SElemType e; + + if(S.base == NULL || S.top == S.base) { + return '\0'; + } + + // ıջԪ + e = *(S.top - 1); + + return e; +} + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + // ջʱ׷Ӵ洢ռ + if((*S).top - (*S).base >= (*S).stacksize) { + (*S).base = (SElemType*) realloc((*S).base, ((*S).stacksize + STACKINCREMENT) * sizeof(SElemType)); + if((*S).base == NULL) { + exit(OVERFLOW); // 洢ʧ + } + + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + + // ջȸֵջָ + *(S->top++) = e; + + return OK; +} + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e) { + if(S == NULL || (*S).base == NULL) { + return ERROR; + } + + if((*S).top == (*S).base) { + return ERROR; + } + + // ջջָȵݼٸֵ + *e = *(--(*S).top); + + return OK; +} diff --git a/VisualC++/CourseBook/0305_Expression/SqStack.h b/VisualC++/CourseBook/0305_Expression/SqStack.h new file mode 100644 index 0000000..74a388a --- /dev/null +++ b/VisualC++/CourseBook/0305_Expression/SqStack.h @@ -0,0 +1,69 @@ +/*============================= + * ջ˳洢ṹ˳ջ + =============================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define STACK_INIT_SIZE 100 // ˳ջ洢ռijʼ +#define STACKINCREMENT 10 // ˳ջ洢ռķ + +/* ʽԪͶ */ +typedef char SElemType; + +// ˳ջԪؽṹ +typedef struct { + SElemType* base; // ջָ + SElemType* top; // ջָ + int stacksize; // ǰѷĴ洢ռ䣬ԪΪλ +} SqStack; + + +/* + * ʼ + * + * һջʼɹ򷵻OK򷵻ERROR + */ +Status InitStack(SqStack* S); + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * TRUE : ˳ջΪ + * FALSE: ˳ջΪ + */ +Status StackEmpty(SqStack S); + +/* + * ȡֵ + * + * ȡջջԪء + * + *ע + * òʵ봫ͳ˳ջȡֵЩͬһ + */ +SElemType GetTop(SqStack S); + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e); + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e); + +#endif diff --git a/VisualC++/CourseBook/0306_Hanoi/0306_Hanoi.vcxproj b/VisualC++/CourseBook/0306_Hanoi/0306_Hanoi.vcxproj new file mode 100644 index 0000000..2c6c298 --- /dev/null +++ b/VisualC++/CourseBook/0306_Hanoi/0306_Hanoi.vcxproj @@ -0,0 +1,76 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {B92DECE4-A1D8-4A31-904B-C2441A1BB053} + My0306_Hanoi + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)\..\Status;$(IncludePath) + + + + Level3 + Disabled + + + true + $(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies) + Console + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0306_Hanoi/0306_Hanoi.vcxproj.filters b/VisualC++/CourseBook/0306_Hanoi/0306_Hanoi.vcxproj.filters new file mode 100644 index 0000000..df8e3d3 --- /dev/null +++ b/VisualC++/CourseBook/0306_Hanoi/0306_Hanoi.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + 源文件 + + + + + 头文件 + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0306_Hanoi/0306_Hanoi.vcxproj.user b/VisualC++/CourseBook/0306_Hanoi/0306_Hanoi.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/CourseBook/0306_Hanoi/0306_Hanoi.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0306_Hanoi/Hanoi-main.c b/VisualC++/CourseBook/0306_Hanoi/Hanoi-main.c new file mode 100644 index 0000000..b05e34f --- /dev/null +++ b/VisualC++/CourseBook/0306_Hanoi/Hanoi-main.c @@ -0,0 +1,15 @@ +#include "Hanoi.h" //**03 ջͶ**// + +int main(int argc, char** argv) { + char x = 'x'; + char y = 'y'; + char z = 'z'; + + printf("ΪʾԲ̸Ϊ %d ...\n", N); + + init(N); + + hanoi(N, x, y, z); + + return 0; +} diff --git a/VisualC++/CourseBook/0306_Hanoi/Hanoi.c b/VisualC++/CourseBook/0306_Hanoi/Hanoi.c new file mode 100644 index 0000000..c772b44 --- /dev/null +++ b/VisualC++/CourseBook/0306_Hanoi/Hanoi.c @@ -0,0 +1,133 @@ +/*============== + * ŵ + * + * 㷨: 3.5 + ===============*/ + +#include "Hanoi.h" //**03 ջͶ**// + +/* + * 㷨3.5 + * + * ŵ⣺yΪxǰnԲƶz + */ +void hanoi(int n, char x, char y, char z) { + if(n == 1) { // ƶnԲ̣ƶϵn-1Բ + move(x, 1, z); // Ϊ1Բ̴xƵz + } else { + hanoi(n - 1, x, z, y); // xϱΪ1n-1ԲƵyz + move(x, n, z); // ΪnԲ̴xƵz + hanoi(n - 1, y, x, z); // yϱΪ1n-1Բƶzx + } +} + +void move(char x, int n, char z) { + // stepΪȫֱmain֮ⶨ + gStep++; + printf("%2d %d Բ̴ %c Ƶ %c \n", gStep, n, x, z); + + // ŵƶͼαʾ + PrintGraph(x, n, z); +} + +/* + * ŵͼϢʼ + * + *ע + * ̲޴˲ + * Ӵ˲ĿΪ˱ڹ۲캺ŵԲ̵ƶ + */ +void init(int n) { + int i; + int* towerX, * towerY, * towerZ; + + T.plates = (int**) malloc(3 * sizeof(int*)); + + towerX = (int*) malloc(n * sizeof(int)); + towerY = (int*) malloc(n * sizeof(int)); + towerZ = (int*) malloc(n * sizeof(int)); + + for(i = 0; i < n; ++i) { + towerX[i] = n-i; + towerY[i] = 0; + towerZ[i] = 0; + } + + T.plates[0] = towerX; + T.plates[1] = towerY; + T.plates[2] = towerZ; + + T.high[0] = n; + T.high[1] = 0; + T.high[2] = 0; + + // ŵƶͼαʾ + PrintGraph('\0', 0, '\0'); +} + +/* + * ŵƶͼαʾ + * + *ע + * ̲޴˲ + * Ӵ˲ĿΪ˱ڹ۲캺ŵԲ̵ƶ + */ +void PrintGraph(char t1, int n, char t2){ + int i, j; + char** s; + + // nӴt1Ƴ + if(t1=='x') { + T.plates[0][T.high[0]-1] = 0; + T.high[0]--; + } else if(t1=='y') { + T.plates[1][T.high[1]-1] = 0; + T.high[1]--; + } else if(t1=='z') { + T.plates[2][T.high[2]-1] = 0; + T.high[2]--; + } else { + // t1ϵԲ̲Ҫƶ + } + + // nӵt2 + if(t2=='x') { + T.plates[0][T.high[0]] = n; + T.high[0]++; + } else if(t2=='y') { + T.plates[1][T.high[1]] = n; + T.high[1]++; + } else if(t2=='z') { + T.plates[2][T.high[2]] = n; + T.high[2]++; + } else { + // t2ϵԲ̲Ҫƶ + } + + s = (char**)malloc((N+2)*sizeof(char*)); + for(i = 0; i= 0; i--) { + printf("%-*s | %-*s | %-*s\n", N, s[T.plates[0][i]], N, s[T.plates[1][i]], N, s[T.plates[2][i]]); + } + printf("%-*s + %-*s + %-*s\n", N, s[N+1], N, s[N+1], N, s[N+1]); + printf("%-*s %-*s %-*s\n", N+2, "x", N+2, "y", N+2, "z"); + + printf("\n"); +} diff --git a/VisualC++/CourseBook/0306_Hanoi/Hanoi.h b/VisualC++/CourseBook/0306_Hanoi/Hanoi.h new file mode 100644 index 0000000..2f3fa97 --- /dev/null +++ b/VisualC++/CourseBook/0306_Hanoi/Hanoi.h @@ -0,0 +1,59 @@ +/*============== + * ŵ + * + * 㷨: 3.5 + ===============*/ + +#ifndef HANOI_H +#define HANOI_H + +#include +#include +#include "Status.h" + +#define N 5 // ŵ + +// ŵͼϢ +typedef struct { + int** plates; // ŵеԲϢ + int high[3]; // ĸ߶ȣе +} Tower; + +// ŵ +Tower T; + +// ͳƶ +int gStep; + + +/* + * 㷨3.5 + * + * ŵ⣺yΪxǰnԲƶz + */ +void hanoi(int n, char x, char y, char z); + +/* + * ƶŵԲ̣nԲ̴xƵz + */ +void move(char x, int n, char z); + +/* + * ŵͼϢʼ + * + *ע + * ̲޴˲ + * Ӵ˲ĿΪ˱ڹ۲캺ŵԲ̵ƶ + */ +void init(int n); + +/* + * ŵƶͼαʾμmove() + * + *ע + * ̲޴˲ + * Ӵ˲ĿΪ˱ڹ۲캺ŵԲ̵ƶ + */ +void PrintGraph(char x, int n, char z); + +#endif diff --git a/VisualC++/CourseBook/0307_LinkQueue/0307_LinkQueue.vcxproj b/VisualC++/CourseBook/0307_LinkQueue/0307_LinkQueue.vcxproj new file mode 100644 index 0000000..011e09f --- /dev/null +++ b/VisualC++/CourseBook/0307_LinkQueue/0307_LinkQueue.vcxproj @@ -0,0 +1,76 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {9ECA0672-0E13-4324-B2EA-1A8153348085} + My0307_LinkQueue + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)\..\Status;$(IncludePath) + + + + Level3 + Disabled + + + true + $(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies) + Console + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0307_LinkQueue/0307_LinkQueue.vcxproj.filters b/VisualC++/CourseBook/0307_LinkQueue/0307_LinkQueue.vcxproj.filters new file mode 100644 index 0000000..b4e2036 --- /dev/null +++ b/VisualC++/CourseBook/0307_LinkQueue/0307_LinkQueue.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + 源文件 + + + + + 头文件 + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0307_LinkQueue/0307_LinkQueue.vcxproj.user b/VisualC++/CourseBook/0307_LinkQueue/0307_LinkQueue.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/CourseBook/0307_LinkQueue/0307_LinkQueue.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0307_LinkQueue/LinkQueue-main.c b/VisualC++/CourseBook/0307_LinkQueue/LinkQueue-main.c new file mode 100644 index 0000000..1e71482 --- /dev/null +++ b/VisualC++/CourseBook/0307_LinkQueue/LinkQueue-main.c @@ -0,0 +1,94 @@ +#include +#include "LinkQueue.h" //**03 ջͶ**// + +// Ժӡ +void PrintElem(QElemType e); + +int main(int argc, char** argv) { + LinkQueue Q; + int i; + QElemType e; + + printf(" InitQueue \n"); + { + printf(" ʼ Q ...\n"); + InitQueue(&Q); + } + PressEnterToContinue(); + + printf(" QueueEmpty \n"); + { + QueueEmpty(Q) ? printf(" Q Ϊգ\n") : printf(" Q Ϊգ\n"); + } + PressEnterToContinue(); + + printf(" EnQueue \n"); + { + for(i = 1; i <= 6; i++) { + EnQueue(&Q, 2 * i); + printf(" Ԫ \"%2d\" ...\n", 2 * i); + } + } + PressEnterToContinue(); + + printf(" QueueTraverse \n"); + { + printf(" Q еԪΪQ = "); + QueueTraverse(Q, PrintElem); + } + PressEnterToContinue(); + + printf(" QueueLength \n"); + { + i = QueueLength(Q); + printf(" Q ijΪ %d \n", i); + } + PressEnterToContinue(); + + printf(" DeQueue \n"); + { + DeQueue(&Q, &e); + printf(" ͷԪ \"%d\" ...\n", e); + printf(" Q еԪΪQ = "); + QueueTraverse(Q, PrintElem); + } + PressEnterToContinue(); + + printf(" GetHead \n"); + { + GetHead(Q, &e); + printf(" ͷԪصֵΪ \"%d\" \n", e); + } + PressEnterToContinue(); + + printf(" ClearQueue \n"); + { + printf(" Q ǰ"); + QueueEmpty(Q) ? printf(" Q Ϊգ\n") : printf(" Q Ϊգ\n"); + + ClearQueue(&Q); + + printf(" Q "); + QueueEmpty(Q) ? printf(" Q Ϊգ\n") : printf(" Q Ϊգ\n"); + } + PressEnterToContinue(); + + printf(" DestroyQueue \n"); + { + printf(" Q ǰ"); + Q.front != NULL && Q.rear != NULL ? printf(" Q ڣ\n") : printf(" Q ڣ\n"); + + DestroyQueue(&Q); + + printf(" Q "); + Q.front != NULL && Q.rear != NULL ? printf(" Q ڣ\n") : printf(" Q ڣ\n"); + } + PressEnterToContinue(); + + return 0; +} + +// Ժӡ +void PrintElem(QElemType e) { + printf("%d ", e); +} diff --git a/VisualC++/CourseBook/0307_LinkQueue/LinkQueue.c b/VisualC++/CourseBook/0307_LinkQueue/LinkQueue.c new file mode 100644 index 0000000..ba578f3 --- /dev/null +++ b/VisualC++/CourseBook/0307_LinkQueue/LinkQueue.c @@ -0,0 +1,204 @@ +/*========================= + * еʽ洢ṹӣ + ==========================*/ + +#ifndef LINKQUEUE_C +#define LINKQUEUE_C + +#include "LinkQueue.h" //**03 ջͶ**// + +/* + * ʼ + * + * һյӡ + * ʼɹ򷵻OK򷵻ERROR + * + *ע + * Ķдͷ + */ +Status InitQueue(LinkQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).front = (*Q).rear = (QueuePtr) malloc(sizeof(QNode)); + if(!(*Q).front) { + exit(OVERFLOW); + } + + (*Q).front->next = NULL; + + return OK; +} + +/* + * (ṹ) + * + * ͷռڴ档 + */ +Status DestroyQueue(LinkQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + while((*Q).front) { + (*Q).rear = (*Q).front->next; + free((*Q).front); + (*Q).front = (*Q).rear; + } + + return OK; +} + +/* + * ÿ() + * + * Ҫͷзͷ㴦Ŀռ䡣 + */ +Status ClearQueue(LinkQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).rear = (*Q).front->next; + + while((*Q).rear) { + (*Q).front->next = (*Q).rear->next; + free((*Q).rear); + (*Q).rear = (*Q).front->next; + } + + (*Q).rear = (*Q).front; + + return OK; +} + +/* + * п + * + * жǷЧݡ + * + * ֵ + * TRUE : Ϊ + * FALSE: ӲΪ + */ +Status QueueEmpty(LinkQueue Q) { + if(Q.front == Q.rear) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * + * + * ӰЧԪص + */ +int QueueLength(LinkQueue Q) { + int count = 0; + QueuePtr p = Q.front; + + while(p != Q.rear) { + count++; + p = p->next; + } + + return count; +} + +/* + * ȡֵ + * + * ȡͷԪأ洢eС + * ҵOK򣬷ERROR + */ +Status GetHead(LinkQueue Q, QElemType* e) { + QueuePtr p; + + if(Q.front == NULL || Q.front == Q.rear) { + return ERROR; + } + + p = Q.front->next; + *e = p->data; + + return OK; +} + +/* + * + * + * Ԫeӵβ + */ +Status EnQueue(LinkQueue* Q, QElemType e) { + QueuePtr p; + + if(Q == NULL || (*Q).front == NULL) { + return ERROR; + } + + p = (QueuePtr) malloc(sizeof(QNode)); + if(!p) { + exit(OVERFLOW); + } + + p->data = e; + p->next = NULL; + + (*Q).rear->next = p; + (*Q).rear = p; + + return OK; +} + +/* + * + * + * ƳͷԪأ洢eС + */ +Status DeQueue(LinkQueue* Q, QElemType* e) { + QueuePtr p; + + if(Q == NULL || (*Q).front == NULL || (*Q).front == (*Q).rear) { + return ERROR; + } + + p = (*Q).front->next; + *e = p->data; + + (*Q).front->next = p->next; + if((*Q).rear == p) { + (*Q).rear = (*Q).front; + } + + free(p); + + return OK; +} + +/* + * + * + * visitʶQ + */ +Status QueueTraverse(LinkQueue Q, void (Visit)(QElemType)) { + QueuePtr p; + + if(Q.front == NULL) { + return ERROR; + } + + p = Q.front->next; + + while(p != NULL) { + Visit(p->data); + p = p->next; + } + + printf("\n"); + + return OK; +} + +#endif diff --git a/VisualC++/CourseBook/0307_LinkQueue/LinkQueue.h b/VisualC++/CourseBook/0307_LinkQueue/LinkQueue.h new file mode 100644 index 0000000..42cc95b --- /dev/null +++ b/VisualC++/CourseBook/0307_LinkQueue/LinkQueue.h @@ -0,0 +1,100 @@ +/*========================= + * еʽ洢ṹӣ + ==========================*/ + +#ifndef LINKQUEUE_H +#define LINKQUEUE_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* ԪͶ */ +typedef int QElemType; + +// Ԫؽṹ +typedef struct QNode { + QElemType data; + struct QNode* next; +} QNode, * QueuePtr; + +// нṹ +typedef struct { + QueuePtr front; // ͷָ + QueuePtr rear; // βָ +} LinkQueue; // еʽ洢ʾ + + +/* + * ʼ + * + * һյӡ + * ʼɹ򷵻OK򷵻ERROR + * + *ע + * Ķдͷ + */ +Status InitQueue(LinkQueue* Q); + +/* + * (ṹ) + * + * ͷռڴ档 + */ +Status DestroyQueue(LinkQueue* Q); + +/* + * ÿ() + * + * Ҫͷзͷ㴦Ŀռ䡣 + */ +Status ClearQueue(LinkQueue* Q); + +/* + * п + * + * жǷЧݡ + * + * ֵ + * TRUE : Ϊ + * FALSE: ӲΪ + */ +Status QueueEmpty(LinkQueue Q); + +/* + * + * + * ӰЧԪص + */ +int QueueLength(LinkQueue Q); + +/* + * ȡֵ + * + * ȡͷԪأ洢eС + * ҵOK򣬷ERROR + */ +Status GetHead(LinkQueue Q, QElemType* e); + +/* + * + * + * Ԫeӵβ + */ +Status EnQueue(LinkQueue* Q, QElemType e); + +/* + * + * + * ƳͷԪأ洢eС + */ +Status DeQueue(LinkQueue* Q, QElemType* e); + +/* + * + * + * visitʶQ + */ +Status QueueTraverse(LinkQueue Q, void(Visit)(QElemType)); + +#endif diff --git a/VisualC++/CourseBook/0308_SqQueue/0308_SqQueue.vcxproj b/VisualC++/CourseBook/0308_SqQueue/0308_SqQueue.vcxproj new file mode 100644 index 0000000..e5dde67 --- /dev/null +++ b/VisualC++/CourseBook/0308_SqQueue/0308_SqQueue.vcxproj @@ -0,0 +1,76 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {28EB7738-3109-4B98-B312-1B3D1DD91DDF} + My0308_SqQueue + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)\..\Status;$(IncludePath) + + + + Level3 + Disabled + + + true + $(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies) + Console + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0308_SqQueue/0308_SqQueue.vcxproj.filters b/VisualC++/CourseBook/0308_SqQueue/0308_SqQueue.vcxproj.filters new file mode 100644 index 0000000..0b09061 --- /dev/null +++ b/VisualC++/CourseBook/0308_SqQueue/0308_SqQueue.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + 源文件 + + + + + 头文件 + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0308_SqQueue/0308_SqQueue.vcxproj.user b/VisualC++/CourseBook/0308_SqQueue/0308_SqQueue.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/CourseBook/0308_SqQueue/0308_SqQueue.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0308_SqQueue/SqQueue-main.c b/VisualC++/CourseBook/0308_SqQueue/SqQueue-main.c new file mode 100644 index 0000000..c4a59d3 --- /dev/null +++ b/VisualC++/CourseBook/0308_SqQueue/SqQueue-main.c @@ -0,0 +1,94 @@ +#include +#include "SqQueue.h" //**03 ջͶ**// + +// Ժӡ +void PrintElem(QElemType e); + +int main(int argc, char** argv) { + SqQueue Q; + int i; + QElemType e; + + printf(" InitQueue ...\n"); + { + printf(" ʼѭ˳ Q ...\n"); + InitQueue(&Q); + } + PressEnterToContinue(); + + printf(" QueueEmpty ...\n"); + { + QueueEmpty(Q) ? printf(" Q Ϊգ\n") : printf(" Q Ϊգ\n"); + } + PressEnterToContinue(); + + printf(" EnQueue ...\n"); + { + for(i = 1; i <= 6; i++) { + EnQueue(&Q, 2 * i); + printf(" Ԫ \"%2d\" Q...\n", 2 * i); + } + } + PressEnterToContinue(); + + printf(" QueueTraverse ...\n"); + { + printf(" Q еԪΪQ = "); + QueueTraverse(Q, PrintElem); + } + PressEnterToContinue(); + + printf(" QueueLength ...\n"); + { + i = QueueLength(Q); + printf(" Q ijΪ %d \n", i); + } + PressEnterToContinue(); + + printf(" DeQueue ...\n"); + { + DeQueue(&Q, &e); + printf(" ͷԪ \"%d\" ...\n", e); + printf(" Q еԪΪQ = "); + QueueTraverse(Q, PrintElem); + } + PressEnterToContinue(); + + printf(" GetHead ...\n"); + { + GetHead(Q, &e); + printf(" ͷԪصֵΪ \"%d\" \n", e); + } + PressEnterToContinue(); + + printf(" ClearQueue ...\n"); + { + printf(" Q ǰ"); + QueueEmpty(Q) ? printf(" Q Ϊգ\n") : printf(" Q Ϊգ\n"); + + ClearQueue(&Q); + + printf(" Q "); + QueueEmpty(Q) ? printf(" Q Ϊգ\n") : printf(" Q Ϊգ\n"); + } + PressEnterToContinue(); + + printf(" DestroyQueue ...\n"); + { + printf(" Q ǰ"); + Q.base != NULL ? printf(" Q ڣ\n") : printf(" Q ڣ\n"); + + DestroyQueue(&Q); + + printf(" Q "); + Q.base != NULL ? printf(" Q ڣ\n") : printf(" Q ڣ\n"); + } + PressEnterToContinue(); + + return 0; +} + +// Ժӡ +void PrintElem(QElemType e) { + printf("%d ", e); +} diff --git a/VisualC++/CourseBook/0308_SqQueue/SqQueue.c b/VisualC++/CourseBook/0308_SqQueue/SqQueue.c new file mode 100644 index 0000000..6079495 --- /dev/null +++ b/VisualC++/CourseBook/0308_SqQueue/SqQueue.c @@ -0,0 +1,182 @@ +/*============================= + * е˳洢ṹ˳У + ==============================*/ + +#include "SqQueue.h" //**03 ջͶ**// + +/* + * ʼ + * + * һյ˳С + * ʼɹ򷵻OK򷵻ERROR + * + *ע + * Ķѭ + */ +Status InitQueue(SqQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).base = (QElemType*) malloc(MAXQSIZE * sizeof(QElemType)); + if(!(*Q).base) { + exit(OVERFLOW); + } + + (*Q).front = (*Q).rear = 0; + + return OK; +} + +/* + * (ṹ) + * + * ͷѭ˳ռڴ档 + */ +Status DestroyQueue(SqQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + if((*Q).base) { + free((*Q).base); + } + + (*Q).base = NULL; + (*Q).front = (*Q).rear = 0; + + return ERROR; +} + +/* + * ÿ() + * + * ֻѭ˳д洢ݣͷ˳ջռڴ档 + */ +Status ClearQueue(SqQueue* Q) { + if(Q == NULL || (*Q).base == NULL) { + return ERROR; + } + + (*Q).front = (*Q).rear = 0; + + return OK; +} + +/* + * п + * + * жѭ˳ǷЧݡ + * + * ֵ + * TRUE : ѭ˳Ϊ + * FALSE: ѭ˳вΪ + */ +Status QueueEmpty(SqQueue Q) { + // пյı־ + if(Q.front == Q.rear) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * + * + * ѭ˳аЧԪص + */ +int QueueLength(SqQueue Q) { + if(Q.base == NULL) { + return 0; + } + + // г + return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE; +} + +/* + * ȡֵ + * + * ȡͷԪأ洢eС + * ҵOK򣬷ERROR + */ +Status GetHead(SqQueue Q, QElemType* e) { + // пյı־ + if(Q.base == NULL || Q.front == Q.rear) { + return ERROR; + } + + *e = Q.base[Q.front]; + + return OK; +} + +/* + * + * + * Ԫeӵβ + */ +Status EnQueue(SqQueue* Q, QElemType e) { + if(Q == NULL || (*Q).base == NULL) { + return ERROR; + } + + // ı־˷һռֶпպͶ + if(((*Q).rear + 1) % MAXQSIZE == (*Q).front) { + return ERROR; + } + + // + (*Q).base[(*Q).rear] = e; + + // βָǰ + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + + return OK; +} + +/* + * + * + * ƳͷԪأ洢eС + */ +Status DeQueue(SqQueue* Q, QElemType* e) { + if(Q == NULL || (*Q).base == NULL) { + return ERROR; + } + + // пյı־ + if((*Q).front == (*Q).rear) { + return ERROR; + } + + // + *e = (*Q).base[(*Q).front]; + + // ͷָǰ + (*Q).front = ((*Q).front + 1) % MAXQSIZE; + + return OK; +} + +/* + * + * + * visitʶQ + */ +Status QueueTraverse(SqQueue Q, void(Visit)(QElemType)) { + int i; + + if(Q.base == NULL) { + return ERROR; + } + + for(i = Q.front; i != Q.rear; i = (i + 1) % MAXQSIZE) { + Visit(Q.base[i]); + } + + printf("\n"); + + return OK; +} diff --git a/VisualC++/CourseBook/0308_SqQueue/SqQueue.h b/VisualC++/CourseBook/0308_SqQueue/SqQueue.h new file mode 100644 index 0000000..18c3ba7 --- /dev/null +++ b/VisualC++/CourseBook/0308_SqQueue/SqQueue.h @@ -0,0 +1,98 @@ +/*============================= + * е˳洢ṹ˳У + ==============================*/ + +#ifndef SQQUEUE_H +#define SQQUEUE_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define MAXQSIZE 1000 //г + +/* ѭԪͶ */ +typedef int QElemType; + +// ѭе˳洢ṹ +typedef struct { + QElemType* base; // ̬洢ռ + int front; // ͷָ룬вգָͷԪ + int rear; // βָ룬вգָβԪصһλ +} SqQueue; + + +/* + * ʼ + * + * һյ˳С + * ʼɹ򷵻OK򷵻ERROR + * + *ע + * Ķѭ + */ +Status InitQueue(SqQueue* Q); + +/* + * (ṹ) + * + * ͷѭ˳ռڴ档 + */ +Status ClearQueue(SqQueue* Q); + +/* + * ÿ() + * + * ֻѭ˳д洢ݣͷ˳ջռڴ档 + */ +Status DestroyQueue(SqQueue* Q); + +/* + * п + * + * жѭ˳ǷЧݡ + * + * ֵ + * TRUE : ѭ˳Ϊ + * FALSE: ѭ˳вΪ + */ +Status QueueEmpty(SqQueue Q); + +/* + * + * + * ѭ˳аЧԪص + */ +int QueueLength(SqQueue Q); + +/* + * ȡֵ + * + * ȡͷԪأ洢eС + * ҵOK򣬷ERROR + */ +Status GetHead(SqQueue Q, QElemType* e); + +/* + * + * + * Ԫeӵβ + */ +Status EnQueue(SqQueue* Q, QElemType e); + +/* + * + * + * ƳͷԪأ洢eС + */ +Status DeQueue(SqQueue* Q, QElemType* e); + +/* + * + * + * visitʶQ + */ +Status QueueTraverse(SqQueue Q, void(Visit)(QElemType)); + +#endif diff --git a/VisualC++/CourseBook/0309_BankQueuing/0309_BankQueuing.vcxproj b/VisualC++/CourseBook/0309_BankQueuing/0309_BankQueuing.vcxproj new file mode 100644 index 0000000..1848bd5 --- /dev/null +++ b/VisualC++/CourseBook/0309_BankQueuing/0309_BankQueuing.vcxproj @@ -0,0 +1,80 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {C44F604C-F8CD-498E-A3EF-04FCF37AA303} + My0309_BankQueuing + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)\..\Status;$(IncludePath) + + + + Level3 + Disabled + + + true + $(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies) + Console + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0309_BankQueuing/0309_BankQueuing.vcxproj.filters b/VisualC++/CourseBook/0309_BankQueuing/0309_BankQueuing.vcxproj.filters new file mode 100644 index 0000000..dcd279b --- /dev/null +++ b/VisualC++/CourseBook/0309_BankQueuing/0309_BankQueuing.vcxproj.filters @@ -0,0 +1,42 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + 源文件 + + + 源文件 + + + 源文件 + + + + + 头文件 + + + 头文件 + + + 头文件 + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0309_BankQueuing/0309_BankQueuing.vcxproj.user b/VisualC++/CourseBook/0309_BankQueuing/0309_BankQueuing.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/CourseBook/0309_BankQueuing/0309_BankQueuing.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/CourseBook/0309_BankQueuing/BankQueuing-main.c b/VisualC++/CourseBook/0309_BankQueuing/BankQueuing-main.c new file mode 100644 index 0000000..7e74c26 --- /dev/null +++ b/VisualC++/CourseBook/0309_BankQueuing/BankQueuing-main.c @@ -0,0 +1,10 @@ +#include "BankQueuing.h" //**03 ջͶ**// + +int main(int argc, char** argv) { + + Bank_Simulation_1(); //㷨3.6 + +// Bank_Simulation_2(); //㷨3.73.6 + + return 0; +} diff --git a/VisualC++/CourseBook/0309_BankQueuing/BankQueuing.c b/VisualC++/CourseBook/0309_BankQueuing/BankQueuing.c new file mode 100644 index 0000000..143402d --- /dev/null +++ b/VisualC++/CourseBook/0309_BankQueuing/BankQueuing.c @@ -0,0 +1,338 @@ +/*================== + * ģŶ + * + * 㷨: 3.63.7 + ===================*/ + +#ifndef BANKQUEUING_C +#define BANKQUEUING_C + +#include "BankQueuing.h" //**03 ջͶ**// + +/* + * 㷨3.6 + * + * ҵģ⣬ͳһڿͻжƽʱ + */ +void Bank_Simulation_1() +{ + char eventType; + + OpenForDay(); // п + + // ¼Ϊ + while(MoreEvent()) { + EventDrived(&eventType); // ¼ + + switch(eventType) { + case 'A': + CustomerArrived(); // ͻ¼ + break; + case 'D': + CustomerDeparture(); // ͻ뿪¼ + break; + default : + Invalid(); + } + } + + CloseForDay(); +} + +/* + * 㷨3.7 + * + * ҵģ⣬ͳһڿͻжƽʱ + * 㷨3.7Ƶ + */ +void Bank_Simulation_2() { + OpenForDay(); // п + + while(!ListEmpty(gEv)) { + ListDelete(gEv, 1, &gEn); + + if(gEn.NType == Arrive) { + CustomerArrived(); // ͻ¼ + } else { + CustomerDeparture(); // ͻ뿪¼ + } + } + + CloseForDay(); // й +} + +/* + * пţʼл + */ +void OpenForDay() { + int i; + + // ʱ,ÿӪҵ8Сʱ480 + gCloseTime = 480; + + // ʼۼʱͿͻΪ0 + gTotalTime = 0; + gCustomerNum = 0; + + // ʼ¼Ϊձ + InitList(&gEv); + + // 趨һͻ¼ + gEn.OccurTime = 0; + gEn.NType = Arrive; + + // ¼ + OrderInsert(gEv, gEn, cmp); + + // ʼ4ն + for(i = 1; i <= N; ++i) { + InitQueue(&gQ[i]); + } + + Show(); +} + +/* + * й + * + * ͷԴӡͳϢ + */ +void CloseForDay() { + printf("ܹ%dͻƽʱΪ%dӡ\n", gCustomerNum, gTotalTime / gCustomerNum); +} + +/* + * ж¼ǷΪա + * Ƿδ¼ + */ +Status MoreEvent() { + return !ListEmpty(gEv); +} + +/* + * ¼¼Ƴ¼洢ȫֱgEnС + * event洢¼ + */ +void EventDrived(char* eventType) { + // ¼лȡ¼ + ListDelete(gEv, 1, &gEn); + + // ʶ¼ + if(gEn.NType == Arrive) { + *eventType = 'A'; + } else { + *eventType = 'D'; + } +} + +/* + * ͻ¼gEn.NType=0 + */ +void CustomerArrived() { + Event en; // ¼ + QElemType customer; // ͻ¼ + + int durtime; // ǰͻҵҪʱ + + int intertime; // һͻﵽʱ + int t; // һͻʱ + + int i; // б + + // ܿͻһ + ++gCustomerNum; + + // ɵǰͻҵҪʱһͻﵽʱ + Random(&durtime, &intertime); + + // һͻʱ + t = gEn.OccurTime + intertime; + + // δţһͻ""¼¼ + if(t < gCloseTime) { + en.OccurTime = t; // һͻĵʱ + en.NType = Arrive; // ""¼ + OrderInsert(gEv, en, cmp); // ""¼¼ + } + + // ȡǰ̵Ķб + i = Minimum(); + + // ¼ǰͻϢ + customer.ArrivedTime = gEn.OccurTime; // ʱ + customer.Duration = durtime; // ҵʱ + customer.Count = gCustomerNum; // ͻ + + // ǰͻ̶Ŷ + EnQueue(&gQ[i], customer); + printf("%3dͻ %d Ŷ...\n", customer.Count, i); + Show(); + + /* + * ǰֻһͻŶӣҪ뿪ʱ䣬 + * һ"뿪"¼뵽¼ + */ + if(QueueLength(gQ[i]) == 1) { + en.OccurTime = gEn.OccurTime + durtime; // ǰͻ뿪ʱ + en.NType = i; // "뿪"¼ֵͣΪ1-4ָʾӵڼ뿪 + OrderInsert(gEv, en, cmp); // "뿪"¼¼ + } +} + +/* + * ͻ뿪¼gEn.NType>0 + */ +void CustomerDeparture() { + Event en; // ¼ + QElemType customer; // ͻ¼ + int i = gEn.NType; // б + + // iеĶͷͻҵ񲢳 + DeQueue(&gQ[i], &customer); + printf("%3dͻӶ %d 뿪...\n", customer.Count, i); + Show(); + + // ۼƿͻʱ + gTotalTime += gEn.OccurTime - customer.ArrivedTime; + + /* + * ǰȻŶӵĿͻҪöжͷͻ뿪ʱ + * ע֮㣬Ϊֻһͻ뿪ˣһͻ뿪ʱŻ + */ + if(!QueueEmpty(gQ[i])) { + // ȡͷͻ + GetHead(gQ[i], &customer); + en.OccurTime = gEn.OccurTime + customer.Duration; // "뿪"¼ʱ + en.NType = i; // "뿪"¼ + OrderInsert(gEv, en, cmp); // "뿪"¼¼ + } +} + +/* + * Ч¼ + */ +void Invalid() { + printf("д"); + exit(OVERFLOW); +} + +/* + * ¼en뵽¼evУevǰʱ絽е¼ + * cmpȽ¼enΪڶʵδȥ + */ +Status OrderInsert(EventList ev, Event en, int(cmp)(Event, Event)) { + EventList p, pre, s; + + if(ev == NULL) { + return ERROR; + } + + for(pre = ev; pre->next != NULL && cmp(pre->next->data, en) < 0; pre = pre->next) { + // + } + + s = (LinkList) malloc(sizeof(LNode)); + if(s == NULL) { + exit(OVERFLOW); + } + s->data = en; + + s->next = pre->next; + pre->next = s; + + return OK; +} + +/* + * Ƚ¼ + */ +int cmp(Event a, Event b) { + if(a.OccurTime < b.OccurTime) { + return -1; // aȽ + } else if(a.OccurTime > b.OccurTime) { + return 1; // aȽ + } else { + return 0; // ͬʱ + } +} + +/* + * + * + * durtime ǰͷҵʱ + * intertimeһͻʱ + */ +void Random(int* durtime, int* intertime) { + srand((unsigned) time(NULL)); + *durtime = rand() % DurationTime + 1; // ҵʱ120 + *intertime = rand() % IntervalTime + 1; // һ˿͵ʱΪ110 +} + +/* + * س̵Ķе + */ +int Minimum() { + int i1 = QueueLength(gQ[1]); + int i2 = QueueLength(gQ[2]); + int i3 = QueueLength(gQ[3]); + int i4 = QueueLength(gQ[4]); + + if(i1 <= i2 && i1 <= i3 && i1 <= i4) { + return 1; + } + + if(i2 < i1 && i2 <= i3 && i2 <= i4) { + return 2; + } + + if(i3 < i1 && i3 < i2 && i3 <= i4) { + return 3; + } + + if(i4 < i1 && i4 < i2 && i4 < i3) { + return 4; + } + + return 0; +} + +/* + * ʾпͻеŶ + */ +void Show() { + int i; + QueuePtr p; // ¼Ŀͻǵڼ + + // пͻ + for(i = 1; i <= N; i++) { + for(p = gQ[i].front; p; p = p->next) { + if(p == gQ[i].front) { + if(i == 1) { + printf("̨١"); + } + if(i == 2) { + printf("̨ڡ"); + } + if(i == 3) { + printf("̨ۡ"); + } + if(i == 4) { + printf("̨ܡ"); + } + } else { + printf("%03d", p->data.Count); + } + + + if(p == gQ[i].rear) { + printf("\n"); + } + } + } + + printf("\n"); + + Wait(SleepTime); +} + +#endif diff --git a/VisualC++/CourseBook/0309_BankQueuing/BankQueuing.h b/VisualC++/CourseBook/0309_BankQueuing/BankQueuing.h new file mode 100644 index 0000000..8d27560 --- /dev/null +++ b/VisualC++/CourseBook/0309_BankQueuing/BankQueuing.h @@ -0,0 +1,121 @@ +/*================== + * ģŶ + * + * 㷨: 3.63.7 + ===================*/ + +#ifndef BANKQUEUING_H +#define BANKQUEUING_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include // ṩtimeԭ +#include "Status.h" //**01 **// +#include "LinkList.h" //**02 Ա**// +#include "LinkQueue.h" //**03 ջͶ**// + +/* 궨 */ +#define N 4 // ͻ +#define SleepTime 1 // SleepTimeʱ +#define DurationTime 20 // ҵʱ1DurationTimeӲ +#define IntervalTime 8 // һͻʱΪ1IntervalTimeӲ + +/* Ͷ */ +typedef LinkList EventList; //¼ͣΪ + +/* ȫֱǰ涼gǣ */ +int gTotalTime; // ۼƿͻ +int gCustomerNum; // ۼƿͻʱ + +int gCloseTime; // ʱ,ÿӪҵ8Сʱ480 + +EventList gEv; // ¼洢д¼ +Event gEn; // ǰڴ¼ + +LinkQueue gQ[N+1]; // 4ͻ,0ŵԪ + + +/* + * 㷨3.6 + * + * ҵģ⣬ͳһڿͻжƽʱ + */ +void Bank_Simulation_1(); + +/* + * 㷨3.7 + * + * ҵģ⣬ͳһڿͻжƽʱ + * 㷨3.7Ƶ + */ +void Bank_Simulation_2(); + +/* + * пţʼл + */ +void OpenForDay(); + +/* + * й + * + * ͷԴӡͳϢ + */ +void CloseForDay(); + +/* + * ж¼ǷΪա + * Ƿδ¼ + */ +Status MoreEvent(); + +/* + * ¼¼Ƴ¼洢ȫֱgEnС + * event洢¼ + */ +void EventDrived(char* event); + +/* + * ͻ¼gEn.NType=0 + */ +void CustomerArrived(); + +/* + * ͻ뿪¼gEn.NType>0 + */ +void CustomerDeparture(); + +/* + * Ч¼ + */ +void Invalid(); + +/* + * ¼en뵽¼evУevǰʱ絽е¼ + * cmpȽ¼enΪڶʵδȥ + */ +Status OrderInsert(EventList gEv, Event gEn, int(cmp)(Event, Event)); + +/* + * Ƚ¼ + */ +int cmp(Event a, Event b); + +/* + * + * + * durtime ǰͷҵʱ + * intertimeһͻʱ + */ +void Random(int* durtime, int* intertime); + +/* + * س̵Ķе + */ +int Minimum(); + +/* + * ʾпͻеŶ + */ +void Show(); + +#endif diff --git a/VisualC++/CourseBook/0309_BankQueuing/LinkList.c b/VisualC++/CourseBook/0309_BankQueuing/LinkList.c new file mode 100644 index 0000000..ecc119c --- /dev/null +++ b/VisualC++/CourseBook/0309_BankQueuing/LinkList.c @@ -0,0 +1,130 @@ +/*=============================== + * Աʽ洢ṹ + * + * 㷨: 2.82.92.102.11 + ================================*/ + +#include "LinkList.h" //**02 Ա**// + +/* + * ʼ + * + * ֻdzʼһͷ㡣 + * ʼɹ򷵻OK򷵻ERROR + */ +Status InitList(LinkList* L) { + (*L) = (LinkList) malloc(sizeof(LNode)); + if(*L == NULL) { + exit(OVERFLOW); + } + + (*L)->next = NULL; + + return OK; +} + +/* + * п + * + * жǷЧݡ + * + * ֵ + * TRUE : Ϊ + * FALSE: Ϊ + */ +Status ListEmpty(LinkList L) { + // ֻͷʱΪΪ + if(L != NULL && L->next == NULL) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * 㷨2.9 + * + * + * + * iλϲeɹ򷵻OK򷵻ERROR + * + *ע + * ̲iĺԪλã1ʼ + */ +Status ListInsert(LinkList L, int i, ElemType e) { + LinkList p, s; + int j; + + // ȷ + if(L == NULL) { + return ERROR; + } + + p = L; + j = 0; + + // Ѱҵi-1㣬ұ֤ý㱾ΪNULL + while(p != NULL && j < i - 1) { + p = p->next; + ++j; + } + + // ͷˣiֵϹ(i<=0)˵ûҵϺĿĽ + if(p == NULL || j > i - 1) { + return ERROR; + } + + // ½ + s = (LinkList) malloc(sizeof(LNode)); + if(s == NULL) { + exit(OVERFLOW); + } + s->data = e; + s->next = p->next; + p->next = s; + + return OK; +} + +/* + * 㷨2.10 + * + * ɾ + * + * ɾiλϵԪأɾԪش洢eС + * ɾɹ򷵻OK򷵻ERROR + * + *ע + * ̲iĺԪλã1ʼ + */ +Status ListDelete(LinkList L, int i, ElemType* e) { + LinkList p, q; + int j; + + // ȷҲΪձ + if(L == NULL || L->next == NULL) { + return ERROR; + } + + p = L; + j = 0; + + // Ѱҵi-1㣬ұ֤ýĺ̲ΪNULL + while(p->next != NULL && j < i - 1) { + p = p->next; + ++j; + } + + // ͷˣiֵϹ(i<=0)˵ûҵϺĿĽ + if(p->next == NULL || j > i - 1) { + return ERROR; + } + + // ɾi + q = p->next; + p->next = q->next; + *e = q->data; + free(q); + + return OK; +} diff --git a/VisualC++/CourseBook/0309_BankQueuing/LinkList.h b/VisualC++/CourseBook/0309_BankQueuing/LinkList.h new file mode 100644 index 0000000..53bc85f --- /dev/null +++ b/VisualC++/CourseBook/0309_BankQueuing/LinkList.h @@ -0,0 +1,83 @@ +/*=============================== + * Աʽ洢ṹ + * + * 㷨: 2.82.92.102.11 + ================================*/ + +#ifndef LINKLIST_H +#define LINKLIST_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +// ¼öٳ0¼14ʾĸڵ뿪¼ +typedef enum { + Arrive, Leave_1, Leave_2, Leave_3, Leave_4 +} EventType; + +/* ¼ԪͶ */ +typedef struct +{ + int OccurTime; // ¼ʱ + EventType NType; // ¼ +} Event, ElemType; // ¼Ԫ + +/* + * ṹ + * + * עĵͷ + */ +typedef struct LNode { + ElemType data; // ݽ + struct LNode* next; // ָһָ +} LNode; + +// ָָ +typedef LNode* LinkList; + + +/* + * ʼ + * + * ʼɹ򷵻OK򷵻ERROR + */ +Status InitList(LinkList* L); + +/* + * п + * + * жǷЧݡ + * + * ֵ + * TRUE : Ϊ + * FALSE: Ϊ + */ +Status ListEmpty(LinkList L); + +/* + * 㷨2.9 + * + * + * + * iλϲeɹ򷵻OK򷵻ERROR + * + *ע + * ̲iĺԪλã1ʼ + */ +Status ListInsert(LinkList L, int i, ElemType e); + +/* + * 㷨2.10 + * + * ɾ + * + * ɾiλϵԪأɾԪش洢eС + * ɾɹ򷵻OK򷵻ERROR + * + *ע + * ̲iĺԪλã1ʼ + */ +Status ListDelete(LinkList L, int i, ElemType* e); + +#endif diff --git a/VisualC++/CourseBook/0309_BankQueuing/LinkQueue.c b/VisualC++/CourseBook/0309_BankQueuing/LinkQueue.c new file mode 100644 index 0000000..b800d2c --- /dev/null +++ b/VisualC++/CourseBook/0309_BankQueuing/LinkQueue.c @@ -0,0 +1,138 @@ +/*========================= + * еʽ洢ṹӣ + ==========================*/ + +#ifndef LINKQUEUE_C +#define LINKQUEUE_C + +#include "LinkQueue.h" //**03 ջͶ**// + +/* + * ʼ + * + * һյӡ + * ʼɹ򷵻OK򷵻ERROR + * + *ע + * Ķдͷ + */ +Status InitQueue(LinkQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).front = (*Q).rear = (QueuePtr) malloc(sizeof(QNode)); + if(!(*Q).front) { + exit(OVERFLOW); + } + + (*Q).front->next = NULL; + + return OK; +} + +/* + * п + * + * жǷЧݡ + * + * ֵ + * TRUE : Ϊ + * FALSE: ӲΪ + */ +Status QueueEmpty(LinkQueue Q) { + if(Q.front == Q.rear) { + return TRUE; + } else { + return FALSE; + } +} + +/* + * + * + * ӰЧԪص + */ +int QueueLength(LinkQueue Q) { + int count = 0; + QueuePtr p = Q.front; + + while(p != Q.rear) { + count++; + p = p->next; + } + + return count; +} + +/* + * ȡֵ + * + * ȡͷԪأ洢eС + * ҵOK򣬷ERROR + */ +Status GetHead(LinkQueue Q, QElemType* e) { + QueuePtr p; + + if(Q.front == NULL || Q.front == Q.rear) { + return ERROR; + } + + p = Q.front->next; + *e = p->data; + + return OK; +} + +/* + * + * + * Ԫeӵβ + */ +Status EnQueue(LinkQueue* Q, QElemType e) { + QueuePtr p; + + if(Q == NULL || (*Q).front == NULL) { + return ERROR; + } + + p = (QueuePtr) malloc(sizeof(QNode)); + if(!p) { + exit(OVERFLOW); + } + + p->data = e; + p->next = NULL; + + (*Q).rear->next = p; + (*Q).rear = p; + + return OK; +} + +/* + * + * + * ƳͷԪأ洢eС + */ +Status DeQueue(LinkQueue* Q, QElemType* e) { + QueuePtr p; + + if(Q == NULL || (*Q).front == NULL || (*Q).front == (*Q).rear) { + return ERROR; + } + + p = (*Q).front->next; + *e = p->data; + + (*Q).front->next = p->next; + if((*Q).rear == p) { + (*Q).rear = (*Q).front; + } + + free(p); + + return OK; +} + +#endif diff --git a/VisualC++/CourseBook/0309_BankQueuing/LinkQueue.h b/VisualC++/CourseBook/0309_BankQueuing/LinkQueue.h new file mode 100644 index 0000000..7600daa --- /dev/null +++ b/VisualC++/CourseBook/0309_BankQueuing/LinkQueue.h @@ -0,0 +1,83 @@ +/*========================= + * еʽ洢ṹӣ + ==========================*/ + +#ifndef LINKQUEUE_H +#define LINKQUEUE_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* ԪͶ壬¼ͻϢ */ +typedef struct { + int ArrivedTime; // ͻʱ + int Duration; // ҵʱ + int Count; // ˱¼ÿеĿͻǵڼ̲޴˱Ӵ˱Ŀǹ۲Ŷ״ +} QElemType; //еԪ + +// Ԫؽṹ +typedef struct QNode { + QElemType data; + struct QNode* next; +} QNode, * QueuePtr; + +// нṹ +typedef struct { + QueuePtr front; // ͷָ + QueuePtr rear; // βָ +} LinkQueue; // еʽ洢ʾ + + +/* + * ʼ + * + * һյӡ + * ʼɹ򷵻OK򷵻ERROR + * + *ע + * Ķдͷ + */ +Status InitQueue(LinkQueue* Q); + +/* + * п + * + * жǷЧݡ + * + * ֵ + * TRUE : Ϊ + * FALSE: ӲΪ + */ +Status QueueEmpty(LinkQueue Q); + +/* + * + * + * ӰЧԪص + */ +int QueueLength(LinkQueue Q); + +/* + * ȡֵ + * + * ȡͷԪأ洢eС + * ҵOK򣬷ERROR + */ +Status GetHead(LinkQueue Q, QElemType* e); + +/* + * + * + * Ԫeӵβ + */ +Status EnQueue(LinkQueue* Q, QElemType e); + +/* + * + * + * ƳͷԪأ洢eС + */ +Status DeQueue(LinkQueue* Q, QElemType* e); + +#endif diff --git a/VisualC++/CourseBook/CourseBook.sdf b/VisualC++/CourseBook/CourseBook.sdf index 67ff2c3..79acf71 100644 Binary files a/VisualC++/CourseBook/CourseBook.sdf and b/VisualC++/CourseBook/CourseBook.sdf differ diff --git a/VisualC++/CourseBook/CourseBook.sln b/VisualC++/CourseBook/CourseBook.sln index e00d873..140ea9b 100644 --- a/VisualC++/CourseBook/CourseBook.sln +++ b/VisualC++/CourseBook/CourseBook.sln @@ -23,6 +23,24 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0210_MergeEList", "0210_Mer EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0211_Polynomial", "0211_Polynomial\0211_Polynomial.vcxproj", "{BA5E9196-08EF-4F25-9F82-D35296370F52}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0301_SqStack", "0301_SqStack\0301_SqStack.vcxproj", "{CBC87773-9686-4E77-BE7E-1335A673E0E1}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0302_Conversion", "0302_Conversion\0302_Conversion.vcxproj", "{B1FD90AC-75CE-404C-907E-8D939C078D79}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0303_LineEdit", "0303_LineEdit\0303_LineEdit.vcxproj", "{55BB85DE-4B42-4EF0-BBF8-F5E03A518376}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0304_Maze", "0304_Maze\0304_Maze.vcxproj", "{38C80F0E-757B-4E11-93F2-C2666AF3617F}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0305_Expression", "0305_Expression\0305_Expression.vcxproj", "{71331A05-F03F-41E1-A7BD-D6F9D2944D8D}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0306_Hanoi", "0306_Hanoi\0306_Hanoi.vcxproj", "{B92DECE4-A1D8-4A31-904B-C2441A1BB053}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0307_LinkQueue", "0307_LinkQueue\0307_LinkQueue.vcxproj", "{9ECA0672-0E13-4324-B2EA-1A8153348085}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0308_SqQueue", "0308_SqQueue\0308_SqQueue.vcxproj", "{28EB7738-3109-4B98-B312-1B3D1DD91DDF}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0309_BankQueuing", "0309_BankQueuing\0309_BankQueuing.vcxproj", "{C44F604C-F8CD-498E-A3EF-04FCF37AA303}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -73,6 +91,42 @@ Global {BA5E9196-08EF-4F25-9F82-D35296370F52}.Debug|Win32.Build.0 = Debug|Win32 {BA5E9196-08EF-4F25-9F82-D35296370F52}.Release|Win32.ActiveCfg = Release|Win32 {BA5E9196-08EF-4F25-9F82-D35296370F52}.Release|Win32.Build.0 = Release|Win32 + {CBC87773-9686-4E77-BE7E-1335A673E0E1}.Debug|Win32.ActiveCfg = Debug|Win32 + {CBC87773-9686-4E77-BE7E-1335A673E0E1}.Debug|Win32.Build.0 = Debug|Win32 + {CBC87773-9686-4E77-BE7E-1335A673E0E1}.Release|Win32.ActiveCfg = Release|Win32 + {CBC87773-9686-4E77-BE7E-1335A673E0E1}.Release|Win32.Build.0 = Release|Win32 + {B1FD90AC-75CE-404C-907E-8D939C078D79}.Debug|Win32.ActiveCfg = Debug|Win32 + {B1FD90AC-75CE-404C-907E-8D939C078D79}.Debug|Win32.Build.0 = Debug|Win32 + {B1FD90AC-75CE-404C-907E-8D939C078D79}.Release|Win32.ActiveCfg = Release|Win32 + {B1FD90AC-75CE-404C-907E-8D939C078D79}.Release|Win32.Build.0 = Release|Win32 + {55BB85DE-4B42-4EF0-BBF8-F5E03A518376}.Debug|Win32.ActiveCfg = Debug|Win32 + {55BB85DE-4B42-4EF0-BBF8-F5E03A518376}.Debug|Win32.Build.0 = Debug|Win32 + {55BB85DE-4B42-4EF0-BBF8-F5E03A518376}.Release|Win32.ActiveCfg = Release|Win32 + {55BB85DE-4B42-4EF0-BBF8-F5E03A518376}.Release|Win32.Build.0 = Release|Win32 + {38C80F0E-757B-4E11-93F2-C2666AF3617F}.Debug|Win32.ActiveCfg = Debug|Win32 + {38C80F0E-757B-4E11-93F2-C2666AF3617F}.Debug|Win32.Build.0 = Debug|Win32 + {38C80F0E-757B-4E11-93F2-C2666AF3617F}.Release|Win32.ActiveCfg = Release|Win32 + {38C80F0E-757B-4E11-93F2-C2666AF3617F}.Release|Win32.Build.0 = Release|Win32 + {71331A05-F03F-41E1-A7BD-D6F9D2944D8D}.Debug|Win32.ActiveCfg = Debug|Win32 + {71331A05-F03F-41E1-A7BD-D6F9D2944D8D}.Debug|Win32.Build.0 = Debug|Win32 + {71331A05-F03F-41E1-A7BD-D6F9D2944D8D}.Release|Win32.ActiveCfg = Release|Win32 + {71331A05-F03F-41E1-A7BD-D6F9D2944D8D}.Release|Win32.Build.0 = Release|Win32 + {B92DECE4-A1D8-4A31-904B-C2441A1BB053}.Debug|Win32.ActiveCfg = Debug|Win32 + {B92DECE4-A1D8-4A31-904B-C2441A1BB053}.Debug|Win32.Build.0 = Debug|Win32 + {B92DECE4-A1D8-4A31-904B-C2441A1BB053}.Release|Win32.ActiveCfg = Release|Win32 + {B92DECE4-A1D8-4A31-904B-C2441A1BB053}.Release|Win32.Build.0 = Release|Win32 + {9ECA0672-0E13-4324-B2EA-1A8153348085}.Debug|Win32.ActiveCfg = Debug|Win32 + {9ECA0672-0E13-4324-B2EA-1A8153348085}.Debug|Win32.Build.0 = Debug|Win32 + {9ECA0672-0E13-4324-B2EA-1A8153348085}.Release|Win32.ActiveCfg = Release|Win32 + {9ECA0672-0E13-4324-B2EA-1A8153348085}.Release|Win32.Build.0 = Release|Win32 + {28EB7738-3109-4B98-B312-1B3D1DD91DDF}.Debug|Win32.ActiveCfg = Debug|Win32 + {28EB7738-3109-4B98-B312-1B3D1DD91DDF}.Debug|Win32.Build.0 = Debug|Win32 + {28EB7738-3109-4B98-B312-1B3D1DD91DDF}.Release|Win32.ActiveCfg = Release|Win32 + {28EB7738-3109-4B98-B312-1B3D1DD91DDF}.Release|Win32.Build.0 = Release|Win32 + {C44F604C-F8CD-498E-A3EF-04FCF37AA303}.Debug|Win32.ActiveCfg = Debug|Win32 + {C44F604C-F8CD-498E-A3EF-04FCF37AA303}.Debug|Win32.Build.0 = Debug|Win32 + {C44F604C-F8CD-498E-A3EF-04FCF37AA303}.Release|Win32.ActiveCfg = Release|Win32 + {C44F604C-F8CD-498E-A3EF-04FCF37AA303}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/VisualC++/CourseBook/CourseBook.suo b/VisualC++/CourseBook/CourseBook.suo index 6c1f351..b5ff616 100644 Binary files a/VisualC++/CourseBook/CourseBook.suo and b/VisualC++/CourseBook/CourseBook.suo differ diff --git a/VisualC++/README.md b/VisualC++/README.md index 7d2370d..19507a8 100644 --- a/VisualC++/README.md +++ b/VisualC++/README.md @@ -30,3 +30,5 @@ ![VC03](image/VC03.png) ![VC04](image/VC04.png) ![VC05](image/VC05.png) +5. 让输出显示在控制台上 +![VC06](image/VC06.png) diff --git a/VisualC++/image/VC06.png b/VisualC++/image/VC06.png new file mode 100644 index 0000000..92f6c8c Binary files /dev/null and b/VisualC++/image/VC06.png differ