- 栈的应用之行编辑程序
- 16年3月2日15:12:53
-
-
- 这个程序并没有写成功,先把自己写的程序粘贴如下:
- /**
- * Error code.............I don't know why.........
- */
-
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
-
- #define STACK_INIT_SIZE 100
- #define STACK_INCREMENT 10
-
- typedef struct {
- char *top;
- char *base;
- int stacksize;
- }Stack;
-
- char * InitStack(Stack *S)
- {
- S->base = (char *)malloc(sizeof(char) * STACK_INIT_SIZE);
- if (!S->base)
- {
- printf("Can not malloc memory for stack.\n");
- exit(-1);
- }
-
- S->top = S->base;
- S->stacksize = STACK_INIT_SIZE;
-
- return S->base;
- }
-
- void Push(Stack *S, char c)
- {
- if ((S->top - S->base) > S->stacksize)
- {
- S->base = (char *)realloc(S->base, (S->stacksize + STACK_INCREMENT));
- if (!S->base)
- {
- printf("Can not reallco memory for Stack.\n");
- exit(-1);
- }
-
- S->top = S->base + S->stacksize;
- S->stacksize += STACK_INCREMENT;
- }
-
- *S->top = c;
- S->top++;
- }
-
- int Pop(Stack *S, char c)
- {
- if (S->top == S->base)
- {
- printf("The stack is empty.\n");
- exit(-1);
- }
-
- c = *(--S->top);
-
- return 1;
- }
-
- int IsEmpty(Stack S)
- {
- return S.top == S.base;
- }
-
- char GetTop(Stack S, char *elem)
- {
- if (S.top == S.base)
- {
- printf("The Stack is Empty.\n");
- exit(-1);
- }
-
- *elem = *--S.top;
- return *elem;
- }
-
- void TraverseStack(Stack S)
- {
- while (S.top > S.base)
- {
- printf("%c", *--S.top);
- }
- printf("\n");
- }
-
- void ClearStack(Stack *S)
- {
- S->top = S->base;
- }
-
- int main(int argc, char const *argv[])
- {
- Stack S;
- char ch, *p, e;
- int i;
- FILE *fd;
-
- fd = fopen("edit.c", "w");
- if (!fd)
- {
- printf("Cannot fopen fd.\n");
- exit(-1);
- }
-
- InitStack(&S);
-
- ch = getchar();
-
- while (ch != EOF)
- {
- while (ch != EOF && ch != '\n')
- {
- switch (ch)
- {
- case '#' :
- Pop(&S, e);
- break;
-
- case '@' :
- ClearStack(&S);
- break;
-
- default : Push(&S, ch);
- }
- ch = getchar();
- }
- #if 0
- for (i = 0; i < (S.top - S.base); i++)
- {
- fputc(*(S.base++), fd);
- }
- #endif
- p = S.base;
- while (p != S.top)
- {
- printf("%c", *p);
- fputc(*p, fd);
- p++;
- }
-
- fputc('\n', fd);
- if (ch != EOF)
- {
- ch = getchar();
- }
- }
-
- return 0;
- }
- 理论上这些程序没有错误,但是就是在里面打开的edit.c文件里面看不到输出的内容。自己在调试的时候,自己添加的打印语句都能够将栈内的元素内容打印到串口中,于是自己写了一个测试程序,一步一步测试,结果发现,问题出在最外面一层while循环上面,如果没有这个while循环的话,能够把字符输出到edit.c
- 这个文件中,但是加上外面这个while循环后,文件内就没有显示了。。。。下面粘贴测试程序,供以后参考吧。。。。。!_!花费了两天时间!_!
- #include <stdio.h>
- #include <stdlib.h>
-
- #define STACK_INIT_SIZE 100
- #define STACK_INCREMENT 10
-
- typedef struct {
- char *top;
- char *base;
- int stacksize;
- }Stack;
-
- char * InitStack(Stack *S)
- {
- S->base = (char *)malloc(sizeof(char) * STACK_INIT_SIZE);
- if (!S->base)
- {
- printf("Can not malloc memory for stack.\n");
- exit(-1);
- }
-
- S->top = S->base;
- S->stacksize = STACK_INIT_SIZE;
-
- return S->base;
- }
-
- void Push(Stack *S, char c)
- {
- if ((S->top - S->base) > S->stacksize)
- {
- S->base = (char *)realloc(S->base, (S->stacksize + STACK_INCREMENT));
- if (!S->base)
- {
- printf("Can not reallco memory for Stack.\n");
- exit(-1);
- }
-
- S->top = S->base + S->stacksize;
- S->stacksize += STACK_INCREMENT;
- }
-
- *S->top = c;
- S->top++;
- }
-
- int Pop(Stack *S, char c)
- {
- if (S->top == S->base)
- {
- printf("The stack is empty.\n");
- exit(-1);
- }
-
- c = *(--S->top);
-
- return 1;
- }
-
- void ClearStack(Stack *S)
- {
- S->top = S->base;
- }
-
- int main(int argc, char const *argv[])
- {
- Stack S;
- FILE *fd;
- char *p;
- char ch, e;
-
- // p = &ch;
- InitStack(&S);
-
- fd = fopen("edit.c", "w");
- if (!fd)
- {
- printf("Create file error!\n");
- }
- #if 0
- Push(&S, 'a');
- Push(&S, 'b');
- Push(&S, 'c');
- Push(&S, 'd');
- #endif
-
- ch = getchar();
- while (ch != EOF && ch != '\n')
- {
- switch (ch)
- {
- case '#' :
- Pop(&S, e);
- break;
-
- case '@' :
- ClearStack(&S);
- break;
-
- default : Push(&S, ch);
- }
- ch = getchar();
- }
-
- p = S.base;
- while (p != S.top)
- {
- printf("%c", *p);
- fputc(*p, fd);
- p++;
- }
-
- fputc('\n', fd);
- ClearStack(&S);
- #if 0
- printf("%c \n", *p);
- fputc(*p, fd);
- fputc(*p, fd);
- fputc(*p, fd);
- fputc(*p, fd);
- #endif
- fclose(fd);
- return 0;
- }
相关热门文章
给主人留下些什么吧!~~
评论热议