I am writing a simple program for managing the final grades of students. The program first read original data from a file called "final.txt", and create a linked list to store the imfornation in memory.
The format of the orginal data is "Class_Character Seat_number Computer_grades Laboratory_grades".
There is only data for one student in every line.
For example,the content of "final.txt" may looks like:
C 9 67 72
B 36 98 60
A 41 23 56
B 51 52 33
C 8 45 12
After I finished creating the Linked List, I want to use "Selection Sort" to sort the data based on the students' classes and the sum of math and english grades. The result of the sort may looks like:
[IMG]http://img37.imageshack.us/img37/9837/89640759.jpg[/IMG]
The following is the code which I wrote:
#include<stdio.h>
#include<stdlib.h>
struct student
{
int com,com_use; //[com:Computer_grades] [com_use:Laboratory_grades]
int num,total; // [num:Seat_number] [total:the sum of math and english grades]
struct student * next;
};
struct classes
{
char t; // [t:Class_Character]
struct student * point;
struct classes * next;
};
void sl_sort(struct classes **,struct student **); // The function of "Selection Sort"
int main()
{
FILE *data;
char temp;
int first=1;
int num_t,com_t,com_u_t;
int stu_count=0;
struct student *stu_head,*stu_pt,*stu_pre,*stu_cur;
struct classes *cla_head,*cla_pt,*cla_pre,*cla_cur;
stu_head=NULL;
cla_head=NULL;
data=fopen("final.txt","r");
while ((fscanf(data,"%c %d %d %d\n",&temp,&num_t,&com_t,&com_u_t)) != EOF)
{
stu_pt=(struct student *)malloc(sizeof(struct student));
stu_pt->num=num_t;
stu_pt->com=com_t;
stu_pt->com_use=com_u_t;
stu_pt->total=com_t+com_u_t;
stu_pt->next=NULL;
if (first == 1)
{
stu_head=stu_pt;
cla_head=(struct classes *)malloc(sizeof(struct classes));
cla_head->t=temp;
cla_head->point=stu_head;
cla_head->next=NULL;
first=0;
}
else
{
cla_cur=cla_head;
while (cla_cur != NULL)
{
if (cla_cur->t == temp)
{
break;
}
cla_cur=cla_cur->next;
}
if (cla_cur == NULL)
{
cla_pt=(struct classes *)malloc(sizeof(struct classes));
cla_pt->t=temp;
cla_pt->point=stu_pt;
cla_pt->next=NULL;
cla_pre=cla_head;
while (cla_pre->next != NULL)
{
cla_pre=cla_pre->next;
}
cla_pre->next=cla_pt;
}
else
{
stu_pre=cla_cur->point;
while (stu_pre->next != NULL)
{
stu_pre=stu_pre->next;
}
stu_pre->next=stu_pt;
}
}
}
fclose(data);
sl_sort(&cla_head,&stu_head);
return 0;
}
void sl_sort(struct classes **cla_head,struct student **stu_head)
{
struct classes *cla_temp,*cla_cur,*cla_pre,*cla_start,*cla_add_at;
int cla_start_count=0,i;
cla_start=*cla_head;
while (1)
{
if (cla_start_count != 0)
{
cla_start=*cla_head;
for (i=cla_start_count;i>0;i--)
{
cla_start=cla_start->next;
}
}
if (cla_start->next = NULL)
{
break;
}
cla_cur=cla_start;
cla_temp=cla_start;
while (cla_cur != NULL)
{
if (cla_cur->t < cla_temp->t)
{
cla_temp=cla_cur;
}
cla_cur=cla_cur->next;
}
cla_pre=*cla_head;
while (cla_pre->next != cla_temp)
{
cla_pre=cla_pre->next;
}
cla_pre->next=cla_temp->next;
if (cla_start_count != 0)
{
cla_add_at=*cla_head;
while (cla_add_at->next != cla_start)
{
cla_add_at=cla_add_at->next;
}
cla_temp->next=cla_pre;
cla_add_at->next=cla_temp;
}
else
{
cla_temp->next=*cla_head;
*cla_head=cla_temp;
}
cla_start_count=cla_start_count+1;
}
}
There's no error messages while compiling. But when I tried to run the program, I got an error message from Windows, it said "grades.exe has encountered a problem and needs to close. We are sorry for the inconvenience." I don't know where the problem is (maybe the sort or the double pointers), could you help me to correct the code? Thanks for any reply.