資料結構-行編輯程式

來源:互聯網
上載者:User

標籤:

一個簡單的行編輯程式的功能是:接受使用者從終端輸入的程式或資料,並存入使用者的資料區。由於使用者在終端上進行輸入時,
不能保證不出差錯,因此,若在行編輯程式中“每接受一個字元即存入使用者區”的做法顯然是不恰當的。
較好的做法是,設立一個輸入緩衝區,用以接收使用者輸入的一行字元,然後逐行存入使用者資料區。允許使用者輸入出差錯,
並在發現有誤時及時改正。
例如:當使用者發現剛剛建入的一個字元是錯的時,可補進一個退格符“#”,以表示前一個字元無效;如果發現當前鍵入的行內
差錯較多或難以補救,則可以輸入一個退格符“@”,以表示當前行中的字元均無效。
例如,假設從終端接受了這兩行字元:
whil##ilr#e(s#*s)
    [email protected](*s=#++)
則實際有效是下列兩行:
while(*s)
    putchar(*s++)

 

程式碼如下:

  1 #include <stdio.h>  2 #include <stdlib.h>  3   4 #define STACK_INIT_SIZE 100  5 #define STACKINCREMENT 10  6 #define OVERFLOW -2  7 #define OK 1  8 #define ERROR 0  9  10 typedef char SElemType; 11  12 //棧結構體 13 typedef struct { 14     SElemType *base; 15     SElemType *top; 16     int stacksize; 17 }SqStack; 18  19 int InitStack(SqStack *S);//初始化棧 20 int Push(SqStack *S,SElemType e);//入棧 21 int Pop(SqStack *S,SElemType *e);//刪除棧中的元素 22 int DestoryStack(SqStack *S);//銷毀棧 23 void LineEdit(SqStack *S);//行編輯程式 24 int ClearStack(SqStack *S);//清空棧中的元素 25  26 //初始化棧 27 int InitStack(SqStack *S) { 28     S->base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); 29     if(!S->base) { 30         exit(OVERFLOW); 31     } 32     S->top = S->base; 33     S->stacksize = STACK_INIT_SIZE; 34  35     return OK; 36 } 37  38 //入棧 39 int Push(SqStack *S,SElemType e) { 40     if((S->top-S->base)>=S->stacksize) { 41         S->base = (SElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); 42         if(!S->base) { 43             exit(OVERFLOW); 44         } 45         S->top = S->base + S->stacksize; 46         S->stacksize += STACKINCREMENT; 47     } 48     *S->top++ = e; 49     //printf("%c\n",e); 50     return OK; 51 } 52  53 //刪除棧中的元素 54 int Pop(SqStack *S,SElemType *e) { 55     if(S->top  == S->base) return ERROR; 56     *e = *--S->top; 57     return OK; 58 } 59  60  61 //清空棧中的元素 62 int ClearStack(SqStack *S) { 63     S->top = S->base; 64     return OK; 65 } 66  67 //銷毀棧 68 int DestoryStack(SqStack *S) { 69     S->top = S->base; 70     free(S->base); 71     S->top = NULL; 72     S->base = NULL; 73     return OK; 74 } 75  76 //行編輯程式 77 void LineEdit(SqStack *S) { 78     SElemType *p,ch,c; 79     InitStack(S); 80     ch = getchar(); 81     while(ch != EOF) { 82         while(ch!=EOF&&ch!=‘\n‘) { 83             switch(ch) { 84                 case ‘#‘:Pop(S,&c);break; 85                 case ‘@‘:ClearStack(S);break; 86                 default:Push(S,ch);break; 87             } 88             ch = getchar(); 89         } 90         p = S->base; 91         while(p!=S->top) { 92             printf("%c",*p); 93             ++p; 94         } 95         ClearStack(S); 96         if(ch!=EOF) ch = getchar(); 97     } 98 } 99 100 int main()101 {102     SqStack sq;103     int f;104     LineEdit(&sq);//進行括行編輯105     DestoryStack(&sq);//將棧銷毀106     return 0;107 }

 

資料結構-行編輯程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.