I have been reading this book recently and found that the exercises in it are really too classic. Many exercises are simple to read, but it takes a lot of money to do well.
[Exercise 1.10]
Here we will first give a rule based on exercise 1.9.Program.
/* This program is used to explicitly output input tabs and escape characters, and output the backslash in the form "\" */ # Include <Stdio. h> Int Main ( Int Argc, Char * Argv []) { Short Int Input; While (EOF! = (Input = Getchar ())){ Switch (Input ){ Case ' \ T ' : Putchar ( ' \\ ' ); Putchar ( ' T ' ); Break ; Case ' \ B ' : Putchar ( ' \\ ' ); Putchar ( ' B ' ); Break ; Case ' \\ ' : Putchar ( ' \\ ' ); Putchar ( ' \\ ' ); Break ; Default : Putchar (input );}} Return 0 ;}
The program execution is as follows:
Here is a question: how to enter the '\ B' character on the terminal. We can find that the above program cannot be used to enter the escape character.
The reason is that the getchar () function reads characters from the input buffer. In the input buffer mechanism, the backspace key outputs the characters at the top of the input buffer stack, so it has an input buffer mechanism.
Library functions cannot implement valid input of the '\ B' character.
Solution: Do not input the buffer characters. The getch () function in the library function can achieve this goal.
However, there is a new problem, that is, the getch () function cannot be ECHO, and the getch () function cannot be combined by pressing CTRL + Z like the getchar () function;
Here we need to handle the problem of input echo.
Key Point: It should be noted that the getch function is not a function in the ANSI standard library. What is defined in the standard library function is
GETC () function. Some symbols in the standard library are defined macros and have functions of the same name.
For example, getchar () has both macros and functions.
Digress:
Here, when we input characters, We output them during line breaks. Sometimes we may need to input them until a specific character is entered. This requires
Modified. What should I do?
I am using the linked list to achieve persistent storage, and then the entire input.
Create, insert, delete, search, and display a linked list.
/* This program is used to test the basic operations of the linked list. */ # Include <Stdio. h> # Include <Stdlib. h> Typedef Struct Node { Char Chinput; Struct Node * Next;} node; Void Createlist (node * Head ); Void Echolist (node * Head ); Void Insertnode (node * head, Char Chposition, Char Chelement ); Void Deletenode (node * head, Char Chelement); Node * Searchnode (node * head, Char Chelement ); Int Main ( Int Argc, Char * Argv [],Char * Env []) {Node * Head; Node * Findnode; Char Chinput; head = (Node *) malloc ( Sizeof (Node); createlist (head); echolist (head); puts ( " Enter the charecter you want to search: " ); Chinput = Getchar (); findnode =Searchnode (Head, chinput ); If (Null = Findnode) puts ( " Can not find the element " ); Else {Putchar (findnode -> Chinput); putchar ( ' \ N ' ); Deletenode (Head, chinput); echolist (head);} getchar (); Return 0 ;} Void Createlist (node * Head) {Node * End; Node * Temp; Short Int Chinput; End = Head; While (Chinput = getchar ())! = EOF) {end -> Chinput = Chinput; temp = (Node *) malloc ( Sizeof (Node); temp -> Next = Null; End -> Next = Temp; End = Temp ;}} Void Echolist (node * Head) {Node * Temp; temp = Head; While (Temp-> next! = Null) {putchar (temp -> Chinput); temp = Temp-> Next;} Node * Searchnode (node * head, Char Chelement) {Node * Temp; temp = Head; // Handling only one node If (Temp-> next = Null ){ If (Temp-> chinput = Chelement) Return Temp; Else Return NULL ;} While (Temp-> next! = Null ){ If (Chelement = temp-> Chinput) Break ; Else Temp = Temp-> Next ;} Return Temp ;} Void Insertnode (node * head, Char Chposition, Char Chelement) {Node * Temp; Node * Newnode; temp = Searchnode (Head, chposition); newnode = (Node *) malloc ( Sizeof (Node); newnode -> Chinput = Chelement; newnode -> Next = temp-> Next; temp -> Next =Newnode ;} Void Deletenode (node * head, Char Chelement) {Node * Temp; Node * Denode; temp = Head; denode = Head; If (Head-> chinput = Chelement) {head = Head-> Next; temp -> Next = NULL; free (temp );} Else { While (Denode-> next! = Null ){ If (Denode-> next-> chinput = Chelement) {temp = Denode-> Next; denode -> Next = denode-> next-> Next; temp -> Next = NULL; free (temp);} denode = Denode-> Next ;}}}
If you want to implement multi-line input and then output the data in a centralized manner, you only need to modify the echo function. The idea is also very simple. When you need to output a specified character
It can be processed separately, otherwise it can be output in sequence.
Void Echolist (node * Head) {Node * Temp; temp = Head; While (Temp-> next! = Null ){ Switch (Temp-> Chinput ){ Case ' \ T ' : Putchar ( ' \\ ' ); Putchar ( ' T ' ); Break ; Case ' \ B ' : Putchar ( ' \\ ' ); Putchar ( ' B ' ); Break ; Case ' \\ ' : Putchar ( ' \\ ' ); Putchar ( ' \\ ' ); Break ; Default : Putchar (input);} temp = Temp-> Next ;}}
So much to say this time.