c语言实现的学生信息管理系统
代码:
#include <stdio.h>#include <stdlib.h> //用于下面的 exit(0);退出程序。#include <string.h> //用于 判断是否有某个学生 利用 字符串的大小比较。struct student //定义结构体 { char name[10]; int number; char sex; float score;}; //莫忘“ ; ”void sort(struct student s[],int m) //定义函数 按照分数排序 { int i,j,k; struct student t;
for(i = 0;i < m - 1;i ++) //选择法 { k = i; for(j = i +1;j < m;j++) if(s[j].score > s[k].score) k = j; t = s[i]; s[i] = s[k]; s[k] = t; }}void copyFile(FILE *source,FILE *intention) //定义函数 文件复制 { int ch;
while((ch = fgetc(source)) != EOF) //不能是while(!(ch = fgetc(source)))因为它是表示 ch != 0。而EOF 是符号常量 ,值等于 -1,......!=EOF 就是表示文件没结束。 fputc(ch,intention);}void fun0();void fun1(struct student s[]);void fun2(struct student s[]);void fun3(struct student s[],int m);void fun4();void fun5();void fun6();FILE *fp;int N;struct student s[10000];int main(){ int choice;
printf( "菜单选择:\n" ); printf( " 0.初始化所有条目\n" ); printf( " 1.增加学生信息\n" ); printf( " 2.删除学生信息\n" ); printf( " 3.查找学生信息\n" ); printf( " 4.学生信息备份\n" ); printf( " 5.删除所有信息\n" ); printf( " 6.退出学生信息管理程序\n" ); printf( "请输入你的选择:\n" ); scanf( "%d",&choice); switch(choice) { case 0: fun0();break ; case 1: fun1(s);break ; case 2: fun2(s);break ; case 3: fun3(s,N);break ; case 4: fun4();break ; case 5: fun5();break ; case 6: fun6();break ; default :printf("选择错误,请输入正确的选择\n" ); }}void fun0(){ int i;
printf( "输入要输学生个数N:\n" ); scanf( "%d",&N); printf( "请输出各学生信息:\n" ); if(!( fp = fopen("c:\\stu.txt" ,"w"))) //注意“w” { printf( "创建文件错误\n" ); exit(1); } for(i = 0;i < N ;i++) scanf( "%s %d %c %f",&s[i].name,&s[i].number,&s[i].sex,&s[i].score); sort(s,N); //不能是s【N】什么的。因为它是个结构体数组。 for(i = 0;i < N ;i++) fprintf(fp, "%s %d %c %.2f\n",s[i].name,s[i].number,s[i].sex,s[i].score); fclose(fp);}void fun1(struct student s[]){ struct student addstu; int i,j;
printf( "请输入要增加学生的信息\n" ); if(!( fp = fopen("c:\\stu.txt" ,"a+"))) //注意是 “ a +”。。。不能是“ a ”。。。 { printf( "打开文件错误\n" ); exit(1); } scanf( "%s %d %c %f",&addstu.name,&addstu.number,&addstu.sex,&addstu.score); int n = 0; //注意文件中的数据要想使用 必须先读出来 。。。。可以这样读。。。。 while(fscanf(fp,"%s" ,&s[n].name) != EOF) { fscanf(fp, "%d %c %f", &s[n].number,&s[n].sex,&s[n].score); n++; } for(i = 0;i < n;i++) //为什么不能是m. if(addstu.score > s[i].score) break; for(j = n;j > i; j--) s[j] = s[j - 1]; s[i] = addstu; fp = fopen( "c:\\stu.txt","w" ); for(i = 0;i < n + 1 ;i++) fprintf(fp, "%s %d %c %.2f\n",s[i].name,s[i].number,s[i].sex,s[i].score); fclose(fp); }void fun2(struct student s[]){ char name[10]; int i,j;
fp = fopen( "c:\\stu.txt","r+" ); printf( "输入需要删除的学生姓名:\n" ); scanf( "%s",name); int n = 0; while(fscanf(fp,"%s" ,&s[n].name) != EOF) //不能是 (!())这样 { fscanf(fp, "%d %c %f", &s[n].number,&s[n].sex,&s[n].score); n++; } for(i = 0;i < n;i++) //这里想很久啊。。。 if(strcmp(name,s[i].name) == 0) break; if(i == n) printf( "无此人\n" ); else for(j =i ;j < n ;j++) //不能是j = n - 1;j <= i;j-- s[j] = s[j + 1]; fp = fopen( "c:\\stu.txt","w" ); for(i = 0;i < n - 1 ;i++) fprintf(fp, "%s %d %c %.2f\n",s[i].name,s[i].number,s[i].sex,s[i].score); fclose(fp); }void fun3(struct student s[],int m){ char name[10]; int i; fp = fopen( "c:\\stu.txt","r" ); printf( "输入需要查找学生的姓名:\n" ); scanf( "%s",name); int n = 0; while(fscanf(fp,"%s" ,&s[n].name) != EOF) { fscanf(fp, "%d %c %f", &s[n].number,&s[n].sex,&s[n].score); n++; } for(i = 0;i < n;i++) //接下来这里想很久啊。。。 if(strcmp(name,s[i].name) == 0) break; if(i == n) printf( "无此人\n" ); else printf( "%s %d %c %.2f\n",s[i].name,s[i].number,s[i].sex,s[i].score);}void fun4(){ FILE *fp1,*fp2;
fp1 = fopen( "c:\\stu.txt","r" ); fp2 = fopen( "d:\\stu.txt","w" ); copyFile(fp1,fp2); fclose(fp1); fclose(fp2);}void fun5(){ FILE *fp; if(!(fp = fopen("c:\\stu.txt" ,"w"))) //需要时“w”或 “w+” { printf( "打开文件错误\n" ); exit(0); }; //打开文件一下 “w” 形式 什么都不需要写入 然后关闭就行了 就相当于清空了 fclose(fp);}void fun6(){ exit(0);}