我的指针和文件出问题了
各位大哥大姐们,课程设计,单链表学生成绩管理系统,编译没什么错,可以运行,但貌似我的指针有问题,文件的保存也无法完成,大家帮我看看这个指针有什么问题,怎么改啊!!!!谢谢了(程序未完成)#include<stdio.h>
#include<alloc.h>
#include<conio.h>
#include<string.h>
#define NULL 0
#define LEN sizeof(struct student)
#define STAR "\n***************************************************"
struct gra
{
float Mathematic;
float Physics;
float English;
float C_Program;
float PE;};
struct student
{
long num;
long Class;
char name[15];
int sum;
float ave;
int order;
struct gra grade;
struct student *next;
};
int n; /*定义全局变量 n*/
struct student *input(struct student *p) /*学生成绩录入*/
{
struct student *p0;
double s=0;
p0=p;
clrscr();
printf("\n\nPlease input a student's NUMBER");
printf("(Input 0 to exit.)\n");
scanf("%ld",&p0->num);
if(p0->num)
{printf("\nInput the Class\n");
scanf("%ld",&p0->Class);
printf("\nInput the name\n\n");
scanf("%s",&p0->name);
printf("\n---------------------------------------");
printf("\nNow please input the %s's grades! \n (including Mathematic,Physics,English,C_Program,PE)\n\n",p0->name);
printf("Enter the Mathematic.(0 to 100)\n"); /*输入数学成绩*/
scanf("%f",&p0->grade.Mathematic);
while(p0->grade.Mathematic>100||p0->grade.Mathematic<0)
{
printf("Data error,please enter again:");
scanf("%f",&p0->grade.Mathematic);}
printf("Enter the Physics.(0 to 100)\n"); /*输入物理成绩*/
scanf("%f",&p0->grade.Physics);
while(p0->grade.Physics>100||p0->grade.Physics<0)
{
printf("Data error,please enter again:");
scanf("%f",&p0->grade.Physics);}
printf("Enter the English.(0 to 100)\n"); /*输入英语成绩*/
scanf("%f",&p0->grade.English);
while(p0->grade.English>100||p0->grade.English<0)
{
printf("Data error,please enter again:");
scanf("%f",&p0->grade.English);}
printf("Enter the C_Program.(0 to 100)\n"); /*输入c语言成绩*/
scanf("%f",&p0->grade.C_Program);
while(p0->grade.C_Program>100||p0->grade.C_Program<0)
{
printf("Data error,please enter again:");
scanf("%f",&p0->grade.C_Program);}
printf("Enter the PE.(0 to 100)\n"); /*输入体育成绩*/
scanf("%f",&p0->grade.PE);
while(p0->grade.PE>100||p0->grade.PE<0)
{
printf("Data error,please enter again:");
scanf("%f",&p0->grade.PE);}
printf("\n---------------------------------------");
s=p0->grade.Mathematic+p0->grade.Physics+p0->grade.English+p0->grade.C_Program+p0->grade.PE;
p0->sum=s; /*将总分保存*/
p0->ave=s/3; /*求出平均值*/
p0->order=0;} /*未排序前此值为0*/
n=n+1;
getchar();
return(p0);
}
void output1(struct student *p1,struct student *p2) /*学生成绩输出1*/
{
struct student *p;
printf(STAR);
printf("OUTPUT:\n");
p=p1;
if(p!=NULL)
do
{
printf("\n%ld\t%ld\t%s\n",p->num,p->Class,p->name);
printf("Mathematic %5.2fPhysics %5.2fEnglish %5.2f C Program %5.2f PE %5.2f\n",p->grade.Mathematic,p->grade.Physics,p->grade.English,p->grade.C_Program,p->grade.PE); printf("Sum:%5.2f Average:%5.2f\n",p->sum,p->ave);
printf("*********************************\n\n");
p=p->next;
}while(p->num<=p2->num&&p!=NULL);
printf(STAR);
}
void output2(struct student *head) /*学生成绩整体输出*/
{
struct student *p;
printf(STAR);
p=head;
printf("OUTPUT:\n");
if(head!=NULL)
do
{
printf("\n%ld\t%ld\t%s\n",p->num,p->Class,p->name);
printf("Mathematic %5.2fPhysics %5.2fEnglish %5.2f C Program %5.2f PE %5.2f\n",p->grade.Mathematic,p->grade.Physics,p->grade.English,p->grade.C_Program,p->grade.PE);
printf("Sum:%5.2f Average:%5.2f\n",p->sum,p->ave);
printf("*********************************\n\n");
p=p->next;
}while(p!=NULL);
printf(STAR);
}
struct student *creat(void) /* 创建,添加学生成绩信息*/
{
struct student *head;
struct student *p1,*p2;
void save(struct student *head);
clrscr();
p1=p2=(struct student *)malloc(LEN); /*开辟一个新的单元*/
input(p1);
head=NULL;
if(n==0) printf("There's no any information in this system.\n ");
while(p1->num)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN); /*开辟一个新的单元*/
input(p1);
}
p2->next=NULL;
printf("\nFinish Creat!\n");
printf("\nSave or not?('Y' or 'N')\n");
if(toupper(getche())=='Y') /*转换大小写*/
save(head);
return(head);
}
struct student *insert(struct student *head) /*插入学生成绩信息*/
{
struct student *stu,*p0,*p1,*p2;
clrscr();
stu=NULL;
input(stu);
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
{p1->next=p0;p0->next=NULL;} /*当p0插到最后结点之后*/
}
printf("\nPress any key to get back...\n");
getch();
return(head);
}
struct student *modify(struct student *head) /*修改学生成绩信息*/
{
long number;
struct student *p1;
clrscr();
printf("Please input the student's number to modify information.\n");
scanf("%ld",&number);
if(head==NULL) {printf("\nNo information can be modified!\n");getch();return;}
p1=head;
while(number!=p1->num&&p1->next!=NULL)
{ p1=p1->next; /*移位*/
if(number==p1->num) /*重新改写数据*/
{printf("Please input new informations.\n");
input(p1);return(head);}
}
printf("Not Found!\n");
return;
}
struct student *del(struct student *head) /*删除学生成绩信息*/
{
struct student *p1,*p2;
long num;
clrscr();
p2=head;
printf("Please Input the student's number who you want to delete!\n");
scanf("%ld",&num);
if(head==NULL)
{printf("\nList Null\n");getch();return;}
else
{ p1=head;
while(num!=p1->num&&p1->next!=NULL)
{p2=p1;p1=p1->next;} /*移位*/
if(num==p1->num)
{ if(p1==head) head=p1->next; /*删除学生信息*/
else p2->next=p1->next;
n=n-1;
}
else {printf("%ld not been found!\n",num);return;}
}
printf("Finished!Press any key to get back...\n");
getch();
return(p2);
}
void save(struct student *head) /*保存文件???????????*/
{
FILE *data;
struct student *p;
char filename[30];
clrscr();
printf("Input the Full Path Name Like this:\n\n"); /*输入完整路径以读取文件*/
printf("Enter the flilename (chengji.txt)\n");
scanf("%s",filename);
data=fopen(filename,"wb+");
if(data==NULL)
{ printf("Can't find the file.\n");
getch();
return;}
else
{
p=head;
while(p!=NULL)
{
fwrite(p,LEN,1,data);
p=p->next;
printf("Success");
}
fclose(data);
printf("Save the file success!\n");
getch();
}
}
struct student *enquire(struct student *head) /*查询菜单*/
{
struct student *numfind(struct student *head);
int choose;
clrscr();
printf("******************* Enquire Menu ********************\n");
printf("\n");
printf("\t1.===Input number to enquire information.\n");
printf("\n");
printf("\t2.===Input name to enquire information.\n");
printf("\n");
printf("\t3.===Enquire the Class informations.\n");
printf("\n");
printf("\t0.===Back to main menu.\n");
printf(STAR);
printf("\nPlease choose!(0~3)\n");
scanf("%d",&choose);
switch(choose)
{
case 1:numfind(head);break;
case 2:break;
case 3:break;
case 4:break;
case 5:
case 0:break;
default :printf("Error!\n");break;
}
}
struct student *numfind(struct student *head) /*按序号查询成绩信息(特点:从多少到多少)*/
{
long num1,num2;
struct student *p0,*p1,*p2;
printf("Please input the information from [num1] to [num2].(*num1<=num2)\n");
scanf("%ld %ld",&num1,&num2);
if(head==NULL)
printf("\nList Null\n");
else
{ if(num1==num2)
{ p1=head;
while(num1!=p1->num&&p1->next!=NULL)
p1=p1->next;
if(num1==p1->num)
{printf("\n%ld\t%ld\t%s\n",p1->num,p1->Class,p1->name);
printf("Mathematic %5.2f\nPhysics %5.2f\nEnglish %5.2f\nC Program %5.2f\nPE %5.2f\n",p1->grade.Mathematic,p1->grade.Physics,p1->grade.English,p1->grade.C_Program,p1->grade.PE);}
printf("Sum:%5.2f\nAverage:%5.2f\n",p1->sum,p1->ave);
printf("\n*******************************\n");
}
else
{ p1=p2=head;
while(num2!=p2->num&&p2->next!=NULL)
p2=p2->next;
while(num1!=p1->num&&p1!=p2->next)
p1=p1->next;
output1(p1,p2);
}
}
return(head);
}
struct student *sort1(struct student *head) /*按序号排列*/
{
struct student *p0,*p1,*p2;
int i,j;
if(head==NULL) printf("\tList NULL");
else
{
for(i=0;i<n-1;i++) /*冒泡排序法*/
{ p1=head;
for(j=n-1;j>0;j--)
while(p1->next!=NULL)
{p2=p1;p1=p1->next;
if(p2->num<p1->num)
{p0=p2;p2=p1;p1=p0;free(p0);}
}
}
output2(head);
}
return(head);
}
struct student *sort2(struct student *head) /*按平均成绩大小排序*/
{
struct student *p0,*p1,*p2;
int i,j,m=0;
if(head==NULL) printf("\tList NULL");
else
{
for(i=0;i<n-1;i++)
{ p1=head;
for(j=n-1;j>0;j--)
while(p1->next!=NULL)
{p2=p1;p1=p1->next;
if(p2->ave<p1->ave)
{p0=p2;p2=p1;p1=p0;free(p0);}
}
}
p0=head;
while(p0!=NULL) /*填写名次*/
{
m++;
p0->order=i;
p0=p0->next;}
}
output2(head);
return(head);
}
void main()
{
struct student *head;
char user_name[20];
long password;
int choose;
head=NULL;
clrscr();
printf("\nPress any key to enter the system...");
getch();
clrscr();
printf("\n\n\n\n\t\tPlease input the user name and password.\n");
printf("\n\n\n\n\n\t\t\tuser name: ");
gets(user_name);
printf("\n\n\t\t\tpassword: ");
scanf("%ld",&password);
clrscr();
if(password==123)
{
printf("\nHello! %s\n\n",user_name);
printf(STAR);
printf("\n\n\n\n\t Welcome to Use the Student System\n");
printf("\n\n\t\tMade by TongGong 0912.\n");
printf("\n\n\t\t 03091445 LvQian\n\n\n");
printf(STAR);
printf("\n\n");
printf("Press any key to continue...\n");
getch();
head=NULL; /*链表初始化,使head的值为NULL*/
while(1)
{clrscr();
printf("\nHello! %s\n",user_name);
printf("\n\t\t******************* Main Menu ****************\n");
printf("\n\n");
printf("\t\t 1.===Input Students' Grades.(Initialization)\n"); /*输入学生成绩(初始化)*/
printf("\n");
printf("\t\t 2.===Input Students' Grades.(Insert)\n"); /*输入学生成绩(插入)*/
printf("\n");
printf("\t\t 3.===Modify Students' Grades.\n"); /*修改学生成绩*/
printf("\n");
printf("\t\t 4.===Delete Student's Grades.\n"); /*删除学生成绩*/
printf("\n");
printf("\t\t 5.===Enquire Students' Grades.\n"); /*查询学生成绩*/
printf("\n");
printf("\t\t 6.===Save the Information into File.\n"); /*保存学生成绩*/
printf("\n");
printf("\t\t 0.===Exit the System.\n"); /*退出系统*/
printf("\n\n\t\t**********************************\n");
printf("\n\t\tPlease choose!(0~5)to continue...\n");
scanf("%d",&choose);
switch(choose)
{
case 1:head=creat();break;
case 2:insert(head);break;
case 3:head=modify(head);break;
case 4:head=del(head);break;
case 5:
case 6:save(head);break;
case 0:exit(0);
default : printf("Error!\n");getch();printf("Back to Main Menu\n");break;
}
}
clrscr();
printf("\n\n\n\n\t\tThank You for Using the System!\n\n");
printf("\n\n\n\t\t\t GoodBye %s",user_name);
}
else
{printf("\n\n\t\t\t\tWrong Password!\n");
printf("\n\n\nPress any key to exit...\n");}
getch();
}