知乐空间

c语言学生信息管理系统代码

问题描述:

在文件studd.txt中存放学生信息,学生信息包含学号、姓名和成绩。要求采用菜单形式实现学生记录的创建、添加、查找(按学号进行)、修改(按学号进行)和删除(按学号进行)、显示所有信息等功能。用户可以循环操作直到选择退出为止。

分析:

本题是对文件的综合应用,采用菜单形式可以方便地实现程序模块的设计方法,这样可以使程序显得简洁明了。设计时可以逐个完成各模块功能,并调试好每个模块,然后再整合各模块。

参考代码:

#include

#include

#include

#include

struct student

{ char no[10];

char name[20];

int score;

};

char filename[100]=”studd.txt”; /*设置文件名*/

FILE *fp;

void create(); /*创建函数声明*/

void append(); /*添加函数声明*/

void search(); /*查找函数声明*/

void del(); /*删除函数声明*/

void modify(); /*修改函数声明*/

void output(); /*显示函数声明*/

int main(void)

{

int num;

while(1)

{

printf(” ***学生成绩系统*** ”);

printf(” 1.创建记录 ”);

printf(” 2.添加记录 ”);

printf(” 3.查找记录 ”);

printf(” 4.修改记录 ”);

printf(” 5.删除记录 ”);

printf(” 6.显示记录 ”);

printf(” 0.退出系统 ”);

printf(“ 选择序号0-6:” );

scanf(“%d”,&num);

if(num>=0&&num<=6)

{

switch(num)

{ case 1:create();break;

case 2:append();break;

case 3:search();break;

case 4:modify();break;

case 5:del();break;

case 6:output();break;

case 0:exit(1);

}

printf(“ 操作完毕,请再次选择! ”);

}

else

printf(“ 选择错误,请再次选择! ”);

}

getch();

return 0;

}

/*创建记录*/

void create()

{

struct student stu;

if((fp=fopen(filename,”w”))==NULL)

{

printf(“Cannot Open File! ”);

exit(0);

}

fprintf(fp,”%-10s%-20s%-50s ”,”学号”,”姓名”,”成绩”);

printf(“ 请输入学号、姓名及成绩(以0结束) ”);

scanf(“%s”,stu.no);

while(strcmp(stu.no,”0″))

{

scanf(“%s %d”,stu.name,&stu.score);

fprintf(fp,”%-10s%-20s%-50d ”,stu.no,stu.name,stu.score);

scanf(“%s”,stu.no);

}

fclose(fp);

}

/*添加记录*/

void append()

{

struct student stu;

if((fp=fopen(filename,”a”))==NULL)

{

printf(“ Cannot Open File!”);

exit(0);

}

printf(“ 请输入要添加的学号、姓名及成绩 ”);

scanf(“%s%s%d”,stu.no,stu.name,&stu.score);

fprintf(fp,”%-10s%-20s%-50d ”,stu.no,stu.name,stu.score);

fclose(fp);

}

/*查找记录*/

void search()

{

int k=0;

char nokey[10];

struct student stu;

printf(“ 请输入学号:”);

scanf(“%s”,nokey);

if((fp=fopen(filename,”r”))==NULL)

{

printf(“ Cannot Open File!”);

exit(0);

}

fseek(fp,1L*sizeof(struct student),0);

while(!feof(fp))

{

fscanf(fp,”%s%s%d”,stu.no,stu.name,&stu.score);

if(strcmp(nokey,stu.no)==0)

{

printf(“ 已查找到,该记录为: ”);

printf(“%-10s%-20s%-50s”,”学号”,”姓名”,”成绩”);

printf(“%-10s%-20s%-50d”,stu.no,stu.name,stu.score);

k=1;

}

}

if(!k)

printf(“ 文件中无此人的记录。”);

fclose(fp);

}

/*修改记录*/

void modify()

{

int k=0;

long position;

char nokey[10];

struct student stu;

printf(“ 请输入学号:”);

scanf(“%s”,nokey);

if((fp=fopen(filename,”r+”))==NULL)

{

printf(“ Cannot Open File!”);

exit(0);

}

fseek(fp,1L*sizeof(struct student),0);

while(!feof(fp))

{

fscanf(fp,”%s%s%d”,stu.no,stu.name,&stu.score);

if(strcmp(nokey,stu.no)==0)

{ position=ftell(fp);

k=1;

break;

}

}

if(k)

{

printf(“ 已查找到,该记录为: ”);

printf(“%-10s%-20s%-50s”,”学号”,”姓名”,”成绩”);

printf(“%-10s%-20s%-50d”,stu.no,stu.name,stu.score);

printf(“ 请输入新的学号、姓名及成绩:”);

scanf(“%s%s%d”,stu.no,stu.name,&stu.score);

fseek(fp,position-1L*sizeof(struct student),SEEK_SET);

fprintf(fp,” %-10s%-20s%-50d”,stu.no,stu.name,stu.score);

}

else

printf(“ 文件中无此人的记录。”);

fclose(fp);

}

/*删除记录*/

void del()

{

int m,k=0;

long position;

char nokey[10];

struct student stu;

printf(“ 请输入学号:”);

scanf(“%s”,nokey);

if((fp=fopen(filename,”r+”))==NULL)

{

printf(“ Cannot Open File!”);

exit(0);

}

fseek(fp,1L*sizeof(struct student),0);

while(!feof(fp))

{

fscanf(fp,”%s%s%d”,stu.no,stu.name,&stu.score);

if(strcmp(nokey,stu.no)==0)

{ position=ftell(fp);

k=1;

break;

}

}

if(k)

{

printf(“ 已查找到,该记录为: ”);

printf(“%-10s%-20s%-50s”,”学号”,”姓名”,”成绩”);

printf(“%-10s%-20s%-50d”,stu.no,stu.name,stu.score);

printf(“ 确实要删除记录,请按1;不删除记录,请按0:”);

scanf(“%d”,&m);

if(m)

{

fseek(fp,position-1L*sizeof(struct student),SEEK_SET);

fprintf(fp,”%-10s%-20s%-50s”,””,””,””);

}

}

else

printf(“ 文件中无此人的记录。”);

fclose(fp);

}

/*显示记录*/

void output()

{

struct student stu;

if((fp=fopen(filename,”r”))==NULL)

{

printf(“ Cannot Open File!”);

exit(0);

}

printf(“ 文件内容为: ”);

fseek(fp,1L*sizeof(struct student),0);

while(!feof(fp))

{

fscanf(fp,”%s%s%d ”,stu.no,stu.name,&stu.score);

printf(“%-10s%-20s%-50d”,stu.no,stu.name,stu.score);

}

fclose(fp);

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 ZLME@ZLME.COM 举报,一经查实,立刻删除。

留言与评论(共有 0 条评论)
验证码: