数据结构 study 9: 行编辑程序
<数据结构>严蔚敏 第一版 page59
题目解析
(1) 通过命令行 读取 用户输入的多行字符串。
linux系统中,用户输入 ctrl+d后,getchar 会返回 EOF。
(2)对用户 输入的 多行数据 ,进行逐行分析。
逐行分析 解析完毕 之后,写入 文件中。
(3)逐行分析的流程,用到了栈。
对每一行的数据 ,逐个字符进行分析
逐个字符入栈,
遇到字符#,那么就把前一个入栈的字符,从栈中,弹出来。
继续分析下一个字符。
到达行位的时候,
把栈中的所有的字符,写入文件中去。
如果遇到@ 字符,那么放弃 这一行,进行下一行的处理和分析。
先实现一个没有栈的,把基本的写文件等操作实现。
第二部再把和栈相关的操作补上。
基本程序
/* c1.h (程序名) */
#include<string.h>
#include<ctype.h>
#include<malloc.h> /* malloc()等 */
#include<limits.h> /* INT_MAX等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include <sys/io.h> /* eof() */
#include<math.h> /* floor(),ceil(),abs() */
#include<pthread.h> /* exit() */
/* 函数结果状态代码 */
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
/* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */
typedef char SElemType;
/* c3-1.h 栈的顺序存储表示 */
#define STACK_INIT_SIZE 10 /* 存储空间初始分配量 */
#define STACKINCREMENT 2 /* 存储空间分配增量 */
FILE *fp;
#if 0
Status copy(SElemType c)
{ /* 将字符c送至fp所指的文件中 */
fputc(c,fp);
return OK;
}
#endif
void Amain()
{
fp=fopen("ED.DAT","w"); /* 在当前目录下建立ED.DAT文件,用于写数据, */
if(fp) /* 如已有同名文件则先删除原文件 */
{
//LineEdit();
fclose(fp); /* 关闭fp所指的文件 */
}
else
printf("建立文件失败!\n");
}
int main()
{
int ch ;
int tmp_ch ;
int i ;
char array[200];
char array_index = 0 ;
fp = fopen("ED.DAT" , "w");
if(!fp){
printf("建立文件失败!\n");
return -1 ;
}
printf("请输入多行字符串,ctrl+d 结束输入\n");
ch =getchar();
while(ch !=EOF){
array_index =0 ;
array[array_index] = ch;
array_index++ ;
/*行内字符处理*/
while(ch!=EOF && ch != '\n'){
ch = getchar();
if(ch == '#'){
array_index--;
array[array_index] = 0;
}
else if(ch == '@'){
while(getchar()!='\n');
break ;
}else{
array[array_index] = ch;
array_index++ ;
}
}
/*行尾 -- 整行处理*/
if(ch!='@'){
for(i=0; i < array_index ; i++){
tmp_ch = array[i];
fputc(tmp_ch,fp);
}
}
ch = getchar();
}
fclose(fp);
return 0 ;
}
使用栈的方法
/* c1.h (程序名) */
#include<string.h>
#include<ctype.h>
#include<malloc.h> /* malloc()等 */
#include<limits.h> /* INT_MAX等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include <sys/io.h> /* eof() */
#include<math.h> /* floor(),ceil(),abs() */
#include<pthread.h> /* exit() */
/* 函数结果状态代码 */
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
/* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */
/* c3-1.h 栈的顺序存储表示 */
#define STACK_INIT_SIZE 20 /* 存储空间初始分配量 */
#define STACKINCREMENT 2 /* 存储空间分配增量 */
FILE *fp;
typedef char ElemType ;
typedef struct sqStack
{
ElemType *top ;
ElemType *base ;
int stack_size ;
}sqStack ;
Status init_stack(sqStack *S)
{
S->base = (ElemType *)malloc(sizeof(ElemType)*STACK_INIT_SIZE);
S->top = S->base ;
S->stack_size = STACK_INIT_SIZE ;
return OK;
}
Status push_stack(sqStack *S, ElemType e)
{
if(S->top - S->base < S->stack_size){
*(S->top++) = e ;
return OK ;
}
return ERROR;
}
Status pop_stack(sqStack *S, ElemType *e)
{
if(!empty_stack(S)){
*e = *(--(S->top));
return OK;
}
return ERROR;
}
Boolean empty_stack(sqStack *S)
{
if(S->top == S->base){
return TRUE;
}
return FALSE ;
}
Status clear_stack(sqStack *S)
{
(*S).top=(*S).base;
return OK;
}
Status destroy_stack(sqStack *S)
{
free((*S).base);
(*S).base=NULL;
(*S).top=NULL;
(*S).stack_size=0;
return OK;
}
void visit(ElemType e)
{
fputc(e,fp);
}
Status traverse_stack(sqStack *S, void (*visit)(ElemType))
{
ElemType *pt ;
pt = S->base ;
while(pt < S->top ){
visit(*(pt++));
}
}
int main()
{
sqStack S;
int ch ;
int flag = 0 ;
init_stack(&S);
fp = fopen("EDW.DAT", "w");
if(!fp){
printf("打开文件错误\n");
return ERROR ;
}
printf("请输入 多行字符串,ctrl+d 结束输入\n");
ch = getchar();
while(ch!=EOF){
if(ch =='\n' ){
push_stack(&S, ch);
traverse_stack(&S,visit);
clear_stack(&S);
}
else if(ch == '#'){
if(!empty_stack(&S)){
pop_stack(&S, &ch);
}
}else if(ch == '@'){
while(getchar()!='\n');
clear_stack(&S);
}else{
push_stack(&S, ch);
}
ch = getchar();
}
destroy_stack(&S);
fclose(fp);
return 0 ;
}