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);
}
總結
以上是生活随笔為你收集整理的c语言在管理系统中的应用,C语言应用——学生管理系统的制作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言异或加密函数,用异或算法实现文件的
- 下一篇: android studio标题怎么改,