81. Call function, complete the establishment of a one-way dynamic linked list, output the values of each node.
1#include <stdio.h>2#include <stdlib.h>3 4typedefstructLST5 {6 intnum;7 structLST *Next;8 }lst;9 TenLST *mycreat () One { A intm; -LST *head, *p, *Q; - theHead = (LST *)malloc(sizeof(LST)); -Q =head; -printf"to create a linked list, enter a value: \ n"); -printf"Input Data:"); +scanf"%d", &m); - while(M! =-1) + { Ap = (LST *)malloc(sizeof(LST)); atQ->next =p; -P->num =m; -Q =p; -printf"Input Data:"); -scanf"%d", &m); - } inQ->next =NULL; - returnhead; to } + - voidMyprint (LST *head) the { *LST *p; $p = head->Next;Panax Notoginseng if(p = =NULL) -printf"linked list is empty table!\n"); the Else + Do A { theprintf"%5d", p->num); +p = p->Next; -} while(P! =NULL); $printf"\ n"); $ } - - voidMain (void) the { -LST *head;WuyiHead =mycreat (); theprintf"The new linked list is:"); - Myprint (head); Wu}
View Code
Results:
To create a linked list, enter a value:
Input data:101
Input data:103
Input data:105
Input Data:-1
New Linked list: 101 103 105
Mark:
"typedef struct LST LST;" Represents the use of LST instead of a struct LST;
82. Write the program string "Let's study the C language." Output to a text file.
1#include <stdio.h>2 3 voidMain (void)4 {5 Chara[ the] ="Let ' s study the C language.";6FILE *FP;7 8fp = fopen ("a.txt","W");9fprintf (FP,"%s", a);Ten fclose (FP); One}
View Code
83. Read the contents of the above file "A.txt" and output it to the screen.
1#include <stdio.h>2 3 voidMain (void)4 {5 Chara[ the];6FILE *FP;7 8fp = fopen ("a.txt","W");9FSCANF (FP,"%s", a);Ten puts (a); One fclose (FP); A}
View Code
Results:
Let ' s
Explain:
Statement "FSCANF (FP,"%s ", a);" The function is to read a string from the file "A.txt" in the file pointer FP, because when the string is read, it encounters a space, the jump character, the carriage return is considered the end of the string, so the output is "let's". If you read the string with the Fgets function, the output will be "Let's study the C." Language. ".
Mark:
"FILE *FP;" The function is to define the file pointer fp, all files opened in the C program must be pointed to by the file pointer before the read operation.
"FP = fopen (" A.txt "," w ");" The function is to open a text file named "A.txt" in "Write" mode, and associate the file with the file pointer fp.
"fprintf (FP,"%s ", a);" The function is to put the string in a "Let ' s study the C language." Output to File "A.txt" referred to in the FP file.
Statement "fclose (FP);" is to close the file "A.txt" of the FP, when the file is disconnected from the file pointer fp.
84. Enter scores of students from the keyboard, end with 1, call the fprintf function, and write the student's score to the file D:\cwz\b.txt in the format.
1#include <stdio.h>2#include <stdlib.h>3 4 voidMain (void)5 {6 intA;7FILE *FP;8 9fp = fopen ("D:\\cwz\\b.txt","W");Ten if(fp = =NULL) One { Aprintf"Can ' t open file!\n"); -Exit0); - } thescanf"%d", &a); - while(A! =-1) - { -fprintf (FP,"%4d", a); +scanf"%d", &a); - } + fclose (FP); A}
View Code
Results:
60 70 80 90 100 80 60-1
After the program runs, no information is displayed on the screen, but the B.txt file is found in the D-disk Cwz directory, and the contents of the file are:
60 70 80 90 100 80 60
If there is no CWZ directory under the D drive, the file fails to open, and a message is displayed on the screen: "Can ' t open file!" and then the execution of the program ends.
85. Call the FSCANF function to read the student scores in the above file D:\cwz\b.txt in the format and output the highest score on the interrupt screen.
1#include <stdio.h>2#include <stdlib.h>3 4 voidMain (void)5 {6 intA, max =0;7FILE *FP;8 9fp = fopen ("D:\\cwz\\b.txt","R");Ten if(fp = =NULL) One { Aprintf"Can ' t open file!\n"); -Exit0); - } the while(feof (fp) = =0) - { -FSCANF (FP,"%d", &a); -printf"%4d", a); + if(Max <a) -Max =A; + } Aprintf"\ n"); atprintf"max =%d\n", max); - fclose (FP); -}
View Code
Results:
60 70 80 90 100 80 60
max = 100
The function feof is used to determine whether the file ends, and when the data is read to the end of the file, the feof (FP) value is 1, otherwise the value of feof (FP) is 0.
C Language Growth Learning topic (17)