Description
Use the stack to write a simple line editing program: Accept the user input from the terminal program or data, during the input process, allow users to input errors, and in the case of errors can be corrected in time. For example, when a user discovers that a character that you just typed is wrong, you can fill in a backspace "#" to indicate that the previous character is invalid, or if you find that there are more or more errors in the currently typed line, you can type a "@" to indicate that the characters in the current line are invalid. For example: Assume that two lines of characters are accepted from the terminal: whli# #ilr #e (s#*s) [email protected] (*s=#++); The following two lines are actually valid: while (*s) Putchar (*s++); This topic gives some functions that require the completion of the line editing function and complete the entire program.
Idea: To determine whether the character is ' # ', is to delete the top element of the stack, otherwise the character will be pressed into the stack, whether the character is ' @ ', is the empty stack, or the character pressed into the stack;
Code Demo:
voidLineEdit () {sqstack s; CharCh,c; intN,i; Initstack (s); scanf ("%d",&N); CH=GetChar (); for(i=1; i<=n;i++) {ch=GetChar (); while(ch!='\ n') { Switch(CH) { Case '#':P op (s,c); Break;//fallback stack only if stack is not empty Case '@': Clearstack (s); Break;//Reset s to empty stack default:P Ush (S,CH);//valid word Fujin} CH=getchar ();//receive the next character from a terminal} stacktraverse (S,visit); //The output of the stack character from the bottom of the stack to the top of the stackClearstack (s);//Reset s to empty stack} destroystack (s); }
Application line editing program for Stacks