#include
#include
#include
#define LEN sizeof(struct student)
int n;
/* printf("/**AUTHOR: Lzm ** 2013-10-28 ****/\n");
//printf("/**Student score record system****/\n");
//printf("/**Select what you want to do:****/\n");
//printf("/***0 : create a document! ******/\n");
//printf("/***1 : insert a new record!******/\n");
//printf("/***2 : delete a record! ******/\n");
//printf("/***3 : save the records! *******/\n");
//printf("/***4 : Open a exited file! ******/\n");
//printf("/***5 : quit the system! ******/\n");
*/
struct student{
long num;
float score;
struct student *next;
};// Don't forget the ";"!!!!!!!!
//1.Create a new chain table list
struct student * creat(void){
struct student *head;
struct student *p1,*p2;
char temp;
n=0;
p1=p2=(struct student *)malloc(LEN);/*CREAT A NEW UNIT*/
printf("Enter school_number and score:\n");
printf("Enter 0 0 to quit!\n");
scanf("%ld %f",&p1->num,&p1->score);
head=NULL;
while(p1->num !=0) // if the num is 0,it is the end of the input
{
n=n+1; //calculate the number of the member in list
if(n==1)
head=p1; //if it is the first one,give the address to the head
else
p2->next=p1; //give the new UNIT address to the last UNIT'S next
p2=p1; //p2 is to save the address of the last.
p1=(struct student *)malloc(LEN);// malloc() a new mem
printf("Enter school_number and score:\n");
scanf("%ld %f",&p1->num,&p1->score);
}
p2->next=NULL;
return (head);
}
//2.show a chain table list
void print(struct student *head){
struct student* p;
printf("Now ,These %d records are:\n",n);
p=head;
if(head!=NULL){
do{
printf("num:%-8ld score:%5.1f\n",p->num,p->score);
p=p->next; //point to the next UNIT
}while(p!=NULL);
}
}
//3.delet the member in a chain table list
struct student * del(struct student *head,long num){
struct student *p1,*p2,*p3;
if(head==NULL){
printf("\n list is null\n");
goto end;
};
p1=head;
while(num!=p1->num&&p1->next!=NULL){
p2=p1;
p1=p1->next;
}
if(num==p1->num){
if(p1==head)
{
p3=head;//to free
head=p1->next;
}
else
{
p3=p1;//to free
p2->next=p1->next;
}
free(p3);//free the malloc, free has no return value,it is a void func.
printf("delete:%ld successful\n",num);
n=n-1;
}
else
printf("can't find the student: %ld\n",num);
end:
return (head);
}
//4.save the traintable into a file
void save_train(struct student *head){
struct student *p;
char address[40];
FILE *fp;
if(head==NULL){
printf("\n list is null\n");
goto end;
};
printf("Enter the path!\n");
scanf("%s",&address[0]);
puts(address);//
fp = fopen(address,"wb+");// create a new file
fputs("school_number",fp);
fputs(" score ",fp);
p=head;
if(head!=NULL){
do{
fputc('\n',fp); // enter a Enter
fprintf(fp," %-10ld",p->num); //read the num to num
fprintf(fp," %-3.2f",p->score); //read the score to score
p=p->next; //point to the next UNIT
}while(p!=NULL);
}
printf("save success to %s \n",address);
fclose(fp);//you should close the file
end:
;
}
//5.insert a chain table list from little to big
struct student *insert(struct student *head,struct student *stu){
struct student *p0,*p1,*p2;
p1=head;
p0=stu;
if(head==NULL){
head = p0; p0->next=NULL;
}
else{
while((p0->num > p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num <= p1->num){
if(head == p1) head = p0;
else p2->next=p0;
p0->next = p1;
}
else{//That is p1->next == NULL
p1->next=p0;
p0->next=NULL;
}
n=n+1;
}
return head;
}
//6.print the head messages!!!
void print_message(){
printf("/**AUTHOR: Lzm ** 2013-10-28 ****/\n");
printf("/**Student score record system****/\n");
printf("/**Select what you want to do:****/\n");
printf("/***0 : create a document! ******/\n");
printf("/***1 : insert a new record!******/\n");
printf("/***2 : delete a record! ******/\n");
printf("/***3 : save the records! *******/\n");
printf("/***4 : Open a exited file! ******/\n");
printf("/***5 : quit the system! ******/\n");
}
//7.import a file as chaintable list
struct student * open_chain(void){
struct student *head;
struct student *p1,*p2;
FILE *fp;
char address[40],a[20];
printf("Enter the path!\n");
scanf("%s",&address[0]);
fp = fopen(address,"rb");// read a file
if(fp == NULL) return 0;
if(!feof(fp)){
fscanf(fp,"%s",&a[0]);
fscanf(fp,"%s",&a[0]);
}
n=0;
p1=p2=(struct student *)malloc(LEN);/*CREAT A NEW UNIT*/
head=NULL;
while(!feof(fp)){
fscanf(fp,"%ld",&p1->num);
fscanf(fp,"%f",&p1->score);
n=n+1; //calculate the number of the member in list
if(n==1)
head=p1; //if it is the first one,give the address to the head
else
p2->next=p1; //give the new UNIT address to the last UNIT'S next
p2=p1; //p2 is to save the address of the last.
p1=(struct student *)malloc(LEN);// malloc() a new mem
}
p2->next=NULL;
printf("read ok!");
close(fp);
return (head);
}
//the main
int main(){
struct student *p,*stu;
long int num;
char select = -1;
char insert_quit=0;
char delete_quit= 0;
print_message();
scanf("%d",&select);
putchar('\n');
while(1)
{// main while
switch (select)
{
case 0 : p=creat(); //create
break;
case 1 : { //insert
stu = (struct student *)malloc(LEN);
printf("Enter the num and score:\n");
printf("Enter 0 0 to quit!\n");
scanf("%ld%f",&stu->num,&stu->score);
while(stu->num !=0){
p=insert(p,stu);
stu = (struct student *)malloc(LEN);
printf("Input the num and score:\n");
scanf("%ld%f",&stu->num,&stu->score);
};
break;
}
case 2 : printf("Enter the num:\n"); //delete
printf("Enter 0 to quit\n");
scanf("%ld",&num);
while(num !=0){
p=del(p,num);
print(p);
printf("Put into the num:\n");
scanf("%ld",&num);
}
break;
case 3 : save_train(p);
goto step1;
break; //save
case 4 : p=open_chain();
break;
case 5 :
default: goto endm;
}
select = -1;
system("clear");
print_message();
print(p);
step1:
printf("/**Select what you want to do!****/\n");
scanf("%d",&select);
putchar('\n');
}// end of main while
endm:
//
return 0;
}