diff --git a/CLion/ExerciseBook/03.15/03.15.c b/CLion/ExerciseBook/03.15/03.15.c new file mode 100644 index 0000000..cbea654 --- /dev/null +++ b/CLion/ExerciseBook/03.15/03.15.c @@ -0,0 +1,162 @@ +#include +#include "Status.h" //**▲01 绪论**// + +/* 宏定义 */ +#define N 100 //栈的容量 + +/* 双向栈元素类型定义 */ +typedef int SElemType; + +/* 双向栈中包含的两个栈的栈名 */ +typedef enum { + Left, Right +} StackName; + +/* 双向栈结构 */ +typedef struct { + SElemType stack[N]; // 用一个容量足够大的数组做栈 + int top[2]; // 存放栈顶指针 +} TWS; + +// 初始化栈 +Status Inistack_3_15(TWS* tws); + +// 入栈,name指示向哪个栈中添加元素 +Status Push_3_15(TWS* tws, StackName name, SElemType x); + +// 出栈,name指示从哪个栈中移除元素 +Status Pop_3_15(TWS* tws, StackName name, SElemType* x); + +// 输出栈中元素,name指示输出哪个栈中的元素 +void OutputStack(TWS tws, StackName name); + + +int main(int argc, char* argv[]) { + TWS S; + int i, x; + + printf("████████ 初始化栈...\n"); + Inistack_3_15(&S); + + + printf("████████ 向两个栈中压入元素...\n"); + + for(i = 1; i <= 5; i++) { + Push_3_15(&S, Left, i); + Push_3_15(&S, Right, 2 * i); + } + printf("█ 左栈中的元素(从栈底到栈顶):"); + OutputStack(S, Left); + printf("█ 右栈中的元素(从栈底到栈顶):"); + OutputStack(S, Right); + + + printf("████████ 分别弹出两个栈的栈顶元素...\n"); + + Pop_3_15(&S, Left, &x); + printf("█ 弹出左栈的栈顶元素为:%d\n", x); + printf("█ 左栈中的元素(从栈底到栈顶):"); + OutputStack(S, Left); + + Pop_3_15(&S, Right, &x); + printf("█ 弹出右栈的栈顶元素为:%d\n", x); + printf("█ 右栈中的元素(从栈底到栈顶):"); + OutputStack(S, Right); + + return 0; +} + + +// 初始化栈 +Status Inistack_3_15(TWS* tws) { + if(tws == NULL) { + return ERROR; + } + + (*tws).top[Left] = -1; // 栈0的栈顶指针,注意不是0 + (*tws).top[Right] = N; // 栈1的栈顶指针,注意不是N-1 + + return OK; +} + +// 入栈,name指示向哪个栈中添加元素 +Status Push_3_15(TWS* tws, StackName name, SElemType x) { + if(tws == NULL) { + return ERROR; + } + + // 注意栈满条件,数组全被填充完才算栈满,不浪费空间 + if((*tws).top[Left] + 1 == (*tws).top[Right]) { + return ERROR; + } + + // 先移动栈顶游标,再存入元素 + switch(name) { + case Left: + (*tws).top[name]++; // 左边栈的游标向右移动 + break; + case Right: + (*tws).top[name]--; // 右边栈的游标向左移动 + break; + default: + break; + } + + // 存入元素 + (*tws).stack[(*tws).top[name]] = x; + + return OK; +} + +// 出栈,name指示从哪个栈中移除元素 +Status Pop_3_15(TWS* tws, StackName name, SElemType* x) { + if(tws == NULL) { + return ERROR; + } + + // 先移除元素,再移动游标 + switch(name) { + case Left: + // 判断左边的栈是否为空 + if((*tws).top[name] == -1) { + return ERROR; + } + *x = (*tws).stack[(*tws).top[name]]; + (*tws).top[name]--; + break; + case Right: + // 判断右边的栈是否为空 + if((*tws).top[name] == N) { + return ERROR; + } + *x = (*tws).stack[(*tws).top[name]]; + (*tws).top[name]++; + break; + default: + break; + } + + return OK; +} + +// 输出栈中元素,name指示输出哪个栈中的元素 +void OutputStack(TWS tws, StackName name) { + int i; + + switch(name) { + case Left: + for(i = 0; i <= tws.top[name]; i++) { + printf("%d ", tws.stack[i]); + } + break; + case Right: + for(i = N - 1; i >= tws.top[name]; i--) { + printf("%d ", tws.stack[i]); + } + break; + default: + break; + } + + printf("\n"); +} diff --git a/CLion/ExerciseBook/03.15/CMakeLists.txt b/CLion/ExerciseBook/03.15/CMakeLists.txt new file mode 100644 index 0000000..94d3c57 --- /dev/null +++ b/CLion/ExerciseBook/03.15/CMakeLists.txt @@ -0,0 +1,6 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件,并链接公共库 +add_executable(03.15 03.15.c) +target_link_libraries(03.15 Scanf_lib) diff --git a/CLion/ExerciseBook/03.16/03.16.c b/CLion/ExerciseBook/03.16/03.16.c new file mode 100644 index 0000000..ffdee98 --- /dev/null +++ b/CLion/ExerciseBook/03.16/03.16.c @@ -0,0 +1,136 @@ +#include +#include "Status.h" //**▲01 绪论**// +#include "SqStack.h" //**▲03 栈和队列**// + +/* + * 生成车厢调度序列,其中'I'代表入栈,'O'代表出栈 + * + * 假设入口在右边,出口在左边,且车厢读取顺序【从左到右】 + * + * En :等待调度的列车序列 + * seq:调度序列 + */ +void Algo_3_16(char* En, char seq[]); + +/* + * 使用约定好的调度序列seq,将入口En处的车厢调度到出口Ex + * + * 该方法可看做是对调度序列的检验 + * + * En :等待调度的列车序列 + * seq:调度序列 + * Ex :调度完成后的序列 + */ +Status Dispatch(char* En, char* seq, char Ex[]); + + +int main(int argc, char* argv[]) { + char* En = "HSHHSHHHSSHSSSHS"; // 等待调度的列车序列 + char Ex[100] = {'\0'}; // 调度完成的列车序列 + char seq[100] = {'\0'}; // 调度序列 + + printf("████ 入口处的序列:\n"); + printf("█ En = %s\n", En); + + Algo_3_16(En, seq); + printf("████ 生成的调度序列:\n"); + printf("█ seq = %s\n", seq); + + Dispatch(En, seq, Ex); + printf("████ 根据生成的调度序列,对入口处的车厢调度,调度完成后的车厢序列:\n"); + printf("█ Ex = %s\n", Ex); + + return 0; +} + + +/* + * 生成车厢调度序列,其中'I'代表入栈,'O'代表出栈 + * + * 假设入口在右边,出口在左边,车厢读取顺序从左到右 + * + * En :等待调度的列车序列 + * seq:调度序列 + */ +void Algo_3_16(char* En, char seq[]) { + int i, j; + SqStack S; + SElemType e; + + // 初始化一个中转栈 + InitStack(&S); + + // 遍历待调度序列 + for(i = j = 0; En[i] != '\0'; i++) { + // 遇到硬席,则将其入栈 + if(En[i] == 'H') { + Push(&S, En[i]); + seq[j++] = 'I'; + } + + // 遇到软席,则先入栈,并立即出栈,相当于用栈做中转 + if(En[i] == 'S') { + Push(&S, En[i]); + Pop(&S, &e); + seq[j++] = 'I'; + seq[j++] = 'O'; + } + } + + // 将中转栈中的硬席出栈 + while(!StackEmpty(S)) { + Pop(&S, &e); + seq[j++] = 'O'; + } + + seq[j] = '\0'; + + DestroyStack(&S); +} + +/* + * 使用约定好的调度序列seq,将入口En处的车厢调度到出口Ex + * + * 该方法可看做是对调度序列的检验 + * + * En :等待调度的列车序列 + * seq:调度序列 + * Ex :调度完成后的序列 + */ +Status Dispatch(char* En, char* seq, char Ex[]) { + int i, j, k; + SqStack S; + SElemType e; + + // 初始化一个中转栈 + InitStack(&S); + + i = j = k = 0; + + // 遍历调度序列 + while(seq[k] != '\0') { + // 遇到入栈标记,则将车厢加入中转栈 + if(seq[k] == 'I') { + Push(&S, En[i++]); + } + + // 遇到出栈标记,则将车厢移除,并转移到出口处 + if(seq[k] == 'O') { + Pop(&S, &e); + Ex[j++] = e; + } + + k++; + } + + // 如果调度序列为空,但是入口处存在未调度的车厢,或者中转栈里存在未处理的车厢,则表示发生错误 + if(seq[k] == '\0' && (En[i] || StackEmpty(S))) { + return ERROR; + } + + Ex[j] = '\0'; + + DestroyStack(&S); + + return OK; +} diff --git a/CLion/ExerciseBook/03.16/CMakeLists.txt b/CLion/ExerciseBook/03.16/CMakeLists.txt new file mode 100644 index 0000000..7a2a75e --- /dev/null +++ b/CLion/ExerciseBook/03.16/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.16 SqStack.h SqStack.c 03.16.c) +# 链接公共库 +target_link_libraries(03.16 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.16/SqStack.c b/CLion/ExerciseBook/03.16/SqStack.c new file mode 100644 index 0000000..2a56211 --- /dev/null +++ b/CLion/ExerciseBook/03.16/SqStack.c @@ -0,0 +1,109 @@ +/*========================= + * 栈的顺序存储结构(顺序栈) + ==========================*/ + +#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; +} + +/* + * 判空 + * + * 判断顺序栈中是否包含有效数据。 + * + * 返回值: + * 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/ExerciseBook/03.16/SqStack.h b/CLion/ExerciseBook/03.16/SqStack.h new file mode 100644 index 0000000..53af926 --- /dev/null +++ b/CLion/ExerciseBook/03.16/SqStack.h @@ -0,0 +1,66 @@ +/*========================= + * 栈的顺序存储结构(顺序栈) + ==========================*/ + +#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); + +/* + * 判空 + * + * 判断顺序栈中是否包含有效数据。 + * + * 返回值: + * 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/ExerciseBook/03.17/03.17.c b/CLion/ExerciseBook/03.17/03.17.c new file mode 100644 index 0000000..62bb0ad --- /dev/null +++ b/CLion/ExerciseBook/03.17/03.17.c @@ -0,0 +1,69 @@ +#include +#include "Status.h" //**▲01 绪论**// +#include "SqStack.h" //**▲03 栈和队列**// + +/* + * 判定输入序列是否为"序列1&序列2"这种模式 + * 其中,序列2是序列1的逆置 + * + * s:待验证的序列,以'@'符号结尾 + */ +Status Algo_3_17(char* s); + + +int main(int argc, char* argv[]) { + char* s = "a+b-c&c-b+a@"; + + printf("判断序列 %s 是否合规...\n", s); + + if(Algo_3_17(s)) { + printf("█ 序列满足题意!\n"); + } else { + printf("█ 序列不满足题意!!\n"); + } + + return 0; +} + + +/* + * 判定输入序列是否为"序列1&序列2"这种模式 + * 其中,序列2是序列1的逆置 + * + * s:待验证的序列,以'@'符号结尾 + */ +Status Algo_3_17(char* s) { + SqStack S; + SElemType e; + int i; + + InitStack(&S); + i = 0; + + // 将'&'前的序列入栈 + while(s[i] != '@' && s[i] != '&') { + Push(&S, s[i]); + i++; + } + + // 如果遇到了'&'符号 + if(s[i] != '@') { + i++; // 跳过&符号 + + // 将'&'后的序列出栈 + while(!StackEmpty(S) && s[i] != '@') { + Pop(&S, &e); + if(s[i] != e) { + return ERROR; + } + i++; + } + } + + // 如果栈为空,且序列恰好访问完,说明符合题意 + if(StackEmpty(S) && s[i] == '@') { + return OK; + } + + return ERROR; +} diff --git a/CLion/ExerciseBook/03.17/CMakeLists.txt b/CLion/ExerciseBook/03.17/CMakeLists.txt new file mode 100644 index 0000000..34aa207 --- /dev/null +++ b/CLion/ExerciseBook/03.17/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.17 SqStack.h SqStack.c 03.17.c) +# 链接公共库 +target_link_libraries(03.17 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.17/SqStack.c b/CLion/ExerciseBook/03.17/SqStack.c new file mode 100644 index 0000000..d709204 --- /dev/null +++ b/CLion/ExerciseBook/03.17/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/ExerciseBook/03.17/SqStack.h b/CLion/ExerciseBook/03.17/SqStack.h new file mode 100644 index 0000000..e840954 --- /dev/null +++ b/CLion/ExerciseBook/03.17/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/ExerciseBook/03.18/03.18.c b/CLion/ExerciseBook/03.18/03.18.c new file mode 100644 index 0000000..fbf3f0d --- /dev/null +++ b/CLion/ExerciseBook/03.18/03.18.c @@ -0,0 +1,72 @@ +#include +#include "Status.h" //**▲01 绪论**// +#include "SqStack.h" //**▲03 栈和队列**// + +/* + * 判定表达式开闭括号是否配对出现 + * + *【注】 + * 只是判断括号是否配对出现,而不是判断括号类型、顺序是否正确 + */ +Status Algo_3_18(char* s); + + +int main(int argc, char* argv[]) { + char* s = "(1+2)*3/{2/[(4-5)*3]-5*(8-7)}"; + + printf("判断表达式 %s 开闭括号数量是否匹配...\n", s); + + if(Algo_3_18(s) == TRUE) { + printf("█ 表达式开闭括号数量匹配!\n"); + } else { + printf("█ 表达式开闭括号数量不匹配!!\n"); + } + + return 0; +} + + +/* + * 判定表达式开闭括号是否配对出现 + * + *【注】 + * 只是判断括号是否配对出现,而不是判断括号类型、顺序是否正确 + */ +Status Algo_3_18(char* s) { + SqStack S; + SElemType e; + int i; + + InitStack(&S); + + for(i = 0; s[i] != '\0'; i++) { + switch(s[i]) { + // 遇到左括号,则入栈 + case '(': + case '[': + case '{': + Push(&S, s[i]); + break; + + // 遇到右括号,则出栈 + case ')': + case ']': + case '}': + if(StackEmpty(S)) { + return ERROR; + } + Pop(&S, &e); + break; + + default: + break; + } + } + + // 如果栈正好为空了,说明校验成功 + if(StackEmpty(S)) { + return OK; + } + + return ERROR; +} diff --git a/CLion/ExerciseBook/03.18/CMakeLists.txt b/CLion/ExerciseBook/03.18/CMakeLists.txt new file mode 100644 index 0000000..06cf548 --- /dev/null +++ b/CLion/ExerciseBook/03.18/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.18 SqStack.h SqStack.c 03.18.c) +# 链接公共库 +target_link_libraries(03.18 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.18/SqStack.c b/CLion/ExerciseBook/03.18/SqStack.c new file mode 100644 index 0000000..d709204 --- /dev/null +++ b/CLion/ExerciseBook/03.18/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/ExerciseBook/03.18/SqStack.h b/CLion/ExerciseBook/03.18/SqStack.h new file mode 100644 index 0000000..e840954 --- /dev/null +++ b/CLion/ExerciseBook/03.18/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/ExerciseBook/03.19/03.19.c b/CLion/ExerciseBook/03.19/03.19.c new file mode 100644 index 0000000..de949a9 --- /dev/null +++ b/CLion/ExerciseBook/03.19/03.19.c @@ -0,0 +1,98 @@ +#include +#include "Status.h" //**▲01 绪论**// +#include "SqStack.h" //**▲03 栈和队列**// + +// 判定表达式开闭括号是否配对出现,且类型是否匹配 +Status Algo_3_19(char* s); + +// 判断括号a和括号b之间的类型是否匹配 +Status Matching(char a, char b); + + +int main(int argc, char* argv[]) { + char* s = "(1+2)*3/{2/[(4-5)*3]-5*(8-7)}"; + + printf("判断表达式 %s 括号是否匹配...\n", s); + + if(Algo_3_19(s)) { + printf("█ 表达式括号匹配!\n"); + } else { + printf("█ 表达式括号不匹配!!\n"); + } + + return 0; +} + + +// 判定表达式开闭括号是否配对出现,且类型是否匹配 +Status Algo_3_19(char* s) { + SqStack S; + SElemType e; + int i; + + InitStack(&S); + + for(i = 0; s[i] != '\0'; i++) { + switch(s[i]) { + // 遇到左括号,则入栈 + case '(': + case '[': + case '{': + Push(&S, s[i]); + break; + + // 遇到右括号,则出栈 + case ')': + case ']': + case '}': + if(StackEmpty(S)) { + return ERROR; + } + + Pop(&S, &e); + + // 判断括号是否匹配 + if(!Matching(e, s[i])) { + return ERROR; + } + break; + + default: + break; + } + } + + if(StackEmpty(S)) { + return OK; + } + + return ERROR; +} + +// 判断括号a和括号b之间的类型是否匹配 +Status Matching(char a, char b) { + switch(a) { + case '(': + if(b != ')') { + return ERROR; + } + break; + + case '[': + if(b != ']') { + return ERROR; + } + break; + + case '{': + if(b != '}') { + return ERROR; + } + break; + + default: + return ERROR; + } + + return OK; +} diff --git a/CLion/ExerciseBook/03.19/CMakeLists.txt b/CLion/ExerciseBook/03.19/CMakeLists.txt new file mode 100644 index 0000000..25c70de --- /dev/null +++ b/CLion/ExerciseBook/03.19/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.19 SqStack.h SqStack.c 03.19.c) +# 链接公共库 +target_link_libraries(03.19 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.19/SqStack.c b/CLion/ExerciseBook/03.19/SqStack.c new file mode 100644 index 0000000..d709204 --- /dev/null +++ b/CLion/ExerciseBook/03.19/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/ExerciseBook/03.19/SqStack.h b/CLion/ExerciseBook/03.19/SqStack.h new file mode 100644 index 0000000..e840954 --- /dev/null +++ b/CLion/ExerciseBook/03.19/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/ExerciseBook/03.20/03.20.c b/CLion/ExerciseBook/03.20/03.20.c new file mode 100644 index 0000000..caf3155 --- /dev/null +++ b/CLion/ExerciseBook/03.20/03.20.c @@ -0,0 +1,220 @@ +#include +#include "Status.h" //**▲01 绪论**// + +/* 此算法类似于迷宫寻路算法 */ + +/* 宏定义 */ +#define SleepTime 2 +#define Row 10 +#define Col 17 + +/* 访问方向 */ +typedef enum { + East, South, West, North +} Direction; + +/* 颜色枚举 */ +typedef enum { + Color_1, Color_2, Color_3 +} Color; + +/* 栈的元素类型 */ +typedef struct { + int x, y; // 像素点的横、纵坐标定义 + int di; // 从此像素点走向下一像素点的"方向" +} SElemType; + +// 迷宫染色函数:对指定的图像染色 +void Algo_3_20(int g[][17], SElemType start); + +// 初始化图像区域和染色起点 +void InitGrap(int g[][17], SElemType* start); + +// 在屏幕上显示当前图像 +void PaintGrap(int g[][17]); + +// 判断某个点是否需要染色 +Status Pass(SElemType e, int g[][17]); + +// 遇到可以染色的点即留下标记,即染上相应的颜色 +void Mark(SElemType* e, int g[][17]); + +// 获取下一个该染色的点的信息 +Status NextPos(SElemType* e); + +// 判断当前的点是否出界 +Status IsCross(SElemType e); + + +int main(int argc, char** argv) { + int g[Row][Col]; // 存放图像区域点的颜色 + SElemType start; // 染色的起点 + + InitGrap(g, &start); + PaintGrap(g); + + Algo_3_20(g, start); + + return 0; +} + + +// 对指定的图像染色 +void Algo_3_20(int g[][Col], SElemType start) { + SElemType e; + SElemType stack[10000]; // 定义一个足够大的数组作为栈,存放染过色的点 + int top = -1; // 栈顶指针 + + e = start; + do { + // 如果需要染色 + if(Pass(e, g)) { + Mark(&e, g); // 将其染色,并标记其前进方向 + PaintGrap(g); + stack[++top] = e; // 将访问过的像素点入栈 + NextPos(&e); // 得出下一个像素点信息 + } else { + if(top != -1) { + e = stack[top--]; + + // 栈中弹出的点已没多余方向可访问 + while(e.di == North && top != -1) { + e = stack[top--]; + } + + if(e.di < North) { + e.di++; + stack[++top] = e; + NextPos(&e); + } + } + } + } while(top != -1); // 栈不为空 +} + +/* 初始化图像区域和染色起点 */ +void InitGrap(int g[][Col], SElemType* start) { + int i, j; + + // 储存颜色0和颜色1 + int a[Row][Col]={{0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0}, + {0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0}, + {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, + {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, + {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, + {0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0}, + {0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0}, + {0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0}}; + + for(i = 0; i < 10; i++) { + for(j = 0; j < 17; j++) { + g[i][j] = a[i][j]; + } + } + + (*start).x = 9; // 起点坐标 + (*start).y = 8; + (*start).di = East; // East代表方向向右 +} + +/* 在屏幕上显示当前图像 */ +void PaintGrap(int g[][Col]) { + int i, j; + + Wait(SleepTime); + + for(i = 0; i < Row; i++) { + for(j = 0; j < Col; j++) { + // 颜色0用"^"显示 + if(g[i][j] == Color_1) { + printf("."); + } + + // 颜色1显示为空白 + if(g[i][j] == Color_2) { + printf(" "); + } + + // 颜色2用"*"显示 + if(g[i][j] == Color_3) { + printf("*"); + } + + if(j && !(j % (Col - 1))) { + printf("\n"); + } + } + } + + printf("\n"); +} + +/* 判断某个点是否需要染色 */ +Status Pass(SElemType e, int g[][Col]) { + int x = e.x; + int y = e.y; + + // 将颜色为1的点染成其他颜色 + if(g[x][y] == 1) { + return TRUE; + } else { + return FALSE; + } +} + +/* 遇到可以染色的点即留下标记,即染上相应的颜色 */ +void Mark(SElemType* e, int g[][Col]) { + int x = (*e).x; + int y = (*e).y; + + (*e).di = East; // 标记此像素点的前进方向 + + g[x][y] = 2; // 将此像素点颜色染为颜色2 +} + +/* 获取下一个该染色的点的信息 */ +Status NextPos(SElemType* e) { + SElemType tmp; + tmp = *e; + + switch(tmp.di) { + case East: + (tmp.y)++; // East代表向右 + break; + case South: + (tmp.x)++; // South代表向下 + break; + case West: + (tmp.y)--; // West代表向左 + break; + case North: + (tmp.x)--; // North代表向上 + break; + default: + return FALSE; + } + + if(IsCross(tmp)) { + ++(*e).di; + NextPos(e); + } else { + *e = tmp; + } + + return TRUE; +} + +/* 判断当前的点是否出界 */ +Status IsCross(SElemType e) { + int x = e.x; + int y = e.y; + + // 越界 + if(x < 0 || y < 0 || x > Row - 1 || y > Col - 1) { + return OK; + } else { + return ERROR; + } +} diff --git a/CLion/ExerciseBook/03.20/CMakeLists.txt b/CLion/ExerciseBook/03.20/CMakeLists.txt new file mode 100644 index 0000000..7947115 --- /dev/null +++ b/CLion/ExerciseBook/03.20/CMakeLists.txt @@ -0,0 +1,6 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件,并链接公共库 +add_executable(03.20 03.20.c) +target_link_libraries(03.20 Scanf_lib) diff --git a/CLion/ExerciseBook/03.21/03.21.c b/CLion/ExerciseBook/03.21/03.21.c new file mode 100644 index 0000000..85f95c4 --- /dev/null +++ b/CLion/ExerciseBook/03.21/03.21.c @@ -0,0 +1,124 @@ +#include +#include // 提供strlen原型 +#include // 提供isalpha原型 +#include "SqStack.h" //**▲03 栈和队列**// + +// 运算符表,即表达式中允许出现的符号 +static const char OP[] = {'+', '-', '*', '/'}; + +/* + * 运算符优先级表,与上面的OP表是呼应的。 + * 这里只有四则运算符号的优先级 + */ +static const char PrecedeTable[7][7] = { + {'>', '>', '<', '<'}, + {'>', '>', '<', '<'}, + {'>', '>', '>', '>'}, + {'>', '>', '>', '>'} +}; + +/* + * 根据给定的中缀表达式,返回其对应的逆波兰表达式(后缀表达式) + * 如将"a+b*c-d/e"将转化为:"abc*+de/-" + * + *【注】 + * 表达式的变量为单字母,操作符只有'+'、'-'、'*'、'/'四则运算 + */ +char* Algo_3_21(char s[]); + +/* + * 判断运算符栈中操作符o1与表达式中的操作符o2的优先级。 + * + * 返回'>'、'<'、'=',以指示o1和o2的优先级 + */ +char Precede(char o1, char o2); + + +int main(int argc, char* argv[]) { + char* s = "a+b*c-d/e"; + + printf("中缀表达式为:"); + printf("%s\n", s); + + printf("后缀表达式为:"); + printf("%s\n", Algo_3_21(s)); + + return 0; +} + + +/* + * 根据给定的中缀表达式,返回其对应的逆波兰表达式(后缀表达式) + * 如将"a+b*c-d/e"将转化为:"abc*+de/-" + * + *【注】 + * 表达式的变量为单字母,操作符只有'+'、'-'、'*'、'/'四则运算 + */ +char* Algo_3_21(char s[]) { + int i, j; + SqStack S; // 运算符栈,临时存储操作符 + SElemType e; // SElemType被定义char类型,char和int类型可直接比较 + char* c; + + // 逆波兰式序列 + c = (char*) malloc((strlen(s) + 1) * sizeof(char)); + + InitStack(&S); + + for(i = j = 0; s[i] != '\0'; i++) { + // 如果读到了字母,将其视为操作数, + if(isalpha(s[i])) { + c[j++] = s[i]; // 将字母直接存入逆波兰式中 + + // 如果读到的是运算符 + } else { + while(!StackEmpty(S)) { + // 获取栈顶元素 + GetTop(S, &e); + + // 如果运算符栈中运算符优先级高 + if(Precede(e, s[i]) == '>') { + Pop(&S, &e); // 将运算符栈中优先级高的操作符出栈 + c[j++] = e; // 将出栈后的操作符添加到逆波兰式中 + + // 如果遇到运算符栈中操作符优先级低时,结束循环 + } else { + break; + } + } + + // 将表达式中读取到的操作符压入运算符栈 + Push(&S, s[i]); + } + } + + // 将运算符栈中所有操作符追加到逆波兰式中 + while(!StackEmpty(S)) { + Pop(&S, &e); + c[j++] = e; + } + + // 数组中转换后的逆波兰式以"\0"标记结束 + c[j] = '\0'; + + return c; +} + +/* + * 判断运算符栈中操作符o1与表达式中的操作符o2的优先级。 + * + * 返回'>'、'<'、'=',以指示o1和o2的优先级 + */ +char Precede(char o1, char o2) { + int x, y; + + // 获取指定的运算符在运算符表中的位置 + char* p1 = strchr(OP, o1); + char* p2 = strchr(OP, o2); + + // 计算出一个运算符优先级表坐标 + x = p1 - OP; + y = p2 - OP; + + return PrecedeTable[x][y]; +} diff --git a/CLion/ExerciseBook/03.21/CMakeLists.txt b/CLion/ExerciseBook/03.21/CMakeLists.txt new file mode 100644 index 0000000..65dcec3 --- /dev/null +++ b/CLion/ExerciseBook/03.21/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.21 SqStack.h SqStack.c 03.21.c) +# 链接公共库 +target_link_libraries(03.21 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.21/SqStack.c b/CLion/ExerciseBook/03.21/SqStack.c new file mode 100644 index 0000000..c7e31cc --- /dev/null +++ b/CLion/ExerciseBook/03.21/SqStack.c @@ -0,0 +1,106 @@ +/*========================= + * 栈的顺序存储结构(顺序栈) + ==========================*/ + +#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 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; +} diff --git a/CLion/ExerciseBook/03.21/SqStack.h b/CLion/ExerciseBook/03.21/SqStack.h new file mode 100644 index 0000000..f936b91 --- /dev/null +++ b/CLion/ExerciseBook/03.21/SqStack.h @@ -0,0 +1,66 @@ +/*========================= + * 栈的顺序存储结构(顺序栈) + ==========================*/ + +#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 GetTop(SqStack S, SElemType* e); + +/* + * 入栈 + * + * 将元素e压入到栈顶。 + */ +Status Push(SqStack* S, SElemType e); + +/* + * 出栈 + * + * 将栈顶元素弹出,并用e接收。 + */ +Status Pop(SqStack* S, SElemType* e); + +#endif diff --git a/CLion/ExerciseBook/03.22/03.22.c b/CLion/ExerciseBook/03.22/03.22.c new file mode 100644 index 0000000..f7901dc --- /dev/null +++ b/CLion/ExerciseBook/03.22/03.22.c @@ -0,0 +1,86 @@ +#include +#include // 提供isdigit原型 +#include "Status.h" //**▲01 绪论**// +#include "SqStack.h" //**▲03 栈和队列**// + +/* + * 对逆波兰表达式求值(理论上要求操作数与运算结果均为个位数) + * + *【注】 + * 表达式的变量为单字母,操作符只有'+'、'-'、'*'、'/'四则运算 + */ +Status Algo_3_22(char c[], SElemType* Value); + +/* 计算 a oper b 的值 */ +char Operate(char a, char oper, char b); + + +int main(int argc, char* argv[]) { + char c[] = "124*+93/-"; + SElemType value; + + printf("已知逆波兰式为:"); + printf("%s\n", c); + + Algo_3_22(c, &value); + printf("逆波兰式的计算结果为:%d\n", value - 48); + + return 0; +} + + +/* + * 对逆波兰表达式求值(理论上要求操作数与运算结果均为个位数) + * + *【注】 + * 表达式的变量为单字母,操作符只有'+'、'-'、'*'、'/'四则运算 + */ +Status Algo_3_22(char c[], SElemType* Value) { + int i; + SqStack S; + SElemType a, b; + + InitStack(&S); + + for(i = 0; c[i]!='\0'; ++i) { + // 如果遇到数字字符 + if(isdigit(c[i])) { + Push(&S, c[i]); + } else { + // 先弹出来的在操作数后边 + Pop(&S, &b); + Pop(&S, &a); + Push(&S, Operate(a, c[i], b)); + } + } + + Pop(&S, Value); + + if(!StackEmpty(S)) { + return ERROR; + } else { + return OK; + } +} + +/* 计算 a oper b 的值 */ +char Operate(char a, char oper, char b) { + char c; + + switch(oper) { + case '+': + c = (a - 48) + (b - 48) + 48; + break; + case '-': + c = (a - 48) - (b - 48) + 48; + break; + case '*': + c = (a - 48) * (b - 48) + 48; + break; + case '/': + c = (a - 48) / (b - 48) + 48; + break; + } + + return c; +} diff --git a/CLion/ExerciseBook/03.22/CMakeLists.txt b/CLion/ExerciseBook/03.22/CMakeLists.txt new file mode 100644 index 0000000..405e9b9 --- /dev/null +++ b/CLion/ExerciseBook/03.22/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.22 SqStack.h SqStack.c 03.22.c) +# 链接公共库 +target_link_libraries(03.22 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.22/SqStack.c b/CLion/ExerciseBook/03.22/SqStack.c new file mode 100644 index 0000000..d709204 --- /dev/null +++ b/CLion/ExerciseBook/03.22/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/ExerciseBook/03.22/SqStack.h b/CLion/ExerciseBook/03.22/SqStack.h new file mode 100644 index 0000000..e840954 --- /dev/null +++ b/CLion/ExerciseBook/03.22/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/ExerciseBook/03.23/03.23.c b/CLion/ExerciseBook/03.23/03.23.c new file mode 100644 index 0000000..baba524 --- /dev/null +++ b/CLion/ExerciseBook/03.23/03.23.c @@ -0,0 +1,109 @@ +#include +#include // 提供malloc、realloc、free、exit原型 +#include // 提供strlen原型 +#include // 提供isalpha原型 +#include "SqStack.h" //**▲03 栈和队列**// + +/* + * 将后缀表达式(逆波兰式)转换为前缀表达式(波兰式) + * + *【注】 + * 表达式的变量为单字母,操作符只有'+'、'-'、'*'、'/'四则运算 + */ +char* Algo_3_23(char* c); + +// 将字符转换为字符串 +char* CharToStr(char c); + +// 拼接字符串a和b,返回拼接后的字符串 +char* StrCat(char* a, char* b); + + +int main(int argc, char* argv[]) { + char* s = "abc+*de/-"; + + printf("中缀表达式:%s\n", "a*(b+c)-d/e"); + + printf("后缀表达式:"); + printf("%s\n", s); + + printf("前缀表达式:"); + printf("%s\n", Algo_3_23(s)); + + return 0; +} + + +/* + * 将后缀表达式(逆波兰式)转换为前缀表达式(波兰式) + * + *【注】 + * 表达式的变量为单字母,操作符只有'+'、'-'、'*'、'/'四则运算 + */ +char* Algo_3_23(char* c) { + int i; + char* s, * e1, * e2; + SqStack S; + + InitStack(&S); + + for(i = 0; c[i] != '\0'; i++) { + s = CharToStr(c[i]); + + // 若c[i]为操作符(不是操作数),则栈中至少应当有两个元素 + if(!isalpha(c[i])) { + if(Pop(&S, &e2) && Pop(&S, &e1)) { + s = StrCat(s, StrCat(e1, e2)); + } else { + return NULL; + } + } + + Push(&S, s); + } + + Pop(&S, &s); + + if(StackEmpty(S)) { + return s; + } + + return NULL; +} + +// 将字符转换为字符串 +char* CharToStr(char c) { + char* s; + + s = (char*) malloc(2 * sizeof(char)); + + s[0] = c; + s[1] = '\0'; + + return s; +} + +// 拼接字符串a和b,返回拼接后的字符串 +char* StrCat(char* a, char* b) { + char* s; + int i, j, alen, blen; + + alen = (int) strlen(a); + blen = (int) strlen(b); + + s = (char*) malloc((alen + blen + 1) * sizeof(char)); + + j = 0; + + for(i = 0; i < alen; i++) { + s[j++] = a[i]; + } + + for(i = 0; i < blen; i++) { + s[j++] = b[i]; + } + + s[j] = '\0'; + + return s; +} diff --git a/CLion/ExerciseBook/03.23/CMakeLists.txt b/CLion/ExerciseBook/03.23/CMakeLists.txt new file mode 100644 index 0000000..a73fecc --- /dev/null +++ b/CLion/ExerciseBook/03.23/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.23 SqStack.h SqStack.c 03.23.c) +# 链接公共库 +target_link_libraries(03.23 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.23/SqStack.c b/CLion/ExerciseBook/03.23/SqStack.c new file mode 100644 index 0000000..d709204 --- /dev/null +++ b/CLion/ExerciseBook/03.23/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/ExerciseBook/03.23/SqStack.h b/CLion/ExerciseBook/03.23/SqStack.h new file mode 100644 index 0000000..c1bc1c3 --- /dev/null +++ b/CLion/ExerciseBook/03.23/SqStack.h @@ -0,0 +1,64 @@ +/*========================= + * 栈的顺序存储结构(顺序栈) + ==========================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +/* 宏定义 */ +#define STACK_INIT_SIZE 100 // 顺序栈存储空间的初始分配量 +#define STACKINCREMENT 10 // 顺序栈存储空间的分配增量 + +/* + * 顺序栈元素类型定义 + * + *【注】 + * 这里使用字符串类型,即SElemType指向一个字符串 + */ +typedef char* 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/ExerciseBook/03.24/03.24.c b/CLion/ExerciseBook/03.24/03.24.c new file mode 100644 index 0000000..9632dad --- /dev/null +++ b/CLion/ExerciseBook/03.24/03.24.c @@ -0,0 +1,36 @@ +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +/* + * 递归计算g(m,n) + */ +int Algo_3_24(int m, int n); + + +int main(int argc, char* argv[]) { + int m, n; + + m = 5; + n = 2; + + printf("g(%d,%d) = %d\n", m, n, Algo_3_24(m, n)); + + return 0; +} + + +/* + * 递归计算g(m,n) + */ +int Algo_3_24(int m, int n) { + if(m < 0 || n < 0) { + exit(ERROR); + } + + if(m == 0) { + return 0; + } else { + return Algo_3_24(m - 1, 2 * n) + n; + } +} diff --git a/CLion/ExerciseBook/03.24/CMakeLists.txt b/CLion/ExerciseBook/03.24/CMakeLists.txt new file mode 100644 index 0000000..65d1ae9 --- /dev/null +++ b/CLion/ExerciseBook/03.24/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.24 03.24.c) +# 链接公共库 +target_link_libraries(03.24 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.25/03.25.c b/CLion/ExerciseBook/03.25/03.25.c new file mode 100644 index 0000000..b2c65be --- /dev/null +++ b/CLion/ExerciseBook/03.25/03.25.c @@ -0,0 +1,67 @@ +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +/* + * 方法1:递归法求解,优点是算法简单 + * + * 递归计算F(n) + */ +int Algo_3_25_1(int n); + +/* + * 方法2:消除递归的迭代法,优点是存储了上一层计算数值 + * + * 迭代计算F(n) + */ +int Algo_3_25_2(int n); + + +int main(int argc, char* argv[]) { + + printf("F(10) = %d\n", Algo_3_25_1(10)); + + printf("F(10) = %d\n", Algo_3_25_2(10)); + + return 0; +} + + +/* + * 方法1:递归法求解,优点是算法简单 + * + * 递归计算F(n) + */ +int Algo_3_25_1(int n) { + if(n < 0) { + exit(ERROR); + } + + if(n == 0) { + return n + 1; + } else { + return n * Algo_3_25_1(n / 2); + } +} + +/* + * 方法2:消除递归的迭代法,优点是存储了上一层计算数值 + * + * 迭代计算F(n) + */ +int Algo_3_25_2(int n) { + int* a; + int i; + + if(n < 0) { + exit(ERROR); + } + + a = (int*) malloc((n + 1) * sizeof(int)); + + for(i = 1, a[0] = 1; i <= n; i++) { + a[i] = i * a[i / 2]; + } + + return a[n]; +} diff --git a/CLion/ExerciseBook/03.25/CMakeLists.txt b/CLion/ExerciseBook/03.25/CMakeLists.txt new file mode 100644 index 0000000..68e286c --- /dev/null +++ b/CLion/ExerciseBook/03.25/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.25 03.25.c) +# 链接公共库 +target_link_libraries(03.25 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.26/03.26.c b/CLion/ExerciseBook/03.26/03.26.c new file mode 100644 index 0000000..7b17930 --- /dev/null +++ b/CLion/ExerciseBook/03.26/03.26.c @@ -0,0 +1,62 @@ +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +/* + * 方法1:递归法 + * + * 递归计算平方根 + */ +float Algo_3_26_1(float A, float p, float e); + +/* + * 方法2:迭代法 + * + * 迭代计算平方根 + */ +float Algo_3_26_2(float A, float p, float e); + + +int main(int argc, char* argv[]) { + + printf("√50 = %f\n", Algo_3_26_1(50, 1, 0.000001f)); + + printf("√50 = %f\n", Algo_3_26_2(50, 1, 0.000001f)); + + return 0; +} + + +/* + * 方法1:递归法 + * + * 递归计算平方根 + */ +float Algo_3_26_1(float A, float p, float e) { + if(A < 0.0) { + exit(ERROR); + } + + if((p * p - A) > -e && (p * p - A) < e) { + return p; + } else { + return Algo_3_26_1(A, (p + A / p) / 2, e); + } +} + +/* + * 方法2:迭代法 + * + * 迭代计算平方根 + */ +float Algo_3_26_2(float A, float p, float e) { + if(A < 0.0) { + exit(ERROR); + } + + while((p * p - A) >= e || (p * p - A) <= -e) { + p = (p + A / p) / 2; + } + + return p; +} diff --git a/CLion/ExerciseBook/03.26/CMakeLists.txt b/CLion/ExerciseBook/03.26/CMakeLists.txt new file mode 100644 index 0000000..ea83805 --- /dev/null +++ b/CLion/ExerciseBook/03.26/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.26 03.26.c) +# 链接公共库 +target_link_libraries(03.26 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.27/03.27.c b/CLion/ExerciseBook/03.27/03.27.c new file mode 100644 index 0000000..83c440d --- /dev/null +++ b/CLion/ExerciseBook/03.27/03.27.c @@ -0,0 +1,105 @@ +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +/* 栈元素类型定义 */ +typedef struct { + int mval; + int nval; +} SElemType; + +/* + * (1)递归法 + * + * 递归计算阿克曼函数 + */ +int Algo_3_27_1(int m, int n); + +/* + * (2)利用栈模拟递归 + * + * 模拟栈计算阿克曼函数 + */ +int Algo_3_27_2(int m, int n); + + +int main(int argc, char* argv[]) { + + printf("akm(3,4) = %d\n", Algo_3_27_1(3, 4)); + + printf("akm(3,4) = %d\n", Algo_3_27_2(3, 4)); + + return 0; +} + + +/* + * (1)递归法 + * + * 递归计算阿克曼函数 + */ +int Algo_3_27_1(int m, int n) { + int akm, tmp; + + if(m < 0 || n < 0) { + exit(ERROR); + } + + if(m == 0) { + akm = n + 1; + } else if(n == 0) { + akm = Algo_3_27_1(m - 1, 1); + } else { + akm = Algo_3_27_1(m - 1, Algo_3_27_1(m, n - 1)); + } + + return akm; +} + +/* + * (2)利用栈模拟递归 + * + * 模拟栈计算阿克曼函数 + */ +int Algo_3_27_2(int m, int n) { + SElemType stack[1000]; + int top = -1; // 初始化为-1,而不是0 + + if(m < 0 || n < 0) { + exit(ERROR); + } + + // 先递增,再存值 + top++; + + stack[top].mval = m; + stack[top].nval = n; + + while(1) { + // m==0 + while(top>0 && stack[top].mval == 0) { + top--; + stack[top].mval = stack[top].mval - 1; + stack[top].nval = stack[top + 1].nval + 1; + } + + if(top==0 && stack[0].mval == 0) { + break; + } + + // m!=0 n!=0 + while(stack[top].nval > 0) { + top++; + stack[top].mval = stack[top - 1].mval; + stack[top].nval = stack[top - 1].nval - 1; + } + + // m!=0 n==0 + if(stack[top].nval == 0) { + stack[top].mval = stack[top].mval - 1; + stack[top].nval = 1; + } + } + + return stack[top].nval + 1; +} diff --git a/CLion/ExerciseBook/03.27/CMakeLists.txt b/CLion/ExerciseBook/03.27/CMakeLists.txt new file mode 100644 index 0000000..c0b0f86 --- /dev/null +++ b/CLion/ExerciseBook/03.27/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.27 03.27.c) +# 链接公共库 +target_link_libraries(03.27 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.28/03.28.c b/CLion/ExerciseBook/03.28/03.28.c new file mode 100644 index 0000000..872fedf --- /dev/null +++ b/CLion/ExerciseBook/03.28/03.28.c @@ -0,0 +1,157 @@ +#include +#include // 提供malloc、realloc、free、exit原型 +#include // 提供一些数值常量 +#include "Status.h" //**▲01 绪论**// + +/* + * ████ 注意 ████ + * + * 这里使用带头结点的循环链表表示队列 + */ + +/* 队列元素类型 */ +typedef int QElemType; + +/* 队列结点类型 */ +typedef struct QNode { + QElemType data; + struct QNode* next; +} QNode, * QueuePtr; + +/* 队列结构 */ +typedef struct { + QueuePtr rear; // 队尾指针 +} LinkQueue; // 队列的链式存储表示 + +// 队列初始化 +Status InitQueue_3_28(LinkQueue* Q); + +// 入队 +Status EnQueue_3_28(LinkQueue* Q, QElemType e); + +// 出队 +Status DeQueue_3_28(LinkQueue* Q, QElemType* e); + +// 输出队列元素 +void Output(LinkQueue Q); + + +int main(int argc, char* argv[]) { + LinkQueue Q; + int i; + QElemType e; + + printf("████ 初始化队列...\n"); + InitQueue_3_28(&Q); + + printf("████ 进行 5 次连续入队操作...\n"); + for(i = 1; i <= 5; i++) { + EnQueue_3_28(&Q, i); + + printf("█ 元素 \"%d\" 入队后,队列中的元素为:", i); + Output(Q); + } + + printf("████ 进行 5 次连续出队操作...\n"); + for(i = 1; i <= 5; i++) { + DeQueue_3_28(&Q, &e); + + printf("█ 元素 \"%d\" 出队后,队列中的元素为:", e); + Output(Q); + } + + return 0; +} + + +// 队列初始化 +Status InitQueue_3_28(LinkQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + // 创建头结点 + (*Q).rear = (QueuePtr) malloc(sizeof(QNode)); + if((*Q).rear == NULL) { + exit(OVERFLOW); + } + + // 头结点数据 + (*Q).rear->data = INT_MAX; + + // 循环队列,首尾相接 + (*Q).rear->next = (*Q).rear; + + return OK; +} + +// 入队 +Status EnQueue_3_28(LinkQueue* Q, QElemType e) { + QueuePtr p; + + if(Q == NULL || (*Q).rear == NULL) { + return ERROR; + } + + // 创建新结点,链式队列被认为不存在队满的问题 + p = (QueuePtr) malloc(sizeof(QNode)); + if(p == NULL) { + exit(OVERFLOW); + } + p->data = e; + + p->next = (*Q).rear->next; + (*Q).rear->next = p; + (*Q).rear = p; + + return OK; +} + +// 出队 +Status DeQueue_3_28(LinkQueue* Q, QElemType* e) { + QueuePtr h, p; + + if(Q == NULL || (*Q).rear == NULL) { + return ERROR; + } + + // 获取头结点 + h = (*Q).rear->next; + + // 如果只有一个头结点,说明没有元素,无法队列 + if(h == (*Q).rear) { + return ERROR; + } + + // 指向待移除元素 + p = h->next; + *e = p->data; + + h->next = p->next; + + // 如果只有一个元素 + if(p == (*Q).rear) { + // 更新队尾游标 + (*Q).rear = h; + } + + free(p); + + return OK; +} + +// 输出队列元素 +void Output(LinkQueue Q) { + QueuePtr p; + + if(Q.rear == NULL) { + printf("\n"); + return; + } + + for(p = Q.rear->next->next; p != Q.rear->next; p = p->next) { + printf("%d ", p->data); + } + + printf("\n"); +} diff --git a/CLion/ExerciseBook/03.28/CMakeLists.txt b/CLion/ExerciseBook/03.28/CMakeLists.txt new file mode 100644 index 0000000..5a07d80 --- /dev/null +++ b/CLion/ExerciseBook/03.28/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.28 03.28.c) +# 链接公共库 +target_link_libraries(03.28 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.29/03.29.c b/CLion/ExerciseBook/03.29/03.29.c new file mode 100644 index 0000000..4e6c3a7 --- /dev/null +++ b/CLion/ExerciseBook/03.29/03.29.c @@ -0,0 +1,130 @@ +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +/* 宏定义 */ +#define MAXQSIZE 100 // 最大队列长度 + +/* 队列元素类型 */ +typedef int QElemType; + +/* 顺序循环队列 */ +typedef struct { + QElemType* base; // 指向队列存储空间 + int front, rear; // 队头和队尾游标 + int tag; // 0表示未满,1表示满 +} SqQueue; + +// 队列初始化 +Status InitQueue_3_29(SqQueue* Q); + +// 入队 +Status EnQueue_3_29(SqQueue* Q, QElemType e); + +// 出队 +Status DeQueue_3_29(SqQueue* Q, QElemType* e); + +// 输出队列元素 +void Output(SqQueue Q); + + +int main(int argc, char* argv[]) { + SqQueue Q; + int i; + QElemType e; + + printf("████ 初始化队列...\n"); + InitQueue_3_29(&Q); + + printf("████ 进行 5 次连续入队操作...\n"); + for(i = 1; i <= 5; i++) { + EnQueue_3_29(&Q, i); + + printf("█ 元素 \"%d\" 入队后,队列中的元素为:", i); + Output(Q); + } + + printf("████ 进行 5 次连续出队操作...\n"); + for(i = 1; i <= 5; i++) { + DeQueue_3_29(&Q, &e); + + printf("█ 元素 \"%d\" 出队后,队列中的元素为:", e); + Output(Q); + } + + return 0; +} + + +// 队列初始化 +Status InitQueue_3_29(SqQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).base = (QElemType*) malloc(MAXQSIZE * sizeof(QElemType)); + if((*Q).base == NULL) { + exit(OVERFLOW); + } + + (*Q).front = (*Q).rear = 0; + (*Q).tag = 0; + + return OK; +} + +// 入队 +Status EnQueue_3_29(SqQueue* Q, QElemType e) { + // 队列满 + if((*Q).tag == 1) { + return ERROR; + } + + (*Q).base[(*Q).rear] = e; + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + + // 如果发现队满,则更新标记 + if((*Q).rear == (*Q).front) { + (*Q).tag = 1; + } + + return OK; +} + +// 出队 +Status DeQueue_3_29(SqQueue* Q, QElemType* e) { + // 队列空 + if((*Q).front == (*Q).rear && (*Q).tag == 0) { + return ERROR; + } + + *e = (*Q).base[(*Q).front]; + (*Q).front = ((*Q).front + 1) % MAXQSIZE; + + // 如果发现队空,则更新标记 + if((*Q).rear == (*Q).front) { + (*Q).tag = 0; + } + + return OK; +} + +// 输出队列元素 +void Output(SqQueue Q) { + int i; + + // 队列空 + if(Q.front == Q.rear && Q.tag == 0) { + printf("\n"); + return; + } + + i = Q.front; + + do { + printf("%d ", Q.base[i]); + i = (i + 1) % MAXQSIZE; + } while(i != Q.rear); + + printf("\n"); +} diff --git a/CLion/ExerciseBook/03.29/CMakeLists.txt b/CLion/ExerciseBook/03.29/CMakeLists.txt new file mode 100644 index 0000000..366926c --- /dev/null +++ b/CLion/ExerciseBook/03.29/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.29 03.29.c) +# 链接公共库 +target_link_libraries(03.29 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.30+03.32/03.30+03.32.c b/CLion/ExerciseBook/03.30+03.32/03.30+03.32.c new file mode 100644 index 0000000..a4dc5bc --- /dev/null +++ b/CLion/ExerciseBook/03.30+03.32/03.30+03.32.c @@ -0,0 +1,209 @@ +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// +#include "SqList.h" //**▲02 线性表**// + +/* 宏定义 */ +#define MAXQSIZE 5 // 队列长度,同时也是后续斐波那契数列数列的维度 +#define MAX 100000 + +/* 队列元素类型 */ +typedef int QElemType; + +/* 顺序循环队列 */ +typedef struct { + QElemType* base; // 指向队列存储空间 + int rear; // 队尾游标 + int length; // 队列元素个数 +} SqQueue; + +// 队列初始化 +Status InitQueue_3_30(SqQueue* Q); + +// 入队 +Status EnQueue_3_30(SqQueue* Q, QElemType e); + +// 出队 +Status DeQueue_3_30(SqQueue* Q, QElemType* e); + +/* + * 求k阶斐波那契数列满足特定条件的前n+1项 + */ +Status Algo_3_32(int k, SqList* fib); + + +// 输出队列元素 +void Output(SqQueue Q) { + int head; + + if(Q.length == 0) { + printf("\n"); + return; + } + + // 队头游标 + head = (Q.rear - Q.length + MAXQSIZE) % MAXQSIZE; + + do { + printf("%d ", Q.base[head]); + head = (head + 1) % MAXQSIZE; + } while(head != Q.rear); + + printf("\n"); +} + + +int main(int argc, char* argv[]) { + int i; + + printf("████████ 题 3.30 验证...\n"); + { + SqQueue Q; + QElemType e; + printf("████ 初始化队列...\n"); + InitQueue_3_30(&Q);\ + + printf("████ 进行 5 次连续入队操作...\n"); + for(i = 1; i <= 5; i++) { + EnQueue_3_30(&Q, i); + printf("█ 元素 \"%d\" 入队后,队列中的元素为:", i); + Output(Q); + } + + printf("████ 进行 5 次连续出队操作...\n"); + for(i = 1; i <= 5; i++) { + DeQueue_3_30(&Q, &e); + printf("█ 元素 \"%d\" 出队后,队列中的元素为:", e); + Output(Q); + } + } + + printf("████████ 题 3.32 验证...\n"); + { + SqList fib; + + Algo_3_32(MAXQSIZE, &fib); + printf("█ %d 阶斐波那契数列的前 %d 项为:\n", MAXQSIZE, fib.length); + for(i = 0; i < fib.length; i++) { + printf("%d ", fib.elem[i]); + } + printf("\n"); + } + + return 0; +} + + +// 队列初始化 +Status InitQueue_3_30(SqQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).base = (QElemType*) malloc(MAXQSIZE * sizeof(QElemType)); + if((*Q).base == NULL) { + exit(OVERFLOW); + } + + (*Q).rear = 0; + (*Q).length = 0; + + return OK; +} + +// 入队 +Status EnQueue_3_30(SqQueue* Q, QElemType e) { + + if(Q == NULL) { + return ERROR; + } + + // 注意队列满的条件 + if((*Q).length == MAXQSIZE) { + return ERROR; + } + + (*Q).base[(*Q).rear] = e; + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + + (*Q).length++; + + return OK; +} + +// 出队 +Status DeQueue_3_30(SqQueue* Q, QElemType* e) { + int head; + + if(Q == NULL) { + return ERROR; + } + + // 队列空 + if((*Q).length == 0) { + return ERROR; + } + + // 队头游标 + head = ((*Q).rear - (*Q).length + MAXQSIZE) % MAXQSIZE; + + *e = (*Q).base[head]; + + (*Q).length--; + + return OK; +} + +/* + * 求k阶斐波那契数列满足特定条件的前n+1项 + * + *【注】 + * 个人感觉这个题出的有些牵强。 + * 因为既然要求计算前n+1项,但队列只保留后k项,这说明那个前n+1项必定需要保存到别的地方。 + * 可是,既然保存了前n+1项,就没必要用到循环队列中的后k项了,多余... + * 如果本题改为计算满足条件的【第】n+1项的值,那么循环队列是有应用价值的 + */ +Status Algo_3_32(int k, SqList* fib) { + int flag; + int i, j, sum; + SqQueue Q; + ElemType e; + + if(k < 2 || MAX < 0) { + return ERROR; + } + + InitQueue_3_30(&Q); + InitList(fib); + + // 前k-1项为0 + for(i = 1; i <= k - 1; i++) { + EnQueue_3_30(&Q, 0); + ListInsert(fib, i, 0); + } + + // 第k项为1 + EnQueue_3_30(&Q, 1); + ListInsert(fib, i, 1); + + while((flag = GetElem(*fib, i, &e))==OK && e<=MAX){ + /* + * 计算循环队列中现有元素的和 + * 其实这一步可以改为计算顺序表中后k项的和,这样一来就没循环队列啥事了 + */ + for(j = 0, sum = 0; j < Q.length; j++) { + sum += Q.base[j]; + } + + // 此处的e只是用作临时变量 + DeQueue_3_30(&Q, &e); + + // 将新计算出的元素入队 + EnQueue_3_30(&Q, sum); + + // 顺便往顺序表中缓存一份 + ListInsert(fib, ++i, sum); + } + + return flag; +} diff --git a/CLion/ExerciseBook/03.30+03.32/CMakeLists.txt b/CLion/ExerciseBook/03.30+03.32/CMakeLists.txt new file mode 100644 index 0000000..a9fecc7 --- /dev/null +++ b/CLion/ExerciseBook/03.30+03.32/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.30+03.32 SqList.h SqList.c 03.30+03.32.c) +# 链接公共库 +target_link_libraries(03.30+03.32 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.30+03.32/SqList.c b/CLion/ExerciseBook/03.30+03.32/SqList.c new file mode 100644 index 0000000..b3087d7 --- /dev/null +++ b/CLion/ExerciseBook/03.30+03.32/SqList.c @@ -0,0 +1,105 @@ +/*============================= + * 线性表的顺序存储结构(顺序表) + * + * 包含算法: 2.3、2.4、2.5、2.6 + =============================*/ + +#include "SqList.h" + +/* + * ████████ 算法2.3 ████████ + * + * 初始化 + * + * 初始化成功则返回OK,否则返回ERROR。 + */ +Status InitList(SqList* L) { + // 分配指定容量的内存,如果分配失败,则返回NULL + (*L).elem = (ElemType*) malloc(LIST_INIT_SIZE * sizeof(ElemType)); + if((*L).elem == NULL) { + // 存储内存失败 + exit(OVERFLOW); + } + + (*L).length = 0; // 初始化顺序表长度为0 + (*L).listsize = LIST_INIT_SIZE; // 顺序表初始内存分配量 + + return OK; // 初始化成功 +} + +/* + * 取值 + * + * 获取顺序表中第i个元素,将其存储到e中。 + * 如果可以找到,返回OK,否则,返回ERROR。 + * + *【备注】 + * 教材中i的含义是元素位置,从1开始计数,但这不符合编码的通用约定。 + * 通常,i的含义应该指索引,即从0开始计数。 + */ +Status GetElem(SqList L, int i, ElemType* e) { + // 因为i的含义是位置,所以其合法范围是:[1, length] + if(i < 1 || i > L.length) { + return ERROR; //i值不合法 + } + + *e = L.elem[i - 1]; + + return OK; +} + +/* + * ████████ 算法2.4 ████████ + * + * 插入 + * + * 向顺序表第i个位置上插入e,插入成功则返回OK,否则返回ERROR。 + * + *【备注】 + * 教材中i的含义是元素位置,从1开始计数 + */ +Status ListInsert(SqList* L, int i, ElemType e) { + ElemType* newbase; + ElemType* p, * q; + + // 确保顺序表结构存在 + if(L == NULL || (*L).elem == NULL) { + return ERROR; + } + + // i值越界 + if(i < 1 || i > (*L).length + 1) { + return ERROR; + } + + // 若存储空间已满,则增加新空间 + if((*L).length >= (*L).listsize) { + // 基于现有空间扩容 + newbase = (ElemType*) realloc((*L).elem, ((*L).listsize + LISTINCREMENT) * sizeof(ElemType)); + if(newbase == NULL) { + // 存储内存失败 + exit(OVERFLOW); + } + + // 新基址 + (*L).elem = newbase; + // 存的存储空间 + (*L).listsize += LISTINCREMENT; + } + + // q为插入位置 + q = &(*L).elem[i - 1]; + + // 1.右移元素,腾出位置 + for(p = &(*L).elem[(*L).length - 1]; p >= q; --p) { + *(p + 1) = *p; + } + + // 2.插入e + *q = e; + + // 3.表长增1 + (*L).length++; + + return OK; +} diff --git a/CLion/ExerciseBook/03.30+03.32/SqList.h b/CLion/ExerciseBook/03.30+03.32/SqList.h new file mode 100644 index 0000000..ad62284 --- /dev/null +++ b/CLion/ExerciseBook/03.30+03.32/SqList.h @@ -0,0 +1,66 @@ +/*============================= + * 线性表的顺序存储结构(顺序表) + * + * 包含算法: 2.3、2.4、2.5、2.6 + =============================*/ + +#ifndef SQLIST_H +#define SQLIST_H + +#include +#include // 提供malloc、realloc、free、exit原型 +#include "Status.h" //**▲01 绪论**// + +/* 宏定义 */ +#define LIST_INIT_SIZE 100 // 顺序表存储空间的初始分配量 +#define LISTINCREMENT 10 // 顺序表存储空间的分配增量 + +/* 顺序表元素类型定义,跟队列元素类型一致 */ +typedef int ElemType; + +/* + * 顺序表结构 + * + * 注:elem在使用前需要先为其分配内存,且元素从elem[0]处开始存储 + */ +typedef struct { + ElemType* elem; // 顺序表存储空间的基址(指向顺序表所占内存的起始位置) + int length; // 当前顺序表长度(包含多少元素) + int listsize; // 当前分配的存储容量(可以存储多少元素) +} SqList; + + +/* + * ████████ 算法2.3 ████████ + * + * 初始化 + * + * 初始化成功则返回OK,否则返回ERROR。 + */ +Status InitList(SqList* L); + +/* + * 取值 + * + * 获取顺序表中第i个元素,将其存储到e中。 + * 如果可以找到,返回OK,否则,返回ERROR。 + * + *【备注】 + * 教材中i的含义是元素位置,从1开始计数,但这不符合编码的通用约定。 + * 通常,i的含义应该指索引,即从0开始计数。 + */ +Status GetElem(SqList L, int i, ElemType* e); + +/* + * ████████ 算法2.4 ████████ + * + * 插入 + * + * 向顺序表第i个位置上插入e,插入成功则返回OK,否则返回ERROR。 + * + *【备注】 + * 教材中i的含义是元素位置,从1开始计数 + */ +Status ListInsert(SqList* L, int i, ElemType e); + +#endif diff --git a/CLion/ExerciseBook/03.31/03.31.c b/CLion/ExerciseBook/03.31/03.31.c new file mode 100644 index 0000000..e50856f --- /dev/null +++ b/CLion/ExerciseBook/03.31/03.31.c @@ -0,0 +1,89 @@ +#include +#include // 提供strlen函数的原型 +#include "Status.h" //**▲01 绪论**// +#include "SqStack.h" //**▲03 栈和队列**// + +/* + * 判断序列s是否为回文序列 + * 空串也被视为回文序列 + */ +Status Algo_3_31(char* s); + + +int main(int argc, char* argv[]) { + char* a = "abcdedcba@"; + char* b = "ababab@"; + + if(Algo_3_31(a)) { + printf("%s(不包括@)是回文序列!\n", a); + } else { + printf("%s(不包括@)不是回文序列!!\n", a); + } + + if(Algo_3_31(b)) { + printf("%s(不包括@)是回文序列!\n", b); + } else { + printf("%s(不包括@)不是回文序列!!\n", b); + } + + return 0; +} + + +/* + * 判断序列s是否为回文序列 + * 空串也被视为回文序列 + */ +Status Algo_3_31(char* s) { + int len; + int i; + int m, n; // 回文串前半段的结尾下标和后半段的开始下标 + SqStack S; + SElemType e; + + len = (int) strlen(s); + + if(len == 0 || s[len - 1] != '@') { + return FALSE; + } + + // 空串被认为是回文序列 + if(len == 1) { + return TRUE; + } + + if(len % 2 == 0) { + m = (len - 2) / 2 - 1; + n = m + 2; + } else { + m = (len - 2) / 2; + n = m + 1; + } + + InitStack(&S); + + // 先把回文串前半段入栈 + for(i = 0; i <= m; i++) { + Push(&S, s[i]); + } + + // 取出回文串前半段,与字符串序列后半段比对 + for(i = n; i <= len - 2; i++) { + if(!StackEmpty(S)) { + Pop(&S, &e); + } else { + break; + } + + if(s[i] != e) { + return ERROR; + } + } + + // 完美的状态应该是栈正好为空,且i为len-1 + if(!(StackEmpty(S) && i == len - 1)) { + return ERROR; + } + + return OK; +} diff --git a/CLion/ExerciseBook/03.31/CMakeLists.txt b/CLion/ExerciseBook/03.31/CMakeLists.txt new file mode 100644 index 0000000..7821e06 --- /dev/null +++ b/CLion/ExerciseBook/03.31/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.31 SqStack.h SqStack.c 03.31.c) +# 链接公共库 +target_link_libraries(03.31 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.31/SqStack.c b/CLion/ExerciseBook/03.31/SqStack.c new file mode 100644 index 0000000..d709204 --- /dev/null +++ b/CLion/ExerciseBook/03.31/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/ExerciseBook/03.31/SqStack.h b/CLion/ExerciseBook/03.31/SqStack.h new file mode 100644 index 0000000..e840954 --- /dev/null +++ b/CLion/ExerciseBook/03.31/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/ExerciseBook/03.33/03.33.c b/CLion/ExerciseBook/03.33/03.33.c new file mode 100644 index 0000000..6a984a3 --- /dev/null +++ b/CLion/ExerciseBook/03.33/03.33.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include "Status.h" //**▲01 绪论**// +#include "SqQueue.h" //**▲03 栈和队列**// + +// 测试函数,打印整型 +void PrintElem(QElemType e) { + printf("%d ", e); +} + + +int main(int argc, char* argv[]) { + SqQueue Q; + QElemType e; + int i; + int random; + + printf("████ 初始化队列...\n"); + InitQueue(&Q); + + printf("████ 进行 5 次连续入队操作...\n"); + srand((unsigned) time(NULL)); + for(i = 1; i <= 5; i++) { + + random = rand() % 100; + + EnQueue_3_33(&Q, random); + + printf("█ 元素 \"%2d\" 入队后,队列中的元素为:", random); + QueueTraverse(Q, PrintElem); + } + + printf("████ 进行 5 次连续出队操作...\n"); + for(i = 1; i <= 5; i++) { + DeQueue(&Q, &e); + + printf("█ 元素 \"%2d\" 出队后,队列中的元素为:", e); + QueueTraverse(Q, PrintElem); + } + + return 0; +} diff --git a/CLion/ExerciseBook/03.33/CMakeLists.txt b/CLion/ExerciseBook/03.33/CMakeLists.txt new file mode 100644 index 0000000..054e337 --- /dev/null +++ b/CLion/ExerciseBook/03.33/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.33 SqQueue.h SqQueue.c 03.33.c) +# 链接公共库 +target_link_libraries(03.33 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.33/SqQueue.c b/CLion/ExerciseBook/03.33/SqQueue.c new file mode 100644 index 0000000..7a44794 --- /dev/null +++ b/CLion/ExerciseBook/03.33/SqQueue.c @@ -0,0 +1,113 @@ +/*============================= + * 队列的顺序存储结构(顺序队列) + ==============================*/ + +#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 EnQueue_3_33(SqQueue* Q, QElemType e) { + int head, tail; + + if(Q == NULL) { + return ERROR; + } + + // 如果队列满,直接返回 + if(((*Q).rear + 1) % MAXQSIZE == (*Q).front) { + return ERROR; + } + + // 如果队列为空,直接插入到队尾 + if((*Q).front == (*Q).rear) { + (*Q).base[(*Q).rear] = e; + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + return OK; + } + + head = (*Q).base[(*Q).front]; // 队头元素值 + tail = (*Q).base[((*Q).rear - 1 + MAXQSIZE) % MAXQSIZE]; // 队尾元素值 + + // 如果待插入元素的作业时间不小于队列首尾元素作业的平均时间,则将元素插在队尾 + if(e >= (head + tail) / 2) { + (*Q).base[(*Q).rear] = e; + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + + // 否则,插在队头 + } else { + (*Q).front = ((*Q).front - 1 + MAXQSIZE) % MAXQSIZE; + (*Q).base[(*Q).front] = e; + } + + 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/ExerciseBook/03.33/SqQueue.h b/CLion/ExerciseBook/03.33/SqQueue.h new file mode 100644 index 0000000..f848167 --- /dev/null +++ b/CLion/ExerciseBook/03.33/SqQueue.h @@ -0,0 +1,63 @@ +/*============================= + * 队列的顺序存储结构(顺序队列) + ==============================*/ + +#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 EnQueue_3_33(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/ExerciseBook/03.34/03.34.c b/CLion/ExerciseBook/03.34/03.34.c new file mode 100644 index 0000000..badbbf3 --- /dev/null +++ b/CLion/ExerciseBook/03.34/03.34.c @@ -0,0 +1,152 @@ +#include +#include "Status.h" //**▲01 绪论**// +#include "SqQueue.h" //**▲03 栈和队列**// + +/* + * 生成车厢调度序列,其中: + * 'L': 直接从入口开到出口 + * 'E': 从队头进入中转队列 + * 'A': 从队尾进入中转队列 + * 'D': 从队头出队 + * + * 假设入口在右边,出口在左边,中转队列的队头在左边,队尾在右边,且车厢读取顺序【从左到右】 + * + * En :等待调度的列车序列 + * seq:调度序列 + */ +void Algo_3_34(char* En, char seq[]); + +/* + * 使用约定好的调度序列seq,将入口En处的车厢调度到出口Ex + * + * 该方法可看做是对调度序列的检验 + * + * En :等待调度的列车序列 + * seq:调度序列 + * Ex :调度完成后的序列 + */ +Status Dispatch(char* En, char* seq, char Ex[]); + + +int main(int argc, char* argv[]) { + char* En = "HPSHHSHPPHHSPSHSPSSHSP"; // 等待调度的列车序列 + char Ex[100] = {'\0'}; // 调度完成的列车序列 + char seq[100] = {'\0'}; // 调度序列 + + printf("████ 入口处的序列:\n"); + printf("█ En = %s\n", En); + + Algo_3_34(En, seq); + printf("████ 生成的调度序列:\n"); + printf("█ seq = %s\n", seq); + + Dispatch(En, seq, Ex); + printf("████ 根据生成的调度序列,对入口处的车厢调度,调度完成后的车厢序列:\n"); + printf("█ Ex = %s\n", Ex); + + return 0; +} + + +/* + * 生成车厢调度序列,其中: + * 'L': 直接从入口开到出口 + * 'E': 从队头进入中转队列 + * 'A': 从队尾进入中转队列 + * 'D': 从队头出队 + * + * 假设入口在右边,出口在左边,中转队列的队头在左边,队尾在右边,且车厢读取顺序【从左到右】 + * + * En :等待调度的列车序列 + * seq:调度序列 + */ +void Algo_3_34(char* En, char seq[]) { + int i, j; + SqQueue Q; + QElemType e; + + // 初始化一个中转队列 + InitQueue(&Q); + + for(i = j = 0; En[i] != '\0'; i++) { + // 如果是硬座,直接调度到目的地 + if(En[i] == 'P') { + seq[j++] = 'L'; // L代表直接从入口开到出口 + } + + // 如果是软卧,从队头进入中转队列 + if(En[i] == 'S') { + EnQueue_3_34(&Q, En[i], 0); + seq[j++] = 'E'; + } + + // 如果是硬卧,从队尾进入中转队列 + if(En[i] == 'H') { + EnQueue_3_34(&Q, En[i], 1); + seq[j++] = 'A'; + } + } + + // 将中转队列中的软卧和硬卧从队头出队 + while(Q.front != Q.rear) { + DeQueue(&Q, &e); + seq[j++] = 'D'; + } + + seq[j] = '\0'; +} + +/* + * 使用约定好的调度序列seq,将入口En处的车厢调度到出口Ex + * + * 该方法可看做是对调度序列的检验 + * + * En :等待调度的列车序列 + * seq:调度序列 + * Ex :调度完成后的序列 + */ +Status Dispatch(char* En, char* seq, char Ex[]) { + int i, j, k; + SqQueue Q; + QElemType e; + + // 初始化一个中转队列 + InitQueue(&Q); + + i = j = k = 0; + + // 遍历调度序列 + while(seq[k] != '\0') { + // 如果是硬座,直接从入口开到出口 + if(seq[k] == 'L') { + Ex[j++] = En[i++]; + } + + // 如果是软卧,从队头进入中转队列 + if(seq[k] == 'E') { + EnQueue_3_34(&Q, En[i++], 0); + } + + // 如果是硬卧,从队尾进入中转队列 + if(seq[k] == 'A') { + EnQueue_3_34(&Q, En[i++], 1); + } + + // 将中转队列中的软卧和硬卧从队头出队 + if(seq[k] == 'D') { + DeQueue(&Q, &e); + Ex[j++] = e; + } + + k++; + } + + // 如果调度序列为空,但是入口处存在未调度的车厢,或者中转队列中存在未处理的车厢,则表示发生错误 + if(seq[k] == '\0' && (En[i] || Q.front == Q.rear)) { + return ERROR; + } + + Ex[j] = '\0'; + + return OK; +} diff --git a/CLion/ExerciseBook/03.34/CMakeLists.txt b/CLion/ExerciseBook/03.34/CMakeLists.txt new file mode 100644 index 0000000..3e0163c --- /dev/null +++ b/CLion/ExerciseBook/03.34/CMakeLists.txt @@ -0,0 +1,7 @@ +# 包含公共库 +include_directories(${CMAKE_SOURCE_DIR}/Status) + +# 生成可执行文件 +add_executable(03.34 SqQueue.h SqQueue.c 03.34.c) +# 链接公共库 +target_link_libraries(03.34 Scanf_lib) \ No newline at end of file diff --git a/CLion/ExerciseBook/03.34/SqQueue.c b/CLion/ExerciseBook/03.34/SqQueue.c new file mode 100644 index 0000000..14e2b1d --- /dev/null +++ b/CLion/ExerciseBook/03.34/SqQueue.c @@ -0,0 +1,98 @@ +/*============================= + * 队列的顺序存储结构(顺序队列) + ==============================*/ + +#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; +} + +/* + * 入队 + * + * di代表入队方向,0指示从队头入队,1指示从队尾入队 + */ +Status EnQueue_3_34(SqQueue* Q, QElemType e, int di) { + + // 从队头入队 + if(di == 0) { + (*Q).front = ((*Q).front - 1 + MAXQSIZE) % MAXQSIZE; + (*Q).base[(*Q).front] = e; + return OK; + } + + // 从队尾入队 + if(di == 1) { + (*Q).base[(*Q).rear] = e; + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + return OK; + } + + return ERROR; +} + +/* + * 出队 + * + * 移除队列头部的元素,将其存储到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/ExerciseBook/03.34/SqQueue.h b/CLion/ExerciseBook/03.34/SqQueue.h new file mode 100644 index 0000000..439862c --- /dev/null +++ b/CLion/ExerciseBook/03.34/SqQueue.h @@ -0,0 +1,65 @@ +/*============================= + * 队列的顺序存储结构(顺序队列) + ==============================*/ + +#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); + +/* + * 入队 + * + * di代表入队方向,0指示从队头入队,1指示从队尾入队 + */ +Status EnQueue_3_34(SqQueue* Q, QElemType e, int di); + +/* + * 出队 + * + * 移除队列头部的元素,将其存储到e中。 + */ +Status DeQueue(SqQueue* Q, QElemType* e); + +/* + * 遍历 + * + * 用visit函数访问队列Q + */ +Status QueueTraverse(SqQueue Q, void(Visit)(QElemType)); + +#endif diff --git a/CLion/ExerciseBook/CMakeLists.txt b/CLion/ExerciseBook/CMakeLists.txt index c0f1208..61c7125 100644 --- a/CLion/ExerciseBook/CMakeLists.txt +++ b/CLion/ExerciseBook/CMakeLists.txt @@ -28,4 +28,24 @@ add_subdirectory(02.34-02.36) add_subdirectory(02.37) add_subdirectory(02.38) add_subdirectory(02.39-02.40) -add_subdirectory(02.41-02.42) \ No newline at end of file +add_subdirectory(02.41-02.42) + +add_subdirectory(03.15) +add_subdirectory(03.16) +add_subdirectory(03.17) +add_subdirectory(03.18) +add_subdirectory(03.19) +add_subdirectory(03.20) +add_subdirectory(03.21) +add_subdirectory(03.22) +add_subdirectory(03.23) +add_subdirectory(03.24) +add_subdirectory(03.25) +add_subdirectory(03.26) +add_subdirectory(03.27) +add_subdirectory(03.28) +add_subdirectory(03.29) +add_subdirectory(03.30+03.32) +add_subdirectory(03.31) +add_subdirectory(03.33) +add_subdirectory(03.34) diff --git a/Dev-C++/ExerciseBook/03.15/03.15.cpp b/Dev-C++/ExerciseBook/03.15/03.15.cpp new file mode 100644 index 0000000..2a7c1ae --- /dev/null +++ b/Dev-C++/ExerciseBook/03.15/03.15.cpp @@ -0,0 +1,162 @@ +#include +#include "Status.h" //**01 **// + +/* 궨 */ +#define N 100 //ջ + +/* ˫ջԪͶ */ +typedef int SElemType; + +/* ˫ջаջջ */ +typedef enum { + Left, Right +} StackName; + +/* ˫ջṹ */ +typedef struct { + SElemType stack[N]; // һ㹻ջ + int top[2]; // ջָ +} TWS; + +// ʼջ +Status Inistack_3_15(TWS* tws); + +// ջnameָʾĸջԪ +Status Push_3_15(TWS* tws, StackName name, SElemType x); + +// ջnameָʾĸջƳԪ +Status Pop_3_15(TWS* tws, StackName name, SElemType* x); + +// ջԪأnameָʾĸջеԪ +void OutputStack(TWS tws, StackName name); + + +int main(int argc, char* argv[]) { + TWS S; + int i, x; + + printf(" ʼջ...\n"); + Inistack_3_15(&S); + + + printf(" ջѹԪ...\n"); + + for(i = 1; i <= 5; i++) { + Push_3_15(&S, Left, i); + Push_3_15(&S, Right, 2 * i); + } + printf(" ջеԪأջ׵ջ"); + OutputStack(S, Left); + printf(" ջеԪأջ׵ջ"); + OutputStack(S, Right); + + + printf(" ֱ𵯳ջջԪ...\n"); + + Pop_3_15(&S, Left, &x); + printf(" ջջԪΪ%d\n", x); + printf(" ջеԪأջ׵ջ"); + OutputStack(S, Left); + + Pop_3_15(&S, Right, &x); + printf(" ջջԪΪ%d\n", x); + printf(" ջеԪأջ׵ջ"); + OutputStack(S, Right); + + return 0; +} + + +// ʼջ +Status Inistack_3_15(TWS* tws) { + if(tws == NULL) { + return ERROR; + } + + (*tws).top[Left] = -1; // ջ0ջָ룬עⲻ0 + (*tws).top[Right] = N; // ջ1ջָ룬עⲻN-1 + + return OK; +} + +// ջnameָʾĸջԪ +Status Push_3_15(TWS* tws, StackName name, SElemType x) { + if(tws == NULL) { + return ERROR; + } + + // עջȫջ˷ѿռ + if((*tws).top[Left] + 1 == (*tws).top[Right]) { + return ERROR; + } + + // ƶջα꣬ٴԪ + switch(name) { + case Left: + (*tws).top[name]++; // ջαƶ + break; + case Right: + (*tws).top[name]--; // ұջαƶ + break; + default: + break; + } + + // Ԫ + (*tws).stack[(*tws).top[name]] = x; + + return OK; +} + +// ջnameָʾĸջƳԪ +Status Pop_3_15(TWS* tws, StackName name, SElemType* x) { + if(tws == NULL) { + return ERROR; + } + + // ƳԪأƶα + switch(name) { + case Left: + // жߵջǷΪ + if((*tws).top[name] == -1) { + return ERROR; + } + *x = (*tws).stack[(*tws).top[name]]; + (*tws).top[name]--; + break; + case Right: + // жұߵջǷΪ + if((*tws).top[name] == N) { + return ERROR; + } + *x = (*tws).stack[(*tws).top[name]]; + (*tws).top[name]++; + break; + default: + break; + } + + return OK; +} + +// ջԪأnameָʾĸջеԪ +void OutputStack(TWS tws, StackName name) { + int i; + + switch(name) { + case Left: + for(i = 0; i <= tws.top[name]; i++) { + printf("%d ", tws.stack[i]); + } + break; + case Right: + for(i = N - 1; i >= tws.top[name]; i--) { + printf("%d ", tws.stack[i]); + } + break; + default: + break; + } + + printf("\n"); +} diff --git a/Dev-C++/ExerciseBook/03.15/03.15.dev b/Dev-C++/ExerciseBook/03.15/03.15.dev new file mode 100644 index 0000000..459dbc1 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.15/03.15.dev @@ -0,0 +1,62 @@ +[Project] +FileName=03.15.dev +Name=03.15 +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=1 + +[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=03.15.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.16/03.16.cpp b/Dev-C++/ExerciseBook/03.16/03.16.cpp new file mode 100644 index 0000000..0ac7498 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.16/03.16.cpp @@ -0,0 +1,136 @@ +#include +#include "Status.h" //**01 **// +#include "SqStack.h" //**03 ջͶ**// + +/* + * ɳУ'I'ջ'O'ջ + * + * ұߣߣҳȡ˳򡾴ҡ + * + * En ȴȵг + * seq + */ +void Algo_3_16(char* En, char seq[]); + +/* + * ʹԼõĵseqEnijȵEx + * + * ÷ɿǶԵеļ + * + * En ȴȵг + * seq + * Ex ɺ + */ +Status Dispatch(char* En, char* seq, char Ex[]); + + +int main(int argc, char* argv[]) { + char* En = "HSHHSHHHSSHSSSHS"; // ȴȵг + char Ex[100] = {'\0'}; // ɵг + char seq[100] = {'\0'}; // + + printf(" ڴУ\n"); + printf(" En = %s\n", En); + + Algo_3_16(En, seq); + printf(" ɵĵУ\n"); + printf(" seq = %s\n", seq); + + Dispatch(En, seq, Ex); + printf(" ɵĵУڴijȣɺijУ\n"); + printf(" Ex = %s\n", Ex); + + return 0; +} + + +/* + * ɳУ'I'ջ'O'ջ + * + * ұߣߣȡ˳ + * + * En ȴȵг + * seq + */ +void Algo_3_16(char* En, char seq[]) { + int i, j; + SqStack S; + SElemType e; + + // ʼһתջ + InitStack(&S); + + // + for(i = j = 0; En[i] != '\0'; i++) { + // Ӳϯջ + if(En[i] == 'H') { + Push(&S, En[i]); + seq[j++] = 'I'; + } + + // ϯջջ൱ջת + if(En[i] == 'S') { + Push(&S, En[i]); + Pop(&S, &e); + seq[j++] = 'I'; + seq[j++] = 'O'; + } + } + + // תջеӲϯջ + while(!StackEmpty(S)) { + Pop(&S, &e); + seq[j++] = 'O'; + } + + seq[j] = '\0'; + + DestroyStack(&S); +} + +/* + * ʹԼõĵseqEnijȵEx + * + * ÷ɿǶԵеļ + * + * En ȴȵг + * seq + * Ex ɺ + */ +Status Dispatch(char* En, char* seq, char Ex[]) { + int i, j, k; + SqStack S; + SElemType e; + + // ʼһתջ + InitStack(&S); + + i = j = k = 0; + + // + while(seq[k] != '\0') { + // ջǣ򽫳תջ + if(seq[k] == 'I') { + Push(&S, En[i++]); + } + + // ջǣ򽫳ƳתƵڴ + if(seq[k] == 'O') { + Pop(&S, &e); + Ex[j++] = e; + } + + k++; + } + + // Ϊգڴδȵijᣬתջδijᣬʾ + if(seq[k] == '\0' && (En[i] || StackEmpty(S))) { + return ERROR; + } + + Ex[j] = '\0'; + + DestroyStack(&S); + + return OK; +} diff --git a/Dev-C++/ExerciseBook/03.16/03.16.dev b/Dev-C++/ExerciseBook/03.16/03.16.dev new file mode 100644 index 0000000..e06a53f --- /dev/null +++ b/Dev-C++/ExerciseBook/03.16/03.16.dev @@ -0,0 +1,82 @@ +[Project] +FileName=03.16.dev +Name=03.16 +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=03.16.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit2] +FileName=SqStack.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit3] +FileName=SqStack.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.16/SqStack.cpp b/Dev-C++/ExerciseBook/03.16/SqStack.cpp new file mode 100644 index 0000000..1919385 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.16/SqStack.cpp @@ -0,0 +1,109 @@ +/*========================= + * ջ˳洢ṹ˳ջ + ==========================*/ + +#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; +} + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * 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++/ExerciseBook/03.16/SqStack.h b/Dev-C++/ExerciseBook/03.16/SqStack.h new file mode 100644 index 0000000..13db596 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.16/SqStack.h @@ -0,0 +1,66 @@ +/*========================= + * ջ˳洢ṹ˳ջ + ==========================*/ + +#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); + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * 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++/ExerciseBook/03.17/03.17.cpp b/Dev-C++/ExerciseBook/03.17/03.17.cpp new file mode 100644 index 0000000..8b5329b --- /dev/null +++ b/Dev-C++/ExerciseBook/03.17/03.17.cpp @@ -0,0 +1,69 @@ +#include +#include "Status.h" //**01 **// +#include "SqStack.h" //**03 ջͶ**// + +/* + * жǷΪ"1&2"ģʽ + * У21 + * + * s֤У'@'Žβ + */ +Status Algo_3_17(char* s); + + +int main(int argc, char* argv[]) { + char* s = "a+b-c&c-b+a@"; + + printf("ж %s ǷϹ...\n", s); + + if(Algo_3_17(s)) { + printf(" ⣡\n"); + } else { + printf(" в⣡\n"); + } + + return 0; +} + + +/* + * жǷΪ"1&2"ģʽ + * У21 + * + * s֤У'@'Žβ + */ +Status Algo_3_17(char* s) { + SqStack S; + SElemType e; + int i; + + InitStack(&S); + i = 0; + + // '&'ǰջ + while(s[i] != '@' && s[i] != '&') { + Push(&S, s[i]); + i++; + } + + // '&' + if(s[i] != '@') { + i++; // & + + // '&'гջ + while(!StackEmpty(S) && s[i] != '@') { + Pop(&S, &e); + if(s[i] != e) { + return ERROR; + } + i++; + } + } + + // ջΪգǡ÷꣬˵ + if(StackEmpty(S) && s[i] == '@') { + return OK; + } + + return ERROR; +} diff --git a/Dev-C++/ExerciseBook/03.17/03.17.dev b/Dev-C++/ExerciseBook/03.17/03.17.dev new file mode 100644 index 0000000..bec20cd --- /dev/null +++ b/Dev-C++/ExerciseBook/03.17/03.17.dev @@ -0,0 +1,82 @@ +[Project] +FileName=03.17.dev +Name=03.17 +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=03.17.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit2] +FileName=SqStack.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit3] +FileName=SqStack.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.17/SqStack.cpp b/Dev-C++/ExerciseBook/03.17/SqStack.cpp new file mode 100644 index 0000000..f056778 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.17/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++/ExerciseBook/03.17/SqStack.h b/Dev-C++/ExerciseBook/03.17/SqStack.h new file mode 100644 index 0000000..a8fe835 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.17/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++/ExerciseBook/03.18/03.18.cpp b/Dev-C++/ExerciseBook/03.18/03.18.cpp new file mode 100644 index 0000000..77f3de4 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.18/03.18.cpp @@ -0,0 +1,72 @@ +#include +#include "Status.h" //**01 **// +#include "SqStack.h" //**03 ջͶ**// + +/* + * жʽǷԳ + * + *ע + * ֻжǷԳ֣ж͡˳Ƿȷ + */ +Status Algo_3_18(char* s); + + +int main(int argc, char* argv[]) { + char* s = "(1+2)*3/{2/[(4-5)*3]-5*(8-7)}"; + + printf("жϱʽ %s Ƿƥ...\n", s); + + if(Algo_3_18(s) == TRUE) { + printf(" ʽƥ䣡\n"); + } else { + printf(" ʽƥ䣡\n"); + } + + return 0; +} + + +/* + * жʽǷԳ + * + *ע + * ֻжǷԳ֣ж͡˳Ƿȷ + */ +Status Algo_3_18(char* s) { + SqStack S; + SElemType e; + int i; + + InitStack(&S); + + for(i = 0; s[i] != '\0'; i++) { + switch(s[i]) { + // ţջ + case '(': + case '[': + case '{': + Push(&S, s[i]); + break; + + // ţջ + case ')': + case ']': + case '}': + if(StackEmpty(S)) { + return ERROR; + } + Pop(&S, &e); + break; + + default: + break; + } + } + + // ջΪˣ˵Уɹ + if(StackEmpty(S)) { + return OK; + } + + return ERROR; +} diff --git a/Dev-C++/ExerciseBook/03.18/03.18.dev b/Dev-C++/ExerciseBook/03.18/03.18.dev new file mode 100644 index 0000000..6a927dd --- /dev/null +++ b/Dev-C++/ExerciseBook/03.18/03.18.dev @@ -0,0 +1,82 @@ +[Project] +FileName=03.18.dev +Name=03.18 +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=03.18.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[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= + diff --git a/Dev-C++/ExerciseBook/03.18/SqStack.cpp b/Dev-C++/ExerciseBook/03.18/SqStack.cpp new file mode 100644 index 0000000..f056778 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.18/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++/ExerciseBook/03.18/SqStack.h b/Dev-C++/ExerciseBook/03.18/SqStack.h new file mode 100644 index 0000000..a8fe835 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.18/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++/ExerciseBook/03.19/03.19.cpp b/Dev-C++/ExerciseBook/03.19/03.19.cpp new file mode 100644 index 0000000..ca3bd19 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.19/03.19.cpp @@ -0,0 +1,98 @@ +#include +#include "Status.h" //**01 **// +#include "SqStack.h" //**03 ջͶ**// + +// жʽǷԳ֣Ƿƥ +Status Algo_3_19(char* s); + +// жab֮Ƿƥ +Status Matching(char a, char b); + + +int main(int argc, char* argv[]) { + char* s = "(1+2)*3/{2/[(4-5)*3]-5*(8-7)}"; + + printf("жϱʽ %s Ƿƥ...\n", s); + + if(Algo_3_19(s)) { + printf(" ʽƥ䣡\n"); + } else { + printf(" ʽŲƥ䣡\n"); + } + + return 0; +} + + +// жʽǷԳ֣Ƿƥ +Status Algo_3_19(char* s) { + SqStack S; + SElemType e; + int i; + + InitStack(&S); + + for(i = 0; s[i] != '\0'; i++) { + switch(s[i]) { + // ţջ + case '(': + case '[': + case '{': + Push(&S, s[i]); + break; + + // ţջ + case ')': + case ']': + case '}': + if(StackEmpty(S)) { + return ERROR; + } + + Pop(&S, &e); + + // жǷƥ + if(!Matching(e, s[i])) { + return ERROR; + } + break; + + default: + break; + } + } + + if(StackEmpty(S)) { + return OK; + } + + return ERROR; +} + +// жab֮Ƿƥ +Status Matching(char a, char b) { + switch(a) { + case '(': + if(b != ')') { + return ERROR; + } + break; + + case '[': + if(b != ']') { + return ERROR; + } + break; + + case '{': + if(b != '}') { + return ERROR; + } + break; + + default: + return ERROR; + } + + return OK; +} diff --git a/Dev-C++/ExerciseBook/03.19/03.19.dev b/Dev-C++/ExerciseBook/03.19/03.19.dev new file mode 100644 index 0000000..7fabf62 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.19/03.19.dev @@ -0,0 +1,82 @@ +[Project] +FileName=03.19.dev +Name=03.19 +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=03.19.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit2] +FileName=SqStack.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit3] +FileName=SqStack.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.19/SqStack.cpp b/Dev-C++/ExerciseBook/03.19/SqStack.cpp new file mode 100644 index 0000000..f056778 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.19/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++/ExerciseBook/03.19/SqStack.h b/Dev-C++/ExerciseBook/03.19/SqStack.h new file mode 100644 index 0000000..a8fe835 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.19/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++/ExerciseBook/03.20/03.20.cpp b/Dev-C++/ExerciseBook/03.20/03.20.cpp new file mode 100644 index 0000000..235879b --- /dev/null +++ b/Dev-C++/ExerciseBook/03.20/03.20.cpp @@ -0,0 +1,222 @@ +#include +#include // ṩ system ԭ +#include "Status.h" //**01 **// + +/* 㷨ԹѰ·㷨 */ + +/* 궨 */ +#define SleepTime 2 +#define Row 10 +#define Col 17 + +/* ʷ */ +typedef enum { + East, South, West, North +} Direction; + +/* ɫö */ +typedef enum { + Color_1, Color_2, Color_3 +} Color; + +/* ջԪ */ +typedef struct { + int x, y; // صĺᡢ궨 + int di; // Ӵصһص"" +} SElemType; + +// ԹȾɫָͼȾɫ +void Algo_3_20(int g[][17], SElemType start); + +// ʼͼȾɫ +void InitGrap(int g[][17], SElemType* start); + +// Ļʾǰͼ +void PaintGrap(int g[][17]); + +// жijǷҪȾɫ +Status Pass(SElemType e, int g[][17]); + +// Ⱦɫĵ㼴±ǣȾӦɫ +void Mark(SElemType* e, int g[][17]); + +// ȡһȾɫĵϢ +Status NextPos(SElemType* e); + +// жϵǰĵǷ +Status IsCross(SElemType e); + + +int main(int argc, char** argv) { + int g[Row][Col]; // ͼɫ + SElemType start; // Ⱦɫ + + InitGrap(g, &start); + PaintGrap(g); + + Algo_3_20(g, start); + + return 0; +} + + +// ָͼȾɫ +void Algo_3_20(int g[][Col], SElemType start) { + SElemType e; + SElemType stack[10000]; // һ㹻ΪջȾɫĵ + int top = -1; // ջָ + + e = start; + do { + // ҪȾɫ + if(Pass(e, g)) { + Mark(&e, g); // Ⱦɫǰ + PaintGrap(g); + stack[++top] = e; // ʹصջ + NextPos(&e); // óһصϢ + } else { + if(top != -1) { + e = stack[top--]; + + // ջеĵû෽ɷ + while(e.di == North && top != -1) { + e = stack[top--]; + } + + if(e.di < North) { + e.di++; + stack[++top] = e; + NextPos(&e); + } + } + } + } while(top != -1); // ջΪ +} + +/* ʼͼȾɫ */ +void InitGrap(int g[][Col], SElemType* start) { + int i, j; + + // ɫ0ɫ1 + int a[Row][Col]={{0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0}, + {0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0}, + {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, + {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, + {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, + {0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0}, + {0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0}, + {0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0}}; + + for(i = 0; i < 10; i++) { + for(j = 0; j < 17; j++) { + g[i][j] = a[i][j]; + } + } + + (*start).x = 9; // + (*start).y = 8; + (*start).di = East; // East +} + +/* Ļʾǰͼ */ +void PaintGrap(int g[][Col]) { + int i, j; + + Wait(SleepTime); + system("cls"); + + for(i = 0; i < Row; i++) { + for(j = 0; j < Col; j++) { + // ɫ0"^"ʾ + if(g[i][j] == Color_1) { + printf("."); + } + + // ɫ1ʾΪհ + if(g[i][j] == Color_2) { + printf(" "); + } + + // ɫ2"*"ʾ + if(g[i][j] == Color_3) { + printf("*"); + } + + if(j && !(j % (Col - 1))) { + printf("\n"); + } + } + } + + printf("\n"); +} + +/* жijǷҪȾɫ */ +Status Pass(SElemType e, int g[][Col]) { + int x = e.x; + int y = e.y; + + // ɫΪ1ĵȾɫ + if(g[x][y] == 1) { + return TRUE; + } else { + return FALSE; + } +} + +/* Ⱦɫĵ㼴±ǣȾӦɫ */ +void Mark(SElemType* e, int g[][Col]) { + int x = (*e).x; + int y = (*e).y; + + (*e).di = East; // Ǵصǰ + + g[x][y] = 2; // صɫȾΪɫ2 +} + +/* ȡһȾɫĵϢ */ +Status NextPos(SElemType* e) { + SElemType tmp; + tmp = *e; + + switch(tmp.di) { + case East: + (tmp.y)++; // East + break; + case South: + (tmp.x)++; // South + break; + case West: + (tmp.y)--; // West + break; + case North: + (tmp.x)--; // North + break; + default: + return FALSE; + } + + if(IsCross(tmp)) { + ++(*e).di; + NextPos(e); + } else { + *e = tmp; + } + + return TRUE; +} + +/* жϵǰĵǷ */ +Status IsCross(SElemType e) { + int x = e.x; + int y = e.y; + + // Խ + if(x < 0 || y < 0 || x > Row - 1 || y > Col - 1) { + return OK; + } else { + return ERROR; + } +} diff --git a/Dev-C++/ExerciseBook/03.20/03.20.dev b/Dev-C++/ExerciseBook/03.20/03.20.dev new file mode 100644 index 0000000..d14d6c1 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.20/03.20.dev @@ -0,0 +1,62 @@ +[Project] +FileName=03.20.dev +Name=03.20 +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=1 + +[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=03.20.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.21/03.21.cpp b/Dev-C++/ExerciseBook/03.21/03.21.cpp new file mode 100644 index 0000000..a7b163e --- /dev/null +++ b/Dev-C++/ExerciseBook/03.21/03.21.cpp @@ -0,0 +1,124 @@ +#include +#include // ṩstrlenԭ +#include // ṩisalphaԭ +#include "SqStack.h" //**03 ջͶ**// + +// ʽֵķ +static const char OP[] = {'+', '-', '*', '/'}; + +/* + * ȼOPǺӦġ + * ֻŵȼ + */ +static const char PrecedeTable[7][7] = { + {'>', '>', '<', '<'}, + {'>', '>', '<', '<'}, + {'>', '>', '>', '>'}, + {'>', '>', '>', '>'} +}; + +/* + * ݸ׺ʽӦ沨ʽ׺ʽ + * 罫"a+b*c-d/e"תΪ"abc*+de/-" + * + *ע + * ʽıΪĸֻ'+''-''*''/' + */ +char* Algo_3_21(char s[]); + +/* + * жջвo1ʽеIJo2ȼ + * + * '>''<''='ָʾo1o2ȼ + */ +char Precede(char o1, char o2); + + +int main(int argc, char* argv[]) { + char* s = "a+b*c-d/e"; + + printf("׺ʽΪ"); + printf("%s\n", s); + + printf("׺ʽΪ"); + printf("%s\n", Algo_3_21(s)); + + return 0; +} + + +/* + * ݸ׺ʽӦ沨ʽ׺ʽ + * 罫"a+b*c-d/e"תΪ"abc*+de/-" + * + *ע + * ʽıΪĸֻ'+''-''*''/' + */ +char* Algo_3_21(char s[]) { + int i, j; + SqStack S; // ջʱ洢 + SElemType e; // SElemTypecharͣcharintͿֱӱȽ + char* c; + + // 沨ʽ + c = (char*) malloc((strlen(s) + 1) * sizeof(char)); + + InitStack(&S); + + for(i = j = 0; s[i] != '\0'; i++) { + // ĸΪ + if(isalpha(s[i])) { + c[j++] = s[i]; // ĸֱӴ沨ʽ + + // + } else { + while(!StackEmpty(S)) { + // ȡջԪ + GetTop(S, &e); + + // ջȼ + if(Precede(e, s[i]) == '>') { + Pop(&S, &e); // ջȼߵIJջ + c[j++] = e; // ջIJӵ沨ʽ + + // ջвȼʱѭ + } else { + break; + } + } + + // ʽжȡIJѹջ + Push(&S, s[i]); + } + } + + // ջв׷ӵ沨ʽ + while(!StackEmpty(S)) { + Pop(&S, &e); + c[j++] = e; + } + + // ת沨ʽ"\0"ǽ + c[j] = '\0'; + + return c; +} + +/* + * жջвo1ʽеIJo2ȼ + * + * '>''<''='ָʾo1o2ȼ + */ +char Precede(char o1, char o2) { + int x, y; + + // ȡָеλ + char* p1 = strchr(OP, o1); + char* p2 = strchr(OP, o2); + + // һȼ + x = p1 - OP; + y = p2 - OP; + + return PrecedeTable[x][y]; +} diff --git a/Dev-C++/ExerciseBook/03.21/03.21.dev b/Dev-C++/ExerciseBook/03.21/03.21.dev new file mode 100644 index 0000000..1e16b61 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.21/03.21.dev @@ -0,0 +1,82 @@ +[Project] +FileName=03.21.dev +Name=03.21 +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 + +[Unit2] +FileName=SqStack.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit1] +FileName=03.21.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit3] +FileName=SqStack.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.21/SqStack.cpp b/Dev-C++/ExerciseBook/03.21/SqStack.cpp new file mode 100644 index 0000000..f0b3b3d --- /dev/null +++ b/Dev-C++/ExerciseBook/03.21/SqStack.cpp @@ -0,0 +1,106 @@ +/*========================= + * ջ˳洢ṹ˳ջ + ==========================*/ + +#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 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; +} diff --git a/Dev-C++/ExerciseBook/03.21/SqStack.h b/Dev-C++/ExerciseBook/03.21/SqStack.h new file mode 100644 index 0000000..a7a5f21 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.21/SqStack.h @@ -0,0 +1,66 @@ +/*========================= + * ջ˳洢ṹ˳ջ + ==========================*/ + +#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 GetTop(SqStack S, SElemType* e); + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e); + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e); + +#endif diff --git a/Dev-C++/ExerciseBook/03.22/03.22.cpp b/Dev-C++/ExerciseBook/03.22/03.22.cpp new file mode 100644 index 0000000..8a9d494 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.22/03.22.cpp @@ -0,0 +1,86 @@ +#include +#include // ṩisdigitԭ +#include "Status.h" //**01 **// +#include "SqStack.h" //**03 ջͶ**// + +/* + * 沨ʽֵҪΪλ + * + *ע + * ʽıΪĸֻ'+''-''*''/' + */ +Status Algo_3_22(char c[], SElemType* Value); + +/* a oper b ֵ */ +char Operate(char a, char oper, char b); + + +int main(int argc, char* argv[]) { + char c[] = "124*+93/-"; + SElemType value; + + printf("֪沨ʽΪ"); + printf("%s\n", c); + + Algo_3_22(c, &value); + printf("沨ʽļΪ%d\n", value - 48); + + return 0; +} + + +/* + * 沨ʽֵҪΪλ + * + *ע + * ʽıΪĸֻ'+''-''*''/' + */ +Status Algo_3_22(char c[], SElemType* Value) { + int i; + SqStack S; + SElemType a, b; + + InitStack(&S); + + for(i = 0; c[i]!='\0'; ++i) { + // ַ + if(isdigit(c[i])) { + Push(&S, c[i]); + } else { + // ȵڲ + Pop(&S, &b); + Pop(&S, &a); + Push(&S, Operate(a, c[i], b)); + } + } + + Pop(&S, Value); + + if(!StackEmpty(S)) { + return ERROR; + } else { + return OK; + } +} + +/* a oper b ֵ */ +char Operate(char a, char oper, char b) { + char c; + + switch(oper) { + case '+': + c = (a - 48) + (b - 48) + 48; + break; + case '-': + c = (a - 48) - (b - 48) + 48; + break; + case '*': + c = (a - 48) * (b - 48) + 48; + break; + case '/': + c = (a - 48) / (b - 48) + 48; + break; + } + + return c; +} diff --git a/Dev-C++/ExerciseBook/03.22/03.22.dev b/Dev-C++/ExerciseBook/03.22/03.22.dev new file mode 100644 index 0000000..84a4334 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.22/03.22.dev @@ -0,0 +1,82 @@ +[Project] +FileName=03.22.dev +Name=03.22 +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 + +[Unit2] +FileName=SqStack.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit1] +FileName=03.22.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit3] +FileName=SqStack.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.22/SqStack.cpp b/Dev-C++/ExerciseBook/03.22/SqStack.cpp new file mode 100644 index 0000000..f056778 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.22/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++/ExerciseBook/03.22/SqStack.h b/Dev-C++/ExerciseBook/03.22/SqStack.h new file mode 100644 index 0000000..a8fe835 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.22/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++/ExerciseBook/03.23/03.23.cpp b/Dev-C++/ExerciseBook/03.23/03.23.cpp new file mode 100644 index 0000000..902b323 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.23/03.23.cpp @@ -0,0 +1,109 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include // ṩstrlenԭ +#include // ṩisalphaԭ +#include "SqStack.h" //**03 ջͶ**// + +/* + * ׺ʽ沨ʽתΪǰ׺ʽʽ + * + *ע + * ʽıΪĸֻ'+''-''*''/' + */ +char* Algo_3_23(char* c); + +// ַתΪַ +char* CharToStr(char c); + +// ƴַabƴӺַ +char* StrCat(char* a, char* b); + + +int main(int argc, char* argv[]) { + char* s = "abc+*de/-"; + + printf("׺ʽ%s\n", "a*(b+c)-d/e"); + + printf("׺ʽ"); + printf("%s\n", s); + + printf("ǰ׺ʽ"); + printf("%s\n", Algo_3_23(s)); + + return 0; +} + + +/* + * ׺ʽ沨ʽתΪǰ׺ʽʽ + * + *ע + * ʽıΪĸֻ'+''-''*''/' + */ +char* Algo_3_23(char* c) { + int i; + char* s, * e1, * e2; + SqStack S; + + InitStack(&S); + + for(i = 0; c[i] != '\0'; i++) { + s = CharToStr(c[i]); + + // c[i]Ϊ(Dz)ջӦԪ + if(!isalpha(c[i])) { + if(Pop(&S, &e2) && Pop(&S, &e1)) { + s = StrCat(s, StrCat(e1, e2)); + } else { + return NULL; + } + } + + Push(&S, s); + } + + Pop(&S, &s); + + if(StackEmpty(S)) { + return s; + } + + return NULL; +} + +// ַתΪַ +char* CharToStr(char c) { + char* s; + + s = (char*) malloc(2 * sizeof(char)); + + s[0] = c; + s[1] = '\0'; + + return s; +} + +// ƴַabƴӺַ +char* StrCat(char* a, char* b) { + char* s; + int i, j, alen, blen; + + alen = (int) strlen(a); + blen = (int) strlen(b); + + s = (char*) malloc((alen + blen + 1) * sizeof(char)); + + j = 0; + + for(i = 0; i < alen; i++) { + s[j++] = a[i]; + } + + for(i = 0; i < blen; i++) { + s[j++] = b[i]; + } + + s[j] = '\0'; + + return s; +} diff --git a/Dev-C++/ExerciseBook/03.23/03.23.dev b/Dev-C++/ExerciseBook/03.23/03.23.dev new file mode 100644 index 0000000..9a689a9 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.23/03.23.dev @@ -0,0 +1,82 @@ +[Project] +FileName=03.23.dev +Name=03.23 +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 + +[Unit2] +FileName=SqStack.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit1] +FileName=03.23.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit3] +FileName=SqStack.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.23/SqStack.cpp b/Dev-C++/ExerciseBook/03.23/SqStack.cpp new file mode 100644 index 0000000..f056778 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.23/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++/ExerciseBook/03.23/SqStack.h b/Dev-C++/ExerciseBook/03.23/SqStack.h new file mode 100644 index 0000000..016fc86 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.23/SqStack.h @@ -0,0 +1,64 @@ +/*========================= + * ջ˳洢ṹ˳ջ + ==========================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define STACK_INIT_SIZE 100 // ˳ջ洢ռijʼ +#define STACKINCREMENT 10 // ˳ջ洢ռķ + +/* + * ˳ջԪͶ + * + *ע + * ʹַͣSElemTypeָһַ + */ +typedef char* 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++/ExerciseBook/03.24/03.24.cpp b/Dev-C++/ExerciseBook/03.24/03.24.cpp new file mode 100644 index 0000000..847e194 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.24/03.24.cpp @@ -0,0 +1,36 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* + * ݹg(m,n) + */ +int Algo_3_24(int m, int n); + + +int main(int argc, char* argv[]) { + int m, n; + + m = 5; + n = 2; + + printf("g(%d,%d) = %d\n", m, n, Algo_3_24(m, n)); + + return 0; +} + + +/* + * ݹg(m,n) + */ +int Algo_3_24(int m, int n) { + if(m < 0 || n < 0) { + exit(ERROR); + } + + if(m == 0) { + return 0; + } else { + return Algo_3_24(m - 1, 2 * n) + n; + } +} diff --git a/Dev-C++/ExerciseBook/03.24/03.24.dev b/Dev-C++/ExerciseBook/03.24/03.24.dev new file mode 100644 index 0000000..9728b9e --- /dev/null +++ b/Dev-C++/ExerciseBook/03.24/03.24.dev @@ -0,0 +1,62 @@ +[Project] +FileName=03.24.dev +Name=03.24 +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=1 + +[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=03.24.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.25/03.25.cpp b/Dev-C++/ExerciseBook/03.25/03.25.cpp new file mode 100644 index 0000000..585b949 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.25/03.25.cpp @@ -0,0 +1,67 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* + * 1ݹ鷨⣬ŵ㷨 + * + * ݹF(n) + */ +int Algo_3_25_1(int n); + +/* + * 2ݹĵŵǴ洢һֵ + * + * F(n) + */ +int Algo_3_25_2(int n); + + +int main(int argc, char* argv[]) { + + printf("F(10) = %d\n", Algo_3_25_1(10)); + + printf("F(10) = %d\n", Algo_3_25_2(10)); + + return 0; +} + + +/* + * 1ݹ鷨⣬ŵ㷨 + * + * ݹF(n) + */ +int Algo_3_25_1(int n) { + if(n < 0) { + exit(ERROR); + } + + if(n == 0) { + return n + 1; + } else { + return n * Algo_3_25_1(n / 2); + } +} + +/* + * 2ݹĵŵǴ洢һֵ + * + * F(n) + */ +int Algo_3_25_2(int n) { + int* a; + int i; + + if(n < 0) { + exit(ERROR); + } + + a = (int*) malloc((n + 1) * sizeof(int)); + + for(i = 1, a[0] = 1; i <= n; i++) { + a[i] = i * a[i / 2]; + } + + return a[n]; +} diff --git a/Dev-C++/ExerciseBook/03.25/03.25.dev b/Dev-C++/ExerciseBook/03.25/03.25.dev new file mode 100644 index 0000000..4150b3c --- /dev/null +++ b/Dev-C++/ExerciseBook/03.25/03.25.dev @@ -0,0 +1,62 @@ +[Project] +FileName=03.25.dev +Name=03.25 +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=1 + +[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=03.25.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.26/03.26.cpp b/Dev-C++/ExerciseBook/03.26/03.26.cpp new file mode 100644 index 0000000..86ed8de --- /dev/null +++ b/Dev-C++/ExerciseBook/03.26/03.26.cpp @@ -0,0 +1,62 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* + * 1ݹ鷨 + * + * ݹƽ + */ +float Algo_3_26_1(float A, float p, float e); + +/* + * 2 + * + * ƽ + */ +float Algo_3_26_2(float A, float p, float e); + + +int main(int argc, char* argv[]) { + + printf("50 = %f\n", Algo_3_26_1(50, 1, 0.000001f)); + + printf("50 = %f\n", Algo_3_26_2(50, 1, 0.000001f)); + + return 0; +} + + +/* + * 1ݹ鷨 + * + * ݹƽ + */ +float Algo_3_26_1(float A, float p, float e) { + if(A < 0.0) { + exit(ERROR); + } + + if((p * p - A) > -e && (p * p - A) < e) { + return p; + } else { + return Algo_3_26_1(A, (p + A / p) / 2, e); + } +} + +/* + * 2 + * + * ƽ + */ +float Algo_3_26_2(float A, float p, float e) { + if(A < 0.0) { + exit(ERROR); + } + + while((p * p - A) >= e || (p * p - A) <= -e) { + p = (p + A / p) / 2; + } + + return p; +} diff --git a/Dev-C++/ExerciseBook/03.26/03.26.dev b/Dev-C++/ExerciseBook/03.26/03.26.dev new file mode 100644 index 0000000..b75206e --- /dev/null +++ b/Dev-C++/ExerciseBook/03.26/03.26.dev @@ -0,0 +1,62 @@ +[Project] +FileName=03.26.dev +Name=03.26 +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=1 + +[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=03.26.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.27/03.27.cpp b/Dev-C++/ExerciseBook/03.27/03.27.cpp new file mode 100644 index 0000000..3a08e00 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.27/03.27.cpp @@ -0,0 +1,105 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* ջԪͶ */ +typedef struct { + int mval; + int nval; +} SElemType; + +/* + * (1)ݹ鷨 + * + * ݹ㰢 + */ +int Algo_3_27_1(int m, int n); + +/* + * (2)ջģݹ + * + * ģջ㰢 + */ +int Algo_3_27_2(int m, int n); + + +int main(int argc, char* argv[]) { + + printf("akm(3,4) = %d\n", Algo_3_27_1(3, 4)); + + printf("akm(3,4) = %d\n", Algo_3_27_2(3, 4)); + + return 0; +} + + +/* + * (1)ݹ鷨 + * + * ݹ㰢 + */ +int Algo_3_27_1(int m, int n) { + int akm, tmp; + + if(m < 0 || n < 0) { + exit(ERROR); + } + + if(m == 0) { + akm = n + 1; + } else if(n == 0) { + akm = Algo_3_27_1(m - 1, 1); + } else { + akm = Algo_3_27_1(m - 1, Algo_3_27_1(m, n - 1)); + } + + return akm; +} + +/* + * (2)ջģݹ + * + * ģջ㰢 + */ +int Algo_3_27_2(int m, int n) { + SElemType stack[1000]; + int top = -1; // ʼΪ-10 + + if(m < 0 || n < 0) { + exit(ERROR); + } + + // ȵٴֵ + top++; + + stack[top].mval = m; + stack[top].nval = n; + + while(1) { + // m==0 + while(top>0 && stack[top].mval == 0) { + top--; + stack[top].mval = stack[top].mval - 1; + stack[top].nval = stack[top + 1].nval + 1; + } + + if(top==0 && stack[0].mval == 0) { + break; + } + + // m!=0 n!=0 + while(stack[top].nval > 0) { + top++; + stack[top].mval = stack[top - 1].mval; + stack[top].nval = stack[top - 1].nval - 1; + } + + // m!=0 n==0 + if(stack[top].nval == 0) { + stack[top].mval = stack[top].mval - 1; + stack[top].nval = 1; + } + } + + return stack[top].nval + 1; +} diff --git a/Dev-C++/ExerciseBook/03.27/03.27.dev b/Dev-C++/ExerciseBook/03.27/03.27.dev new file mode 100644 index 0000000..55ae128 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.27/03.27.dev @@ -0,0 +1,62 @@ +[Project] +FileName=03.27.dev +Name=03.27 +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=1 + +[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=03.27.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.28/03.28.cpp b/Dev-C++/ExerciseBook/03.28/03.28.cpp new file mode 100644 index 0000000..150bcb2 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.28/03.28.cpp @@ -0,0 +1,157 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include // ṩһЩֵ +#include "Status.h" //**01 **// + +/* + * ע + * + * ʹôͷѭʾ + */ + +/* Ԫ */ +typedef int QElemType; + +/* н */ +typedef struct QNode { + QElemType data; + struct QNode* next; +} QNode, * QueuePtr; + +/* нṹ */ +typedef struct { + QueuePtr rear; // βָ +} LinkQueue; // еʽ洢ʾ + +// гʼ +Status InitQueue_3_28(LinkQueue* Q); + +// +Status EnQueue_3_28(LinkQueue* Q, QElemType e); + +// +Status DeQueue_3_28(LinkQueue* Q, QElemType* e); + +// Ԫ +void Output(LinkQueue Q); + + +int main(int argc, char* argv[]) { + LinkQueue Q; + int i; + QElemType e; + + printf(" ʼ...\n"); + InitQueue_3_28(&Q); + + printf(" 5 Ӳ...\n"); + for(i = 1; i <= 5; i++) { + EnQueue_3_28(&Q, i); + + printf(" Ԫ \"%d\" Ӻ󣬶еԪΪ", i); + Output(Q); + } + + printf(" 5 Ӳ...\n"); + for(i = 1; i <= 5; i++) { + DeQueue_3_28(&Q, &e); + + printf(" Ԫ \"%d\" Ӻ󣬶еԪΪ", e); + Output(Q); + } + + return 0; +} + + +// гʼ +Status InitQueue_3_28(LinkQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + // ͷ + (*Q).rear = (QueuePtr) malloc(sizeof(QNode)); + if((*Q).rear == NULL) { + exit(OVERFLOW); + } + + // ͷ + (*Q).rear->data = INT_MAX; + + // ѭУβ + (*Q).rear->next = (*Q).rear; + + return OK; +} + +// +Status EnQueue_3_28(LinkQueue* Q, QElemType e) { + QueuePtr p; + + if(Q == NULL || (*Q).rear == NULL) { + return ERROR; + } + + // ½㣬ʽбΪڶ + p = (QueuePtr) malloc(sizeof(QNode)); + if(p == NULL) { + exit(OVERFLOW); + } + p->data = e; + + p->next = (*Q).rear->next; + (*Q).rear->next = p; + (*Q).rear = p; + + return OK; +} + +// +Status DeQueue_3_28(LinkQueue* Q, QElemType* e) { + QueuePtr h, p; + + if(Q == NULL || (*Q).rear == NULL) { + return ERROR; + } + + // ȡͷ + h = (*Q).rear->next; + + // ֻһͷ㣬˵ûԪأ޷ + if(h == (*Q).rear) { + return ERROR; + } + + // ָƳԪ + p = h->next; + *e = p->data; + + h->next = p->next; + + // ֻһԪ + if(p == (*Q).rear) { + // ¶βα + (*Q).rear = h; + } + + free(p); + + return OK; +} + +// Ԫ +void Output(LinkQueue Q) { + QueuePtr p; + + if(Q.rear == NULL) { + printf("\n"); + return; + } + + for(p = Q.rear->next->next; p != Q.rear->next; p = p->next) { + printf("%d ", p->data); + } + + printf("\n"); +} diff --git a/Dev-C++/ExerciseBook/03.28/03.28.dev b/Dev-C++/ExerciseBook/03.28/03.28.dev new file mode 100644 index 0000000..a9b1c42 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.28/03.28.dev @@ -0,0 +1,62 @@ +[Project] +FileName=03.28.dev +Name=03.28 +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=1 + +[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=03.28.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.29/03.29.cpp b/Dev-C++/ExerciseBook/03.29/03.29.cpp new file mode 100644 index 0000000..711938e --- /dev/null +++ b/Dev-C++/ExerciseBook/03.29/03.29.cpp @@ -0,0 +1,130 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define MAXQSIZE 100 // г + +/* Ԫ */ +typedef int QElemType; + +/* ˳ѭ */ +typedef struct { + QElemType* base; // ָд洢ռ + int front, rear; // ͷͶβα + int tag; // 0ʾδ1ʾ +} SqQueue; + +// гʼ +Status InitQueue_3_29(SqQueue* Q); + +// +Status EnQueue_3_29(SqQueue* Q, QElemType e); + +// +Status DeQueue_3_29(SqQueue* Q, QElemType* e); + +// Ԫ +void Output(SqQueue Q); + + +int main(int argc, char* argv[]) { + SqQueue Q; + int i; + QElemType e; + + printf(" ʼ...\n"); + InitQueue_3_29(&Q); + + printf(" 5 Ӳ...\n"); + for(i = 1; i <= 5; i++) { + EnQueue_3_29(&Q, i); + + printf(" Ԫ \"%d\" Ӻ󣬶еԪΪ", i); + Output(Q); + } + + printf(" 5 Ӳ...\n"); + for(i = 1; i <= 5; i++) { + DeQueue_3_29(&Q, &e); + + printf(" Ԫ \"%d\" Ӻ󣬶еԪΪ", e); + Output(Q); + } + + return 0; +} + + +// гʼ +Status InitQueue_3_29(SqQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).base = (QElemType*) malloc(MAXQSIZE * sizeof(QElemType)); + if((*Q).base == NULL) { + exit(OVERFLOW); + } + + (*Q).front = (*Q).rear = 0; + (*Q).tag = 0; + + return OK; +} + +// +Status EnQueue_3_29(SqQueue* Q, QElemType e) { + // + if((*Q).tag == 1) { + return ERROR; + } + + (*Q).base[(*Q).rear] = e; + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + + // ֶ± + if((*Q).rear == (*Q).front) { + (*Q).tag = 1; + } + + return OK; +} + +// +Status DeQueue_3_29(SqQueue* Q, QElemType* e) { + // п + if((*Q).front == (*Q).rear && (*Q).tag == 0) { + return ERROR; + } + + *e = (*Q).base[(*Q).front]; + (*Q).front = ((*Q).front + 1) % MAXQSIZE; + + // ֶӿգ± + if((*Q).rear == (*Q).front) { + (*Q).tag = 0; + } + + return OK; +} + +// Ԫ +void Output(SqQueue Q) { + int i; + + // п + if(Q.front == Q.rear && Q.tag == 0) { + printf("\n"); + return; + } + + i = Q.front; + + do { + printf("%d ", Q.base[i]); + i = (i + 1) % MAXQSIZE; + } while(i != Q.rear); + + printf("\n"); +} diff --git a/Dev-C++/ExerciseBook/03.29/03.29.dev b/Dev-C++/ExerciseBook/03.29/03.29.dev new file mode 100644 index 0000000..cc03c1b --- /dev/null +++ b/Dev-C++/ExerciseBook/03.29/03.29.dev @@ -0,0 +1,62 @@ +[Project] +FileName=03.29.dev +Name=03.29 +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=1 + +[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=03.29.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.30+03.32/03.30+03.32.cpp b/Dev-C++/ExerciseBook/03.30+03.32/03.30+03.32.cpp new file mode 100644 index 0000000..8b246e8 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.30+03.32/03.30+03.32.cpp @@ -0,0 +1,209 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// +#include "SqList.h" //**02 Ա**// + +/* 궨 */ +#define MAXQSIZE 5 // гȣͬʱҲǺ쳲еά +#define MAX 100000 + +/* Ԫ */ +typedef int QElemType; + +/* ˳ѭ */ +typedef struct { + QElemType* base; // ָд洢ռ + int rear; // βα + int length; // Ԫظ +} SqQueue; + +// гʼ +Status InitQueue_3_30(SqQueue* Q); + +// +Status EnQueue_3_30(SqQueue* Q, QElemType e); + +// +Status DeQueue_3_30(SqQueue* Q, QElemType* e); + +/* + * k쳲ضǰn+1 + */ +Status Algo_3_32(int k, SqList* fib); + + +// Ԫ +void Output(SqQueue Q) { + int head; + + if(Q.length == 0) { + printf("\n"); + return; + } + + // ͷα + head = (Q.rear - Q.length + MAXQSIZE) % MAXQSIZE; + + do { + printf("%d ", Q.base[head]); + head = (head + 1) % MAXQSIZE; + } while(head != Q.rear); + + printf("\n"); +} + + +int main(int argc, char* argv[]) { + int i; + + printf(" 3.30 ֤...\n"); + { + SqQueue Q; + QElemType e; + printf(" ʼ...\n"); + InitQueue_3_30(&Q);\ + + printf(" 5 Ӳ...\n"); + for(i = 1; i <= 5; i++) { + EnQueue_3_30(&Q, i); + printf(" Ԫ \"%d\" Ӻ󣬶еԪΪ", i); + Output(Q); + } + + printf(" 5 Ӳ...\n"); + for(i = 1; i <= 5; i++) { + DeQueue_3_30(&Q, &e); + printf(" Ԫ \"%d\" Ӻ󣬶еԪΪ", e); + Output(Q); + } + } + + printf(" 3.32 ֤...\n"); + { + SqList fib; + + Algo_3_32(MAXQSIZE, &fib); + printf(" %d 쳲еǰ %d Ϊ\n", MAXQSIZE, fib.length); + for(i = 0; i < fib.length; i++) { + printf("%d ", fib.elem[i]); + } + printf("\n"); + } + + return 0; +} + + +// гʼ +Status InitQueue_3_30(SqQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).base = (QElemType*) malloc(MAXQSIZE * sizeof(QElemType)); + if((*Q).base == NULL) { + exit(OVERFLOW); + } + + (*Q).rear = 0; + (*Q).length = 0; + + return OK; +} + +// +Status EnQueue_3_30(SqQueue* Q, QElemType e) { + + if(Q == NULL) { + return ERROR; + } + + // ע + if((*Q).length == MAXQSIZE) { + return ERROR; + } + + (*Q).base[(*Q).rear] = e; + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + + (*Q).length++; + + return OK; +} + +// +Status DeQueue_3_30(SqQueue* Q, QElemType* e) { + int head; + + if(Q == NULL) { + return ERROR; + } + + // п + if((*Q).length == 0) { + return ERROR; + } + + // ͷα + head = ((*Q).rear - (*Q).length + MAXQSIZE) % MAXQSIZE; + + *e = (*Q).base[head]; + + (*Q).length--; + + return OK; +} + +/* + * k쳲ضǰn+1 + * + *ע + * ˸оЩǣǿ + * ΪȻҪǰn+1ֻk˵Ǹǰn+1ضҪ浽ĵط + * ǣȻǰn+1ûҪõѭеĺkˣ... + * Ϊġڡn+1ֵôѭӦüֵ + */ +Status Algo_3_32(int k, SqList* fib) { + int flag; + int i, j, sum; + SqQueue Q; + ElemType e; + + if(k < 2 || MAX < 0) { + return ERROR; + } + + InitQueue_3_30(&Q); + InitList(fib); + + // ǰk-1Ϊ0 + for(i = 1; i <= k - 1; i++) { + EnQueue_3_30(&Q, 0); + ListInsert(fib, i, 0); + } + + // kΪ1 + EnQueue_3_30(&Q, 1); + ListInsert(fib, i, 1); + + while((flag = GetElem(*fib, i, &e))==OK && e<=MAX){ + /* + * ѭԪصĺ + * ʵһԸΪ˳кkĺͣһûѭɶ + */ + for(j = 0, sum = 0; j < Q.length; j++) { + sum += Q.base[j]; + } + + // ˴eֻʱ + DeQueue_3_30(&Q, &e); + + // ¼Ԫ + EnQueue_3_30(&Q, sum); + + // ˳˳лһ + ListInsert(fib, ++i, sum); + } + + return flag; +} diff --git a/Dev-C++/ExerciseBook/03.30+03.32/03.30+03.32.dev b/Dev-C++/ExerciseBook/03.30+03.32/03.30+03.32.dev new file mode 100644 index 0000000..7d45b39 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.30+03.32/03.30+03.32.dev @@ -0,0 +1,82 @@ +[Project] +FileName=03.30+03.32.dev +Name=03.30+03.32 +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 + +[Unit2] +FileName=SqList.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit1] +FileName=03.30+03.32.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit3] +FileName=SqList.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.30+03.32/SqList.cpp b/Dev-C++/ExerciseBook/03.30+03.32/SqList.cpp new file mode 100644 index 0000000..624aa2e --- /dev/null +++ b/Dev-C++/ExerciseBook/03.30+03.32/SqList.cpp @@ -0,0 +1,105 @@ +/*============================= + * Ա˳洢ṹ˳ + * + * 㷨: 2.32.42.52.6 + =============================*/ + +#include "SqList.h" + +/* + * 㷨2.3 + * + * ʼ + * + * ʼɹ򷵻OK򷵻ERROR + */ +Status InitList(SqList* L) { + // ָڴ棬ʧܣ򷵻NULL + (*L).elem = (ElemType*) malloc(LIST_INIT_SIZE * sizeof(ElemType)); + if((*L).elem == NULL) { + // 洢ڴʧ + exit(OVERFLOW); + } + + (*L).length = 0; // ʼ˳Ϊ0 + (*L).listsize = LIST_INIT_SIZE; // ˳ʼڴ + + return OK; // ʼɹ +} + +/* + * ȡֵ + * + * ȡ˳еiԪأ洢eС + * ҵOK򣬷ERROR + * + *ע + * ̲iĺԪλã1ʼⲻϱͨԼ + * ͨiĺӦָ0ʼ + */ +Status GetElem(SqList L, int i, ElemType* e) { + // ΪiĺλãϷΧǣ[1, length] + if(i < 1 || i > L.length) { + return ERROR; //iֵϷ + } + + *e = L.elem[i - 1]; + + return OK; +} + +/* + * 㷨2.4 + * + * + * + * ˳iλϲeɹ򷵻OK򷵻ERROR + * + *ע + * ̲iĺԪλã1ʼ + */ +Status ListInsert(SqList* L, int i, ElemType e) { + ElemType* newbase; + ElemType* p, * q; + + // ȷ˳ṹ + if(L == NULL || (*L).elem == NULL) { + return ERROR; + } + + // iֵԽ + if(i < 1 || i > (*L).length + 1) { + return ERROR; + } + + // 洢ռ¿ռ + if((*L).length >= (*L).listsize) { + // пռ + newbase = (ElemType*) realloc((*L).elem, ((*L).listsize + LISTINCREMENT) * sizeof(ElemType)); + if(newbase == NULL) { + // 洢ڴʧ + exit(OVERFLOW); + } + + // »ַ + (*L).elem = newbase; + // Ĵ洢ռ + (*L).listsize += LISTINCREMENT; + } + + // qΪλ + q = &(*L).elem[i - 1]; + + // 1.Ԫأڳλ + for(p = &(*L).elem[(*L).length - 1]; p >= q; --p) { + *(p + 1) = *p; + } + + // 2.e + *q = e; + + // 3.1 + (*L).length++; + + return OK; +} diff --git a/Dev-C++/ExerciseBook/03.30+03.32/SqList.h b/Dev-C++/ExerciseBook/03.30+03.32/SqList.h new file mode 100644 index 0000000..5b3392e --- /dev/null +++ b/Dev-C++/ExerciseBook/03.30+03.32/SqList.h @@ -0,0 +1,66 @@ +/*============================= + * Ա˳洢ṹ˳ + * + * 㷨: 2.32.42.52.6 + =============================*/ + +#ifndef SQLIST_H +#define SQLIST_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define LIST_INIT_SIZE 100 // ˳洢ռijʼ +#define LISTINCREMENT 10 // ˳洢ռķ + +/* ˳ԪͶ壬Ԫһ */ +typedef int ElemType; + +/* + * ˳ṹ + * + * עelemʹǰҪΪڴ棬Ԫشelem[0]ʼ洢 + */ +typedef struct { + ElemType* elem; // ˳洢ռĻַָ˳ռڴʼλã + int length; // ǰ˳ȣԪأ + int listsize; // ǰĴ洢Դ洢Ԫأ +} SqList; + + +/* + * 㷨2.3 + * + * ʼ + * + * ʼɹ򷵻OK򷵻ERROR + */ +Status InitList(SqList* L); + +/* + * ȡֵ + * + * ȡ˳еiԪأ洢eС + * ҵOK򣬷ERROR + * + *ע + * ̲iĺԪλã1ʼⲻϱͨԼ + * ͨiĺӦָ0ʼ + */ +Status GetElem(SqList L, int i, ElemType* e); + +/* + * 㷨2.4 + * + * + * + * ˳iλϲeɹ򷵻OK򷵻ERROR + * + *ע + * ̲iĺԪλã1ʼ + */ +Status ListInsert(SqList* L, int i, ElemType e); + +#endif diff --git a/Dev-C++/ExerciseBook/03.31/03.31.cpp b/Dev-C++/ExerciseBook/03.31/03.31.cpp new file mode 100644 index 0000000..88ff97b --- /dev/null +++ b/Dev-C++/ExerciseBook/03.31/03.31.cpp @@ -0,0 +1,89 @@ +#include +#include // ṩstrlenԭ +#include "Status.h" //**01 **// +#include "SqStack.h" //**03 ջͶ**// + +/* + * жsǷΪ + * մҲΪ + */ +Status Algo_3_31(char* s); + + +int main(int argc, char* argv[]) { + char* a = "abcdedcba@"; + char* b = "ababab@"; + + if(Algo_3_31(a)) { + printf("%s@ǻУ\n", a); + } else { + printf("%s@ǻУ\n", a); + } + + if(Algo_3_31(b)) { + printf("%s@ǻУ\n", b); + } else { + printf("%s@ǻУ\n", b); + } + + return 0; +} + + +/* + * жsǷΪ + * մҲΪ + */ +Status Algo_3_31(char* s) { + int len; + int i; + int m, n; // ĴǰεĽβ±ͺεĿʼ± + SqStack S; + SElemType e; + + len = (int) strlen(s); + + if(len == 0 || s[len - 1] != '@') { + return FALSE; + } + + // մΪǻ + if(len == 1) { + return TRUE; + } + + if(len % 2 == 0) { + m = (len - 2) / 2 - 1; + n = m + 2; + } else { + m = (len - 2) / 2; + n = m + 1; + } + + InitStack(&S); + + // ȰѻĴǰջ + for(i = 0; i <= m; i++) { + Push(&S, s[i]); + } + + // ȡĴǰΣַкαȶ + for(i = n; i <= len - 2; i++) { + if(!StackEmpty(S)) { + Pop(&S, &e); + } else { + break; + } + + if(s[i] != e) { + return ERROR; + } + } + + // ״̬ӦջΪգiΪlen-1 + if(!(StackEmpty(S) && i == len - 1)) { + return ERROR; + } + + return OK; +} diff --git a/Dev-C++/ExerciseBook/03.31/03.31.dev b/Dev-C++/ExerciseBook/03.31/03.31.dev new file mode 100644 index 0000000..43f7547 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.31/03.31.dev @@ -0,0 +1,82 @@ +[Project] +FileName=03.31.dev +Name=03.31 +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 + +[Unit2] +FileName=SqStack.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit1] +FileName=03.31.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit3] +FileName=SqStack.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.31/SqStack.cpp b/Dev-C++/ExerciseBook/03.31/SqStack.cpp new file mode 100644 index 0000000..f056778 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.31/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++/ExerciseBook/03.31/SqStack.h b/Dev-C++/ExerciseBook/03.31/SqStack.h new file mode 100644 index 0000000..a8fe835 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.31/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++/ExerciseBook/03.33/03.33.cpp b/Dev-C++/ExerciseBook/03.33/03.33.cpp new file mode 100644 index 0000000..87db969 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.33/03.33.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include "Status.h" //**01 **// +#include "SqQueue.h" //**03 ջͶ**// + +// Ժӡ +void PrintElem(QElemType e) { + printf("%d ", e); +} + + +int main(int argc, char* argv[]) { + SqQueue Q; + QElemType e; + int i; + int random; + + printf(" ʼ...\n"); + InitQueue(&Q); + + printf(" 5 Ӳ...\n"); + srand((unsigned) time(NULL)); + for(i = 1; i <= 5; i++) { + + random = rand() % 100; + + EnQueue_3_33(&Q, random); + + printf(" Ԫ \"%2d\" Ӻ󣬶еԪΪ", random); + QueueTraverse(Q, PrintElem); + } + + printf(" 5 Ӳ...\n"); + for(i = 1; i <= 5; i++) { + DeQueue(&Q, &e); + + printf(" Ԫ \"%2d\" Ӻ󣬶еԪΪ", e); + QueueTraverse(Q, PrintElem); + } + + return 0; +} diff --git a/Dev-C++/ExerciseBook/03.33/03.33.dev b/Dev-C++/ExerciseBook/03.33/03.33.dev new file mode 100644 index 0000000..2ca8e30 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.33/03.33.dev @@ -0,0 +1,82 @@ +[Project] +FileName=03.33.dev +Name=03.33 +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 + +[Unit2] +FileName=SqQueue.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit1] +FileName=03.33.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit3] +FileName=SqQueue.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.33/SqQueue.cpp b/Dev-C++/ExerciseBook/03.33/SqQueue.cpp new file mode 100644 index 0000000..c9f15af --- /dev/null +++ b/Dev-C++/ExerciseBook/03.33/SqQueue.cpp @@ -0,0 +1,113 @@ +/*============================= + * е˳洢ṹ˳У + ==============================*/ + +#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 EnQueue_3_33(SqQueue* Q, QElemType e) { + int head, tail; + + if(Q == NULL) { + return ERROR; + } + + // ֱӷ + if(((*Q).rear + 1) % MAXQSIZE == (*Q).front) { + return ERROR; + } + + // ΪգֱӲ뵽β + if((*Q).front == (*Q).rear) { + (*Q).base[(*Q).rear] = e; + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + return OK; + } + + head = (*Q).base[(*Q).front]; // ͷԪֵ + tail = (*Q).base[((*Q).rear - 1 + MAXQSIZE) % MAXQSIZE]; // βԪֵ + + // Ԫصҵʱ䲻СڶβԪҵƽʱ䣬Ԫزڶβ + if(e >= (head + tail) / 2) { + (*Q).base[(*Q).rear] = e; + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + + // 򣬲ڶͷ + } else { + (*Q).front = ((*Q).front - 1 + MAXQSIZE) % MAXQSIZE; + (*Q).base[(*Q).front] = e; + } + + 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++/ExerciseBook/03.33/SqQueue.h b/Dev-C++/ExerciseBook/03.33/SqQueue.h new file mode 100644 index 0000000..99f9fca --- /dev/null +++ b/Dev-C++/ExerciseBook/03.33/SqQueue.h @@ -0,0 +1,63 @@ +/*============================= + * е˳洢ṹ˳У + ==============================*/ + +#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 EnQueue_3_33(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++/ExerciseBook/03.34/03.34.cpp b/Dev-C++/ExerciseBook/03.34/03.34.cpp new file mode 100644 index 0000000..faa34b1 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.34/03.34.cpp @@ -0,0 +1,152 @@ +#include +#include "Status.h" //**01 **// +#include "SqQueue.h" //**03 ջͶ**// + +/* + * ɳУУ + * 'L': ֱӴڿ + * 'E': Ӷͷת + * 'A': Ӷβת + * 'D': Ӷͷ + * + * ұߣߣתеĶͷߣβұߣҳȡ˳򡾴ҡ + * + * En ȴȵг + * seq + */ +void Algo_3_34(char* En, char seq[]); + +/* + * ʹԼõĵseqEnijȵEx + * + * ÷ɿǶԵеļ + * + * En ȴȵг + * seq + * Ex ɺ + */ +Status Dispatch(char* En, char* seq, char Ex[]); + + +int main(int argc, char* argv[]) { + char* En = "HPSHHSHPPHHSPSHSPSSHSP"; // ȴȵг + char Ex[100] = {'\0'}; // ɵг + char seq[100] = {'\0'}; // + + printf(" ڴУ\n"); + printf(" En = %s\n", En); + + Algo_3_34(En, seq); + printf(" ɵĵУ\n"); + printf(" seq = %s\n", seq); + + Dispatch(En, seq, Ex); + printf(" ɵĵУڴijȣɺijУ\n"); + printf(" Ex = %s\n", Ex); + + return 0; +} + + +/* + * ɳУУ + * 'L': ֱӴڿ + * 'E': Ӷͷת + * 'A': Ӷβת + * 'D': Ӷͷ + * + * ұߣߣתеĶͷߣβұߣҳȡ˳򡾴ҡ + * + * En ȴȵг + * seq + */ +void Algo_3_34(char* En, char seq[]) { + int i, j; + SqQueue Q; + QElemType e; + + // ʼһת + InitQueue(&Q); + + for(i = j = 0; En[i] != '\0'; i++) { + // ӲֱӵȵĿĵ + if(En[i] == 'P') { + seq[j++] = 'L'; // LֱӴڿ + } + + // ԣӶͷת + if(En[i] == 'S') { + EnQueue_3_34(&Q, En[i], 0); + seq[j++] = 'E'; + } + + // ӲԣӶβת + if(En[i] == 'H') { + EnQueue_3_34(&Q, En[i], 1); + seq[j++] = 'A'; + } + } + + // תеԺӲԴӶͷ + while(Q.front != Q.rear) { + DeQueue(&Q, &e); + seq[j++] = 'D'; + } + + seq[j] = '\0'; +} + +/* + * ʹԼõĵseqEnijȵEx + * + * ÷ɿǶԵеļ + * + * En ȴȵг + * seq + * Ex ɺ + */ +Status Dispatch(char* En, char* seq, char Ex[]) { + int i, j, k; + SqQueue Q; + QElemType e; + + // ʼһת + InitQueue(&Q); + + i = j = k = 0; + + // + while(seq[k] != '\0') { + // ӲֱӴڿ + if(seq[k] == 'L') { + Ex[j++] = En[i++]; + } + + // ԣӶͷת + if(seq[k] == 'E') { + EnQueue_3_34(&Q, En[i++], 0); + } + + // ӲԣӶβת + if(seq[k] == 'A') { + EnQueue_3_34(&Q, En[i++], 1); + } + + // תеԺӲԴӶͷ + if(seq[k] == 'D') { + DeQueue(&Q, &e); + Ex[j++] = e; + } + + k++; + } + + // Ϊգڴδȵijᣬתдδijᣬʾ + if(seq[k] == '\0' && (En[i] || Q.front == Q.rear)) { + return ERROR; + } + + Ex[j] = '\0'; + + return OK; +} diff --git a/Dev-C++/ExerciseBook/03.34/03.34.dev b/Dev-C++/ExerciseBook/03.34/03.34.dev new file mode 100644 index 0000000..9de47cc --- /dev/null +++ b/Dev-C++/ExerciseBook/03.34/03.34.dev @@ -0,0 +1,82 @@ +[Project] +FileName=03.34.dev +Name=03.34 +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 + +[Unit2] +FileName=SqQueue.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit1] +FileName=03.34.cpp +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit3] +FileName=SqQueue.h +CompileCpp=0 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Dev-C++/ExerciseBook/03.34/SqQueue.cpp b/Dev-C++/ExerciseBook/03.34/SqQueue.cpp new file mode 100644 index 0000000..0e99977 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.34/SqQueue.cpp @@ -0,0 +1,98 @@ +/*============================= + * е˳洢ṹ˳У + ==============================*/ + +#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; +} + +/* + * + * + * diӷ0ָʾӶͷӣ1ָʾӶβ + */ +Status EnQueue_3_34(SqQueue* Q, QElemType e, int di) { + + // Ӷͷ + if(di == 0) { + (*Q).front = ((*Q).front - 1 + MAXQSIZE) % MAXQSIZE; + (*Q).base[(*Q).front] = e; + return OK; + } + + // Ӷβ + if(di == 1) { + (*Q).base[(*Q).rear] = e; + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + return OK; + } + + return ERROR; +} + +/* + * + * + * ƳͷԪأ洢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++/ExerciseBook/03.34/SqQueue.h b/Dev-C++/ExerciseBook/03.34/SqQueue.h new file mode 100644 index 0000000..9de3f05 --- /dev/null +++ b/Dev-C++/ExerciseBook/03.34/SqQueue.h @@ -0,0 +1,65 @@ +/*============================= + * е˳洢ṹ˳У + ==============================*/ + +#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); + +/* + * + * + * diӷ0ָʾӶͷӣ1ָʾӶβ + */ +Status EnQueue_3_34(SqQueue* Q, QElemType e, int di); + +/* + * + * + * ƳͷԪأ洢eС + */ +Status DeQueue(SqQueue* Q, QElemType* e); + +/* + * + * + * visitʶQ + */ +Status QueueTraverse(SqQueue Q, void(Visit)(QElemType)); + +#endif diff --git a/VisualC++/ExerciseBook/03.15/03.15.c b/VisualC++/ExerciseBook/03.15/03.15.c new file mode 100644 index 0000000..2a7c1ae --- /dev/null +++ b/VisualC++/ExerciseBook/03.15/03.15.c @@ -0,0 +1,162 @@ +#include +#include "Status.h" //**01 **// + +/* 궨 */ +#define N 100 //ջ + +/* ˫ջԪͶ */ +typedef int SElemType; + +/* ˫ջаջջ */ +typedef enum { + Left, Right +} StackName; + +/* ˫ջṹ */ +typedef struct { + SElemType stack[N]; // һ㹻ջ + int top[2]; // ջָ +} TWS; + +// ʼջ +Status Inistack_3_15(TWS* tws); + +// ջnameָʾĸջԪ +Status Push_3_15(TWS* tws, StackName name, SElemType x); + +// ջnameָʾĸջƳԪ +Status Pop_3_15(TWS* tws, StackName name, SElemType* x); + +// ջԪأnameָʾĸջеԪ +void OutputStack(TWS tws, StackName name); + + +int main(int argc, char* argv[]) { + TWS S; + int i, x; + + printf(" ʼջ...\n"); + Inistack_3_15(&S); + + + printf(" ջѹԪ...\n"); + + for(i = 1; i <= 5; i++) { + Push_3_15(&S, Left, i); + Push_3_15(&S, Right, 2 * i); + } + printf(" ջеԪأջ׵ջ"); + OutputStack(S, Left); + printf(" ջеԪأջ׵ջ"); + OutputStack(S, Right); + + + printf(" ֱ𵯳ջջԪ...\n"); + + Pop_3_15(&S, Left, &x); + printf(" ջջԪΪ%d\n", x); + printf(" ջеԪأջ׵ջ"); + OutputStack(S, Left); + + Pop_3_15(&S, Right, &x); + printf(" ջջԪΪ%d\n", x); + printf(" ջеԪأջ׵ջ"); + OutputStack(S, Right); + + return 0; +} + + +// ʼջ +Status Inistack_3_15(TWS* tws) { + if(tws == NULL) { + return ERROR; + } + + (*tws).top[Left] = -1; // ջ0ջָ룬עⲻ0 + (*tws).top[Right] = N; // ջ1ջָ룬עⲻN-1 + + return OK; +} + +// ջnameָʾĸջԪ +Status Push_3_15(TWS* tws, StackName name, SElemType x) { + if(tws == NULL) { + return ERROR; + } + + // עջȫջ˷ѿռ + if((*tws).top[Left] + 1 == (*tws).top[Right]) { + return ERROR; + } + + // ƶջα꣬ٴԪ + switch(name) { + case Left: + (*tws).top[name]++; // ջαƶ + break; + case Right: + (*tws).top[name]--; // ұջαƶ + break; + default: + break; + } + + // Ԫ + (*tws).stack[(*tws).top[name]] = x; + + return OK; +} + +// ջnameָʾĸջƳԪ +Status Pop_3_15(TWS* tws, StackName name, SElemType* x) { + if(tws == NULL) { + return ERROR; + } + + // ƳԪأƶα + switch(name) { + case Left: + // жߵջǷΪ + if((*tws).top[name] == -1) { + return ERROR; + } + *x = (*tws).stack[(*tws).top[name]]; + (*tws).top[name]--; + break; + case Right: + // жұߵջǷΪ + if((*tws).top[name] == N) { + return ERROR; + } + *x = (*tws).stack[(*tws).top[name]]; + (*tws).top[name]++; + break; + default: + break; + } + + return OK; +} + +// ջԪأnameָʾĸջеԪ +void OutputStack(TWS tws, StackName name) { + int i; + + switch(name) { + case Left: + for(i = 0; i <= tws.top[name]; i++) { + printf("%d ", tws.stack[i]); + } + break; + case Right: + for(i = N - 1; i >= tws.top[name]; i--) { + printf("%d ", tws.stack[i]); + } + break; + default: + break; + } + + printf("\n"); +} diff --git a/VisualC++/ExerciseBook/03.15/03.15.vcxproj b/VisualC++/ExerciseBook/03.15/03.15.vcxproj new file mode 100644 index 0000000..54d7a6d --- /dev/null +++ b/VisualC++/ExerciseBook/03.15/03.15.vcxproj @@ -0,0 +1,72 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {2D94B566-3A72-46FA-9C14-A8B2FD1B8D38} + My0315 + + + + 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++/ExerciseBook/03.15/03.15.vcxproj.filters b/VisualC++/ExerciseBook/03.15/03.15.vcxproj.filters new file mode 100644 index 0000000..749dcdf --- /dev/null +++ b/VisualC++/ExerciseBook/03.15/03.15.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {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++/ExerciseBook/03.15/03.15.vcxproj.user b/VisualC++/ExerciseBook/03.15/03.15.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.15/03.15.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.16/03.16.c b/VisualC++/ExerciseBook/03.16/03.16.c new file mode 100644 index 0000000..0ac7498 --- /dev/null +++ b/VisualC++/ExerciseBook/03.16/03.16.c @@ -0,0 +1,136 @@ +#include +#include "Status.h" //**01 **// +#include "SqStack.h" //**03 ջͶ**// + +/* + * ɳУ'I'ջ'O'ջ + * + * ұߣߣҳȡ˳򡾴ҡ + * + * En ȴȵг + * seq + */ +void Algo_3_16(char* En, char seq[]); + +/* + * ʹԼõĵseqEnijȵEx + * + * ÷ɿǶԵеļ + * + * En ȴȵг + * seq + * Ex ɺ + */ +Status Dispatch(char* En, char* seq, char Ex[]); + + +int main(int argc, char* argv[]) { + char* En = "HSHHSHHHSSHSSSHS"; // ȴȵг + char Ex[100] = {'\0'}; // ɵг + char seq[100] = {'\0'}; // + + printf(" ڴУ\n"); + printf(" En = %s\n", En); + + Algo_3_16(En, seq); + printf(" ɵĵУ\n"); + printf(" seq = %s\n", seq); + + Dispatch(En, seq, Ex); + printf(" ɵĵУڴijȣɺijУ\n"); + printf(" Ex = %s\n", Ex); + + return 0; +} + + +/* + * ɳУ'I'ջ'O'ջ + * + * ұߣߣȡ˳ + * + * En ȴȵг + * seq + */ +void Algo_3_16(char* En, char seq[]) { + int i, j; + SqStack S; + SElemType e; + + // ʼһתջ + InitStack(&S); + + // + for(i = j = 0; En[i] != '\0'; i++) { + // Ӳϯջ + if(En[i] == 'H') { + Push(&S, En[i]); + seq[j++] = 'I'; + } + + // ϯջջ൱ջת + if(En[i] == 'S') { + Push(&S, En[i]); + Pop(&S, &e); + seq[j++] = 'I'; + seq[j++] = 'O'; + } + } + + // תջеӲϯջ + while(!StackEmpty(S)) { + Pop(&S, &e); + seq[j++] = 'O'; + } + + seq[j] = '\0'; + + DestroyStack(&S); +} + +/* + * ʹԼõĵseqEnijȵEx + * + * ÷ɿǶԵеļ + * + * En ȴȵг + * seq + * Ex ɺ + */ +Status Dispatch(char* En, char* seq, char Ex[]) { + int i, j, k; + SqStack S; + SElemType e; + + // ʼһתջ + InitStack(&S); + + i = j = k = 0; + + // + while(seq[k] != '\0') { + // ջǣ򽫳תջ + if(seq[k] == 'I') { + Push(&S, En[i++]); + } + + // ջǣ򽫳ƳתƵڴ + if(seq[k] == 'O') { + Pop(&S, &e); + Ex[j++] = e; + } + + k++; + } + + // Ϊգڴδȵijᣬתջδijᣬʾ + if(seq[k] == '\0' && (En[i] || StackEmpty(S))) { + return ERROR; + } + + Ex[j] = '\0'; + + DestroyStack(&S); + + return OK; +} diff --git a/VisualC++/ExerciseBook/03.16/03.16.vcxproj b/VisualC++/ExerciseBook/03.16/03.16.vcxproj new file mode 100644 index 0000000..976bba7 --- /dev/null +++ b/VisualC++/ExerciseBook/03.16/03.16.vcxproj @@ -0,0 +1,76 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {DF3537A1-D723-4619-932D-F7A782197B6C} + My0316 + + + + 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++/ExerciseBook/03.16/03.16.vcxproj.filters b/VisualC++/ExerciseBook/03.16/03.16.vcxproj.filters new file mode 100644 index 0000000..d13e57d --- /dev/null +++ b/VisualC++/ExerciseBook/03.16/03.16.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++/ExerciseBook/03.16/03.16.vcxproj.user b/VisualC++/ExerciseBook/03.16/03.16.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.16/03.16.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.16/SqStack.c b/VisualC++/ExerciseBook/03.16/SqStack.c new file mode 100644 index 0000000..1919385 --- /dev/null +++ b/VisualC++/ExerciseBook/03.16/SqStack.c @@ -0,0 +1,109 @@ +/*========================= + * ջ˳洢ṹ˳ջ + ==========================*/ + +#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; +} + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * 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++/ExerciseBook/03.16/SqStack.h b/VisualC++/ExerciseBook/03.16/SqStack.h new file mode 100644 index 0000000..13db596 --- /dev/null +++ b/VisualC++/ExerciseBook/03.16/SqStack.h @@ -0,0 +1,66 @@ +/*========================= + * ջ˳洢ṹ˳ջ + ==========================*/ + +#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); + +/* + * п + * + * ж˳ջǷЧݡ + * + * ֵ + * 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++/ExerciseBook/03.17/03.17.c b/VisualC++/ExerciseBook/03.17/03.17.c new file mode 100644 index 0000000..8b5329b --- /dev/null +++ b/VisualC++/ExerciseBook/03.17/03.17.c @@ -0,0 +1,69 @@ +#include +#include "Status.h" //**01 **// +#include "SqStack.h" //**03 ջͶ**// + +/* + * жǷΪ"1&2"ģʽ + * У21 + * + * s֤У'@'Žβ + */ +Status Algo_3_17(char* s); + + +int main(int argc, char* argv[]) { + char* s = "a+b-c&c-b+a@"; + + printf("ж %s ǷϹ...\n", s); + + if(Algo_3_17(s)) { + printf(" ⣡\n"); + } else { + printf(" в⣡\n"); + } + + return 0; +} + + +/* + * жǷΪ"1&2"ģʽ + * У21 + * + * s֤У'@'Žβ + */ +Status Algo_3_17(char* s) { + SqStack S; + SElemType e; + int i; + + InitStack(&S); + i = 0; + + // '&'ǰջ + while(s[i] != '@' && s[i] != '&') { + Push(&S, s[i]); + i++; + } + + // '&' + if(s[i] != '@') { + i++; // & + + // '&'гջ + while(!StackEmpty(S) && s[i] != '@') { + Pop(&S, &e); + if(s[i] != e) { + return ERROR; + } + i++; + } + } + + // ջΪգǡ÷꣬˵ + if(StackEmpty(S) && s[i] == '@') { + return OK; + } + + return ERROR; +} diff --git a/VisualC++/ExerciseBook/03.17/03.17.vcxproj b/VisualC++/ExerciseBook/03.17/03.17.vcxproj new file mode 100644 index 0000000..8459dce --- /dev/null +++ b/VisualC++/ExerciseBook/03.17/03.17.vcxproj @@ -0,0 +1,76 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {B9129167-2A92-4544-85CC-8414EF965898} + My0317 + + + + 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++/ExerciseBook/03.17/03.17.vcxproj.filters b/VisualC++/ExerciseBook/03.17/03.17.vcxproj.filters new file mode 100644 index 0000000..d5729f8 --- /dev/null +++ b/VisualC++/ExerciseBook/03.17/03.17.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++/ExerciseBook/03.17/03.17.vcxproj.user b/VisualC++/ExerciseBook/03.17/03.17.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.17/03.17.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.17/SqStack.c b/VisualC++/ExerciseBook/03.17/SqStack.c new file mode 100644 index 0000000..f056778 --- /dev/null +++ b/VisualC++/ExerciseBook/03.17/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++/ExerciseBook/03.17/SqStack.h b/VisualC++/ExerciseBook/03.17/SqStack.h new file mode 100644 index 0000000..a8fe835 --- /dev/null +++ b/VisualC++/ExerciseBook/03.17/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++/ExerciseBook/03.18/03.18.c b/VisualC++/ExerciseBook/03.18/03.18.c new file mode 100644 index 0000000..77f3de4 --- /dev/null +++ b/VisualC++/ExerciseBook/03.18/03.18.c @@ -0,0 +1,72 @@ +#include +#include "Status.h" //**01 **// +#include "SqStack.h" //**03 ջͶ**// + +/* + * жʽǷԳ + * + *ע + * ֻжǷԳ֣ж͡˳Ƿȷ + */ +Status Algo_3_18(char* s); + + +int main(int argc, char* argv[]) { + char* s = "(1+2)*3/{2/[(4-5)*3]-5*(8-7)}"; + + printf("жϱʽ %s Ƿƥ...\n", s); + + if(Algo_3_18(s) == TRUE) { + printf(" ʽƥ䣡\n"); + } else { + printf(" ʽƥ䣡\n"); + } + + return 0; +} + + +/* + * жʽǷԳ + * + *ע + * ֻжǷԳ֣ж͡˳Ƿȷ + */ +Status Algo_3_18(char* s) { + SqStack S; + SElemType e; + int i; + + InitStack(&S); + + for(i = 0; s[i] != '\0'; i++) { + switch(s[i]) { + // ţջ + case '(': + case '[': + case '{': + Push(&S, s[i]); + break; + + // ţջ + case ')': + case ']': + case '}': + if(StackEmpty(S)) { + return ERROR; + } + Pop(&S, &e); + break; + + default: + break; + } + } + + // ջΪˣ˵Уɹ + if(StackEmpty(S)) { + return OK; + } + + return ERROR; +} diff --git a/VisualC++/ExerciseBook/03.18/03.18.vcxproj b/VisualC++/ExerciseBook/03.18/03.18.vcxproj new file mode 100644 index 0000000..c996a1b --- /dev/null +++ b/VisualC++/ExerciseBook/03.18/03.18.vcxproj @@ -0,0 +1,76 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {101C3B10-07F9-43D9-93B6-00AE59FF84E0} + My0318 + + + + 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++/ExerciseBook/03.18/03.18.vcxproj.filters b/VisualC++/ExerciseBook/03.18/03.18.vcxproj.filters new file mode 100644 index 0000000..e8f7c0e --- /dev/null +++ b/VisualC++/ExerciseBook/03.18/03.18.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++/ExerciseBook/03.18/03.18.vcxproj.user b/VisualC++/ExerciseBook/03.18/03.18.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.18/03.18.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.18/SqStack.c b/VisualC++/ExerciseBook/03.18/SqStack.c new file mode 100644 index 0000000..f056778 --- /dev/null +++ b/VisualC++/ExerciseBook/03.18/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++/ExerciseBook/03.18/SqStack.h b/VisualC++/ExerciseBook/03.18/SqStack.h new file mode 100644 index 0000000..a8fe835 --- /dev/null +++ b/VisualC++/ExerciseBook/03.18/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++/ExerciseBook/03.19/03.19.c b/VisualC++/ExerciseBook/03.19/03.19.c new file mode 100644 index 0000000..ca3bd19 --- /dev/null +++ b/VisualC++/ExerciseBook/03.19/03.19.c @@ -0,0 +1,98 @@ +#include +#include "Status.h" //**01 **// +#include "SqStack.h" //**03 ջͶ**// + +// жʽǷԳ֣Ƿƥ +Status Algo_3_19(char* s); + +// жab֮Ƿƥ +Status Matching(char a, char b); + + +int main(int argc, char* argv[]) { + char* s = "(1+2)*3/{2/[(4-5)*3]-5*(8-7)}"; + + printf("жϱʽ %s Ƿƥ...\n", s); + + if(Algo_3_19(s)) { + printf(" ʽƥ䣡\n"); + } else { + printf(" ʽŲƥ䣡\n"); + } + + return 0; +} + + +// жʽǷԳ֣Ƿƥ +Status Algo_3_19(char* s) { + SqStack S; + SElemType e; + int i; + + InitStack(&S); + + for(i = 0; s[i] != '\0'; i++) { + switch(s[i]) { + // ţջ + case '(': + case '[': + case '{': + Push(&S, s[i]); + break; + + // ţջ + case ')': + case ']': + case '}': + if(StackEmpty(S)) { + return ERROR; + } + + Pop(&S, &e); + + // жǷƥ + if(!Matching(e, s[i])) { + return ERROR; + } + break; + + default: + break; + } + } + + if(StackEmpty(S)) { + return OK; + } + + return ERROR; +} + +// жab֮Ƿƥ +Status Matching(char a, char b) { + switch(a) { + case '(': + if(b != ')') { + return ERROR; + } + break; + + case '[': + if(b != ']') { + return ERROR; + } + break; + + case '{': + if(b != '}') { + return ERROR; + } + break; + + default: + return ERROR; + } + + return OK; +} diff --git a/VisualC++/ExerciseBook/03.19/03.19.vcxproj b/VisualC++/ExerciseBook/03.19/03.19.vcxproj new file mode 100644 index 0000000..8ecd437 --- /dev/null +++ b/VisualC++/ExerciseBook/03.19/03.19.vcxproj @@ -0,0 +1,76 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {18FCCC0C-0DD4-4DBF-99E2-78F96A358233} + My0319 + + + + 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++/ExerciseBook/03.19/03.19.vcxproj.filters b/VisualC++/ExerciseBook/03.19/03.19.vcxproj.filters new file mode 100644 index 0000000..b44c95b --- /dev/null +++ b/VisualC++/ExerciseBook/03.19/03.19.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++/ExerciseBook/03.19/03.19.vcxproj.user b/VisualC++/ExerciseBook/03.19/03.19.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.19/03.19.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.19/SqStack.c b/VisualC++/ExerciseBook/03.19/SqStack.c new file mode 100644 index 0000000..f056778 --- /dev/null +++ b/VisualC++/ExerciseBook/03.19/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++/ExerciseBook/03.19/SqStack.h b/VisualC++/ExerciseBook/03.19/SqStack.h new file mode 100644 index 0000000..a8fe835 --- /dev/null +++ b/VisualC++/ExerciseBook/03.19/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++/ExerciseBook/03.20/03.20.c b/VisualC++/ExerciseBook/03.20/03.20.c new file mode 100644 index 0000000..235879b --- /dev/null +++ b/VisualC++/ExerciseBook/03.20/03.20.c @@ -0,0 +1,222 @@ +#include +#include // ṩ system ԭ +#include "Status.h" //**01 **// + +/* 㷨ԹѰ·㷨 */ + +/* 궨 */ +#define SleepTime 2 +#define Row 10 +#define Col 17 + +/* ʷ */ +typedef enum { + East, South, West, North +} Direction; + +/* ɫö */ +typedef enum { + Color_1, Color_2, Color_3 +} Color; + +/* ջԪ */ +typedef struct { + int x, y; // صĺᡢ궨 + int di; // Ӵصһص"" +} SElemType; + +// ԹȾɫָͼȾɫ +void Algo_3_20(int g[][17], SElemType start); + +// ʼͼȾɫ +void InitGrap(int g[][17], SElemType* start); + +// Ļʾǰͼ +void PaintGrap(int g[][17]); + +// жijǷҪȾɫ +Status Pass(SElemType e, int g[][17]); + +// Ⱦɫĵ㼴±ǣȾӦɫ +void Mark(SElemType* e, int g[][17]); + +// ȡһȾɫĵϢ +Status NextPos(SElemType* e); + +// жϵǰĵǷ +Status IsCross(SElemType e); + + +int main(int argc, char** argv) { + int g[Row][Col]; // ͼɫ + SElemType start; // Ⱦɫ + + InitGrap(g, &start); + PaintGrap(g); + + Algo_3_20(g, start); + + return 0; +} + + +// ָͼȾɫ +void Algo_3_20(int g[][Col], SElemType start) { + SElemType e; + SElemType stack[10000]; // һ㹻ΪջȾɫĵ + int top = -1; // ջָ + + e = start; + do { + // ҪȾɫ + if(Pass(e, g)) { + Mark(&e, g); // Ⱦɫǰ + PaintGrap(g); + stack[++top] = e; // ʹصջ + NextPos(&e); // óһصϢ + } else { + if(top != -1) { + e = stack[top--]; + + // ջеĵû෽ɷ + while(e.di == North && top != -1) { + e = stack[top--]; + } + + if(e.di < North) { + e.di++; + stack[++top] = e; + NextPos(&e); + } + } + } + } while(top != -1); // ջΪ +} + +/* ʼͼȾɫ */ +void InitGrap(int g[][Col], SElemType* start) { + int i, j; + + // ɫ0ɫ1 + int a[Row][Col]={{0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0}, + {0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0}, + {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, + {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, + {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, + {0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0}, + {0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0}, + {0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0}}; + + for(i = 0; i < 10; i++) { + for(j = 0; j < 17; j++) { + g[i][j] = a[i][j]; + } + } + + (*start).x = 9; // + (*start).y = 8; + (*start).di = East; // East +} + +/* Ļʾǰͼ */ +void PaintGrap(int g[][Col]) { + int i, j; + + Wait(SleepTime); + system("cls"); + + for(i = 0; i < Row; i++) { + for(j = 0; j < Col; j++) { + // ɫ0"^"ʾ + if(g[i][j] == Color_1) { + printf("."); + } + + // ɫ1ʾΪհ + if(g[i][j] == Color_2) { + printf(" "); + } + + // ɫ2"*"ʾ + if(g[i][j] == Color_3) { + printf("*"); + } + + if(j && !(j % (Col - 1))) { + printf("\n"); + } + } + } + + printf("\n"); +} + +/* жijǷҪȾɫ */ +Status Pass(SElemType e, int g[][Col]) { + int x = e.x; + int y = e.y; + + // ɫΪ1ĵȾɫ + if(g[x][y] == 1) { + return TRUE; + } else { + return FALSE; + } +} + +/* Ⱦɫĵ㼴±ǣȾӦɫ */ +void Mark(SElemType* e, int g[][Col]) { + int x = (*e).x; + int y = (*e).y; + + (*e).di = East; // Ǵصǰ + + g[x][y] = 2; // صɫȾΪɫ2 +} + +/* ȡһȾɫĵϢ */ +Status NextPos(SElemType* e) { + SElemType tmp; + tmp = *e; + + switch(tmp.di) { + case East: + (tmp.y)++; // East + break; + case South: + (tmp.x)++; // South + break; + case West: + (tmp.y)--; // West + break; + case North: + (tmp.x)--; // North + break; + default: + return FALSE; + } + + if(IsCross(tmp)) { + ++(*e).di; + NextPos(e); + } else { + *e = tmp; + } + + return TRUE; +} + +/* жϵǰĵǷ */ +Status IsCross(SElemType e) { + int x = e.x; + int y = e.y; + + // Խ + if(x < 0 || y < 0 || x > Row - 1 || y > Col - 1) { + return OK; + } else { + return ERROR; + } +} diff --git a/VisualC++/ExerciseBook/03.20/03.20.vcxproj b/VisualC++/ExerciseBook/03.20/03.20.vcxproj new file mode 100644 index 0000000..c3c61c8 --- /dev/null +++ b/VisualC++/ExerciseBook/03.20/03.20.vcxproj @@ -0,0 +1,72 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {2BF34829-27A6-4295-9CEF-858BF01BE158} + My0320 + + + + 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++/ExerciseBook/03.20/03.20.vcxproj.filters b/VisualC++/ExerciseBook/03.20/03.20.vcxproj.filters new file mode 100644 index 0000000..afdf969 --- /dev/null +++ b/VisualC++/ExerciseBook/03.20/03.20.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {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++/ExerciseBook/03.20/03.20.vcxproj.user b/VisualC++/ExerciseBook/03.20/03.20.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.20/03.20.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.21/03.21.c b/VisualC++/ExerciseBook/03.21/03.21.c new file mode 100644 index 0000000..a7b163e --- /dev/null +++ b/VisualC++/ExerciseBook/03.21/03.21.c @@ -0,0 +1,124 @@ +#include +#include // ṩstrlenԭ +#include // ṩisalphaԭ +#include "SqStack.h" //**03 ջͶ**// + +// ʽֵķ +static const char OP[] = {'+', '-', '*', '/'}; + +/* + * ȼOPǺӦġ + * ֻŵȼ + */ +static const char PrecedeTable[7][7] = { + {'>', '>', '<', '<'}, + {'>', '>', '<', '<'}, + {'>', '>', '>', '>'}, + {'>', '>', '>', '>'} +}; + +/* + * ݸ׺ʽӦ沨ʽ׺ʽ + * 罫"a+b*c-d/e"תΪ"abc*+de/-" + * + *ע + * ʽıΪĸֻ'+''-''*''/' + */ +char* Algo_3_21(char s[]); + +/* + * жջвo1ʽеIJo2ȼ + * + * '>''<''='ָʾo1o2ȼ + */ +char Precede(char o1, char o2); + + +int main(int argc, char* argv[]) { + char* s = "a+b*c-d/e"; + + printf("׺ʽΪ"); + printf("%s\n", s); + + printf("׺ʽΪ"); + printf("%s\n", Algo_3_21(s)); + + return 0; +} + + +/* + * ݸ׺ʽӦ沨ʽ׺ʽ + * 罫"a+b*c-d/e"תΪ"abc*+de/-" + * + *ע + * ʽıΪĸֻ'+''-''*''/' + */ +char* Algo_3_21(char s[]) { + int i, j; + SqStack S; // ջʱ洢 + SElemType e; // SElemTypecharͣcharintͿֱӱȽ + char* c; + + // 沨ʽ + c = (char*) malloc((strlen(s) + 1) * sizeof(char)); + + InitStack(&S); + + for(i = j = 0; s[i] != '\0'; i++) { + // ĸΪ + if(isalpha(s[i])) { + c[j++] = s[i]; // ĸֱӴ沨ʽ + + // + } else { + while(!StackEmpty(S)) { + // ȡջԪ + GetTop(S, &e); + + // ջȼ + if(Precede(e, s[i]) == '>') { + Pop(&S, &e); // ջȼߵIJջ + c[j++] = e; // ջIJӵ沨ʽ + + // ջвȼʱѭ + } else { + break; + } + } + + // ʽжȡIJѹջ + Push(&S, s[i]); + } + } + + // ջв׷ӵ沨ʽ + while(!StackEmpty(S)) { + Pop(&S, &e); + c[j++] = e; + } + + // ת沨ʽ"\0"ǽ + c[j] = '\0'; + + return c; +} + +/* + * жջвo1ʽеIJo2ȼ + * + * '>''<''='ָʾo1o2ȼ + */ +char Precede(char o1, char o2) { + int x, y; + + // ȡָеλ + char* p1 = strchr(OP, o1); + char* p2 = strchr(OP, o2); + + // һȼ + x = p1 - OP; + y = p2 - OP; + + return PrecedeTable[x][y]; +} diff --git a/VisualC++/ExerciseBook/03.21/03.21.vcxproj b/VisualC++/ExerciseBook/03.21/03.21.vcxproj new file mode 100644 index 0000000..adf231d --- /dev/null +++ b/VisualC++/ExerciseBook/03.21/03.21.vcxproj @@ -0,0 +1,76 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {7E5791FB-5D18-45A8-BD9E-8982CA33B925} + My0321 + + + + 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++/ExerciseBook/03.21/03.21.vcxproj.filters b/VisualC++/ExerciseBook/03.21/03.21.vcxproj.filters new file mode 100644 index 0000000..b1204f0 --- /dev/null +++ b/VisualC++/ExerciseBook/03.21/03.21.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++/ExerciseBook/03.21/03.21.vcxproj.user b/VisualC++/ExerciseBook/03.21/03.21.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.21/03.21.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.21/SqStack.c b/VisualC++/ExerciseBook/03.21/SqStack.c new file mode 100644 index 0000000..f0b3b3d --- /dev/null +++ b/VisualC++/ExerciseBook/03.21/SqStack.c @@ -0,0 +1,106 @@ +/*========================= + * ջ˳洢ṹ˳ջ + ==========================*/ + +#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 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; +} diff --git a/VisualC++/ExerciseBook/03.21/SqStack.h b/VisualC++/ExerciseBook/03.21/SqStack.h new file mode 100644 index 0000000..a7a5f21 --- /dev/null +++ b/VisualC++/ExerciseBook/03.21/SqStack.h @@ -0,0 +1,66 @@ +/*========================= + * ջ˳洢ṹ˳ջ + ==========================*/ + +#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 GetTop(SqStack S, SElemType* e); + +/* + * ջ + * + * Ԫeѹ뵽ջ + */ +Status Push(SqStack* S, SElemType e); + +/* + * ջ + * + * ջԪصeա + */ +Status Pop(SqStack* S, SElemType* e); + +#endif diff --git a/VisualC++/ExerciseBook/03.22/03.22.c b/VisualC++/ExerciseBook/03.22/03.22.c new file mode 100644 index 0000000..8a9d494 --- /dev/null +++ b/VisualC++/ExerciseBook/03.22/03.22.c @@ -0,0 +1,86 @@ +#include +#include // ṩisdigitԭ +#include "Status.h" //**01 **// +#include "SqStack.h" //**03 ջͶ**// + +/* + * 沨ʽֵҪΪλ + * + *ע + * ʽıΪĸֻ'+''-''*''/' + */ +Status Algo_3_22(char c[], SElemType* Value); + +/* a oper b ֵ */ +char Operate(char a, char oper, char b); + + +int main(int argc, char* argv[]) { + char c[] = "124*+93/-"; + SElemType value; + + printf("֪沨ʽΪ"); + printf("%s\n", c); + + Algo_3_22(c, &value); + printf("沨ʽļΪ%d\n", value - 48); + + return 0; +} + + +/* + * 沨ʽֵҪΪλ + * + *ע + * ʽıΪĸֻ'+''-''*''/' + */ +Status Algo_3_22(char c[], SElemType* Value) { + int i; + SqStack S; + SElemType a, b; + + InitStack(&S); + + for(i = 0; c[i]!='\0'; ++i) { + // ַ + if(isdigit(c[i])) { + Push(&S, c[i]); + } else { + // ȵڲ + Pop(&S, &b); + Pop(&S, &a); + Push(&S, Operate(a, c[i], b)); + } + } + + Pop(&S, Value); + + if(!StackEmpty(S)) { + return ERROR; + } else { + return OK; + } +} + +/* a oper b ֵ */ +char Operate(char a, char oper, char b) { + char c; + + switch(oper) { + case '+': + c = (a - 48) + (b - 48) + 48; + break; + case '-': + c = (a - 48) - (b - 48) + 48; + break; + case '*': + c = (a - 48) * (b - 48) + 48; + break; + case '/': + c = (a - 48) / (b - 48) + 48; + break; + } + + return c; +} diff --git a/VisualC++/ExerciseBook/03.22/03.22.vcxproj b/VisualC++/ExerciseBook/03.22/03.22.vcxproj new file mode 100644 index 0000000..1dbaa59 --- /dev/null +++ b/VisualC++/ExerciseBook/03.22/03.22.vcxproj @@ -0,0 +1,76 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {3B22A494-7A0D-4CBA-A2A1-F70D189B3214} + My0322 + + + + 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++/ExerciseBook/03.22/03.22.vcxproj.filters b/VisualC++/ExerciseBook/03.22/03.22.vcxproj.filters new file mode 100644 index 0000000..c4844b6 --- /dev/null +++ b/VisualC++/ExerciseBook/03.22/03.22.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++/ExerciseBook/03.22/03.22.vcxproj.user b/VisualC++/ExerciseBook/03.22/03.22.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.22/03.22.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.22/SqStack.c b/VisualC++/ExerciseBook/03.22/SqStack.c new file mode 100644 index 0000000..f056778 --- /dev/null +++ b/VisualC++/ExerciseBook/03.22/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++/ExerciseBook/03.22/SqStack.h b/VisualC++/ExerciseBook/03.22/SqStack.h new file mode 100644 index 0000000..a8fe835 --- /dev/null +++ b/VisualC++/ExerciseBook/03.22/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++/ExerciseBook/03.23/03.23.c b/VisualC++/ExerciseBook/03.23/03.23.c new file mode 100644 index 0000000..902b323 --- /dev/null +++ b/VisualC++/ExerciseBook/03.23/03.23.c @@ -0,0 +1,109 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include // ṩstrlenԭ +#include // ṩisalphaԭ +#include "SqStack.h" //**03 ջͶ**// + +/* + * ׺ʽ沨ʽתΪǰ׺ʽʽ + * + *ע + * ʽıΪĸֻ'+''-''*''/' + */ +char* Algo_3_23(char* c); + +// ַתΪַ +char* CharToStr(char c); + +// ƴַabƴӺַ +char* StrCat(char* a, char* b); + + +int main(int argc, char* argv[]) { + char* s = "abc+*de/-"; + + printf("׺ʽ%s\n", "a*(b+c)-d/e"); + + printf("׺ʽ"); + printf("%s\n", s); + + printf("ǰ׺ʽ"); + printf("%s\n", Algo_3_23(s)); + + return 0; +} + + +/* + * ׺ʽ沨ʽתΪǰ׺ʽʽ + * + *ע + * ʽıΪĸֻ'+''-''*''/' + */ +char* Algo_3_23(char* c) { + int i; + char* s, * e1, * e2; + SqStack S; + + InitStack(&S); + + for(i = 0; c[i] != '\0'; i++) { + s = CharToStr(c[i]); + + // c[i]Ϊ(Dz)ջӦԪ + if(!isalpha(c[i])) { + if(Pop(&S, &e2) && Pop(&S, &e1)) { + s = StrCat(s, StrCat(e1, e2)); + } else { + return NULL; + } + } + + Push(&S, s); + } + + Pop(&S, &s); + + if(StackEmpty(S)) { + return s; + } + + return NULL; +} + +// ַתΪַ +char* CharToStr(char c) { + char* s; + + s = (char*) malloc(2 * sizeof(char)); + + s[0] = c; + s[1] = '\0'; + + return s; +} + +// ƴַabƴӺַ +char* StrCat(char* a, char* b) { + char* s; + int i, j, alen, blen; + + alen = (int) strlen(a); + blen = (int) strlen(b); + + s = (char*) malloc((alen + blen + 1) * sizeof(char)); + + j = 0; + + for(i = 0; i < alen; i++) { + s[j++] = a[i]; + } + + for(i = 0; i < blen; i++) { + s[j++] = b[i]; + } + + s[j] = '\0'; + + return s; +} diff --git a/VisualC++/ExerciseBook/03.23/03.23.vcxproj b/VisualC++/ExerciseBook/03.23/03.23.vcxproj new file mode 100644 index 0000000..03b31a0 --- /dev/null +++ b/VisualC++/ExerciseBook/03.23/03.23.vcxproj @@ -0,0 +1,76 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {6BFF283E-28CF-4C2C-8E26-248142B9EDFF} + My0323 + + + + 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++/ExerciseBook/03.23/03.23.vcxproj.filters b/VisualC++/ExerciseBook/03.23/03.23.vcxproj.filters new file mode 100644 index 0000000..93a092a --- /dev/null +++ b/VisualC++/ExerciseBook/03.23/03.23.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++/ExerciseBook/03.23/03.23.vcxproj.user b/VisualC++/ExerciseBook/03.23/03.23.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.23/03.23.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.23/SqStack.c b/VisualC++/ExerciseBook/03.23/SqStack.c new file mode 100644 index 0000000..f056778 --- /dev/null +++ b/VisualC++/ExerciseBook/03.23/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++/ExerciseBook/03.23/SqStack.h b/VisualC++/ExerciseBook/03.23/SqStack.h new file mode 100644 index 0000000..016fc86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.23/SqStack.h @@ -0,0 +1,64 @@ +/*========================= + * ջ˳洢ṹ˳ջ + ==========================*/ + +#ifndef SQSTACK_H +#define SQSTACK_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define STACK_INIT_SIZE 100 // ˳ջ洢ռijʼ +#define STACKINCREMENT 10 // ˳ջ洢ռķ + +/* + * ˳ջԪͶ + * + *ע + * ʹַͣSElemTypeָһַ + */ +typedef char* 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++/ExerciseBook/03.24/03.24.c b/VisualC++/ExerciseBook/03.24/03.24.c new file mode 100644 index 0000000..847e194 --- /dev/null +++ b/VisualC++/ExerciseBook/03.24/03.24.c @@ -0,0 +1,36 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* + * ݹg(m,n) + */ +int Algo_3_24(int m, int n); + + +int main(int argc, char* argv[]) { + int m, n; + + m = 5; + n = 2; + + printf("g(%d,%d) = %d\n", m, n, Algo_3_24(m, n)); + + return 0; +} + + +/* + * ݹg(m,n) + */ +int Algo_3_24(int m, int n) { + if(m < 0 || n < 0) { + exit(ERROR); + } + + if(m == 0) { + return 0; + } else { + return Algo_3_24(m - 1, 2 * n) + n; + } +} diff --git a/VisualC++/ExerciseBook/03.24/03.24.vcxproj b/VisualC++/ExerciseBook/03.24/03.24.vcxproj new file mode 100644 index 0000000..948a84f --- /dev/null +++ b/VisualC++/ExerciseBook/03.24/03.24.vcxproj @@ -0,0 +1,72 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {AFFBB6C2-ED4A-446A-B562-9AB50472EB0A} + My0324 + + + + 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++/ExerciseBook/03.24/03.24.vcxproj.filters b/VisualC++/ExerciseBook/03.24/03.24.vcxproj.filters new file mode 100644 index 0000000..6be9a33 --- /dev/null +++ b/VisualC++/ExerciseBook/03.24/03.24.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {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++/ExerciseBook/03.24/03.24.vcxproj.user b/VisualC++/ExerciseBook/03.24/03.24.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.24/03.24.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.25/03.25.c b/VisualC++/ExerciseBook/03.25/03.25.c new file mode 100644 index 0000000..585b949 --- /dev/null +++ b/VisualC++/ExerciseBook/03.25/03.25.c @@ -0,0 +1,67 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* + * 1ݹ鷨⣬ŵ㷨 + * + * ݹF(n) + */ +int Algo_3_25_1(int n); + +/* + * 2ݹĵŵǴ洢һֵ + * + * F(n) + */ +int Algo_3_25_2(int n); + + +int main(int argc, char* argv[]) { + + printf("F(10) = %d\n", Algo_3_25_1(10)); + + printf("F(10) = %d\n", Algo_3_25_2(10)); + + return 0; +} + + +/* + * 1ݹ鷨⣬ŵ㷨 + * + * ݹF(n) + */ +int Algo_3_25_1(int n) { + if(n < 0) { + exit(ERROR); + } + + if(n == 0) { + return n + 1; + } else { + return n * Algo_3_25_1(n / 2); + } +} + +/* + * 2ݹĵŵǴ洢һֵ + * + * F(n) + */ +int Algo_3_25_2(int n) { + int* a; + int i; + + if(n < 0) { + exit(ERROR); + } + + a = (int*) malloc((n + 1) * sizeof(int)); + + for(i = 1, a[0] = 1; i <= n; i++) { + a[i] = i * a[i / 2]; + } + + return a[n]; +} diff --git a/VisualC++/ExerciseBook/03.25/03.25.vcxproj b/VisualC++/ExerciseBook/03.25/03.25.vcxproj new file mode 100644 index 0000000..4727a18 --- /dev/null +++ b/VisualC++/ExerciseBook/03.25/03.25.vcxproj @@ -0,0 +1,72 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {8C7CCC84-ABF6-462F-A095-12443829D133} + My0325 + + + + 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++/ExerciseBook/03.25/03.25.vcxproj.filters b/VisualC++/ExerciseBook/03.25/03.25.vcxproj.filters new file mode 100644 index 0000000..e636123 --- /dev/null +++ b/VisualC++/ExerciseBook/03.25/03.25.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {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++/ExerciseBook/03.25/03.25.vcxproj.user b/VisualC++/ExerciseBook/03.25/03.25.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.25/03.25.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.26/03.26.c b/VisualC++/ExerciseBook/03.26/03.26.c new file mode 100644 index 0000000..86ed8de --- /dev/null +++ b/VisualC++/ExerciseBook/03.26/03.26.c @@ -0,0 +1,62 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* + * 1ݹ鷨 + * + * ݹƽ + */ +float Algo_3_26_1(float A, float p, float e); + +/* + * 2 + * + * ƽ + */ +float Algo_3_26_2(float A, float p, float e); + + +int main(int argc, char* argv[]) { + + printf("50 = %f\n", Algo_3_26_1(50, 1, 0.000001f)); + + printf("50 = %f\n", Algo_3_26_2(50, 1, 0.000001f)); + + return 0; +} + + +/* + * 1ݹ鷨 + * + * ݹƽ + */ +float Algo_3_26_1(float A, float p, float e) { + if(A < 0.0) { + exit(ERROR); + } + + if((p * p - A) > -e && (p * p - A) < e) { + return p; + } else { + return Algo_3_26_1(A, (p + A / p) / 2, e); + } +} + +/* + * 2 + * + * ƽ + */ +float Algo_3_26_2(float A, float p, float e) { + if(A < 0.0) { + exit(ERROR); + } + + while((p * p - A) >= e || (p * p - A) <= -e) { + p = (p + A / p) / 2; + } + + return p; +} diff --git a/VisualC++/ExerciseBook/03.26/03.26.vcxproj b/VisualC++/ExerciseBook/03.26/03.26.vcxproj new file mode 100644 index 0000000..7770edd --- /dev/null +++ b/VisualC++/ExerciseBook/03.26/03.26.vcxproj @@ -0,0 +1,72 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {C51CA8CF-CAF8-4013-859D-CB7E2266B6B6} + My0326 + + + + 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++/ExerciseBook/03.26/03.26.vcxproj.filters b/VisualC++/ExerciseBook/03.26/03.26.vcxproj.filters new file mode 100644 index 0000000..cd32ffa --- /dev/null +++ b/VisualC++/ExerciseBook/03.26/03.26.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {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++/ExerciseBook/03.26/03.26.vcxproj.user b/VisualC++/ExerciseBook/03.26/03.26.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.26/03.26.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.27/03.27.c b/VisualC++/ExerciseBook/03.27/03.27.c new file mode 100644 index 0000000..3a08e00 --- /dev/null +++ b/VisualC++/ExerciseBook/03.27/03.27.c @@ -0,0 +1,105 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* ջԪͶ */ +typedef struct { + int mval; + int nval; +} SElemType; + +/* + * (1)ݹ鷨 + * + * ݹ㰢 + */ +int Algo_3_27_1(int m, int n); + +/* + * (2)ջģݹ + * + * ģջ㰢 + */ +int Algo_3_27_2(int m, int n); + + +int main(int argc, char* argv[]) { + + printf("akm(3,4) = %d\n", Algo_3_27_1(3, 4)); + + printf("akm(3,4) = %d\n", Algo_3_27_2(3, 4)); + + return 0; +} + + +/* + * (1)ݹ鷨 + * + * ݹ㰢 + */ +int Algo_3_27_1(int m, int n) { + int akm, tmp; + + if(m < 0 || n < 0) { + exit(ERROR); + } + + if(m == 0) { + akm = n + 1; + } else if(n == 0) { + akm = Algo_3_27_1(m - 1, 1); + } else { + akm = Algo_3_27_1(m - 1, Algo_3_27_1(m, n - 1)); + } + + return akm; +} + +/* + * (2)ջģݹ + * + * ģջ㰢 + */ +int Algo_3_27_2(int m, int n) { + SElemType stack[1000]; + int top = -1; // ʼΪ-10 + + if(m < 0 || n < 0) { + exit(ERROR); + } + + // ȵٴֵ + top++; + + stack[top].mval = m; + stack[top].nval = n; + + while(1) { + // m==0 + while(top>0 && stack[top].mval == 0) { + top--; + stack[top].mval = stack[top].mval - 1; + stack[top].nval = stack[top + 1].nval + 1; + } + + if(top==0 && stack[0].mval == 0) { + break; + } + + // m!=0 n!=0 + while(stack[top].nval > 0) { + top++; + stack[top].mval = stack[top - 1].mval; + stack[top].nval = stack[top - 1].nval - 1; + } + + // m!=0 n==0 + if(stack[top].nval == 0) { + stack[top].mval = stack[top].mval - 1; + stack[top].nval = 1; + } + } + + return stack[top].nval + 1; +} diff --git a/VisualC++/ExerciseBook/03.27/03.27.vcxproj b/VisualC++/ExerciseBook/03.27/03.27.vcxproj new file mode 100644 index 0000000..a843d33 --- /dev/null +++ b/VisualC++/ExerciseBook/03.27/03.27.vcxproj @@ -0,0 +1,72 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {72A6BBFE-D06E-4F04-93EC-D9A8E520C191} + My0327 + + + + 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++/ExerciseBook/03.27/03.27.vcxproj.filters b/VisualC++/ExerciseBook/03.27/03.27.vcxproj.filters new file mode 100644 index 0000000..d552569 --- /dev/null +++ b/VisualC++/ExerciseBook/03.27/03.27.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {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++/ExerciseBook/03.27/03.27.vcxproj.user b/VisualC++/ExerciseBook/03.27/03.27.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.27/03.27.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.28/03.28.c b/VisualC++/ExerciseBook/03.28/03.28.c new file mode 100644 index 0000000..150bcb2 --- /dev/null +++ b/VisualC++/ExerciseBook/03.28/03.28.c @@ -0,0 +1,157 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include // ṩһЩֵ +#include "Status.h" //**01 **// + +/* + * ע + * + * ʹôͷѭʾ + */ + +/* Ԫ */ +typedef int QElemType; + +/* н */ +typedef struct QNode { + QElemType data; + struct QNode* next; +} QNode, * QueuePtr; + +/* нṹ */ +typedef struct { + QueuePtr rear; // βָ +} LinkQueue; // еʽ洢ʾ + +// гʼ +Status InitQueue_3_28(LinkQueue* Q); + +// +Status EnQueue_3_28(LinkQueue* Q, QElemType e); + +// +Status DeQueue_3_28(LinkQueue* Q, QElemType* e); + +// Ԫ +void Output(LinkQueue Q); + + +int main(int argc, char* argv[]) { + LinkQueue Q; + int i; + QElemType e; + + printf(" ʼ...\n"); + InitQueue_3_28(&Q); + + printf(" 5 Ӳ...\n"); + for(i = 1; i <= 5; i++) { + EnQueue_3_28(&Q, i); + + printf(" Ԫ \"%d\" Ӻ󣬶еԪΪ", i); + Output(Q); + } + + printf(" 5 Ӳ...\n"); + for(i = 1; i <= 5; i++) { + DeQueue_3_28(&Q, &e); + + printf(" Ԫ \"%d\" Ӻ󣬶еԪΪ", e); + Output(Q); + } + + return 0; +} + + +// гʼ +Status InitQueue_3_28(LinkQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + // ͷ + (*Q).rear = (QueuePtr) malloc(sizeof(QNode)); + if((*Q).rear == NULL) { + exit(OVERFLOW); + } + + // ͷ + (*Q).rear->data = INT_MAX; + + // ѭУβ + (*Q).rear->next = (*Q).rear; + + return OK; +} + +// +Status EnQueue_3_28(LinkQueue* Q, QElemType e) { + QueuePtr p; + + if(Q == NULL || (*Q).rear == NULL) { + return ERROR; + } + + // ½㣬ʽбΪڶ + p = (QueuePtr) malloc(sizeof(QNode)); + if(p == NULL) { + exit(OVERFLOW); + } + p->data = e; + + p->next = (*Q).rear->next; + (*Q).rear->next = p; + (*Q).rear = p; + + return OK; +} + +// +Status DeQueue_3_28(LinkQueue* Q, QElemType* e) { + QueuePtr h, p; + + if(Q == NULL || (*Q).rear == NULL) { + return ERROR; + } + + // ȡͷ + h = (*Q).rear->next; + + // ֻһͷ㣬˵ûԪأ޷ + if(h == (*Q).rear) { + return ERROR; + } + + // ָƳԪ + p = h->next; + *e = p->data; + + h->next = p->next; + + // ֻһԪ + if(p == (*Q).rear) { + // ¶βα + (*Q).rear = h; + } + + free(p); + + return OK; +} + +// Ԫ +void Output(LinkQueue Q) { + QueuePtr p; + + if(Q.rear == NULL) { + printf("\n"); + return; + } + + for(p = Q.rear->next->next; p != Q.rear->next; p = p->next) { + printf("%d ", p->data); + } + + printf("\n"); +} diff --git a/VisualC++/ExerciseBook/03.28/03.28.vcxproj b/VisualC++/ExerciseBook/03.28/03.28.vcxproj new file mode 100644 index 0000000..15b0ada --- /dev/null +++ b/VisualC++/ExerciseBook/03.28/03.28.vcxproj @@ -0,0 +1,72 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {352A25BF-FD02-4EB9-B285-1195029D8465} + My0328 + + + + 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++/ExerciseBook/03.28/03.28.vcxproj.filters b/VisualC++/ExerciseBook/03.28/03.28.vcxproj.filters new file mode 100644 index 0000000..389a4bb --- /dev/null +++ b/VisualC++/ExerciseBook/03.28/03.28.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {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++/ExerciseBook/03.28/03.28.vcxproj.user b/VisualC++/ExerciseBook/03.28/03.28.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.28/03.28.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.29/03.29.c b/VisualC++/ExerciseBook/03.29/03.29.c new file mode 100644 index 0000000..711938e --- /dev/null +++ b/VisualC++/ExerciseBook/03.29/03.29.c @@ -0,0 +1,130 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define MAXQSIZE 100 // г + +/* Ԫ */ +typedef int QElemType; + +/* ˳ѭ */ +typedef struct { + QElemType* base; // ָд洢ռ + int front, rear; // ͷͶβα + int tag; // 0ʾδ1ʾ +} SqQueue; + +// гʼ +Status InitQueue_3_29(SqQueue* Q); + +// +Status EnQueue_3_29(SqQueue* Q, QElemType e); + +// +Status DeQueue_3_29(SqQueue* Q, QElemType* e); + +// Ԫ +void Output(SqQueue Q); + + +int main(int argc, char* argv[]) { + SqQueue Q; + int i; + QElemType e; + + printf(" ʼ...\n"); + InitQueue_3_29(&Q); + + printf(" 5 Ӳ...\n"); + for(i = 1; i <= 5; i++) { + EnQueue_3_29(&Q, i); + + printf(" Ԫ \"%d\" Ӻ󣬶еԪΪ", i); + Output(Q); + } + + printf(" 5 Ӳ...\n"); + for(i = 1; i <= 5; i++) { + DeQueue_3_29(&Q, &e); + + printf(" Ԫ \"%d\" Ӻ󣬶еԪΪ", e); + Output(Q); + } + + return 0; +} + + +// гʼ +Status InitQueue_3_29(SqQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).base = (QElemType*) malloc(MAXQSIZE * sizeof(QElemType)); + if((*Q).base == NULL) { + exit(OVERFLOW); + } + + (*Q).front = (*Q).rear = 0; + (*Q).tag = 0; + + return OK; +} + +// +Status EnQueue_3_29(SqQueue* Q, QElemType e) { + // + if((*Q).tag == 1) { + return ERROR; + } + + (*Q).base[(*Q).rear] = e; + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + + // ֶ± + if((*Q).rear == (*Q).front) { + (*Q).tag = 1; + } + + return OK; +} + +// +Status DeQueue_3_29(SqQueue* Q, QElemType* e) { + // п + if((*Q).front == (*Q).rear && (*Q).tag == 0) { + return ERROR; + } + + *e = (*Q).base[(*Q).front]; + (*Q).front = ((*Q).front + 1) % MAXQSIZE; + + // ֶӿգ± + if((*Q).rear == (*Q).front) { + (*Q).tag = 0; + } + + return OK; +} + +// Ԫ +void Output(SqQueue Q) { + int i; + + // п + if(Q.front == Q.rear && Q.tag == 0) { + printf("\n"); + return; + } + + i = Q.front; + + do { + printf("%d ", Q.base[i]); + i = (i + 1) % MAXQSIZE; + } while(i != Q.rear); + + printf("\n"); +} diff --git a/VisualC++/ExerciseBook/03.29/03.29.vcxproj b/VisualC++/ExerciseBook/03.29/03.29.vcxproj new file mode 100644 index 0000000..405e616 --- /dev/null +++ b/VisualC++/ExerciseBook/03.29/03.29.vcxproj @@ -0,0 +1,72 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {9190852B-353A-4F23-89A8-9632F9C81F8D} + My0329 + + + + 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++/ExerciseBook/03.29/03.29.vcxproj.filters b/VisualC++/ExerciseBook/03.29/03.29.vcxproj.filters new file mode 100644 index 0000000..32f1247 --- /dev/null +++ b/VisualC++/ExerciseBook/03.29/03.29.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {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++/ExerciseBook/03.29/03.29.vcxproj.user b/VisualC++/ExerciseBook/03.29/03.29.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.29/03.29.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.30+03.32/03.30+03.32.c b/VisualC++/ExerciseBook/03.30+03.32/03.30+03.32.c new file mode 100644 index 0000000..8b246e8 --- /dev/null +++ b/VisualC++/ExerciseBook/03.30+03.32/03.30+03.32.c @@ -0,0 +1,209 @@ +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// +#include "SqList.h" //**02 Ա**// + +/* 궨 */ +#define MAXQSIZE 5 // гȣͬʱҲǺ쳲еά +#define MAX 100000 + +/* Ԫ */ +typedef int QElemType; + +/* ˳ѭ */ +typedef struct { + QElemType* base; // ָд洢ռ + int rear; // βα + int length; // Ԫظ +} SqQueue; + +// гʼ +Status InitQueue_3_30(SqQueue* Q); + +// +Status EnQueue_3_30(SqQueue* Q, QElemType e); + +// +Status DeQueue_3_30(SqQueue* Q, QElemType* e); + +/* + * k쳲ضǰn+1 + */ +Status Algo_3_32(int k, SqList* fib); + + +// Ԫ +void Output(SqQueue Q) { + int head; + + if(Q.length == 0) { + printf("\n"); + return; + } + + // ͷα + head = (Q.rear - Q.length + MAXQSIZE) % MAXQSIZE; + + do { + printf("%d ", Q.base[head]); + head = (head + 1) % MAXQSIZE; + } while(head != Q.rear); + + printf("\n"); +} + + +int main(int argc, char* argv[]) { + int i; + + printf(" 3.30 ֤...\n"); + { + SqQueue Q; + QElemType e; + printf(" ʼ...\n"); + InitQueue_3_30(&Q);\ + + printf(" 5 Ӳ...\n"); + for(i = 1; i <= 5; i++) { + EnQueue_3_30(&Q, i); + printf(" Ԫ \"%d\" Ӻ󣬶еԪΪ", i); + Output(Q); + } + + printf(" 5 Ӳ...\n"); + for(i = 1; i <= 5; i++) { + DeQueue_3_30(&Q, &e); + printf(" Ԫ \"%d\" Ӻ󣬶еԪΪ", e); + Output(Q); + } + } + + printf(" 3.32 ֤...\n"); + { + SqList fib; + + Algo_3_32(MAXQSIZE, &fib); + printf(" %d 쳲еǰ %d Ϊ\n", MAXQSIZE, fib.length); + for(i = 0; i < fib.length; i++) { + printf("%d ", fib.elem[i]); + } + printf("\n"); + } + + return 0; +} + + +// гʼ +Status InitQueue_3_30(SqQueue* Q) { + if(Q == NULL) { + return ERROR; + } + + (*Q).base = (QElemType*) malloc(MAXQSIZE * sizeof(QElemType)); + if((*Q).base == NULL) { + exit(OVERFLOW); + } + + (*Q).rear = 0; + (*Q).length = 0; + + return OK; +} + +// +Status EnQueue_3_30(SqQueue* Q, QElemType e) { + + if(Q == NULL) { + return ERROR; + } + + // ע + if((*Q).length == MAXQSIZE) { + return ERROR; + } + + (*Q).base[(*Q).rear] = e; + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + + (*Q).length++; + + return OK; +} + +// +Status DeQueue_3_30(SqQueue* Q, QElemType* e) { + int head; + + if(Q == NULL) { + return ERROR; + } + + // п + if((*Q).length == 0) { + return ERROR; + } + + // ͷα + head = ((*Q).rear - (*Q).length + MAXQSIZE) % MAXQSIZE; + + *e = (*Q).base[head]; + + (*Q).length--; + + return OK; +} + +/* + * k쳲ضǰn+1 + * + *ע + * ˸оЩǣǿ + * ΪȻҪǰn+1ֻk˵Ǹǰn+1ضҪ浽ĵط + * ǣȻǰn+1ûҪõѭеĺkˣ... + * Ϊġڡn+1ֵôѭӦüֵ + */ +Status Algo_3_32(int k, SqList* fib) { + int flag; + int i, j, sum; + SqQueue Q; + ElemType e; + + if(k < 2 || MAX < 0) { + return ERROR; + } + + InitQueue_3_30(&Q); + InitList(fib); + + // ǰk-1Ϊ0 + for(i = 1; i <= k - 1; i++) { + EnQueue_3_30(&Q, 0); + ListInsert(fib, i, 0); + } + + // kΪ1 + EnQueue_3_30(&Q, 1); + ListInsert(fib, i, 1); + + while((flag = GetElem(*fib, i, &e))==OK && e<=MAX){ + /* + * ѭԪصĺ + * ʵһԸΪ˳кkĺͣһûѭɶ + */ + for(j = 0, sum = 0; j < Q.length; j++) { + sum += Q.base[j]; + } + + // ˴eֻʱ + DeQueue_3_30(&Q, &e); + + // ¼Ԫ + EnQueue_3_30(&Q, sum); + + // ˳˳лһ + ListInsert(fib, ++i, sum); + } + + return flag; +} diff --git a/VisualC++/ExerciseBook/03.30+03.32/03.30+03.32.vcxproj b/VisualC++/ExerciseBook/03.30+03.32/03.30+03.32.vcxproj new file mode 100644 index 0000000..1c0de56 --- /dev/null +++ b/VisualC++/ExerciseBook/03.30+03.32/03.30+03.32.vcxproj @@ -0,0 +1,76 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {F107E88C-97BB-4276-AB01-BE69DF7765C7} + My03300332 + + + + 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++/ExerciseBook/03.30+03.32/03.30+03.32.vcxproj.filters b/VisualC++/ExerciseBook/03.30+03.32/03.30+03.32.vcxproj.filters new file mode 100644 index 0000000..081698a --- /dev/null +++ b/VisualC++/ExerciseBook/03.30+03.32/03.30+03.32.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++/ExerciseBook/03.30+03.32/03.30+03.32.vcxproj.user b/VisualC++/ExerciseBook/03.30+03.32/03.30+03.32.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.30+03.32/03.30+03.32.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.30+03.32/SqList.c b/VisualC++/ExerciseBook/03.30+03.32/SqList.c new file mode 100644 index 0000000..624aa2e --- /dev/null +++ b/VisualC++/ExerciseBook/03.30+03.32/SqList.c @@ -0,0 +1,105 @@ +/*============================= + * Ա˳洢ṹ˳ + * + * 㷨: 2.32.42.52.6 + =============================*/ + +#include "SqList.h" + +/* + * 㷨2.3 + * + * ʼ + * + * ʼɹ򷵻OK򷵻ERROR + */ +Status InitList(SqList* L) { + // ָڴ棬ʧܣ򷵻NULL + (*L).elem = (ElemType*) malloc(LIST_INIT_SIZE * sizeof(ElemType)); + if((*L).elem == NULL) { + // 洢ڴʧ + exit(OVERFLOW); + } + + (*L).length = 0; // ʼ˳Ϊ0 + (*L).listsize = LIST_INIT_SIZE; // ˳ʼڴ + + return OK; // ʼɹ +} + +/* + * ȡֵ + * + * ȡ˳еiԪأ洢eС + * ҵOK򣬷ERROR + * + *ע + * ̲iĺԪλã1ʼⲻϱͨԼ + * ͨiĺӦָ0ʼ + */ +Status GetElem(SqList L, int i, ElemType* e) { + // ΪiĺλãϷΧǣ[1, length] + if(i < 1 || i > L.length) { + return ERROR; //iֵϷ + } + + *e = L.elem[i - 1]; + + return OK; +} + +/* + * 㷨2.4 + * + * + * + * ˳iλϲeɹ򷵻OK򷵻ERROR + * + *ע + * ̲iĺԪλã1ʼ + */ +Status ListInsert(SqList* L, int i, ElemType e) { + ElemType* newbase; + ElemType* p, * q; + + // ȷ˳ṹ + if(L == NULL || (*L).elem == NULL) { + return ERROR; + } + + // iֵԽ + if(i < 1 || i > (*L).length + 1) { + return ERROR; + } + + // 洢ռ¿ռ + if((*L).length >= (*L).listsize) { + // пռ + newbase = (ElemType*) realloc((*L).elem, ((*L).listsize + LISTINCREMENT) * sizeof(ElemType)); + if(newbase == NULL) { + // 洢ڴʧ + exit(OVERFLOW); + } + + // »ַ + (*L).elem = newbase; + // Ĵ洢ռ + (*L).listsize += LISTINCREMENT; + } + + // qΪλ + q = &(*L).elem[i - 1]; + + // 1.Ԫأڳλ + for(p = &(*L).elem[(*L).length - 1]; p >= q; --p) { + *(p + 1) = *p; + } + + // 2.e + *q = e; + + // 3.1 + (*L).length++; + + return OK; +} diff --git a/VisualC++/ExerciseBook/03.30+03.32/SqList.h b/VisualC++/ExerciseBook/03.30+03.32/SqList.h new file mode 100644 index 0000000..5b3392e --- /dev/null +++ b/VisualC++/ExerciseBook/03.30+03.32/SqList.h @@ -0,0 +1,66 @@ +/*============================= + * Ա˳洢ṹ˳ + * + * 㷨: 2.32.42.52.6 + =============================*/ + +#ifndef SQLIST_H +#define SQLIST_H + +#include +#include // ṩmallocreallocfreeexitԭ +#include "Status.h" //**01 **// + +/* 궨 */ +#define LIST_INIT_SIZE 100 // ˳洢ռijʼ +#define LISTINCREMENT 10 // ˳洢ռķ + +/* ˳ԪͶ壬Ԫһ */ +typedef int ElemType; + +/* + * ˳ṹ + * + * עelemʹǰҪΪڴ棬Ԫشelem[0]ʼ洢 + */ +typedef struct { + ElemType* elem; // ˳洢ռĻַָ˳ռڴʼλã + int length; // ǰ˳ȣԪأ + int listsize; // ǰĴ洢Դ洢Ԫأ +} SqList; + + +/* + * 㷨2.3 + * + * ʼ + * + * ʼɹ򷵻OK򷵻ERROR + */ +Status InitList(SqList* L); + +/* + * ȡֵ + * + * ȡ˳еiԪأ洢eС + * ҵOK򣬷ERROR + * + *ע + * ̲iĺԪλã1ʼⲻϱͨԼ + * ͨiĺӦָ0ʼ + */ +Status GetElem(SqList L, int i, ElemType* e); + +/* + * 㷨2.4 + * + * + * + * ˳iλϲeɹ򷵻OK򷵻ERROR + * + *ע + * ̲iĺԪλã1ʼ + */ +Status ListInsert(SqList* L, int i, ElemType e); + +#endif diff --git a/VisualC++/ExerciseBook/03.31/03.31.c b/VisualC++/ExerciseBook/03.31/03.31.c new file mode 100644 index 0000000..88ff97b --- /dev/null +++ b/VisualC++/ExerciseBook/03.31/03.31.c @@ -0,0 +1,89 @@ +#include +#include // ṩstrlenԭ +#include "Status.h" //**01 **// +#include "SqStack.h" //**03 ջͶ**// + +/* + * жsǷΪ + * մҲΪ + */ +Status Algo_3_31(char* s); + + +int main(int argc, char* argv[]) { + char* a = "abcdedcba@"; + char* b = "ababab@"; + + if(Algo_3_31(a)) { + printf("%s@ǻУ\n", a); + } else { + printf("%s@ǻУ\n", a); + } + + if(Algo_3_31(b)) { + printf("%s@ǻУ\n", b); + } else { + printf("%s@ǻУ\n", b); + } + + return 0; +} + + +/* + * жsǷΪ + * մҲΪ + */ +Status Algo_3_31(char* s) { + int len; + int i; + int m, n; // ĴǰεĽβ±ͺεĿʼ± + SqStack S; + SElemType e; + + len = (int) strlen(s); + + if(len == 0 || s[len - 1] != '@') { + return FALSE; + } + + // մΪǻ + if(len == 1) { + return TRUE; + } + + if(len % 2 == 0) { + m = (len - 2) / 2 - 1; + n = m + 2; + } else { + m = (len - 2) / 2; + n = m + 1; + } + + InitStack(&S); + + // ȰѻĴǰջ + for(i = 0; i <= m; i++) { + Push(&S, s[i]); + } + + // ȡĴǰΣַкαȶ + for(i = n; i <= len - 2; i++) { + if(!StackEmpty(S)) { + Pop(&S, &e); + } else { + break; + } + + if(s[i] != e) { + return ERROR; + } + } + + // ״̬ӦջΪգiΪlen-1 + if(!(StackEmpty(S) && i == len - 1)) { + return ERROR; + } + + return OK; +} diff --git a/VisualC++/ExerciseBook/03.31/03.31.vcxproj b/VisualC++/ExerciseBook/03.31/03.31.vcxproj new file mode 100644 index 0000000..08ca15d --- /dev/null +++ b/VisualC++/ExerciseBook/03.31/03.31.vcxproj @@ -0,0 +1,76 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {321D37FA-0B11-403F-90C6-2C9F8F95F8CC} + My0331 + + + + 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++/ExerciseBook/03.31/03.31.vcxproj.filters b/VisualC++/ExerciseBook/03.31/03.31.vcxproj.filters new file mode 100644 index 0000000..fb41768 --- /dev/null +++ b/VisualC++/ExerciseBook/03.31/03.31.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++/ExerciseBook/03.31/03.31.vcxproj.user b/VisualC++/ExerciseBook/03.31/03.31.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.31/03.31.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.31/SqStack.c b/VisualC++/ExerciseBook/03.31/SqStack.c new file mode 100644 index 0000000..f056778 --- /dev/null +++ b/VisualC++/ExerciseBook/03.31/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++/ExerciseBook/03.31/SqStack.h b/VisualC++/ExerciseBook/03.31/SqStack.h new file mode 100644 index 0000000..a8fe835 --- /dev/null +++ b/VisualC++/ExerciseBook/03.31/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++/ExerciseBook/03.33/03.33.c b/VisualC++/ExerciseBook/03.33/03.33.c new file mode 100644 index 0000000..87db969 --- /dev/null +++ b/VisualC++/ExerciseBook/03.33/03.33.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include "Status.h" //**01 **// +#include "SqQueue.h" //**03 ջͶ**// + +// Ժӡ +void PrintElem(QElemType e) { + printf("%d ", e); +} + + +int main(int argc, char* argv[]) { + SqQueue Q; + QElemType e; + int i; + int random; + + printf(" ʼ...\n"); + InitQueue(&Q); + + printf(" 5 Ӳ...\n"); + srand((unsigned) time(NULL)); + for(i = 1; i <= 5; i++) { + + random = rand() % 100; + + EnQueue_3_33(&Q, random); + + printf(" Ԫ \"%2d\" Ӻ󣬶еԪΪ", random); + QueueTraverse(Q, PrintElem); + } + + printf(" 5 Ӳ...\n"); + for(i = 1; i <= 5; i++) { + DeQueue(&Q, &e); + + printf(" Ԫ \"%2d\" Ӻ󣬶еԪΪ", e); + QueueTraverse(Q, PrintElem); + } + + return 0; +} diff --git a/VisualC++/ExerciseBook/03.33/03.33.vcxproj b/VisualC++/ExerciseBook/03.33/03.33.vcxproj new file mode 100644 index 0000000..ee15775 --- /dev/null +++ b/VisualC++/ExerciseBook/03.33/03.33.vcxproj @@ -0,0 +1,76 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {9A9C1E7C-E429-4E05-B187-994DECDB884B} + My0333 + + + + 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++/ExerciseBook/03.33/03.33.vcxproj.filters b/VisualC++/ExerciseBook/03.33/03.33.vcxproj.filters new file mode 100644 index 0000000..3527603 --- /dev/null +++ b/VisualC++/ExerciseBook/03.33/03.33.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++/ExerciseBook/03.33/03.33.vcxproj.user b/VisualC++/ExerciseBook/03.33/03.33.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.33/03.33.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.33/SqQueue.c b/VisualC++/ExerciseBook/03.33/SqQueue.c new file mode 100644 index 0000000..c9f15af --- /dev/null +++ b/VisualC++/ExerciseBook/03.33/SqQueue.c @@ -0,0 +1,113 @@ +/*============================= + * е˳洢ṹ˳У + ==============================*/ + +#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 EnQueue_3_33(SqQueue* Q, QElemType e) { + int head, tail; + + if(Q == NULL) { + return ERROR; + } + + // ֱӷ + if(((*Q).rear + 1) % MAXQSIZE == (*Q).front) { + return ERROR; + } + + // ΪգֱӲ뵽β + if((*Q).front == (*Q).rear) { + (*Q).base[(*Q).rear] = e; + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + return OK; + } + + head = (*Q).base[(*Q).front]; // ͷԪֵ + tail = (*Q).base[((*Q).rear - 1 + MAXQSIZE) % MAXQSIZE]; // βԪֵ + + // Ԫصҵʱ䲻СڶβԪҵƽʱ䣬Ԫزڶβ + if(e >= (head + tail) / 2) { + (*Q).base[(*Q).rear] = e; + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + + // 򣬲ڶͷ + } else { + (*Q).front = ((*Q).front - 1 + MAXQSIZE) % MAXQSIZE; + (*Q).base[(*Q).front] = e; + } + + 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++/ExerciseBook/03.33/SqQueue.h b/VisualC++/ExerciseBook/03.33/SqQueue.h new file mode 100644 index 0000000..99f9fca --- /dev/null +++ b/VisualC++/ExerciseBook/03.33/SqQueue.h @@ -0,0 +1,63 @@ +/*============================= + * е˳洢ṹ˳У + ==============================*/ + +#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 EnQueue_3_33(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++/ExerciseBook/03.34/03.34.c b/VisualC++/ExerciseBook/03.34/03.34.c new file mode 100644 index 0000000..faa34b1 --- /dev/null +++ b/VisualC++/ExerciseBook/03.34/03.34.c @@ -0,0 +1,152 @@ +#include +#include "Status.h" //**01 **// +#include "SqQueue.h" //**03 ջͶ**// + +/* + * ɳУУ + * 'L': ֱӴڿ + * 'E': Ӷͷת + * 'A': Ӷβת + * 'D': Ӷͷ + * + * ұߣߣתеĶͷߣβұߣҳȡ˳򡾴ҡ + * + * En ȴȵг + * seq + */ +void Algo_3_34(char* En, char seq[]); + +/* + * ʹԼõĵseqEnijȵEx + * + * ÷ɿǶԵеļ + * + * En ȴȵг + * seq + * Ex ɺ + */ +Status Dispatch(char* En, char* seq, char Ex[]); + + +int main(int argc, char* argv[]) { + char* En = "HPSHHSHPPHHSPSHSPSSHSP"; // ȴȵг + char Ex[100] = {'\0'}; // ɵг + char seq[100] = {'\0'}; // + + printf(" ڴУ\n"); + printf(" En = %s\n", En); + + Algo_3_34(En, seq); + printf(" ɵĵУ\n"); + printf(" seq = %s\n", seq); + + Dispatch(En, seq, Ex); + printf(" ɵĵУڴijȣɺijУ\n"); + printf(" Ex = %s\n", Ex); + + return 0; +} + + +/* + * ɳУУ + * 'L': ֱӴڿ + * 'E': Ӷͷת + * 'A': Ӷβת + * 'D': Ӷͷ + * + * ұߣߣתеĶͷߣβұߣҳȡ˳򡾴ҡ + * + * En ȴȵг + * seq + */ +void Algo_3_34(char* En, char seq[]) { + int i, j; + SqQueue Q; + QElemType e; + + // ʼһת + InitQueue(&Q); + + for(i = j = 0; En[i] != '\0'; i++) { + // ӲֱӵȵĿĵ + if(En[i] == 'P') { + seq[j++] = 'L'; // LֱӴڿ + } + + // ԣӶͷת + if(En[i] == 'S') { + EnQueue_3_34(&Q, En[i], 0); + seq[j++] = 'E'; + } + + // ӲԣӶβת + if(En[i] == 'H') { + EnQueue_3_34(&Q, En[i], 1); + seq[j++] = 'A'; + } + } + + // תеԺӲԴӶͷ + while(Q.front != Q.rear) { + DeQueue(&Q, &e); + seq[j++] = 'D'; + } + + seq[j] = '\0'; +} + +/* + * ʹԼõĵseqEnijȵEx + * + * ÷ɿǶԵеļ + * + * En ȴȵг + * seq + * Ex ɺ + */ +Status Dispatch(char* En, char* seq, char Ex[]) { + int i, j, k; + SqQueue Q; + QElemType e; + + // ʼһת + InitQueue(&Q); + + i = j = k = 0; + + // + while(seq[k] != '\0') { + // ӲֱӴڿ + if(seq[k] == 'L') { + Ex[j++] = En[i++]; + } + + // ԣӶͷת + if(seq[k] == 'E') { + EnQueue_3_34(&Q, En[i++], 0); + } + + // ӲԣӶβת + if(seq[k] == 'A') { + EnQueue_3_34(&Q, En[i++], 1); + } + + // תеԺӲԴӶͷ + if(seq[k] == 'D') { + DeQueue(&Q, &e); + Ex[j++] = e; + } + + k++; + } + + // Ϊգڴδȵijᣬתдδijᣬʾ + if(seq[k] == '\0' && (En[i] || Q.front == Q.rear)) { + return ERROR; + } + + Ex[j] = '\0'; + + return OK; +} diff --git a/VisualC++/ExerciseBook/03.34/03.34.vcxproj b/VisualC++/ExerciseBook/03.34/03.34.vcxproj new file mode 100644 index 0000000..9db329e --- /dev/null +++ b/VisualC++/ExerciseBook/03.34/03.34.vcxproj @@ -0,0 +1,76 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {0DCA5F0D-B6FB-44E8-9AA4-0806C870D386} + My0334 + + + + 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++/ExerciseBook/03.34/03.34.vcxproj.filters b/VisualC++/ExerciseBook/03.34/03.34.vcxproj.filters new file mode 100644 index 0000000..c58564b --- /dev/null +++ b/VisualC++/ExerciseBook/03.34/03.34.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++/ExerciseBook/03.34/03.34.vcxproj.user b/VisualC++/ExerciseBook/03.34/03.34.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/VisualC++/ExerciseBook/03.34/03.34.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VisualC++/ExerciseBook/03.34/SqQueue.c b/VisualC++/ExerciseBook/03.34/SqQueue.c new file mode 100644 index 0000000..0e99977 --- /dev/null +++ b/VisualC++/ExerciseBook/03.34/SqQueue.c @@ -0,0 +1,98 @@ +/*============================= + * е˳洢ṹ˳У + ==============================*/ + +#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; +} + +/* + * + * + * diӷ0ָʾӶͷӣ1ָʾӶβ + */ +Status EnQueue_3_34(SqQueue* Q, QElemType e, int di) { + + // Ӷͷ + if(di == 0) { + (*Q).front = ((*Q).front - 1 + MAXQSIZE) % MAXQSIZE; + (*Q).base[(*Q).front] = e; + return OK; + } + + // Ӷβ + if(di == 1) { + (*Q).base[(*Q).rear] = e; + (*Q).rear = ((*Q).rear + 1) % MAXQSIZE; + return OK; + } + + return ERROR; +} + +/* + * + * + * ƳͷԪأ洢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++/ExerciseBook/03.34/SqQueue.h b/VisualC++/ExerciseBook/03.34/SqQueue.h new file mode 100644 index 0000000..9de3f05 --- /dev/null +++ b/VisualC++/ExerciseBook/03.34/SqQueue.h @@ -0,0 +1,65 @@ +/*============================= + * е˳洢ṹ˳У + ==============================*/ + +#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); + +/* + * + * + * diӷ0ָʾӶͷӣ1ָʾӶβ + */ +Status EnQueue_3_34(SqQueue* Q, QElemType e, int di); + +/* + * + * + * ƳͷԪأ洢eС + */ +Status DeQueue(SqQueue* Q, QElemType* e); + +/* + * + * + * visitʶQ + */ +Status QueueTraverse(SqQueue Q, void(Visit)(QElemType)); + +#endif diff --git a/VisualC++/ExerciseBook/ExerciseBook.sdf b/VisualC++/ExerciseBook/ExerciseBook.sdf index baa3716..94b10c3 100644 Binary files a/VisualC++/ExerciseBook/ExerciseBook.sdf and b/VisualC++/ExerciseBook/ExerciseBook.sdf differ diff --git a/VisualC++/ExerciseBook/ExerciseBook.sln b/VisualC++/ExerciseBook/ExerciseBook.sln index b340daa..e203e9d 100644 --- a/VisualC++/ExerciseBook/ExerciseBook.sln +++ b/VisualC++/ExerciseBook/ExerciseBook.sln @@ -61,6 +61,44 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "02.39-02.40", "02.39-02.40\ EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "02.41-02.42", "02.41-02.42\02.41-02.42.vcxproj", "{40DA8411-A6E0-488C-83B2-00ED82B716AC}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.15", "03.15\03.15.vcxproj", "{2D94B566-3A72-46FA-9C14-A8B2FD1B8D38}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.16", "03.16\03.16.vcxproj", "{DF3537A1-D723-4619-932D-F7A782197B6C}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.17", "03.17\03.17.vcxproj", "{B9129167-2A92-4544-85CC-8414EF965898}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.18", "03.18\03.18.vcxproj", "{101C3B10-07F9-43D9-93B6-00AE59FF84E0}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.19", "03.19\03.19.vcxproj", "{18FCCC0C-0DD4-4DBF-99E2-78F96A358233}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.20", "03.20\03.20.vcxproj", "{2BF34829-27A6-4295-9CEF-858BF01BE158}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.21", "03.21\03.21.vcxproj", "{7E5791FB-5D18-45A8-BD9E-8982CA33B925}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.22", "03.22\03.22.vcxproj", "{3B22A494-7A0D-4CBA-A2A1-F70D189B3214}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.23", "03.23\03.23.vcxproj", "{6BFF283E-28CF-4C2C-8E26-248142B9EDFF}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.24", "03.24\03.24.vcxproj", "{AFFBB6C2-ED4A-446A-B562-9AB50472EB0A}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.25", "03.25\03.25.vcxproj", "{8C7CCC84-ABF6-462F-A095-12443829D133}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.26", "03.26\03.26.vcxproj", "{C51CA8CF-CAF8-4013-859D-CB7E2266B6B6}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.27", "03.27\03.27.vcxproj", "{72A6BBFE-D06E-4F04-93EC-D9A8E520C191}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.28", "03.28\03.28.vcxproj", "{352A25BF-FD02-4EB9-B285-1195029D8465}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.29", "03.29\03.29.vcxproj", "{9190852B-353A-4F23-89A8-9632F9C81F8D}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.30+03.32", "03.30+03.32\03.30+03.32.vcxproj", "{F107E88C-97BB-4276-AB01-BE69DF7765C7}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.31", "03.31\03.31.vcxproj", "{321D37FA-0B11-403F-90C6-2C9F8F95F8CC}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.33", "03.33\03.33.vcxproj", "{9A9C1E7C-E429-4E05-B187-994DECDB884B}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03.34", "03.34\03.34.vcxproj", "{0DCA5F0D-B6FB-44E8-9AA4-0806C870D386}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -187,6 +225,82 @@ Global {40DA8411-A6E0-488C-83B2-00ED82B716AC}.Debug|Win32.Build.0 = Debug|Win32 {40DA8411-A6E0-488C-83B2-00ED82B716AC}.Release|Win32.ActiveCfg = Release|Win32 {40DA8411-A6E0-488C-83B2-00ED82B716AC}.Release|Win32.Build.0 = Release|Win32 + {2D94B566-3A72-46FA-9C14-A8B2FD1B8D38}.Debug|Win32.ActiveCfg = Debug|Win32 + {2D94B566-3A72-46FA-9C14-A8B2FD1B8D38}.Debug|Win32.Build.0 = Debug|Win32 + {2D94B566-3A72-46FA-9C14-A8B2FD1B8D38}.Release|Win32.ActiveCfg = Release|Win32 + {2D94B566-3A72-46FA-9C14-A8B2FD1B8D38}.Release|Win32.Build.0 = Release|Win32 + {DF3537A1-D723-4619-932D-F7A782197B6C}.Debug|Win32.ActiveCfg = Debug|Win32 + {DF3537A1-D723-4619-932D-F7A782197B6C}.Debug|Win32.Build.0 = Debug|Win32 + {DF3537A1-D723-4619-932D-F7A782197B6C}.Release|Win32.ActiveCfg = Release|Win32 + {DF3537A1-D723-4619-932D-F7A782197B6C}.Release|Win32.Build.0 = Release|Win32 + {B9129167-2A92-4544-85CC-8414EF965898}.Debug|Win32.ActiveCfg = Debug|Win32 + {B9129167-2A92-4544-85CC-8414EF965898}.Debug|Win32.Build.0 = Debug|Win32 + {B9129167-2A92-4544-85CC-8414EF965898}.Release|Win32.ActiveCfg = Release|Win32 + {B9129167-2A92-4544-85CC-8414EF965898}.Release|Win32.Build.0 = Release|Win32 + {101C3B10-07F9-43D9-93B6-00AE59FF84E0}.Debug|Win32.ActiveCfg = Debug|Win32 + {101C3B10-07F9-43D9-93B6-00AE59FF84E0}.Debug|Win32.Build.0 = Debug|Win32 + {101C3B10-07F9-43D9-93B6-00AE59FF84E0}.Release|Win32.ActiveCfg = Release|Win32 + {101C3B10-07F9-43D9-93B6-00AE59FF84E0}.Release|Win32.Build.0 = Release|Win32 + {18FCCC0C-0DD4-4DBF-99E2-78F96A358233}.Debug|Win32.ActiveCfg = Debug|Win32 + {18FCCC0C-0DD4-4DBF-99E2-78F96A358233}.Debug|Win32.Build.0 = Debug|Win32 + {18FCCC0C-0DD4-4DBF-99E2-78F96A358233}.Release|Win32.ActiveCfg = Release|Win32 + {18FCCC0C-0DD4-4DBF-99E2-78F96A358233}.Release|Win32.Build.0 = Release|Win32 + {2BF34829-27A6-4295-9CEF-858BF01BE158}.Debug|Win32.ActiveCfg = Debug|Win32 + {2BF34829-27A6-4295-9CEF-858BF01BE158}.Debug|Win32.Build.0 = Debug|Win32 + {2BF34829-27A6-4295-9CEF-858BF01BE158}.Release|Win32.ActiveCfg = Release|Win32 + {2BF34829-27A6-4295-9CEF-858BF01BE158}.Release|Win32.Build.0 = Release|Win32 + {7E5791FB-5D18-45A8-BD9E-8982CA33B925}.Debug|Win32.ActiveCfg = Debug|Win32 + {7E5791FB-5D18-45A8-BD9E-8982CA33B925}.Debug|Win32.Build.0 = Debug|Win32 + {7E5791FB-5D18-45A8-BD9E-8982CA33B925}.Release|Win32.ActiveCfg = Release|Win32 + {7E5791FB-5D18-45A8-BD9E-8982CA33B925}.Release|Win32.Build.0 = Release|Win32 + {3B22A494-7A0D-4CBA-A2A1-F70D189B3214}.Debug|Win32.ActiveCfg = Debug|Win32 + {3B22A494-7A0D-4CBA-A2A1-F70D189B3214}.Debug|Win32.Build.0 = Debug|Win32 + {3B22A494-7A0D-4CBA-A2A1-F70D189B3214}.Release|Win32.ActiveCfg = Release|Win32 + {3B22A494-7A0D-4CBA-A2A1-F70D189B3214}.Release|Win32.Build.0 = Release|Win32 + {6BFF283E-28CF-4C2C-8E26-248142B9EDFF}.Debug|Win32.ActiveCfg = Debug|Win32 + {6BFF283E-28CF-4C2C-8E26-248142B9EDFF}.Debug|Win32.Build.0 = Debug|Win32 + {6BFF283E-28CF-4C2C-8E26-248142B9EDFF}.Release|Win32.ActiveCfg = Release|Win32 + {6BFF283E-28CF-4C2C-8E26-248142B9EDFF}.Release|Win32.Build.0 = Release|Win32 + {AFFBB6C2-ED4A-446A-B562-9AB50472EB0A}.Debug|Win32.ActiveCfg = Debug|Win32 + {AFFBB6C2-ED4A-446A-B562-9AB50472EB0A}.Debug|Win32.Build.0 = Debug|Win32 + {AFFBB6C2-ED4A-446A-B562-9AB50472EB0A}.Release|Win32.ActiveCfg = Release|Win32 + {AFFBB6C2-ED4A-446A-B562-9AB50472EB0A}.Release|Win32.Build.0 = Release|Win32 + {8C7CCC84-ABF6-462F-A095-12443829D133}.Debug|Win32.ActiveCfg = Debug|Win32 + {8C7CCC84-ABF6-462F-A095-12443829D133}.Debug|Win32.Build.0 = Debug|Win32 + {8C7CCC84-ABF6-462F-A095-12443829D133}.Release|Win32.ActiveCfg = Release|Win32 + {8C7CCC84-ABF6-462F-A095-12443829D133}.Release|Win32.Build.0 = Release|Win32 + {C51CA8CF-CAF8-4013-859D-CB7E2266B6B6}.Debug|Win32.ActiveCfg = Debug|Win32 + {C51CA8CF-CAF8-4013-859D-CB7E2266B6B6}.Debug|Win32.Build.0 = Debug|Win32 + {C51CA8CF-CAF8-4013-859D-CB7E2266B6B6}.Release|Win32.ActiveCfg = Release|Win32 + {C51CA8CF-CAF8-4013-859D-CB7E2266B6B6}.Release|Win32.Build.0 = Release|Win32 + {72A6BBFE-D06E-4F04-93EC-D9A8E520C191}.Debug|Win32.ActiveCfg = Debug|Win32 + {72A6BBFE-D06E-4F04-93EC-D9A8E520C191}.Debug|Win32.Build.0 = Debug|Win32 + {72A6BBFE-D06E-4F04-93EC-D9A8E520C191}.Release|Win32.ActiveCfg = Release|Win32 + {72A6BBFE-D06E-4F04-93EC-D9A8E520C191}.Release|Win32.Build.0 = Release|Win32 + {352A25BF-FD02-4EB9-B285-1195029D8465}.Debug|Win32.ActiveCfg = Debug|Win32 + {352A25BF-FD02-4EB9-B285-1195029D8465}.Debug|Win32.Build.0 = Debug|Win32 + {352A25BF-FD02-4EB9-B285-1195029D8465}.Release|Win32.ActiveCfg = Release|Win32 + {352A25BF-FD02-4EB9-B285-1195029D8465}.Release|Win32.Build.0 = Release|Win32 + {9190852B-353A-4F23-89A8-9632F9C81F8D}.Debug|Win32.ActiveCfg = Debug|Win32 + {9190852B-353A-4F23-89A8-9632F9C81F8D}.Debug|Win32.Build.0 = Debug|Win32 + {9190852B-353A-4F23-89A8-9632F9C81F8D}.Release|Win32.ActiveCfg = Release|Win32 + {9190852B-353A-4F23-89A8-9632F9C81F8D}.Release|Win32.Build.0 = Release|Win32 + {F107E88C-97BB-4276-AB01-BE69DF7765C7}.Debug|Win32.ActiveCfg = Debug|Win32 + {F107E88C-97BB-4276-AB01-BE69DF7765C7}.Debug|Win32.Build.0 = Debug|Win32 + {F107E88C-97BB-4276-AB01-BE69DF7765C7}.Release|Win32.ActiveCfg = Release|Win32 + {F107E88C-97BB-4276-AB01-BE69DF7765C7}.Release|Win32.Build.0 = Release|Win32 + {321D37FA-0B11-403F-90C6-2C9F8F95F8CC}.Debug|Win32.ActiveCfg = Debug|Win32 + {321D37FA-0B11-403F-90C6-2C9F8F95F8CC}.Debug|Win32.Build.0 = Debug|Win32 + {321D37FA-0B11-403F-90C6-2C9F8F95F8CC}.Release|Win32.ActiveCfg = Release|Win32 + {321D37FA-0B11-403F-90C6-2C9F8F95F8CC}.Release|Win32.Build.0 = Release|Win32 + {9A9C1E7C-E429-4E05-B187-994DECDB884B}.Debug|Win32.ActiveCfg = Debug|Win32 + {9A9C1E7C-E429-4E05-B187-994DECDB884B}.Debug|Win32.Build.0 = Debug|Win32 + {9A9C1E7C-E429-4E05-B187-994DECDB884B}.Release|Win32.ActiveCfg = Release|Win32 + {9A9C1E7C-E429-4E05-B187-994DECDB884B}.Release|Win32.Build.0 = Release|Win32 + {0DCA5F0D-B6FB-44E8-9AA4-0806C870D386}.Debug|Win32.ActiveCfg = Debug|Win32 + {0DCA5F0D-B6FB-44E8-9AA4-0806C870D386}.Debug|Win32.Build.0 = Debug|Win32 + {0DCA5F0D-B6FB-44E8-9AA4-0806C870D386}.Release|Win32.ActiveCfg = Release|Win32 + {0DCA5F0D-B6FB-44E8-9AA4-0806C870D386}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/VisualC++/ExerciseBook/ExerciseBook.suo b/VisualC++/ExerciseBook/ExerciseBook.suo index 6aaf49c..42293d9 100644 Binary files a/VisualC++/ExerciseBook/ExerciseBook.suo and b/VisualC++/ExerciseBook/ExerciseBook.suo differ