共享答案
#include<stdio.h>
#include<string.h>
/*#include <malloc.h>*/
#define N sizeof(struct student)
#define M "%d %s %.1f %.1f %.1f %.1f",&p->num,p->name,&p->chinese,&p->math,&p->english,&p->pe
#define K "%d %s %.1f %.1f %.1f %.1f",p->num,p->name,p->chinese,p->math,p->english,p->pe
#define PRINT1 printf("number
name
chinese
math
english
pe")
#define PRINT2 printf(" %d
%s
%.1f
%.1f
%.1f
%.1f\n",p->num,p->name,p->chinese,p->math,p->english,p->pe)
#define PRINT3 printf("*********************************************************\n")
struct student
{
int num;
char name[10];
float chinese;
float math;
float english;
float pe;
struct student *next;
};
/*input message*/
struct student *input_message(void)
{
struct student *p1,*p2,*head;
char ch;
head=NULL;
head=p2=p1=(struct student*)malloc(N);
do
{
p1=(struct student *)malloc(N);
clrscr();
printf("\n\n\t******Input student message******\n");
printf("\t
student num:");
scanf("%d",&p1->num);
printf("\t
student name:");
scanf("%s",p1->name);
printf("\t
student chinese:");
scanf("%f",&p1->chinese);
printf("\t
student math:");
scanf("%f",&p1->math);
printf("\t
student english:");
scanf("%f",&p1->english);
printf("\t
student pe:");
scanf("%f",&p1->pe);
p2->next=p1;
p2=p1;
printf("\t\tContinue? (Y/N):");
ch=getch();
}while(ch=='Y'||ch=='y');
p2->next=NULL;
p1=head;
head=p1->next;
free(p1);
printf("\n\tInput over!");
ch=getchar();
return head;
}
struct student *insert_message(struct student *h)
{
struct student *p,*p1;
char ch;
do
{
int i=1;
clrscr();
p1=(struct student *)malloc(N);
printf("
Please input student message\n");
printf("\t
student num:");
scanf("%d",&p1->num);
printf("\t
student name:");
scanf("%s",p1->name);
printf("\t
student chinese:");
scanf("%f",&p1->chinese);
printf("\t
student math:");
scanf("%f",&p1->math);
printf("\t
student english:");
scanf("%f",&p1->english);
printf("\t
student pe:");
scanf("%f",&p1->pe);
p1->next=NULL;
if(h==NULL)
h=p1;
else
{
p=h;
if(p->num>p1->num)
{
p1->next=h;
h=p1;
}
else
{
while(p->next!=NULL&&p->next->num<p1->num)
p=p->next;
if(p->next==NULL) p->next=p1;
else if(p->next->num==p1->num)
{
free(p1);
i=0;
}
else
{
p1->next=p->next;
p->next=p1;
}
}
}
if(i==0)
printf("\n\n
Failure!");
else
printf("\n\t
Continue? (Y/N):");
ch=getch();
if(ch=='Y'||ch=='y')
printf("\n
Go on!");
else
return;
}while(ch=='Y'||ch=='y');
}
struct student *del_message(struct student *h)
{
struct student *p,*p1;
int num;
char ch;
do
{
int i=1;
clrscr();
if(h==NULL) i=0;
else
{
printf("Please input number you want to delete:");
scanf("%d",&num);
p=h;
if(p->num==num)
{
h=p->next;
free(p);
}
else
{
p1=(h)->next;
while(p1!=NULL)
{
if(p1->num==num)
{
p->next=p1->next;
free(p1);
}
p=p1;
p1=p1->next;
}
}
}
if(i==0) printf("\n\n\tFailure! Not find the student number!");
else printf("\n\nFinish! Continue? (Y/N):");
ch=getch();
if(ch=='Y'||ch=='y')
printf("\n
Go on");
else
return;
}while(ch=='Y'||ch=='y');
}
/*print message*/
void print_message(struct student *h)
{
struct student *p;
p=h;
clrscr();
if(p==NULL)
{
printf("\n\n\t\t
NO message! Press any key back to main menu");
bioskey(0);
}
else
{
printf("\n\n\t\t\t
*****student Message*****
\n\n");
PRINT3;
PRINT1;
printf("\n");
while(p!=NULL)
{
PRINT2;
p=p->next;
}
printf("\n\n\t\t Print over! Press any key continue!\n\n");
bioskey(0);
}
}
/*average_message*/
void average_message(struct student *head)
{
struct student *p;
clrscr();
p=head;
if(head!=NULL)
do
{printf("average=%f\n",(p->chinese+p->math+p->english+p->pe)/4);
p=p->next;
}while(p!=NULL);
getch();
}
/*sum_message*/
void sum_message(struct student *head)
{
struct student *p;
clrscr();
p=head;
if(head!=NULL)
do
{printf("sum=%f\n",p->chinese+p->math+p->english+p->pe);
p=p->next;
}while(p!=NULL);
getch();
}
/*highest_score*/
void highest_score(struct student *head)
{
struct student *p=head;
float score[10],tem;
int i=0,j;
clrscr();
while(p!=NULL){
score[i]=p->chinese+p->math+p->english+p->pe;
i++;
p=p->next;}
for(j=0;j<i;j++)
if(score[0]<score[j]){
tem=score[0];
score[0]=score[j];
score[j]=tem;
}
p=head;
while((p->chinese+p->math+p->english+p->pe)!=score[0]){
p=p->next;}
PRINT1;printf("\n");
PRINT2;
getch();
}
void main()
{
struct student *head;
char ch;
do
{
clrscr();
printf("\n\n\n\t
****student information management system****\n\n");
printf("\t
A.Input student message\n\n");
printf("\t
B.Print student message\n\n");
printf("\t
C.Delete student message\n\n");
printf("\t
D.insert student message\n\n");
printf("\t
E.Sum
student message\n\n");
printf("\t
F.Average
student message\n\n");
printf("\t
G.Highest student message\n\n");
printf("\t
Q.Exit\n\n");
printf("\t
Select (A-Q):");
ch=getchar();
switch(ch)
{
case 'A': head=input_message();break;
case 'B': print_message(head);break;
case 'C': del_message(head);break;
case 'D': insert_message(head);break;
case 'E': sum_message(head);break;
case 'F': average_message(head);break;
case 'G': highest_score(head);break;
case 'Q': break;
}
}while(ch!='Q');
}
用Turbo C V2.01可运行