设计一个学生成绩表,数据库包含:学生姓名(name), 成绩(mark)
并且在文档中写明设计思路,流程,以及各个函数的作用
用C语言写,要求就这么简单
会的帮帮忙!!
[此贴子已经被作者于2006-12-19 10:15:29编辑过]
# include<stdio.h>
# include<conio.h>
# include<string.h>
# define swn 3
# define courselen 12
struct stu{
char name[10];
char code[10];
int marks[3];
int total;
struct stu *next;
}stu;
int n;
char filename[10];
char course[swn][courselen]={"chinese","math","English"};
struct stu *creatlist(int m);
writerecord(FILE *fp,struct stu *s);
void find(struct stu *t);
void printlist1(struct stu *s);
void deleted(FILE *fp,struct stu *s);
void insert(FILE *fp,struct stu *s);
void sort(FILE *fp, struct stu *t);
void printlist(struct stu *s);
void main()
{
FILE *fp;
char ch,op,th='y';
struct stu *head=NULL,*p;
clrscr();
printf("How many students are in your class? ");
scanf("%d",&n);
head=creatlist(n);
p=head;
printf("input the filename:");
scanf("%s",filename);
if((fp=fopen(filename,"r"))==NULL)
{
printf("cannot find the file~!\n");
printf("if you want to creat a file,enter 'y':");
getchar();
ch=getchar();
if(ch=='y'||ch=='Y')
{
fp=fopen(filename,"w");
while(p)
{
writerecord(fp,p);
p=p->next;
}
fclose(fp);
}
else exit(0);
}
fclose(fp);
printf("the system have been created,what do you want it to do for you?\n");
while(th=='y'||th=='Y')
{
printf("deleted: please enter '1'\n");
printf("find: please enter '2'\n");
printf("insert:please enter '3'\n");
printf("sort:please enter '4'\n");
printf("quit:please enter '5'\n");
scanf("%d",&op);
switch(op)
{
case 1: deleted(fp,head); break;
case 2: find(head); break;
case 3: insert(fp,head); break;
case 4: sort(fp,head); break;
}
if(op==5)
break;
printf("continue? please enter 'y': ");
getchar();
th=getchar();
}
printf("Thanks for your using~~!");
getch();
}
struct stu *creatlist(int m)
{
struct stu *head,*p=NULL,*rear=NULL;
int marks[3],total=0;
int i=0,j,k,h=0,a=0;
head=(struct stu*)malloc(sizeof(struct stu));
head=rear;
printf("Input the students'infomation:\n");
p=(struct stu*)malloc(sizeof(struct stu));
printf("the student's name:");
scanf("%s",p->name);
printf("the student's code:");
scanf("%s",p->code);
for(j=0;j<swn;j++)
{
printf(" ");
printf("%s:",course[j]);
scanf("%d",&marks[j]);
total=total+marks[j];
}
while((i<m-1)||(h<m))
{
for(k=0;k<3;k++)
p->marks[k]=marks[k];
p->total=total;
h++;
total=0;
rear->next=p;
rear=p;
if(a++<m-1)
{p=(struct stu*)malloc(sizeof(struct stu));
printf("the student's name:");
scanf("%s",p->name);
printf("the student's code:");
scanf("%s",p->code);
for(j=0;j<swn;j++)
{
printf(" ");
printf("%s:",course[j]);
scanf("%d",&marks[j]);
total=total+marks[j];
}
}
i++;
}
rear->next=NULL;
return (head->next);
}
writerecord(FILE *fp,struct stu *s)
{
int i;
struct stu *p;
p=s;
while(p)
{
fprintf(fp,"%3s",p->name);
fprintf(fp,"%3s",p->code);
for(i=0;i<swn;i++)
fprintf(fp,"%3d",p->marks[i]);
fputs("\n",fp);
p=p->next;
}
}
void find(struct stu *t)
{
struct stu *s=t;
char name[10];
int c=0;
printf("input the student's name you want to find:");
scanf("%s",name);
printf("The student you want to find:\n");
while(s)
{
if(strcmp(s->name,name)==0)
{
printlist(s);
c++;
}
s=s->next;
}
if(c==0)
printf("The student not exist in the system!!");
}
void printlist1(struct stu *s)
{
struct stu *p;
int i;
p=s;
while(p)
{
printf("name:");
printf("%s\n",p->name);
printf("code:");
printf("%s\n",p->code);
for(i=0;i<swn;i++)
{
printf("%s:",course[i]);
printf("%d",p->marks[i]);
printf("\n");
}
printf("total:");
printf("%d\n",p->total);
p=p->next;
}
}
void deleted(FILE *fp,struct stu *s)
{
struct stu *t,*q;
char name[10];
printf("input the name you want to delete:");
scanf("%s",name);
t=s;
fp=fopen(filename,"w");
if(strcmp(t->name,name)==0)
{
s=s->next;
writerecord(fp,t);
}
else
{
while(t&&strcmp(t->name,name)!=0)
{
q=t;
t=t->next;
}
if(!t)
{
printf("error");
return;
}
else
q->next=t->next;
}
writerecord(fp,s);
fclose(fp);
printf("the deleted system is:\n");
printlist1(s);
}
void insert(FILE *fp,struct stu *s)
{
struct stu *p,*q=NULL;
char name[10];
int total=0,i;
p=s;
fp=fopen(filename,"w");
q=(struct stu*)malloc(sizeof(struct stu));
printf("input infomation about the student:\n");
printf("student's name:");
scanf("%s",q->name);
printf("student's code:");
scanf("%s",q->code);
for(i=0;i<swn;i++)
{
printf("%s",course[i]);
printf(":");
scanf("%d",&q->marks[i]);
total+=q->marks[i];
}
q->total=total;
printf("input name that the position that you want to insert is behind:");
scanf("%s",name);
while(p&&strcmp(p->name,name)!=0)
p=p->next;
if(!p)
{
printf("error");
return;
}
else {
q->next=p->next;
p->next=q;
}
writerecord(fp,p);
fclose(fp);
printf("the inserted system is:\n");
printlist1(p);
}
void sort(FILE *fp, struct stu *t)
{
struct stu *p,*q,*r,*s,*head;
char op;
head=(struct stu*)malloc(sizeof(struct stu));
head->next=t;
r=head;
p=t;
fp=fopen(filename,"w");
printf("if you want to sort it according to 'chinese',please enter '0'\n");
printf("if you want to sort it according to 'math',please enter '1'\n");
printf("if you want to sort it according to 'English',please enter '2'\n");
scanf("%d",&op);
while(p)
{
s=p;
q=s->next;
while(q)
{
if(p->marks[op]>q->marks[op])
{
s->next=q->next;
r->next=q;
q->next=p;
q=s->next;
p=r->next;
}
else
{
s=s->next;
q=q->next;
}
}
r=r->next;
p=r->next;
}
writerecord(fp,head->next);
fclose(fp);
printf("the sorted system is:\n ");
t=head->next;
printlist1(t);
}
void printlist(struct stu *s)
{
int i;
printf("name:");
printf("%s\n",s->name);
printf("code:");
printf("%s\n",s->code);
for(i=0;i<swn;i++)
{
printf("%s:",course[i]);
printf("%d",s->marks[i]);
printf("\n");
}
printf("total:");
printf("%d\n",s->total);
}