各位帮帮忙吧,本人急需一道有关学生成绩管理系统的程序啊
用C语言编写,不用到链表,要用到文件
# include "stdio.h" # include "string.h" # include "stdlib.h" # define NULL 0 # define MAX 5 # define TRUE 1
typedef struct node { char name[8]; int number; double score; }node;
static int recnum;
void creat() { char scorestr[100],ch,numstr[100]; node student; FILE *fp; if((fp=fopen("C:\stud.ree","wb"))==NULL) { printf("不能打开stud.ree!"); exit(0); } /*rewind(fp); 郁闷的定位函数!!! fseek(fp,recnum*sizeof(node),0); */ do { printf("\n姓名:");gets(student.name); printf("\n学号:"); gets(numstr);student.number=atoi(numstr); printf("\n成绩:"); gets(scorestr);student.score=atof(scorestr); fwrite(&student,sizeof(node),1,fp); recnum++; printf("输入其他的学生记录:(y/n)?"); ch=getchar();getchar(); }while(ch=='y'); fclose(fp); }
void list() { node student; char scorestr[100],ch; FILE *fp; if((fp=fopen("C:\stud.ree","rb"))==NULL) {printf("不能打开stud.ree!"); return; } printf("\n下面是学生资料:\n"); printf(" 学号 姓名 成绩\n");
while(fread(&student,sizeof(node),1,fp)==1) { printf("%1d %s %6.2f\n",student.number,student.name,student.score); } fclose(fp); }
int totalrec() /*计算文件的记录数,用recnum表示总记录数*/ { node student; FILE *fp; recnum=0; if((fp=fopen("C:\stud.ree","rb"))==NULL) return 0; while(fread(&student,sizeof(node),1,fp)==1) { recnum++; } fclose(fp); return (recnum); }
searchnum() { node student; FILE *fp; int i=1; int num1; printf("\n请输入学号:"); scanf("%d",&num1); if((fp=fopen("C:\stud.ree","rb"))==NULL) {printf("不能打开stud.ree!"); return; } while(i<=recnum) { if(fread(&student,sizeof(node),1,fp)==1 && student.number==num1) { printf("\n 姓名: %s \n",student.name); printf("\n学号: %1d \n",student.number); printf("\n成绩: %6.2f\n",student.score); } i++; } fclose(fp); }
searchscore() { node student; FILE *fp; int i=1; double score1; printf("\n请输入成绩(小数点后保留2位):"); scanf("%lf",&score1); if((fp=fopen("C:\stud.ree","rb"))==NULL) {printf("不能打开stud.ree"); return; } while(i<=recnum) { if(fread(&student,sizeof(node),1,fp)==1 && student.score==score1) { printf("\n 姓名 : %s \n",student.name); printf("\n学号: %1d \n",student.number); printf("\n年龄: %6.2f\n",student.score); } i++; } fclose(fp); }
main() {int n; /*N为选择函数变量*/ clrscr(); gotoxy(10,10); printf("欢迎进入学生学籍管理系统"); gotoxy(10,12); printf("1:显示学生信息 2:成绩查找信息"); gotoxy(10,14); printf("3:添加学生信息 4:删除学生信息"); gotoxy(10,16); printf("5:学号查找信息 6:退出管理系统");
while(TRUE) { totalrec(); printf("\n当前文件的记录数是:%d",recnum); printf("\n请选择(1-6)"); scanf("%d",&n); getchar(); switch(n) { case 1:list();break; case 2:searchscore();break; case 3:creat();break; case 4:printf("本函数尚未定义!");break; case 5:searchnum();break; case 6:exit(0);break; default :printf("\n请选择1-6这几个数字!!!"); } }
}
我知道怎么不能追加了,creat函数里的文件打开方式错了,应该是fopen("C:\stud.ree","ab")以追加方式打开,然后配合 rewind(fp); fseek(fp,recnum*sizeof(node),0); 语句就可以了。 下面是正确的代码! # include "stdio.h" # include "string.h" # include "stdlib.h" # define NULL 0 # define MAX 5 # define TRUE 1
typedef struct node { char name[8]; int number; double score; }node;
static int recnum;
void creat() { char scorestr[100],ch,numstr[100]; node student; FILE *fp; if((fp=fopen("C:\stud.ree","ab"))==NULL) { printf("不能打开stud.ree!"); exit(0); } rewind(fp); fseek(fp,recnum*sizeof(node),0); do { printf("\n姓名:");gets(student.name); printf("\n学号:"); gets(numstr);student.number=atoi(numstr); printf("\n成绩:"); gets(scorestr);student.score=atof(scorestr); fwrite(&student,sizeof(node),1,fp); recnum++; printf("输入其他的学生记录:(y/n)?"); ch=getchar();getchar(); }while(ch=='y'); fclose(fp); }
void list() { node student; char scorestr[100],ch; FILE *fp; if((fp=fopen("C:\stud.ree","rb"))==NULL) {printf("不能打开stud.ree!"); return; } printf("\n下面是学生资料:\n"); printf(" 学号 姓名 成绩\n");
while(fread(&student,sizeof(node),1,fp)==1) { printf("%1d %s %6.2f\n",student.number,student.name,student.score); } fclose(fp); }
int totalrec() /*计算文件的记录数,用recnum表示总记录数*/ { node student; FILE *fp; recnum=0; if((fp=fopen("C:\stud.ree","rb"))==NULL) return 0; while(fread(&student,sizeof(node),1,fp)==1) { recnum++; } fclose(fp); return (recnum); }
searchnum() { node student; FILE *fp; int i=1; int num1; printf("\n请输入学号:"); scanf("%d",&num1); if((fp=fopen("C:\stud.ree","rb"))==NULL) {printf("不能打开stud.ree!"); return; } while(i<=recnum) { if(fread(&student,sizeof(node),1,fp)==1 && student.number==num1) { printf("\n 姓名: %s \n",student.name); printf("\n学号: %1d \n",student.number); printf("\n成绩: %6.2f\n",student.score); } i++; } fclose(fp); }
searchscore() { node student; FILE *fp; int i=1; double score1; printf("\n请输入成绩(小数点后保留2位):"); scanf("%lf",&score1); if((fp=fopen("C:\stud.ree","rb"))==NULL) {printf("不能打开stud.ree"); return; } while(i<=recnum) { if(fread(&student,sizeof(node),1,fp)==1 && student.score==score1) { printf("\n 姓名 : %s \n",student.name); printf("\n学号: %1d \n",student.number); printf("\n年龄: %6.2f\n",student.score); } i++; } fclose(fp); }
main() {int n; /*N为选择函数变量*/ clrscr(); gotoxy(10,10); printf("欢迎进入学生学籍管理系统"); gotoxy(10,12); printf("1:显示学生信息 2:成绩查找信息"); gotoxy(10,14); printf("3:添加学生信息 4:删除学生信息"); gotoxy(10,16); printf("5:学号查找信息 6:退出管理系统");
while(TRUE) { totalrec(); printf("\n当前文件的记录数是:%d",recnum); printf("\n请选择(1-6)"); scanf("%d",&n); getchar(); switch(n) { case 1:list();break; case 2:searchscore();break; case 3:creat();break; case 4:printf("本函数尚未定义!");break; case 5:searchnum();break; case 6:exit(0);break; default :printf("\n请选择1-6这几个数字!!!"); } }
}