Application line editing program for Stacks

Source: Internet
Author: User

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

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.