行编辑程序 LineEdit

/*************************************
 *							      	 *
 * 文件夹: ▲03 栈和队列\03 LineEdit *
 * 							      	 *
 * 文件名: LineEdit.h             	 *
 * 							      	 *
 * 内  容: 行编辑程序相关操作列表  	 *
 *                                	 *
 *************************************/

#ifndef LINEEDIT_H
#define LINEEDIT_H

#include <stdio.h> 
#include "../01 SequenceStack/SequenceStack.c" 		//**▲03 栈和队列**//

/* 行编辑程序函数列表 */
void LineEdit(char Buffer[]);
/*━━━━━━━━━━━━━┓
┃(01)算法3.2:行编辑程序。 ┃
┗━━━━━━━━━━━━━*/

void Print(SElemType_Sq e);
/*━━━━━━━━┓
┃(02)打印元素e。 ┃
┗━━━━━━━━*/

#endif
/*************************************
 *					     			 *
 * 文件夹: ▲03 栈和队列\03 LineEdit *
 * 					     			 *
 * 文件名: LineEdit.c    			 *
 * 				         			 *
 * 算  法: 3.2           			 *
 *                       			 *
 *************************************/

#ifndef LINEEDIT_C
#define LINEEDIT_C

#include "LineEdit.h"				//**▲03 栈和队列**//

/*════╗
║ 算法3.2║ 
╚════*/
/* 与严蔚敏课本所述算法略有差别,但算法思想一致 */
void LineEdit(char Buffer[])
{
	SqStack S;						//接收输入的字符
	SElemType_Sq e;
	int i;
	char ch;
	
	InitStack_Sq(&S);
	
	i = 0;
	ch = Buffer[i++];
	while(ch!='\0')
	{
		while(ch!='\0' && ch!='\n')
		{
			switch(ch)
			{
				case '#': Pop_Sq(&S, &e);
					break;
				case '@': ClearStack_Sq(&S);
					break;
				default : Push_Sq(&S, ch);
			}
			ch = Buffer[i++];
		}
		
		if(ch=='\n')
		{
			Push_Sq(&S, ch);			
			StackTraverse_Sq(S, Print);
			ClearStack_Sq(&S);
			ch = Buffer[i++];
		}
	}
	
	if(ch=='\0')
	{
		StackTraverse_Sq(S, Print);
		DestroyStack_Sq(&S);
	}
}

void Print(SElemType_Sq e)
{
	printf("%c", e);
}

#endif
/*************************************
 *						             *
 * 文件夹: ▲03 栈和队列\03 LineEdit *
 * 						             *
 * 内  容: 行编辑程序相关函数测试    *
 *                                   *
 *************************************/

#include "LineEdit.c"					//**▲03 栈和队列**//					

int main(int argc, char *argv[])
{
	char *buf = "whli##ilr#e(s#*s)\noutcha@  putchar(*s=#++);"; //需要录入的内容 
	
	printf("作为示范,用户输入的文本内容为:\n");
	printf("%s\n", buf);
	printf("\n");
	printf("进入行编辑程序...\n"); 
	printf("特殊符号:“#” 代表删除上一元素,“@”代表删除当前输入行,\n");
	printf("          “\\n”代表确认此行无误,“\\0”代表输入结束。\n");
	printf("最终存储的内容为:\n");
	LineEdit(buf);
	printf("\n\n");

	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42815188/article/details/87824700
今日推荐