成绩管理系统
#include <stdio.h>#include <string.h>
#include <stdlib.h>
#include <malloc.h>
enum
{
TypeFind,
TypeAdd,
TypeUpdate_Name,
TypeUpdate_Tel,
TypeDelete,
};
struct Student
{
char ID[10];
char name[20];
int c1;
int c2;
int c3;
struct Student*next;
};
typedef struct Student Stu,*pStu;
void input(char *name,char *ID,int *c1,int *c2,int *c3,int type)
{
switch(type)
{
case TypeAdd:
printf("[Add] input[name] [ID] [数学] [语文] [英语]: \n");
break;
case TypeFind:
printf("[Find] input telbook name: ");
break;
case TypeDelete:
printf("[Delete] input telbook name: ");
break;
default:
break;
}
if(name!=NULL)
{
scanf("%s",name);
}
if(ID!=NULL)
{
scanf("%s",ID);
}
if(c1!=NULL)
{
scanf("%d",c1);
}
if(c2!=NULL)
{
scanf("%d",c2);
}
if(c3!=NULL)
{
scanf("%d",c3);
}
}
//显示
void show(pStu head)
{
while(head)
{
printf("学号 姓名 数学 语文 英语 均分\n");
printf("%s %s %d %d %d %d\n",head->ID,head->name,head->c1,head->c2,head->c3,(head->c1+head->c2+head->c3)/3);
head=head->next;
}
}
//头插法输入
void push_front(pStu *head,char *name,char *ID,int *c1,int *c2,int *c3)
{
pStu pt=NULL;
//input(name,ID,c1,c2,c3,TypeAdd);
pt=malloc(sizeof(Stu));
strcpy(pt->name,name);
strcpy(pt->ID,ID);
pt->c1=*c1;
pt->c2=*c2;
pt->c3=*c3;
pt->next=*head;
*head=pt;
return;
}
/*void insert()
{
}
*/
void sort(pStu head)
{
Stu temp;
pStu pr=head,p=head;
int i=1,j,k;
while(p->next!=NULL)
{
p=p->next;
i++;
}
for(j=1;j<i;j++)
{
for(k=0;k<(i-j);k++)
{
if((pr->c1+pr->c2+pr->c3)/3<(p->c1+p->c2+p->c3))
{
temp=*pr;
temp.next=p->next;
*pr=*p;
*p=temp;
pr->next=p;
}
p=p->next;
pr=pr->next;
}
p=head;
pr=head;
}
return;
}
void delete(pStu *head,char *name,char*ID)
{
input(name,ID,NULL,NULL,NULL,TypeDelete);
pStu p=*head;
pStu q=NULL;
int flag=0;
while(p)
{
if(strcmp(p->name,name)==0)
{
if(strcmp(p->ID,ID)==0)
{
if(p==*head)
{
*head=p->next;
}
else
{
q->next=p->next;
}
free(p);
flag=1;
}
q=p;
p=p->next;
}
}
if(flag==0)
printf("error,no record!\n");
return;
}
void find(pStu head,char *name,char *ID)
{
input(name,ID,NULL,NULL,NULL,TypeFind);
while(head)
{
if(strcmp(head->name,name)==0||strcmp(head->ID,ID)==0)
{
printf("%s: %d\n",name,ID);
return;
}
head=head->next;
}
printf("error,no record!\n");
return;
}
pStu read(pStu *head)
{
Stu *p=NULL;
Stu *ptr=*head;
Stu temp;
FILE *fp=fopen("grade.txt","r");
if(fp==NULL)
return *head;
while(5==fscanf(fp,"%s %s %d %d %d\n",temp.ID,temp.name,&temp.c1,&temp.c2,&temp.c3))
{
push_front(head,temp.name,temp.ID,&temp.c1,&temp.c2,&temp.c3);
}
fclose(fp);
return *head;
}
void save(pStu *head)
{
FILE *fp=fopen("grade.txt","w");
Stu *p=*head;
if(fp==NULL)
{
printf("save faild!\n");
return;
}
while(p!=NULL)
{
fprintf(fp,"%s %s %d %d %d\n",p->ID,p->name,p->c1,p->c2,p->c3);
p=p->next;
}
fclose(fp);
printf("save success!\n");
}
int main(int argc,char *argv[])
{
int a;
int n=1;
pStu head=NULL;
char name[20];
char ID[10];
int c1;
int c2;
int c3;
head=read(&head);
while(n)
{
printf("1 添加数据\n");
//printf("2 插入数据\n");
printf("3 删除数据\n");
printf("4 数据排序\n");
printf("5 显示数据\n");
printf("6 查询数据\n");
printf("7 退出\n");
printf("选择:");
scanf("%d",&a);
switch(a)
{
case 1:
push_front(&head,name,ID,&c1,&c2,&c3);
break;
/*case 2:
insert();
break;
*/
case 3:
delete(&head,name,ID);
break;
case 4:
sort(head);
break;
case 5:
show(head);
break;
case 6:
find(head,name,ID);
break;
case 7:
n=0;
break;
default:
printf("重新输入!!");
}
}
save(&head);
return 0;
}