Data structure-line editing program

Source: Internet
Author: User

The function of a simple line editing program is to accept the program or data entered by the user from the terminal and deposit it into the user's data area. As the user enters on the terminal,
There is no guarantee that there will be no error, so it is obviously inappropriate to "store each character in the user area" in the line editing program.
It is a good practice to set up an input buffer to receive a line of characters entered by the user, and then row-by-line into the user data area. Allow user input error,
and correct them in time when they are found wrong.
For example, when a user discovers that a character that has just been built is wrong, it can be filled in with a backspace "#" to indicate that the previous character is invalid, if the current type is found in the line
If the error is more or more difficult to remedy, you can enter a backspace "@" to indicate that the characters in the current line are invalid.
For example, suppose you accept these two lines of characters from the terminal:
whil# #ilr #e (s#*s)
[Email protected] (*s=#++)
The following two lines are actually valid:
while (*s)
Putchar (*s++)

The program code is as follows:

1#include <stdio.h>2#include <stdlib.h>3 4 #defineStack_init_size 1005 #defineStackincrement 106 #defineOVERFLOW-27 #defineOK 18 #defineERROR 09 TentypedefCharSelemtype; One  A //Stack structure Body -typedefstruct { -Selemtype *Base; theSelemtype *top; -     intstacksize; - }sqstack; -  + intInitstack (Sqstack *s);//Initialize Stack - intPush (Sqstack *s,selemtype e);//into the stack + intPop (Sqstack *s,selemtype *e);//Delete elements from the stack A intDestorystack (Sqstack *s);//Destroying Stacks at voidLineEdit (Sqstack *s);//Line editing Program - intClearstack (Sqstack *s);//empty the elements in the stack -  - //Initialize Stack - intInitstack (Sqstack *S) { -S->Base= (Selemtype *)malloc(stack_init_size*sizeof(Selemtype)); in     if(! S->Base) { - exit (OVERFLOW); to     } +S->top = s->Base; -S->stacksize =stack_init_size; the  *     returnOK; $ }Panax Notoginseng  - //into the stack the intPush (Sqstack *S,selemtype e) { +     if((s->top-s->Base) >=S->stacksize) { AS->Base= (selemtype*)realloc(s->Base, (s->stacksize+stackincrement) *sizeof(Selemtype)); the         if(! S->Base) { + exit (OVERFLOW); -         } $S->top = s->Base+ s->stacksize; $S->stacksize + =stackincrement; -     } -*s->top++ =e; the     //printf ("%c\n", e); -     returnOK;Wuyi } the  - //Delete elements from the stack Wu intPop (Sqstack *s,selemtype *e) { -     if(S->top = = s->Base)returnERROR; About*e = *--s->top; $     returnOK; - } -  -  A //empty the elements in the stack + intClearstack (Sqstack *S) { theS->top = s->Base; -     returnOK; $ } the  the //Destroying Stacks the intDestorystack (Sqstack *S) { theS->top = s->Base; -      Free(s->Base); inS->top =NULL; theS->Base=NULL; the     returnOK; About } the  the //Line editing Program the voidLineEdit (Sqstack *S) { +Selemtype *p,ch,c; - Initstack (S); theCH =GetChar ();Bayi      while(ch! =EOF) { the          while(ch!=eof&&ch!='\ n') { the             Switch(CH) { -                  Case '#':P op (s,&c); Break; -                  Case '@': Clearstack (S); Break; the                 default:P Ush (S,CH); Break; the             } theCH =GetChar (); the         } -p = s->Base; the          while(p!=s->top) { theprintf"%c",*p); the++p;94         } the Clearstack (S); the         if(ch!=eof) ch =GetChar (); the     }98 } About  - intMain ()101 {102 Sqstack sq;103     intF;104LineEdit (&AMP;SQ);//make a row edit theDestorystack (&AMP;SQ);//destroying the Stack106     return 0;107}

Data structure-line editing program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.