1 //行编辑程序 2 #include<stdio.h> 3 #include <stdlib.h> 4 #define OK 1 5 #define ERROR -1 6 #define OVERFLOW -1 7 8 #define STACK_INIT_SIZE 100 9 #define STACKADD 10 10 11 typedef int Status; 12 typedef char SElemtype; 13 14 typedef struct stack 15 { 16 SElemtype *top; 17 SElemtype *base; 18 int stacksize; 19 }Stack; 20 Status Init_Stack(Stack *s);//栈的初始化 21 Status Stack_push(Stack *s,SElemtype e);//加数值e压进栈 22 Status Stack_pop(Stack *s,SElemtype e);//数值e出栈 23 Status Line_edit(Stack *s);//用与行编辑 24 Status Stack_Clear(Stack *s);//将栈置为空 25 Status Stack_destory(Stack *s); 26 int main() 27 { 28 Stack *s; 29 Line_edit(s); 30 return 0; 31 } 32 Status Line_edit(Stack *s) 33 { 34 Init_Stack(s); 35 char c; 36 printf("enter a char \n"); 37 c=getchar(); 38 while(c!=EOF) 39 { 40 while(c!=EOF&&c!='\n') 41 { 42 switch(c) 43 { 44 case '#':Stack_pop(s,c);break; 45 case '@':Stack_Clear(s);//@表示是退行符 46 47 default:Stack_push(s,c);break; 48 } 49 50 51 c=getchar(); 52 } 53 Stack_Clear(s); 54 if(c!=EOF) 55 c=getchar(); 56 } 57 58 Stack_destory(s); 59 60 return OK; 61 62 } 63 Status Stack_pop(Stack *s,SElemtype e) 64 { 65 if(s->top==s->base) 66 { 67 return ERROR; 68 } 69 e=*s->top; 70 s->top--; 71 72 73 74 return OK; 75 } 76 Status Stack_push(Stack *s,SElemtype e) 77 { 78 if(s->top-s->base>=s->stacksize) 79 { 80 s->base=(SElemtype *)realloc(s->base,(s->stacksize+STACKADD)*sizeof(SElemtype)); 81 if(!s->base) 82 exit(OVERFLOW); 83 s->top=s->base+s->stacksize; 84 s->stacksize+=STACKADD; 85 } 86 s->top++; 87 *s->top=e; 88 return OK; 89 } 90 Status Init_Stack(Stack *s) 91 { 92 s->base=(SElemtype *)malloc(sizeof(SElemtype)); 93 if(!s->base) 94 return OVERFLOW; 95 s->base=s->top; 96 s->stacksize=STACK_INIT_SIZE; 97 return OK; 98 } 99 Status Stack_Clear(Stack *s) 100 { 101 s->top=s->base; 102 s->stacksize=0; 103 104 return OK; 105 } 106 Status Stack_destory(Stack *s) 107 { 108 free(s->base); 109 free(s); 110 }