频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

简单的行编辑器

来源: 作者: 出处:巧巧读书 2006-05-06 进入讨论组

#include <stdlib.h>         /* For _MAX_PATH definition */
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>


#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10


struct SqStack
{
 char *base;
 char *top;
 int stacksize;
};

void InitStack(SqStack &S)
{
 S.base=(char*)malloc(STACK_INIT_SIZE *sizeof(char));
 if (!S.base)
  exit(1);
 S.top=S.base;
 S.stacksize=STACK_INIT_SIZE;
}

void push(SqStack &S,char e)
{
 if(S.top-S.base>=S.stacksize)
 {
  S.base=(char*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(char));
  if (!S.base)
   exit(1);
  S.top=S.base+S.stacksize;
  S.stacksize+=STACKINCREMENT;
 }
 *S.top++=e;
}

char pop(SqStack &S,char &e)
{

 if (S.top==S.base)
      return false;
 e=*--S.top;
 return e;
}

void ClearStack(SqStack &S)
{
 S.top=S.base;
}

void DestroyStack(SqStack &S)
{
 free(S.base);
 S.top=S.base;
}

bool StackEmpty(SqStack &S)
{
 if (S.top==S.base)
  return true;
 return false;
}

/*void PrintStack(SqStack &S)
{
 char e;
 while (!StackEmpty(S))
 {
  pop(S,e);
  printf("%d",e);
 }
}*/

void main()
{
 char ch,e;
 SqStack S,D;
 InitStack(S);
 InitStack(D);
 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);break;
   }
   ch=getchar();
  }
  while (!StackEmpty(S))
  {
   e=pop(S,e);
   push (D,e);
   
  }
  while (!StackEmpty(D))
  {
   e=pop(D,e);
   printf("%c",e);
  }
  ClearStack(S);
  if(ch!=EOF)
   ch=getchar();
 }
 DestroyStack(S);
}

URl收藏 http://www.qqread.com/data-structure/g929105102.html进入讨论组讨论。
收藏此文】【 】【打印】【关闭
较早的文章:排序及查找方法

较新的文章:用栈设置密码
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章