Lenguaje tipo C - uso de línea para implementar el programa de edición de línea - algoritmo y estructura de datos

Algoritmo de programa de edición de línea de lenguaje tipo C y estructura de datos, totalmente alcanzable, completo y preciso, adjunto con imágenes en ejecución

Área de código


#include<stdio.h>
#include<string.h>
#define MAXSIZE 100
typedef char SElemType;

typedef struct 
{
	SElemType  Elem[MAXSIZE];
	int top;  //数值下标
}SqStack;

enum Status{ERROR,OK};

//初始化栈,为空,栈顶指针置为-1
Status InitStack(SqStack &S)
{
	S.top=-1;
	return OK;
}

//判空操作,
Status StackEmpty(SqStack S)
{
	if(S.top==-1)
		return OK;
	else 
	return ERROR;
}

//入栈操作
Status Push(SqStack &S,SElemType e)
{
	if(S.top==MAXSIZE-1)
	{
       printf("stack is full\n");
	   return  ERROR;
	}
	else
	{
		S.top++;
		S.Elem[S.top]=e;
		return OK;
	}

}

//出栈
Status Pop(SqStack &S,SElemType &e)
{
	if(S.top==-1)
		printf("the stack is empty\n");
	else 
	{
		e=S.Elem[S.top];
		S.top--;
	}
	return OK;
}

//清空栈
Status ClearStack(SqStack &S)
{
	S.top=-1;
	return OK;
}

//打印栈元素
Status show(SqStack S)
{
	int i;
	for(i=0;i<=S.top;i++)
	{
		printf("%c",S.Elem[i]);
	}
	printf("\n");
	return OK;
}

//行编辑程序
Status LineEdit(SqStack S)  //利用字符栈S,从终端接收一行并传送至调用过程的数据区
{
 SElemType e;
 InitStack(S); //构造空栈
 int j=0;
 
 while((e=getchar())!='\n')  //EOF为全文结束符 e=getchar(); //从终端接收第一个字符
 {
	   switch(e)
	   {
	   case '#':if(!StackEmpty(S)) Pop(S,e);break;  //栈非空时退栈
	   case '@':ClearStack(S);break;  //重置S为空
	   default :Push(S,e);break;  //有效字符进栈
	   }
	   //e=getchar();  //从终端接收下一个字符
   //将从栈底到栈顶的栈内字符传送到调用过程的数据区
 }
    show(S);
   ClearStack(S);  //重置S为空
 return OK;
}

int main()
{
	SqStack S;
	InitStack(S);
	LineEdit(S);

	return 0;
}

Inserte la descripción de la imagen aquí

57 artículos originales publicados · Me gustaron 54 · Visitas 2348

Supongo que te gusta

Origin blog.csdn.net/September_C/article/details/105083769
Recomendado
Clasificación