Home > Back-end >  Read the file creation linked list
Read the file creation linked list

Time:09-21

C + + final grade in a text file "Score. TXT", to code application reads the text file to create list, and according to the student id to find names in the list and grades,
?? Separated by Spaces between the columns of a text file, the file rows uncertain,?????? Requires the use of ifstream object of class and ofstream class reading and writing data,?????? A linked list node can be declared as follows: struct student {unsigned long id; String name; Float score; Student * next;//the next node pointer}
?? Loop read from keyboard student id, query the corresponding names and grades, enter student id 0 exit the loop and

CodePudding user response:

Each line is line read, read three fields

CodePudding user response:

reference 1/f, Simple, Soft reply:
is read one line, each line read three fields

Eldest brother is free to help write the program, can be paid, list don't have the time to start learning, thank you

CodePudding user response:

Write a sample to you

 # include & lt; Iostream> 
#include
#include
#include
#include
Typedef struct student {
Unsigned long id;
String name;
Float score;
Student * next;
} STU.

STU * loadScore (const char * filename) {//load the file information
Int CNT=0, independence idx=0;
The string line item;
Head STU * and * STU=NULL;
Ifstream in ("/Users/qinyb/Score. TXT ");
Stu=head=new stu.
While (getline (in the line, '\ n'))
{
If (line. The empty ())
continue;//empty lines do not handle
If (cnt> 0 {
Stu - & gt; Next=new STU.
Stu=stu - & gt; next;
}
Stringstream ss (line);
Independence idx=0;
While (getline (ss, item, ' '))//press the space segmentation
{
if(! Item. The empty ()) {
If (independence idx==0)
Stu - & gt; Id=stol (item);
Else if (independence idx==1)
Stu - & gt; Name=item;
Else if (independence idx==2)
Stu - & gt; Score=stof (item);
Idx++;
}
}
cnt++;
}
Stu - & gt; Next=NULL;
in.close();
return head;
}

Void printScore (STU * list) {//print list
STU * p=list;
Int CNT=0;
While (p!=NULL) {
cnt++;
Printf (" % d students: student id [% ld], name [% s], grades [. % 2 f] \ n ", CNT, p - & gt; Id, p - & gt; Name. The c_str (), p - & gt; Score);
P=p - & gt; next;
}
}

Void releaseScore (STU * list) {//release list
STU * p=list;
While (p!=NULL) {
STU * q=p;
P=p - & gt; next;
delete q;
}
}

STU * searchStudent (STU * list, unsigned long id) {//retrieve student id
STU * p=list;
While (p!=NULL) {
If (p - & gt; Id=={
id)break;
}
P=p - & gt; next;
}
return p;
}

Int main ()
{
STU * list=loadScore (" Score. TXT ");
PrintScore (list);
Unsigned long id=0;
While (1) {//simulation students query
Cout<& lt;" Please enter a query student's student id: ";
cin> Id;
If (id==0) {
Cout<& lt;" Exit the student query system... "break;
}
STU * STU=searchStudent (list, id);
If (stu==NULL) {
Cout<& lt;" Can't find information!" } else {
Printf (" find information: student id [% ld], name [% s], grades [. % 2 f] \ n ", stu - & gt; Id, stu - & gt; Name. C_str (), stu - & gt; Score);
}
}
ReleaseScore (list);
return 0;
}
  • Related