1.内容用到结构体,链表,函数,文件操作
2.该代码实现了增删改查,不过缺点,没有进行链表的内存释放会造成内存泄漏,写还是挺简单的,不过懒得写了
直接上代码自己感悟:
代码:
#include
#include
#include
#include
#include
#include
#include
typedef struct student{
char name[64];
int age;
char id[24];
char sex[5];
float cn;
float en;
struct student *next;
}stu;
void menu(void)
{
int choice;
printf("welcome to student systemsn");
printf("1.add 2.delect n");
printf("3.show 4.update n");
printf("0.exitn");
printf("n");
}
void add(void)
{
int nu;
FILE *pf=NULL;
pf=fopen("student.txt","a");
if(NULL==pf)
{
printf("open false!n");
return ;
}
while(1)
{
getchar();
printf("input 1=contine;0=break;n");
scanf("%d",&nu);
if(!nu)
{
break;
}
stu*s=(stu*)malloc(sizeof(stu));
s->next=NULL;
getchar();
printf("please input student name:");
fgets(s->name,64,stdin);
s->name[strlen(s->name)-1]=' ';
printf("please input age:");
scanf("%d",&(s->age));
getchar();
printf("please input id:");
fgets(s->id,16,stdin);
s->id[strlen(s->id)-1]=' ';
printf("please input sex:");
fgets(s->sex,5,stdin);
s->sex[strlen(s->sex)-1]=' ';
printf("please input cn sorce :");
scanf("%f",&(s->cn));
printf("please input en sorce :");
scanf("%f",&(s->en));
fprintf(pf,"%s %d %s %s %.2f %.2fn",s->name,s->age,s->id,s->sex,s->cn,s->en);
free(s);
}
fclose(pf);
pf=NULL;
}
void delect(char*ck)
{
FILE*pf=NULL;
pf=fopen("student.txt","r");
char pk;
stu*p=(stu*)malloc(sizeof(stu));
p->next=NULL;
stu*po=p;
while(1)
{
stu*a=(stu*)malloc(sizeof(stu));
a->next=NULL;
p->next=a;
if(fscanf(pf,"%s %d %s %s %f %f",a->name,&(a->age),a->id,a->sex,&(a->cn),&(a->en))==EOF)
{
break;
}
pk=fgetc(pf);
p=a;
}
stu*pd=po;
stu*pg;
fclose(pf);
pf=NULL;
while(po->next!=NULL)
{
pg=po;
if(strcmp(po->next->id,ck)==0)
{
po=po->next;
pg->next=po->next;
free(po);
break;
}
po=po->next;
}
pf=fopen("student.txt","w");
pd=pd->next;
while(pd!=NULL)
{
if(pd->id!=0)
{
fprintf(pf,"%s %d %s %s %.2f %.2fn",pd->name,pd->age,pd->id,pd->sex,pd->cn,pd->en);
}
pd=pd->next;
}
fclose(pf);
pf=NULL;
}
int show(char*ck)
{
int i=0;
FILE*pf=NULL;
pf=fopen("student.txt","r");
char pk;
stu*p=(stu*)malloc(sizeof(stu));
p->next=NULL;
stu*po=p;
while(1)
{
stu*a=(stu*)malloc(sizeof(stu));
a->next=NULL;
p->next=a;
if(fscanf(pf,"%s %d %s %s %f %f",a->name,&(a->age),a->id,a->sex,&(a->cn),&(a->en))==EOF)
{
break;
}
pk=fgetc(pf);
p=a;
}
fclose(pf);
pf=NULL;
po=po->next;
while(po!=NULL)
{
if(strcmp(po->id,ck)==0)
{
i=1;
printf("name=%s ,age=%d ,id=%s ,sex=%s ,cn=%.2f ,en=%.2f n",po->name,po->age,po->id,po->sex,po->cn,po->en);
break;
}
po=po->next;
}
return i;
}
void menu_2(void)
{
printf("update information n");
printf("1.id 2.name n");
printf("3.en 4.cn n");
printf("5.sex 6.sex 0.exit n");
printf("n");
}
void update_id(char*ck)
{
int i=0;
FILE*pf=NULL;
pf=fopen("student.txt","r");
char pk;
stu*p=(stu*)malloc(sizeof(stu));
p->next=NULL;
stu*po=p;
while(1)
{
stu*a=(stu*)malloc(sizeof(stu));
a->next=NULL;
p->next=a;
if(fscanf(pf,"%s %d %s %s %f %f",a->name,&(a->age),a->id,a->sex,&(a->cn),&(a->en))==EOF)
{
break;
}
pk=fgetc(pf);
p=a;
}
fclose(pf);
pf=NULL;
po=po->next;
stu *pt=po;
while(po!=NULL)
{
if(strcmp(po->name,ck)==0)
{
printf("please update id:");
fgets(po->id,24,stdin);
po->id[strlen(po->id)-1]=' ';
break;
}
po=po->next;
}
pf=fopen("student.txt","w");
while(pt!=NULL)
{
if(pt->age!=0)
{
fprintf(pf,"%s %d %s %s %.2f %.2fn",pt->name,pt->age,pt->id,pt->sex,pt->cn,pt->en);
}
pt=pt->next;
}
fclose(pf);
pf=NULL;
}
void update_name(char*ck)
{
int i=0;
FILE*pf=NULL;
pf=fopen("student.txt","r");
char pk;
stu*p=(stu*)malloc(sizeof(stu));
p->next=NULL;
stu*po=p;
while(1)
{
stu*a=(stu*)malloc(sizeof(stu));
a->next=NULL;
p->next=a;
if(fscanf(pf,"%s %d %s %s %f %f",a->name,&(a->age),a->id,a->sex,&(a->cn),&(a->en))==EOF)
{
break;
}
pk=fgetc(pf);
p=a;
}
fclose(pf);
pf=NULL;
po=po->next;
stu *pt=po;
while(po!=NULL)
{
if(strcmp(po->id,ck)==0)
{
printf("please update name:");
fgets(po->name,64,stdin);
po->name[strlen(po->name)-1]=' ';
break;
}
po=po->next;
}
pf=fopen("student.txt","w");
while(pt!=NULL)
{
if(pt->age!=0)
fprintf(pf,"%s %d %s %s %.2f %.2fn",pt->name,pt->age,pt->id,pt->sex,pt->cn,pt->en);
pt=pt->next;
}
fclose(pf);
pf=NULL;
}
void update_cn(char*ck)
{
FILE*pf=NULL;
pf=fopen("student.txt","r");
char pk;
stu*p=(stu*)malloc(sizeof(stu));
p->next=NULL;
stu*po=p;
while(1)
{
stu*a=(stu*)malloc(sizeof(stu));
a->next=NULL;
p->next=a;
if(fscanf(pf,"%s %d %s %s %f %f",a->name,&(a->age),a->id,a->sex,&(a->cn),&(a->en))==EOF)
{
break;
}
pk=fgetc(pf);
p=a;
}
fclose(pf);
pf=NULL;
po=po->next;
stu *pt=po;
while(po!=NULL)
{
if(strcmp(po->id,ck)==0)
{
printf("please update cn:");
scanf("%f",&(p->cn));
break;
}
po=po->next;
}
pf=fopen("student.txt","w");
while(pt!=NULL)
{
if(pt->age!=0)
fprintf(pf,"%s %d %s %s %.2f %.2fn",pt->name,pt->age,pt->id,pt->sex,pt->cn,pt->en);
pt=pt->next;
}
fclose(pf);
}
void update_en(char*ck)
{
FILE*pf=NULL;
pf=fopen("student.txt","r");
char pk;
stu*p=(stu*)malloc(sizeof(stu));
p->next=NULL;
stu*po=p;
while(1)
{
stu*a=(stu*)malloc(sizeof(stu));
a->next=NULL;
p->next=a;
if(fscanf(pf,"%s %d %s %s %f %f",a->name,&(a->age),a->id,a->sex,&(a->cn),&(a->en))==EOF)
{
break;
}
pk=fgetc(pf);
p=a;
}
fclose(pf);
pf=NULL;
po=po->next;
stu *pt=po;
while(po!=NULL)
{
if(strcmp(po->id,ck)==0)
{
printf("please update en:");
scanf("%f",&(po->en));
break;
}
po=po->next;
}
pf=fopen("student.txt","w");
while(pt!=NULL)
{
if(pt->age!=0)
fprintf(pf,"%s %d %s %s %.2f %.2fn",pt->name,pt->age,pt->id,pt->sex,pt->cn,pt->en);
pt=pt->next;
}
}
void update_sex(char*ck)
{
FILE*pf=NULL;
pf=fopen("student.txt","r");
char pk;
stu*p=(stu*)malloc(sizeof(stu));
p->next=NULL;
stu*po=p;
while(1)
{
stu*a=(stu*)malloc(sizeof(stu));
a->next=NULL;
p->next=a;
if(fscanf(pf,"%s %d %s %s %f %f",a->name,&(a->age),a->id,a->sex,&(a->cn),&(a->en))==EOF)
{
break;
}
pk=fgetc(pf);
p=a;
}
fclose(pf);
pf=NULL;
po=po->next;
stu *pt=po;
while(po!=NULL)
{
if(strcmp(po->id,ck)==0)
{
printf("please update sex:");
fgets(po->sex,5,stdin);
po->sex[strlen(po->sex)-1]=' ';
break;
}
po=po->next;
}
pf=fopen("student.txt","w");
while(pt!=NULL)
{
if(pt->age!=0)
fprintf(pf,"%s %d %s %s %.2f %.2fn",pt->name,pt->age,pt->id,pt->sex,pt->cn,pt->en);
pt=pt->next;
}
}
void update_age(char*ck)
{
FILE*pf=NULL;
pf=fopen("student.txt","r");
char pk;
stu*p=(stu*)malloc(sizeof(stu));
p->next=NULL;
stu*po=p;
while(1)
{
stu*a=(stu*)malloc(sizeof(stu));
a->next=NULL;
p->next=a;
if(fscanf(pf,"%s %d %s %s %f %f",a->name,&(a->age),a->id,a->sex,&(a->cn),&(a->en))==EOF)
{
break;
}
pk=fgetc(pf);
p=a;
}
fclose(pf);
pf=NULL;
po=po->next;
stu *pt=po;
while(po!=NULL)
{
if(strcmp(po->id,ck)==0)
{
printf("please update age:");
scanf("%d",&(po->age));
break;
}
po=po->next;
}
pf=fopen("student.txt","w");
while(pt!=NULL)
{
if(pt->age!=0)
fprintf(pf,"%s %d %s %s %.2f %.2fn",pt->name,pt->age,pt->id,pt->sex,pt->cn,pt->en);
pt=pt->next;
}
}
int main(void)
{
int choice,i,k;
stu*p=(stu*)malloc(sizeof(stu));
p->next=NULL;
menu();
while(1)
{
printf("please input nums:");
scanf("%d",&choice);
switch(choice)
{
case 0:
goto a;
break;
case 1:
add();
break;
case 2:
getchar();
printf("please iput id:");
fgets(p->id,16,stdin);
p->id[strlen(p->id)-1]=' ';
delect(p->id);
break;
case 3:
getchar();
printf("please input student id:");
fgets(p->id,16,stdin);
p->id[strlen(p->id)-1]=' ';
i=show(p->id);
if(!i)
{
printf("select false!n");
}
break;
case 4:
getchar();
menu_2();
while(1)
{
printf("please input nums:");
scanf("%d",&k);
switch(k)
{
case 0:
goto cp;
break;
case 1:
getchar();
printf("please the id of the student_name:");
fgets(p->name,64,stdin);
p->name[strlen(p->name)-1]=' ';
update_id(p->name);
break;
case 2:
getchar();
printf("please iput the student_name of id:");
fgets(p->id,24,stdin);
p->id[strlen(p->id)-1]=' ';
i=show(p->id);
if(!i)
{
printf("no student!n");
break;
}
update_name(p->id);
break;
case 3:
getchar();
printf("please iput the student_name of id:");
fgets(p->id,24,stdin);
p->id[strlen(p->id)-1]=' ';
update_cn(p->id);
break;
case 4:
getchar();
printf("please iput the student_name of id:");
fgets(p->id,24,stdin);
p->id[strlen(p->id)-1]=' ';
update_en(p->id);
break;
case 5:
getchar();
printf("please iput the student_name of id:");
fgets(p->id,24,stdin);
p->id[strlen(p->id)-1]=' ';
update_sex(p->id);
break;
case 6:
getchar();
printf("please iput the student_name of id:");
fgets(p->id,24,stdin);
p->id[strlen(p->id)-1]=' ';
update_age(p->id);
break;
}
}
cp:
break;
}
}
a:
free(p);
return 0;
}
会学生管理系统和什么管理系统啊,订餐系统之类的都差不多.



