Current location - Training Enrollment Network - Mathematics courses - Read the data (structured data) in the text file into the linked list with C language. How to write the code?
Read the data (structured data) in the text file into the linked list with C language. How to write the code?
This is a small program that I wrote when I first started learning linked lists. It doesn't look perfect, but there should be no problem. You can refer to it It is too late to write now.

# Contains? & ltstdio.h & gt

# Contains? & ltio.h & gt

# Contains? & ltconio.h & gt

# Contains? & ltstdlib.h & gt

# Contains? & ltstring.h & gt

# Definition? Max. 200

struct? student

{

Charles? No [10]; //? Student number

Charles? Name [50]; ? //? (full name)

Floating? Score [3]; //? mark

Floating? Mathematics;

Floating? eng

Floating? phy

};

struct? data

{

int? Count; ? //? The number of students in the current student array

struct? Student? Stu [Max]; ? //? Student array

} dd

//? Show main menu

Invalid? Menu ()

{

System ("CLS"); //Clear the screen?

printf(" \ n \ n \ n ");

printf(" \ t \ t \ t * * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");

printf(" \ t \ t \ t \ n ");

printf("\t\t\t? Student achievement management system? \ n ");

printf("\t\t\t? \ n ");

printf("\t\t\t[ 1]? View all student information \ n ");

printf("\t\t\t[2]? Enter student record \ n ");

printf("\t\t\t[3]? Delete student records? \ n ");

printf("\t\t\t[4]? Modify student record \ n ");

printf("\t\t\t[5]? Inquire about students' grades (student numbers)? \ n ");

printf("\t\t\t[6]? Query to a student's grade (name) \ n ");

printf("\t\t\t[7]? Check the grade ranking (student number) \ n ");

printf("\t\t\t[0]? Exit \ n ");

printf("\t\t\t? \ n ");

}

//? Wait for the user to press Enter and return to the main menu.

Invalid? Recipient menu ()

{

Charles? c 1,C2;

Printf ("\ n \ n \ nPress Enter to return to the main menu ...");

scanf("%c%c ",& ampc 1; C2); //The first character absorbs the last confirmation enter key?

menu();

}

//Statistical information

Invalid? let

{

int? A=0,B=0,C=0,D=0,E=0,s;

int? Me;

for(I = 0; I & ltdd.counti++)

{

s=(int)(dd.stu[i]。 Mathematics/10);

switch

{

Case? 10:

Case? 9:

a++;

Break;

Case? 8:

b++;

Break;

Case? 7:

c++;

Break;

Case? 6:

d++;

Break;

Default? :

e++;

Break;

}

}

Printf ("population: %d\n ",DD . count); ?

Printf ("excellent" number: %d\n ",a);

Printf ("number of people with" good ":%d\n", b);

Printf ("Number of winners: %d\n", c);

Printf ("Pass number: %d\n", d);

Printf ("number of failures: %d\n", e);

}

//? View all student information

Invalid? view_data()

{

int? Me;

Printf ("Student ID \ tname \ Math \ tEnglish \ tPhysics");

printf(" \ n-\ n ");

for(I = 0; I & ltdd.counti++)

printf(" % s \ t % s \ t %-7. 1f \ t %-7. 1f \ t %-7. 1f \ n ",dd.stu[i]。 No, dd.stu[i]. Name, dd.stu[i]. Mathematics, dd.stu[i]. eng,dd.stu[i]。 phy);

printf(" \ n-\ n ");

Ren ();

}

//? Save the data in the structure array to a file.

Invalid? save_data()

{

Documents? * fp// file pointer?

int? I, k;

k? =? Dd.count// Actually, k has no practical significance, so it is directly used in fwrite &;; Dd.count will do.

fp=fopen("D:\\data.txt "," w "); //File storage location: current folder?

fwrite(& amp; k,sizeof(int), 1,FP);

for(I = 0; I & ltdd.counti++)

fwrite(& amp; dd.stu[i],sizeof(struct? Students), 1, fp);

fclose(FP);

}

//? Enter student records

Invalid? add_data()

{

int? b;

int? k? =? dd.count

while(k & lt; Max)

{

Printf ("Please enter your student number:"); scanf("%s ",dd.stu[k]。 No);

Printf ("Please enter student name:"); scanf("%s ",dd.stu[k]。 Name);

Printf ("Please enter your math score:"); scanf("%f ",& ampdd.stu[k]。 Mathematics);

Printf ("Please enter English scores:"); scanf("%f ",& ampdd.stu[k]。 eng);

Printf ("Please enter your physics score:"); scanf("%f ",& ampdd.stu[k]。 phy);

DD . count++; //Need to set 0 in advance, in the main function.

k? =? dd.count

Printf ("\ n \ nContinue to add student information [1- Yes? 0- No]:);

scanf("%d ",& ampb);

if(b==0)? Break;

}

save _ data();

}

//? Read data from a file into a structure array.

Invalid? read_data()

{

Documents? * fp

int? I, k;

k = 0;

if((fp=fopen("D:\\data.txt "," r ")= = NULL)? //? If the file does not exist

{

dd.count? =? 0;

}

other

{

Fred (& ampk, sizeof(int), 1, fp);

dd.count? =? k;

for(I = 0; I & ltk;; i++)

{

Fred (&dd. Stu [i], sizeof(struct? Students), 1, fp);

}

fclose(FP);

}

}

//? Delete student records

Invalid? delete_data()

{

int? I, k;

Charles? No [10];

Printf ("\ nPlease enter the student ID of the student to be deleted:");

Scanf("%s ",no);

k? =? - 1;

for(I = 0; I & ltdd.counti++)

{

If(strcmp(dd.stu[i]. No, no) ==0)

{

k? =? Me;

Break;

}

}

If (k==- 1)

{

Printf ("\ n \ nStudent ID -%s!" not found. , no);

}

other

{

//You didn't write or delete it.

for(I = k; ? I & ltdd.count-1; ? I++)// Move the following data forward by one bit.

dd.stu[i]? =? DD . stu[I+ 1];

memset(& amp; dd.stu[dd.count- 1],0,sizeof(struct? Students)); //Set the dd.stu[dd.count- 1] data to 0.

DD . count-;

save _ data();

Printf ("\ n \ nThe record of student (student ID -%s) has been deleted successfully!" , no);

}

}

//? Modify student records

Invalid? Edit data ()

{

int? I, k;

Charles? no[ 10],name[50];

Printf ("\ nPlease enter the student ID of the student to be modified:");

Scanf("%s ",no);

k =- 1;

for(I = 0; I & ltdd.counti++)

{

If(strcmp(dd.stu[i]. No, no) ==0)

{

k = I;

Break;

}

}

If (k==- 1)

{

Printf ("\ n \ nStudent ID -%s!" not found. , no);

}

other

{

Printf ("\ nPlease enter student data:");

printf(" \ n name ");

printf(" \ n-\ n ");

Scanf("%s ",name);

strcpy(dd.stu[k])。 Name, name);

save _ data();

Printf ("\ n \ nModifying student record (student ID -%s) succeeded!" , no);

}

}

//? Inquire about students' grades (student numbers)

Invalid? Query data number ()

{

int? I, k;

Charles? No [10];

Printf ("\ nPlease enter the student ID of the student to be queried:");

Scanf("%s ",no);

k =- 1;

for(I = 0; I & ltdd.counti++)

{

If(strcmp(dd.stu[i]. No, no) ==0)

{

Printf ("\ n \ nStudent ID \ tname \ Math \ tEnglish \ tPhysics");

printf(" \ n-\ n ");

k = I;

printf(" % s \ t % s \ t %-7. 1f \ t %-7. 1f \ t %-7. 1f \ n ",dd.stu[i]。 No, dd.stu[i]. Name, dd.stu[i]. Mathematics, dd.stu[i]. eng,dd.stu[i]。 phy);

Break; //The student number must be unique. If you find one, it means the future is gone.

}

}

If (k==- 1)

{

Printf ("\ n \ nStudent ID -%s!" not found. , no);

}

}

//? Inquire about students' grades (names)

Invalid? Query data name ()

{

int? I, k;

Charles? Name [10];

Printf ("\ nPlease enter the name of the student to be queried:");

Scanf("%s ",name);

k =- 1;

for(I = 0; I & ltdd.counti++)

{

if(strcmp(dd.stu[i].name,name)==0)

{

Printf ("\ n \ nStudent ID \ tname \ Math \ tEnglish \ tPhysics");

printf(" \ n-\ n ");

k = I;

printf(" % s \ t % s \ t %-7. 1f \ t %-7. 1f \ t %-7. 1f \ n ",dd.stu[i]。 No, dd.stu[i]. Name, dd.stu[i]. Mathematics, dd.stu[i]. eng,dd.stu[i]。 phy);

Break;

}

}

If (k==- 1)

{

Printf ("\ n \ nStudent not found (name -%s!)" , name);

}

}

//? Sorting (student number)

Invalid? Sort_data_no (structure? Data? Buff)

{

int? Me;

int? j;

int? k;

int? h;

struct? Student? Temperature;

Documents? * fp

if((fp=fopen("D:\\data.txt "," w+")= = NULL)

{

Printf ("No? Open? File! \ n ");

Exit (0);

}

Printf ("Please enter the subject to be queried: 1- Mathematics, 2- English, 3- Physics:");

scanf("%d ",& ampk);

Switch (k)

{

Case? 1:

for(I = 0; ? I< buff. count-1; ? i++)

for(j = I+ 1; ? j & ltBuff.count? j++)

If(Buff.stu[i]. Math? & gt? Buff.stu[j]。 Mathematics)

{

temp = buff . stu[I];

buff . stu[I]= buff . stu[j];

buff . stu[j]= temp;

}

Printf ("rank \ tnumber of students \ t? name \ t Math \ n ");

for(I = 0; ? Me & ltBuff.count? i++)

printf(" % d \ t % s \ t % s \ t %-7. 1f \ n ",i+ 1,Buff.stu[i]。 No, buff, Stu [me]. Name, Buff.stu[i]. Mathematics);

Break;

Case? 2:

for(I = 0; ? I< buff. count-1; ? i++)

for(j = I+ 1; ? j & ltBuff.count? j++)

if(Buff.stu[i].eng? & gt? Buff.stu[j]。 English)

{

temp = buff . stu[I];

buff . stu[I]= buff . stu[j];

buff . stu[j]= temp;

}

Printf ("rank \ tnumber of students \ t? name \ t English \ n ");

for(I = 0; ? Me & ltBuff.count? i++)

printf(" % d \ t % s \ t % s \ t %-7. 1f \ n ",i+ 1,Buff.stu[i]。 No, buff, Stu [me]. Name, Buff.stu[i]. eng);

Break;

Case? 3:

for(I = 0; ? I< buff. count-1; ? i++)

for(j = I+ 1; ? j & ltBuff.count? j++)

if(Buff.stu[i].phy? & gt? Buff.stu[j]。 Physical layer)

{

temp = buff . stu[I];

buff . stu[I]= buff . stu[j];

buff . stu[j]= temp;

}

Printf ("rank \ tnumber of students \ t? name \ tpphysical \ n ");

for(I = 0; ? Me & ltBuff.count? i++)

printf(" % d \ t % s \ t % s \ t %-7. 1f \ n ",i+ 1,Buff.stu[i]。 No, buff, Stu [me]. Name, Buff.stu[i]. phy);

Break;

Default? :

Printf ("Input error!" );

Exit (0);

}

Printf ("Please press any key to continue ...");

getch();

fclose(FP);

}

//? major function

Invalid? Master ()

{

int? Fun;

dd.count? =? 0;

read _ data();

menu();

while( 1)

{

Printf ("Please enter the function number [0-7]:); //Nothing is needed here &; Fun,

Scanf("%d ",&fun);

Switch (interesting)

{

Case? 1:? view _ data(); Break; //? View all student information?

Case? 2:? add _ data(); ? Break; ? //? Enter student records

Case? 3:? delete _ data(); Break; //? Delete student records

Case? 4:? edit _ data(); Break; //? Modify student records

Case? 5:? query _ data _ no(); Break; ? //? Inquire about students' grades (student numbers)

Case? 6:? query _ data _ name(); Break; ? //Inquire about students' grades (names)?

Case? 7:? Sort _ Data _ Number (DD); ? Break; ? //? Check the score ranking (student number)? //Just send the structure dd directly.

Case? 0:? Break; ? //? give up

}

if(fun==0)? Break;

to _ menu();

}

Return;

}