我这程序为什么出错,难道文件文件函数不是这用的么?()
#include<stdio.h>#include<string.h>
struct Student
{
int number;
char name[20];
float score;
struct Student *next;
};
void Display();
void Choice();
struct Student *CreateLinkList();
void PrintScoreList();
void SearchStudent();
void DeleteStudent();
void AddStudent();
int CountNode();
void AmendInformation();
void InserStudent(int n);
void Array();
int main()
{
Display();
Choice();
return 0;
}
struct Student *CreateLinkList()
{
int number;
FILE *p=fopen("d:\\student.mdb","w");
struct Student *linklist1,*head,*linklist2;
linklist1=(struct Student*)malloc(sizeof(struct Student));
head=linklist1;
linklist2=linklist1;
printf("Input student's nubmer(input 0 to end):");
scanf("%d",&number);
while(number!=0)
{
linklist2=(struct Student*)malloc(sizeof(struct Student));
linklist2->number=number;
printf("Input student's name:");
scanf("%s",linklist2->name);
printf("Input student's score:");
scanf("%f",&linklist2->score);
linklist1->next=linklist2;
linklist1=linklist1->next;
printf("Input student's nubmer(input 0 to end):");
scanf("%d",&number);
}
linklist2->next=NULL;
fwrite(&head,sizeof(struct Student),1,p);
fclose(p);
printf("\n");
return head;
}
void PrintScoreList()
{
struct Student *head;
FILE *p=fopen("d:\\student.mdb","r");
fread(&head,sizeof(struct Student),1,p);
head=head->next;
printf("Number Name Score\n");
while(head!=NULL)
{
printf("%-8d%-6s%-5.2f\n",head->number,head->name,head->score);
head=head->next;
}
fclose(p);
printf("\n");
}
void SearchStudent()
{
struct Student *head;
FILE *p=fopen("d:\\student.mdb","r");
int number;
fread(&head,sizeof(struct Student),1,p);
head=head->next;
printf("Please input student's number to search:");
scanf("%d",&number);
while(head->number!=number)
{
head=head->next;
}
printf("Number Name Score\n");
printf("%-8d%-6s%-5.2f\n",head->number,head->name,head->score);
printf("\n");
fclose(p);
}
void DeleteStudent()
{
struct Student *head;
FILE *p=fopen("d:\\student.mdb","r+");
fread(&head,sizeof(struct Student),1,p);
int number;
struct Student *link1=head->next,*link2=head;
printf("Please input student's nubmer to delete:");
scanf("%d",&number);
while(link1->number!=number)
{
link1=link1->next;
link2=link2->next;
}
link2->next=link1->next;
free(link1);
fwrite(&head,sizeof(struct Student),1,p);
fclose(p);
printf("\n");
}
void AddStudent()
{
struct Student *head;
FILE *p=fopen("d:\\student.mdb","r+");
fread(&head,sizeof(struct Student),1,p);
struct Student *link=head;
while(link->next!=NULL)
{
link=link->next;
}
link->next=CreateLinkList()->next;
fwrite(&head,sizeof(struct Student),1,p);
fclose(p);
printf("\n");
}
int CountNode()
{
struct Student *head;
FILE *p=fopen("d:\\student.mdb","r");
fread(&head,sizeof(struct Student),1,p);
int count=0;
head=head->next;
while(head!=NULL)
{
count ++;
head=head->next;
}
fclose(p);
printf("\n");
return count;
}
void InserStudent(int n)
{
struct Student *head;
FILE *p=fopen("d:\\student.mdb","r+");
int i=1;
struct Student *link,*temp;
fread(&head,sizeof(struct Student),1,p);
link=head;temp=CreateLinkList()->next;
while(i++<n)
{
link=link->next;
}
temp->next=link->next;
link->next=temp;
fwrite(&head,sizeof(struct Student),1,p);
fclose(p);
printf("\n");
}
void AmendInformation()
{
struct Student *head;
FILE *p=fopen("d:\\student.mdb","r+");
int number;
fread(&head,sizeof(struct Student),1,p);
head=head->next;
printf("Please input student's number to amend information:\n");
scanf("%d",&number);
while(head->number!=number)head=head->next;
printf("Number Name Score\n");
printf("%-8d%-6s%-5.2f\n",head->number,head->name,head->score);
printf("Please input name:");
scanf("%s",head->name);
printf("Input score:");
scanf("%f",&head->score);
printf("Number Name Score\n");
printf("%-8d%-6s%-5.2f\n",head->number,head->name,head->score);
printf("\n");
fwrite(&head,sizeof(struct Student),1,p);
fclose(p);
}
void Array()
{
struct Student *head;
FILE *p=fopen("d:\\student.mdb","r+");
int i,j,length=CountNode(head);
float score;
int number;
char name[20];
fread(&head,sizeof(struct Student),1,p);
head=head->next;
for(i=0; i<length; i++)
{
for(j=i; j<length-1-i; j++)
{
if(head->score<head->next->score)
{
score=head->score;
head->score=head->next->score;
head->next->score=score;
number=head->number;
head->number=head->next->number;
head->next->number=number;
strcpy(name,head->name);
strcpy(head->name,head->next->name);
strcpy(head->next->name,name);
}
head=head->next;
}
}
fwrite(&head,sizeof(struct Student),1,p);
fclose(p);
}
void Display()
{
printf("***********************************\n");
printf("* STUDENT DATA 2011 *\n");
printf("***********************************\n");
printf("* A.Input student's information *\n");
printf("* B.Output stdent information *\n");
printf("* C.Rserch by input number *\n");
printf("* D.Delete by input number *\n");
printf("* E.Add student int the end *\n");
printf("* F.Insert student *\n");
printf("* G.Amend student information *\n");
printf("* H.Array student information *\n");
printf("***********************************\n");
printf("** Y. Exit ***\n");
printf("***********************************\n");
}
void Choice()
{
char ch;
int n;
struct Student *head;
printf("Please choise function:");
while(scanf("%c",&ch),ch!='Y'&&ch!='y')
{
switch(ch)
{
case 'A':
case 'a':
head=CreateLinkList();
break;
case 'b':
case 'B':
PrintScoreList(head);
break;
case 'c':
case 'C':
SearchStudent(head);
break;
case 'd':
case 'D':
DeleteStudent(head);
break;
case 'e':
case 'E':
AddStudent(head);
break;
case 'F':
case 'f':
{
printf("please input the the position:");
scanf("%d",&n);
InserStudent(n);
}
break;
case 'G':
case 'g':
AmendInformation(head);
break;
case 'H':
case 'h':
{
Array(head);
PrintScoreList(head);
}
break;
}
printf("Please choise function:");
}
}
没有用文件函数之前没有错:
#include<stdio.h>
#include<string.h>
struct Student
{
int number;
char name[20];
float score;
struct Student *next;
};
void Display();
struct Student *CreateLinkList();
void PrintScoreList(struct Student *head);
void SearchStudent(struct Student *head);
void DeleteStudent(struct Student *head);
void AddStudent(struct Student *head);
int CountNode(struct Student *head);
void AmendInformation(struct Student *head);
void InserStudent(struct Student *head,int n);
void Array(struct Student *head);
int main()
{
Display();
Choice();
return 0;
}
struct Student *CreateLinkList()
{
int number;
struct Student *linklist1,*head,*linklist2;
linklist1=(struct Student*)malloc(sizeof(struct Student));
head=linklist1;
linklist2=linklist1;
printf("Input student's nubmer(input 0 to end):");
scanf("%d",&number);
while(number!=0)
{
linklist2=(struct Student*)malloc(sizeof(struct Student));
linklist2->number=number;
printf("Input student's name:");
scanf("%s",linklist2->name);
printf("Input student's score:");
scanf("%f",&linklist2->score);
linklist1->next=linklist2;
linklist1=linklist1->next;
printf("Input student's nubmer(input 0 to end):");
scanf("%d",&number);
}
linklist2->next=NULL;
printf("\n");
return head;
}
void PrintScoreList(struct Student *head)
{
head=head->next;
printf("Number Name Score\n");
while(head!=NULL)
{
printf("%-8d%-6s%-5.2f\n",head->number,head->name,head->score);
head=head->next;
}
printf("\n");
}
void SearchStudent(struct Student *head)
{
int number;
head=head->next;
printf("Please input student's number to search:");
scanf("%d",&number);
while(head->number!=number)
{
head=head->next;
}
printf("Number Name Score\n");
printf("%-8d%-6s%-5.2f\n",head->number,head->name,head->score);
printf("\n");
}
void DeleteStudent(struct Student *head)
{
int number;
struct Student *link1=head->next,*link2=head;
printf("Please input student's nubmer to delete:");
scanf("%d",&number);
while(link1->number!=number)
{
link1=link1->next;
link2=link2->next;
}
link2->next=link1->next;
free(link1);
printf("\n");
}
void AddStudent(struct Student *head)
{
struct Student *link=head;
while(link->next!=NULL)
{
link=link->next;
}
link->next=CreateLinkList()->next;
printf("\n");
}
int CountNode(struct Student *head)
{
int count=0;
head=head->next;
while(head!=NULL)
{
count ++;
head=head->next;
}
printf("\n");
return count;
}
void InserStudent(struct Student *head,int n)
{
int i=1;
struct Student *link=head,*temp=CreateLinkList()->next;
while(i++<n)
{
link=link->next;
}
temp->next=link->next;
link->next=temp;
printf("\n");
}
void AmendInformation(struct Student *head)
{
int number;
head=head->next;
printf("Please input student's number to amend information:\n");
scanf("%d",&number);
while(head->number!=number)head=head->next;
printf("Number Name Score\n");
printf("%-8d%-6s%-5.2f\n",head->number,head->name,head->score);
printf("Please input name:");
scanf("%s",head->name);
printf("Input score:");
scanf("%f",&head->score);
printf("Number Name Score\n");
printf("%-8d%-6s%-5.2f\n",head->number,head->name,head->score);
printf("\n");
}
void Array(struct Student *head)
{
int i,j,length=CountNode(head);
float score;
int number;
char name[20];
head=head->next;
for(i=0; i<length; i++)
{
for(j=i; j<length-1-i; j++)
{
if(head->score<head->next->score)
{
score=head->score;
head->score=head->next->score;
head->next->score=score;
number=head->number;
head->number=head->next->number;
head->next->number=number;
strcpy(name,head->name);
strcpy(head->name,head->next->name);
strcpy(head->next->name,name);
}
head=head->next;
}
}
}
void Display()
{
printf("***********************************\n");
printf("* STUDENT DATA 2011 *\n");
printf("***********************************\n");
printf("* A.Input student's information *\n");
printf("* B.Output stdent information *\n");
printf("* C.Rserch by input number *\n");
printf("* D.Delete by input number *\n");
printf("* E.Add student int the end *\n");
printf("* F.Insert student *\n");
printf("* G.Amend student information *\n");
printf("* H.Array student information *\n");
printf("***********************************\n");
printf("** Y. Exit ***\n");
printf("***********************************\n");
}
void Choice()
{
char ch;
int n;
struct Student *head;
printf("Please choise function:");
while(scanf("%c",&ch),ch!='Y'&&ch!='y')
{
switch(ch)
{
case 'A':
case 'a':
head=CreateLinkList();
break;
case 'b':
case 'B':
PrintScoreList(head);
break;
case 'c':
case 'C':
SearchStudent(head);
break;
case 'd':
case 'D':
DeleteStudent(head);
break;
case 'e':
case 'E':
AddStudent(head);
break;
case 'F':
case 'f':
{
printf("please input the the position:");
scanf("%d",&n);
InserStudent(head,n);
}
break;
case 'G':
case 'g':
AmendInformation(head);
break;
case 'H':
case 'h':
{
Array(head);
PrintScoreList(head);
}
break;
}
printf("Please choise function:");
}
}