这个程序哪里错了?求指点迷津!
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include "math.h"
#define LEN sizeof(struct student)
第二次上机作业.rar
(1.93 KB)
struct student
{
int num;
char name[20];
float score;
struct student *next;
} *head;
typedef struct student St;
St* Input(St *head,St *st);
St* Start();
St* Inputp(St *head,St *w);
St* Delete(St* st);
St* Deletep(St* q1,int number);
St* DeleteCom(St* head,St* q2);
void Save(char fname, St* p);
void Output(St* st);
void Tongji(St* head);
St* Start() //3õê¼»ˉá′±í
{
head=NULL;
return head;
};
St* Inputp(St *head,St *w) //ìí¼óᬽóμã
{
St *p1;
if(head=NULL)
head=w;
else
{
p1=head;
while(p1->next != NULL)
p1=p1->next;
p1->next = w;
}
printf("ìí¼ó3é1|!\n");
return head;
};
St* Input(St *st) //ìí¼óѧéúDÅÏ¢
{
St *p;
p=(St*)malloc(sizeof(LEN));
printf("êäèëѧéúѧoÅ:\t");
scanf("%d",&p->num );
printf("DÕÃû:\t");
scanf("%s",p->name);
printf("3鼨:\t");
scanf("%f",&p->score );
p->next = NULL;
st=Inputp(st,p);
//Save("CJ.txt",st); //±£′æѧéúDÅÏ¢£¬CJ.txtÎa±£′æμÄÎļtÃû×Ö
return st;
};
void Output(St* st) //êä3öѧéúDÅÏ¢
{
St *x;
x=(St*)malloc(sizeof(LEN));
x=st;
if(x=NULL)
printf("ÎTDÅÏ¢!!\n");
else
{
while(x->next != NULL)
printf("ѧoÅ:\t%dDÕÃû\t&s3鼨\t%f",x->num,x->name,x->score);
x=x->next;
}
};
St* Delete(St* head) //é¾3yѧéúDÅÏ¢
{
St *q1,*q2;
int number;
q1=(St*)malloc(sizeof(LEN));
q2=(St*)malloc(sizeof(LEN));
q1=head;
printf("êäèëÄúòaé¾3yѧéúμÄѧoÅ:");
scanf("%d",&number);
if(q1==NULL)
printf("ÎTDÅÏ¢!\n");
else
{
q2=Deletep(head,number); //μ÷óÃDeletepoˉêyÕòμ½numberËùÔúμĽúμã
if(q2==NULL)
printf("′íÎó!!!\n");
else
head=DeleteCom(head,q2); //μ÷óÃDeleteComoˉêyíê3éé¾3y q2êÇòaé¾3yμĽúμã
printf("é¾3yíê3é!!\n");
}
return head;
};
struct student* Deletep(St* head,int number) //Õòòaé¾3yѧéúμÄμØÖ·
{ //extern number;
St *p;
p=head;
while(p->next != NULL && p->num != number)
p=p->next ;
return p;
};
St* DeleteCom(St* head,St* q2)
{ //extern *q2;
St *m1,*m2,*temp;
m1=(St*)malloc(sizeof(LEN));
m2=(St*)malloc(sizeof(LEN));
temp=(St*)malloc(sizeof(LEN));
if(head==q2)
{
if(head->next != NULL)
head=head->next ;
else
head=NULL;
}
else
{
m1=head;
while(m1!=NULL)
m2=m1->next;
if(m2==q2 && q2->next != NULL)
{
m2->next = m1->next;
free(m2);
}
else if( m2 == q2 && q2->next == NULL)
{
m1->next =NULL;
free(q2);
}
m1=m1->next ;
}
return head;
};
void Tongji(St* head)
{
int a=0,b=0,c=0,d=0,e=0;
St *y;
y=(St*)malloc(sizeof(LEN));
y=head;
if(y==NULL)
printf("\t\tÎTDÅÏ¢!\n");
else
while(y->next != NULL)
{
if(y->score >=90)
a++;
if(y->score >= 80 && y->score <90)
b++;
if(y->score >= 70 && y->score <80)
c++;
if(y->score >= 60 && y->score <70)
d++;
if(y->score < 60)
e++;
y=y->next ;
}
free(y);
printf("\tóÅ%dèË\tá¼%dèË\tÖD%dèË\t¼°¸ñ%dèË\t2»¼°¸ñ%dèË\n",a,b,c,d,e);
};
void Save(char *fname, St* p)
{
FILE *fp;
St *pst=NULL;
fp=fopen(fname,"w+");
if((fopen(fname,"w+"))==NULL)
printf("\t′íÎó!\n");
exit(0);
pst=(St*)malloc(sizeof(LEN));
fprintf(fp,"\t\t\têy¾Y½á113鼨\t\t\t\n");
fprintf(fp,"\tѧoÅ\tDÕÃû\t3鼨\n");
pst=p;
while(fp != NULL)
{
fwrite(pst,sizeof(St),1,fp);
pst=pst->next ;
}
fclose(fp);
};
void main()
{
St *st;
st=Start();
printf("\t\t\têy¾Y½á113鼨1üàíÏμí3\t\t\t\n");
int choice;
while(choice != 6)
{
printf("\n\t\t\t1--êäèëѧéú3鼨\n\t\t\t2--êä3öѧéú3鼨\n\t\t\t3--é¾3yÄ3ѧéú3鼨\n\t\t\t4--í3¼Æѧéú3鼨\n\t\t\t5--±£′æѧéúDÅÏ¢\n\t\t\t6--íË3öÏμí3\n");
printf("êäèëÄúòa½øDDμÄ2ù×÷:\t\t(1,2,3,4,5)\n");
scanf("%d",&choice);
switch(choice)
{
case 1:st = Input(st);break;
case 2:Output(st);break;
case 3:st=Delete(st);break;
case 4:Tongji(st);break;
case 5:Save("CJ.txt",st);break;
}
}
printf("\n»¶ó-Ôù′Îê1óÃ!\n");
};