1.1 "Hello World" program does not say, codeblocks directly generated.
1.2 Title: Read a few lines of input from the standard input. Each line of input is printed to the standard output, preceded by a line number. There is no length limit for the input lines that the program can handle.
/*my program*//*int main () {int ch; int line=1; while ((Ch=getchar ())!=eof) {printf ('%d ', line); if (ch!= ' \ n ') printf ("%c", ch); else {printf ("\ n"); line++; }} return exit_success;}*//*Answer resolution*/#include<stdio.h>#include<stdlib.h>intMain () {intCh,line=0, at_beginning=1;//even the books are initialized and assigned separately, presumably to take care of beginners. while((Ch=getchar ())! =EOF) { if(at_beginning==1) {at_beginning=0; Line+=1; printf ("%d", line); } putchar (CH); if(ch=='\ n') at_beginning=1; } returnexit_success;}/*Read the answer really understand their differences, set at_beginning did not think, feel oneself used to scanf and Printf,getchar and Putchar feel instead forget. */
This is a wrong question. As for why the GetChar function, mainly to let the input line has no length limit, one character read into.
1.3 Title: Read some characters from the standard input and write them to the standard output. He should also calculate the checksum value and write it at the back of the character. Checksum is calculated with a variable of type signed char, initially 1. When each character is read from the standard input, he is added to the checksum. If the checksum variable generates an overflow, the overflow is ignored. When all the characters are written after the program has a decimal integer format to print out the value of checksum, he may be negative, note that you want to hit a newline character after it.
#include <stdio.h> #include <stdlib.h> int main () {signed char checksum=-1 ; int ch; while ((Ch=getchar ())!=eof) {Putchar (CH) ; Checksum +=CH; } printf ( " %d\n " ,checksum); return exit_success;}
There is no standard answer in the book, but I run the result is correct, because the title required to run in the file, so I changed to the shell window, understanding is good. No need to dwell on this detail. Most importantly, I found that "C and pointers" rarely use char to define a character variable, such as Char ch, which I see in the book: EOF is an integral type, so it has more bits than character, and declaring Ch as shaping prevents characters read from the input from being interpreted as EOF, And Ch is large enough to accommodate EOF, so it is defined as int.
At the same time I always thought that only the char type, this time know also has unsigned char (0~255) and signed char ( -128~127), as to our char type default is signed or unsigned, according to the IDE and the capital is different. My codeblocks is signed. In fact, it's better to understand that either character type is 8 bytes, but the latter is the symbol bit, so the range is only 127. It seems that this kind of detail will always care about ...
1.4
Line-by-row reads the input line until the end of the file is reached. Calculate the length of each line of input lines, and then hit the longest line, assuming the input line is no more than 1000 characters.
#include <stdio.h>#include<stdlib.h>#defineN 1000//Enter the maximum length of the row#defineM 20//Maximum number of rows enteredintMain () {CharB[m][m]; inta[m]={0},i=0, j,k,max,flag=1;/*CH is used to read characters, a This array is used to store the length of each line I used to denote the line number*/ for(i=0; i<m;i++) { for(j=0; j<n;j++) {B[i][j]=GetChar (); A[i]++; if(b[i][j]==EOF) {Flag=0; Break; } if(b[i][j]=='\ n') Break; } if(!flag) Break; } Max=0; for(k=0; k<i;k++) { if(a[max]<A[k]) Max=K; } J=0; Do{printf ("%c", B[max][j]); J++; } while(b[max][j]!='\ n'); returnexit_success;}
I have to say, this problem I think write very bad, but no book answer, my program can also AC, but as obsessive-compulsive disorder I always feel not beautiful, give me a similar use goto statement feeling. Remember that the number of goto statements is inversely proportional to the person's ability to program. First of all, I think the book should still want to use the definition of int ch, but I finally adopted a familiar two-dimensional array. The thing that makes me feel bad, is because of the problem of Ctrlz (seemingly and the keyboard input output has its problem). Because if you want to end the input by Ctrlz in a row, it doesn't work to enter EOF at the end of a line with other characters, and the program input does not end. And if you do not enter Ctrlz on the next line but other characters, the IDE will output both lines in some way as the same line. So there's really something wrong with that. Of course, we have to end the input when the general input Ctrlz no longer input other characters, so in the actual use should not have these problems. The topic is also written out, if someone can write better, hope to get guidance. Because I know that my own is not good. There are two exercises to write tomorrow.
"C and Pointers" study notes (2)