龙空技术网

大学C基础经典案例——学生信息管理系统

妙趣小管家 515

前言:

现时大家对“c写学生信息管理系统”大约比较关注,我们都想要学习一些“c写学生信息管理系统”的相关知识。那么小编也在网摘上搜集了一些对于“c写学生信息管理系统””的相关内容,希望我们能喜欢,各位老铁们一起来了解一下吧!

问题描述:

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

分析:

本题是对文件的综合应用,采用菜单形式可以方便地实现程序模块的设计方法,这样可以使程序显得简洁明了。设计时可以逐个完成各模块功能,并调试好每个模块,然后再整合各模块。欢迎大家加入C++学习交流群598131849 群内有资料分享

参考代码:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<conio.h>

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(" ***学生成绩系统***\n\n");

printf(" 1.创建记录 \n");

printf(" 2.添加记录 \n");

printf(" 3.查找记录 \n");

printf(" 4.修改记录 \n");

printf(" 5.删除记录 \n");

printf(" 6.显示记录 \n");

printf(" 0.退出系统 \n");

printf("\n 选择序号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("\n\n操作完毕,请再次选择!\n");

}

else

printf("\n\n选择错误,请再次选择!\n");

}

getch();

return 0;

}

/*创建记录*/

void create()

{

struct student stu;

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

{

printf("Cannot Open File!\n");

exit(0);

}

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

printf("\n请输入学号、姓名及成绩(以0结束)\n");

scanf("%s",stu.no);

while(strcmp(stu.no,"0"))

{

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

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

scanf("%s",stu.no);

}

fclose(fp);

}

/*添加记录*/

void append()

{

struct student stu;

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

{

printf("\nCannot Open File!");

exit(0);

}

printf("\n请输入要添加的学号、姓名及成绩\n");

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

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

fclose(fp);

}

/*查找记录*/

void search()

{

int k=0;

char nokey[10];

struct student stu;

printf("\n请输入学号:");

scanf("%s",nokey);

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

{

printf("\nCannot 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("\n\n已查找到,该记录为:\n\n");

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

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

k=1;

}

}

if(!k)

printf("\n文件中无此人的记录。");

fclose(fp);

}

/*修改记录*/

void modify()

{

int k=0;

long position;

char nokey[10];

struct student stu;

printf("\n请输入学号:");

scanf("%s",nokey);

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

{

printf("\nCannot 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("\n\n已查找到,该记录为:\n\n");

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

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

printf("\n请输入新的学号、姓名及成绩:");

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

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

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

}

else

printf("\n\n文件中无此人的记录。");

fclose(fp);

}

/*删除记录*/

void del()

{

int m,k=0;

long position;

char nokey[10];

struct student stu;

printf("\n请输入学号:");

scanf("%s",nokey);

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

{

printf("\nCannot 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("\n\n已查找到,该记录为:\n\n");

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

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

printf("\n确实要删除记录,请按1;不删除记录,请按0:");

scanf("%d",&m);

if(m)

{

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

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

}

}

else

printf("\n\n文件中无此人的记录。");

fclose(fp);

}

/*显示记录*/

void output()

{

struct student stu;

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

{

printf("\nCannot Open File!");

exit(0);

}

printf("\n\n文件内容为:\n");

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

while(!feof(fp))

{

fscanf(fp,"%s%s%d\n",stu.no,stu.name,&stu.score);

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

} 欢迎大家加入C++学习交流群598131849 群内有资料分享

fclose(fp);

}

标签: #c写学生信息管理系统