Program day (1)

xiaoxiao2021-03-06  37

Note: These exercises come from the C classic tutorial "The C Programming Language, 2nd". To do these exercises is to cultivate a programming idea (efficiency, reuse ...), not a simple implementation. EX: EXERCISE 1-12 WRITE A Program That Prints ITS INPUT One Word Per line.date: 2005.1.6solution 1: 1. Judging each of the characters, if the input character is '' or '/ t', will this character Convert to '/ n'; 2. The combined continuous '/ n' is one '/ n'; 3. Output. Analysis: This method is clear, and the problem is that a container is needed to be entered, and the size of the container can be determined in Run-TIME. The solution can be used in STL, and the size of these containers can be dynamically changed. Solution 2: 1. Defines an identifier to indicate whether '' or '/ t' appears continuously. 2. Define a char variable to indicate the character of the previous output. 2.IF (Input Character is not '' and '/ t') output this character; IF = 0; ELSE IF (IF == 0 && Previous characters! = '/ N') Output Character '/ n' Identifier = 1; Else Do Nothing; Analysis: This method logic is more complicated, but there is no other container, the efficiency is higher than the first. Below is the code 2: int Main (int Argc, char * argv []) {int count_char = 0; int flag = 0; int C; int c_before; for (count_char = 0; (c = getchar ())! = EOF; count_char ) {if (c! = '' && c! = '/ T') {PUTCHAR (C); c_before = C; FLAG = 0;} else} ((! Flag) && (c_before! = ' / n ')) {FLAG = 1; Putchar (' / n '); c_before =' / n ';} else {// do nothing.}} return 0;} This code is under Windows2000, DEV-C 5.0b Compiled.

转载请注明原文地址:https://www.9cbs.com/read-72400.html

New Post(0)