Chinaunix首页 | 论坛 | 博客
  • 博客访问: 298902
  • 博文数量: 76
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 715
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-20 20:38
文章分类
文章存档

2016年(20)

2015年(56)

分类: 嵌入式

2016-03-02 20:01:06


  1. 栈的应用之行编辑程序
  2. 16年3月2日15:12:53


  3. 这个程序并没有写成功,先把自己写的程序粘贴如下:
  4. /**
  5.  * Error code.............I don't know why.........
  6.  */

  7. #include <stdio.h>
  8. #include <malloc.h>
  9. #include <stdlib.h>

  10. #define STACK_INIT_SIZE 100
  11. #define STACK_INCREMENT 10

  12. typedef struct {
  13.     char *top;
  14.     char *base;
  15.     int stacksize;
  16. }Stack;

  17. char * InitStack(Stack *S)
  18. {
  19.     S->base = (char *)malloc(sizeof(char) * STACK_INIT_SIZE);
  20.     if (!S->base)
  21.     {
  22.         printf("Can not malloc memory for stack.\n");
  23.         exit(-1);
  24.     }

  25.     S->top = S->base;
  26.     S->stacksize = STACK_INIT_SIZE;

  27.     return S->base;
  28. }

  29. void Push(Stack *S, char c)
  30. {
  31.     if ((S->top - S->base) > S->stacksize)
  32.     {
  33.          S->base = (char *)realloc(S->base, (S->stacksize + STACK_INCREMENT));
  34.          if (!S->base)
  35.          {
  36.              printf("Can not reallco memory for Stack.\n");
  37.              exit(-1);    
  38.          }

  39.          S->top = S->base + S->stacksize;
  40.          S->stacksize += STACK_INCREMENT;
  41.     }

  42.     *S->top = c;
  43.     S->top++;
  44. }

  45. int Pop(Stack *S, char c)
  46. {
  47.     if (S->top == S->base)
  48.     {
  49.         printf("The stack is empty.\n");
  50.         exit(-1);
  51.     }

  52.     c = *(--S->top);

  53.     return 1;
  54. }

  55. int IsEmpty(Stack S)
  56. {
  57.     return S.top == S.base;
  58. }

  59. char GetTop(Stack S, char *elem)
  60. {
  61.     if (S.top == S.base)
  62.     {
  63.         printf("The Stack is Empty.\n");
  64.         exit(-1);
  65.     }

  66.     *elem = *--S.top;
  67.     return *elem;
  68. }

  69. void TraverseStack(Stack S)
  70. {
  71.     while (S.top > S.base)
  72.     {
  73.         printf("%c", *--S.top);
  74.     }
  75.     printf("\n");
  76. }

  77. void ClearStack(Stack *S)
  78. {
  79.     S->top = S->base;
  80. }

  81. int main(int argc, char const *argv[])
  82. {
  83.     Stack S;
  84.     char ch, *p, e;
  85.     int i;
  86.     FILE *fd;

  87.     fd = fopen("edit.c", "w");
  88.     if (!fd)
  89.     {
  90.         printf("Cannot fopen fd.\n");    
  91.         exit(-1);
  92.     }

  93.     InitStack(&S);

  94.     ch = getchar();

  95.     while (ch != EOF)
  96.     {
  97.         while (ch != EOF && ch != '\n')
  98.         {
  99.             switch (ch)
  100.             {
  101.                 case '#' :
  102.                 Pop(&S, e);
  103.                 break;

  104.                 case '@' :
  105.                 ClearStack(&S);
  106.                 break;

  107.                 default : Push(&S, ch);
  108.             }
  109.             ch = getchar();
  110.         }
  111. #if 0
  112.         for (i = 0; i < (S.top - S.base); i++)
  113.         {
  114.             fputc(*(S.base++), fd);
  115.         }
  116. #endif
  117.         p = S.base;
  118.         while (p != S.top)
  119.         {
  120.             printf("%c", *p);
  121.             fputc(*p, fd);    
  122.             p++;
  123.         }

  124.         fputc('\n', fd);
  125.         if (ch != EOF)
  126.         {
  127.             ch = getchar();    
  128.         }
  129.     }

  130.     return 0;
  131. }
  132. 理论上这些程序没有错误,但是就是在里面打开的edit.c文件里面看不到输出的内容。自己在调试的时候,自己添加的打印语句都能够将栈内的元素内容打印到串口中,于是自己写了一个测试程序,一步一步测试,结果发现,问题出在最外面一层while循环上面,如果没有这个while循环的话,能够把字符输出到edit.c
  133. 这个文件中,但是加上外面这个while循环后,文件内就没有显示了。。。。下面粘贴测试程序,供以后参考吧。。。。。!_!花费了两天时间!_!
  134. #include <stdio.h>
  135. #include <stdlib.h>

  136. #define STACK_INIT_SIZE 100
  137. #define STACK_INCREMENT 10

  138. typedef struct {
  139.     char *top;
  140.     char *base;
  141.     int stacksize;
  142. }Stack;
  143.  
  144. char * InitStack(Stack *S)
  145. {
  146.     S->base = (char *)malloc(sizeof(char) * STACK_INIT_SIZE);
  147.     if (!S->base)
  148.     {
  149.         printf("Can not malloc memory for stack.\n");
  150.         exit(-1);
  151.     }

  152.     S->top = S->base;
  153.     S->stacksize = STACK_INIT_SIZE;

  154.     return S->base;
  155. }

  156. void Push(Stack *S, char c)
  157. {
  158.     if ((S->top - S->base) > S->stacksize)
  159.     {
  160.          S->base = (char *)realloc(S->base, (S->stacksize + STACK_INCREMENT));
  161.          if (!S->base)
  162.          {
  163.              printf("Can not reallco memory for Stack.\n");
  164.              exit(-1);    
  165.          }

  166.          S->top = S->base + S->stacksize;
  167.          S->stacksize += STACK_INCREMENT;
  168.     }

  169.     *S->top = c;
  170.     S->top++;
  171. }

  172. int Pop(Stack *S, char c)
  173. {
  174.     if (S->top == S->base)
  175.     {
  176.         printf("The stack is empty.\n");
  177.         exit(-1);
  178.     }

  179.     c = *(--S->top);

  180.     return 1;
  181. }

  182. void ClearStack(Stack *S)
  183. {
  184.     S->top = S->base;
  185. }

  186. int main(int argc, char const *argv[])
  187. {
  188.     Stack S;
  189.     FILE *fd;
  190.     char *p;
  191.     char ch, e;
  192.  
  193. //    p = &ch;
  194.     InitStack(&S);

  195.     fd = fopen("edit.c", "w");
  196.     if (!fd)
  197.     {
  198.         printf("Create file error!\n");
  199.     }
  200. #if 0
  201.     Push(&S, 'a');
  202.     Push(&S, 'b');
  203.     Push(&S, 'c');
  204.     Push(&S, 'd');
  205. #endif

  206.     ch = getchar();
  207.     while (ch != EOF && ch != '\n')
  208.     {
  209.         switch (ch)
  210.         {
  211.             case '#' :
  212.             Pop(&S, e);
  213.             break;

  214.             case '@' :
  215.             ClearStack(&S);
  216.             break;

  217.             default : Push(&S, ch);
  218.         }
  219.         ch = getchar();
  220.     }

  221.     p = S.base;
  222.     while (p != S.top)
  223.     {
  224.         printf("%c", *p);
  225.         fputc(*p, fd);    
  226.         p++;
  227.     }

  228.     fputc('\n', fd);
  229.     ClearStack(&S);
  230. #if 0
  231.     printf("%c \n", *p);
  232.     fputc(*p, fd);    
  233.     fputc(*p, fd);    
  234.     fputc(*p, fd);    
  235.     fputc(*p, fd);    
  236. #endif
  237.     fclose(fd);
  238.     return 0;
  239. }

阅读(1800) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~