怎样用c语言制作系统,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);

}

82124ac20473a9a88d61f7a466c70bba.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值