程序代码:
/*这是一个基于链表的学生管理系统,我练习用的,还有两个大的功能(一是排序,二是插入)和许多细小的功能没有完成,*/
/*这是一个学生信息管理系统*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct achievement{
int Chinese;
int Mathe;
int English;
};
struct stdinf{
char stdid[14];
char name[20];
struct achievement achiev;
int score;
float aver;
struct stdinf *next;
};
struct stdinf *openstdinf(void);
int savestdinf(struct stdinf *phead);
struct stdinf *addstdinf(void);
void showstdinf(const struct stdinf *phead);
int modifystdinf(struct stdinf *phead);
void insertstdinf(struct stdinf *phead);
struct stdinf *delstdinf(struct stdinf *phead);
int inputstdinf(struct stdinf *pstdinf);
char mainmenu(void);
struct stdinf *findstdinf(struct stdinf *phead);
int main(void)
{
struct stdinf *phead,*pend,*pcurrent;
char menuid=1;
phead=openstdinf();
pend=phead;
if(pend!=NULL)
{
while(pend->next!=NULL)
pend=pend->next;
}
while(menuid)
{
switch(menuid=mainmenu())
{
case 'a':
if((pcurrent=addstdinf()) != NULL)
{
if(phead==NULL)
{
phead=pcurrent;
pend=pcurrent;
}
else
{
pend->next=pcurrent;
pend=pcurrent;
}
while(pend->next!=NULL)
pend=pend->next;
}
break;
case 's':
showstdinf(phead);
break;
case 'f':
if((pcurrent=findstdinf(phead))!=NULL)
printf("%s %s %d %d %d %d %f\n",pcurrent->stdid,pcurrent->name,pcurrent->achiev.Chinese,pcurrent->achiev.Mathe,pcurrent->achiev.English,pcurrent->score,pcurrent->aver);
else
printf("No this student's infor\n");
getchar();
break;
case 'm':
modifystdinf(phead);
break;
case 'i':
printf("you press %c\n",menuid);
break;
case 'd':
phead=delstdinf(phead);
break;
case 'q':
savestdinf(phead);
menuid=0;
break;
}
}
return 0;
}
struct stdinf *openstdinf(void)
{
int n;
struct stdinf *phead=NULL,*pend=NULL,*pcurrent=NULL;
FILE *pfile;
pfile=fopen("stdinf.txt","r");
if(pfile!=NULL)
{
pcurrent=(struct stdinf *)malloc(sizeof(struct stdinf));
if(pcurrent==NULL)
{
printf("System error ,exit!\n");
exit(1);
}
while((n=fscanf(pfile,"%s%s%d%d%d%d%f",pcurrent->stdid,pcurrent->name,&pcurrent->achiev.Chinese,&pcurrent->achiev.Mathe,&pcurrent->achiev.English,&pcurrent->score,&pcurrent->aver))!=EOF && n==7 )
{
pcurrent->next=NULL;
if(phead==NULL)
{
phead=pcurrent;
pend=pcurrent;
}
else
{
pend->next=pcurrent;
pend=pcurrent;
}
pcurrent=(struct stdinf *)malloc(sizeof(struct stdinf));
if(pcurrent==NULL)
{
printf("System error ,exit!\n");
exit(1);
}
}
free(pcurrent);
fclose(pfile);
}
return phead;
}
char mainmenu(void)
{
char c;
int i=1;
while(i)
{
system("cls");
printf("\n\n ******* Welcome to student information manage system *******\n\n");
printf(" a Add student infor s Show student infor\n");
printf(" f Find student infor m Modi student infor\n");
printf(" i Insert student infor d Del student infor\n");
printf(" q Save and exit system\n\n");
printf(" Plaese choose:");
c=getch();
if(c=='a' || c=='s' || c=='f' || c=='m' || c=='i' || c=='d' || c=='q')
{
break;
}
}
return c;
}
struct stdinf *delstdinf(struct stdinf *phead)
{
char szstdid[7],szbuffer[30];
struct stdinf *pcurrent,*pbefore;
printf("\nInput student's ID that you want to delete:");
gets(szbuffer);
sscanf(szbuffer,"%6s",szstdid);
if(szstdid[0]!='\0')
{
pcurrent=phead;
while(pcurrent!=NULL)
{
if(strcmp(pcurrent->stdid,szstdid)==0)
break;
pbefore=pcurrent;
pcurrent=pcurrent->next;
}
}
if(pcurrent!=NULL)
{
if(pcurrent==phead)
{
phead=pcurrent->next;
free(pcurrent);
}
else
{
pbefore->next=pcurrent->next;
free(pcurrent);
}
}
else
{
printf("No this student infor!\n");
return 0;
}
return phead;
}
int modifystdinf(struct stdinf *phead)
{
char szstdid[7],szbuffer[30];
struct stdinf *pcurrent;
printf("\nInput student's ID that you want to modify:");
gets(szbuffer);
sscanf(szbuffer,"%6s",szstdid);
if(szstdid[0]!='\0')
{
pcurrent=phead;
while(pcurrent!=NULL)
{
if(strcmp(pcurrent->stdid,szstdid)==0)
break;
pcurrent=pcurrent->next;
}
}
if(pcurrent!=NULL)
{
inputstdinf(pcurrent);
return 1;
}
else
{
printf("No this student infor!\n");
return 0;
}
}
struct stdinf *findstdinf(struct stdinf *phead)
{
char szstdid[7],szbuffer[30];
struct stdinf *pcurrent;
printf("\nInput student's ID:");
gets(szbuffer);
sscanf(szbuffer,"%6s",szstdid);
if(szstdid[0]!='\0')
{
pcurrent=phead;
while(pcurrent!=NULL)
{
if(strcmp(pcurrent->stdid,szstdid)==0)
break;
pcurrent=pcurrent->next;
}
}
return pcurrent;
}
int savestdinf(struct stdinf *phead)
{
struct stdinf *pcurrent,*pbefor;
FILE *pfile;
if((pfile=fopen("stdinf.txt","w"))==NULL)
{
printf("Can't save file.\n");
return 0;
}
pcurrent = phead;
while(pcurrent != NULL)
{
fprintf(pfile,"%s %s %d %d %d %d %f\n",pcurrent->stdid,pcurrent->name,pcurrent->achiev.Chinese,pcurrent->achiev.Mathe,pcurrent->achiev.English,pcurrent->score,pcurrent->aver);
pbefor=pcurrent;
pcurrent=pcurrent->next;
free(pbefor);
}
fclose(pfile);
return 1;
}
struct stdinf *addstdinf(void)
{
struct stdinf *phead=NULL,*pend=NULL,*pcurrent=NULL;
int n=5;
while(n==5)
{
pcurrent=(struct stdinf *)malloc(sizeof(struct stdinf));
if(pcurrent==NULL)
{
printf("Error ,can't add stdinf.\n");
break;
}
n=0;
n=inputstdinf(pcurrent);
if(n==5)
{
pcurrent->next=NULL;
if(phead==NULL)
{
phead=pcurrent;
pend=pcurrent;
}
else
{
pend->next=pcurrent;
pend=pcurrent;
pend->next=NULL;
}
}
}
if(pcurrent!=NULL)
free(pcurrent);
return phead;
}
void showstdinf(const struct stdinf *phead)
{
const struct stdinf *pcurrent;
pcurrent=phead;
if(pcurrent==NULL)
printf("\nNo record !\n");
else
{
printf("\n stdid name Chinese Mathe English score aver \n");
while(pcurrent!=NULL)
{
printf("%6s %-8s %7d %7d %7d %8d %6.1f\n",pcurrent->stdid,pcurrent->name,pcurrent->achiev.Chinese,pcurrent->achiev.Mathe,pcurrent->achiev.English,pcurrent->score,pcurrent->aver);
pcurrent=pcurrent->next;
}
}
getch();
}
int inputstdinf(struct stdinf *pcurrent)
{
char szarr[51],c;
int ibool=1,n=0,istrlen;
printf("\nPlease input student information ,press Enter only return.\nstdid name Chinese Mathe English\n");
while(ibool)
{
fgets(szarr,51,stdin);
istrlen=strlen(szarr);
fflush(stdin);
if(szarr[0]=='\0' || istrlen<17 )
{
printf("Do you want to return(y/n):");
c=getch();
fflush(stdin);
while(c!='y' && c!='Y' && c!='n' && c!='N')
{
c=getch();
}
if(c=='y' || c=='Y')
{
ibool=0;
n=0;
}
else
printf("Input again:\n");
}
else //分离出所要的数据
{
n=sscanf(szarr,"%6s %18s %d %d %d",pcurrent->stdid,pcurrent->name,&pcurrent->achiev.Chinese,&pcurrent->achiev.Mathe,&pcurrent->achiev.English);
if(n!=5)
{
printf("Data error! Input again.\n");
}
else
{
ibool=0;
pcurrent->score=pcurrent->achiev.Chinese + pcurrent->achiev.Mathe + pcurrent->achiev.English;
pcurrent->aver=(float)pcurrent->score/(float)3;
//pcurrent->next=NULL;
}
}
}
return n;
}