C语言 学生信息管理系统(教务系统)——数组、文件实现

一、建立文件

首先建立一个文件夹,建立如下文件
C语言 学生信息管理系统(教务系统)——数组、文件实现_第1张图片
admin.txt 管理员账号密码文件
C语言 学生信息管理系统(教务系统)——数组、文件实现_第2张图片
copyright.txt 版权信息文件
C语言 学生信息管理系统(教务系统)——数组、文件实现_第3张图片

stu_list 学生信息文件(初始为空,从系统录入)

二、代码

/* *** 学生管理系统 *** */
#include 
#include 
#include 
#include 

#define N 100

void init();     // 函数初始化
void showCpyright();  //版权说明
void teacherLogin();    //教师登录
void studentLogin();  //学生登录
void teacherMenu();     //教师登录后的菜单
void changePassword();  //修改密码 
void addStudent();    //添加学生
void showStudent();     //显示所有学生
int findStudent(int);    //查询学生
void searchScore();   //查询成绩
void recordScore();  //记录成绩
void saveData();    //保存数据
void pressKey();    //按任意键继续的函数

char tname[10];    //教师的登录名
char tpwd[10];     //教师的登录密码

//学生成绩结构
struct Score {
	double english;
	double c;
	double java;
};

//地址结构
struct Address {
	char province[10];
	char city[10];
};

//学生结构
struct Student {
	int sno;
	char sname[10];
	int age;
	struct Score scores;
	struct Address address;
}; 

struct Student students[N];  //学生数组
int index = 0;  //学生人数

//主函数
int main() {
	char ch;
	int flag = 1;

	init();
	while (flag) {
		system("cls");  //清屏
		printf("\n=============学生信息管理系统==============\n");
		printf("1.管理员登录\n");
		printf("2.学生登录\n");
	    printf("3.版权\n");
		printf("0.退出系统\n");
		printf("\n==============================================\n");
		printf("请输人您的选项:\n");
		scanf("%c",&ch);
		fflush( stdin );  //清空输入缓冲区
		switch( ch ){
			case '1':
				teacherLogin();  //教师登录
				break;
			case '2':
				studentLogin();  //学生登录
				break;
			case '3':
				showCpyright();  //版权说明
				break;
			case '0':
				flag=0;
				break;
			default: 
				printf("查无此选项,请确认后输入...\n");
				system("pause");
		}
	}
	printf("\n系统安全退出\n");
	return 0;
}

//版权说明
void showCpyright(){
	char buffer[100] = {0};
	FILE* fp = NULL;
	fp = fopen("copyright.txt", "r");
	 while(1){
		fgets(buffer,100,fp);
		if(feof(fp)){    //防止多读一次
			printf("%s\n",buffer);
			break;
		}
	}
	printf("\n");
	fclose(fp);  //关闭文件指针
}

//读取数据
void init() {
	FILE* fp;
	if ((fp = fopen("admin.txt", "r")) == NULL) {
		printf("\n没有管理员密码文件,系统错误!\n");
		exit(1);
	}
	fscanf(fp, "%s %s", tname, tpwd);  //读取管理员账号、密码
	fclose(fp);  //关闭文件指针
	if ((fp = fopen("stu_list", "rb")) != NULL) {
		//     fread( students,sizeof( struct Student),1,fp )  为什么错误 (students为第一个学生的存储地址,数据的读取始终赋值给第一个学生)
		while (fread(&students[index], sizeof(struct Student), 1, fp) > 0) {  //读取学生信息并统计人数
			index ++;
		}
		printf("\n初始化完成\n");
	}
}

//学生英语成绩排序
void sort_english_scores(){
	int i;
	int j;
	struct Student temp;
	for(i = 0; i < index-1; i++){
		for(j = 0; j < index-1-i; j++){
			if(students[j].scores.english < students[j+1].scores.english){
				temp = students[j+1];
				students[j+1] = students[j];
				students[j] = temp;
			}
		}
	}
}

//学生C语言成绩排序
void sort_c_scores(){
	int i;
	int j;
	struct Student temp;
	for(i = 0; i < index-1; i++){
		for(j = 0; j < index-1-i; j++){
			if(students[j].scores.c < students[j+1].scores.c){
				temp = students[j+1];
				students[j+1] = students[j];
				students[j] = temp;
			}
		}
	}
}

//学生java语言成绩排序
void sort_java_scores(){
	int i;
	int j;
	struct Student temp;
	for(i = 0; i < index-1; i++){
		for(j = 0; j < index-1-i; j++){
			if(students[j].scores.java < students[j+1].scores.java){
				temp = students[j+1];
				students[j+1] = students[j];
				students[j] = temp;
			}
		}
	}
}

//修改管理员密码 
void changePassword(){
	FILE* fp;
	char newPassword[10];
	printf("输入新的密码:");
	scanf("%s",newPassword);
	strcpy(tpwd, newPassword);
	if((fp=fopen("tpwd.txt","wt"))==NULL){ 
		printf("无法打开要保存的文件!"); 
		pressKey();
		return;
	}
	fwrite(tname,sizeof(char),strlen(tname),fp);
	fwrite("\r\n",sizeof(char),1,fp);
	fwrite(newPassword,sizeof(char),strlen(newPassword),fp);  //保存密码
	fclose(fp);
	printf("修改成功!\n");
	system("pause");
}

//查找学生
int findStudent(int sno) { 
	int i;
	for (i = 0; i < index; i++) {
		struct Student s = students[i];
		if (s.sno == sno) {
			return i;
		}
	}
	return -1;
}

//查询成绩
void searchScore() {
	char ch;
	int sno; 
	char sname[10]; 
	int index; 
	system("cls");
	printf("======学生成绩查询========\n");
	printf("1.根据学号查询\n"); 
	printf("2.根据学生姓名查询\n"); 
	printf("================\n");
	printf("请输入选项\n"); 
	scanf("%c", &ch); 
	fflush(stdin); 
	if (ch != '1' && ch != '2') {
		printf("没有这个选项\n");
		pressKey();
		return; 
	} 
	if (ch == '1') { 
		printf("请输入待查学号:");
		scanf("%d", &sno); 
		index = findStudent(sno); 
	}
	else if (ch == '2') {
		printf("请输入待查学生姓名:");
		scanf("%s", sname);
	} 
	if(  index<0  ){ 
		printf("查查此学生.....\n"); 
	}
	else{ 
		struct Student s= students[index];
		printf("学号\t姓名\t英语\tC\tJAVA\n"); 
		printf("%d\t%s\t%5.2lf\t%5.2lf\t%5.2lf\n",s.sno,s.sname,s.scores.english,s.scores.c,s.scores.java);
	} 
	system("pause");
}

//成绩统计
void scoresStatistics(){
	int i;
	double avg_english = 0;
	double avg_c = 0;
	double avg_java = 0;
	printf("\n英语成绩\n");
	sort_english_scores();
	for( i = 0; i < index; i++){
		avg_english += students[i].scores.english / index;
	}
	printf("平均分:%6.2lf\n", avg_english);
	printf("最高分:%6.2lf\n", students[0].scores.english);
	printf("最低分:%6.2lf\n", students[index-1].scores.english);
	if( index % 2 != 0 ){
		printf("中位数:%6.2lf\n", students[index/2+1].scores.english);
	}
	else{
		printf("中位数:%6.2lf\n", (students[index/2].scores.english + students[index/2+1].scores.english)/2);
	}
	printf("TOP5:");
	for(i = 0; i < 5; i++){
		printf(" %6.2lf ",students[i].scores.english);
	}
	printf("\n\nC语言成绩\n");
	sort_c_scores();
	for( i = 0; i < index; i++){
		avg_c += students[i].scores.c / index;
	}
	printf("平均分:%6.2lf\n", avg_c);
	printf("最高分:%6.2lf\n", students[0].scores.c);
	printf("最低分:%6.2lf\n", students[index-1].scores.c);
	if( index % 2 == 0 ){
		printf("中位数:%6.2lf\n", students[index/2+1].scores.c);
	}
	else{
		printf("中位数:%6.2lf\n", (students[index/2].scores.c + students[index/2+1].scores.c)/2);
	}
	printf("TOP5:");
	for(i = 0; i < 5; i++){
		printf(" %6.2lf ",students[i].scores.c);
	}
	printf("\n\njava成绩\n");
	sort_java_scores();
	for( i = 0; i < index; i++){
		avg_java += students[i].scores.java / index;
	}
	printf("平均分:%6.2lf\n", avg_java);
	printf("最高分:%6.2lf\n", students[0].scores.java);
	printf("最低分:%6.2lf\n", students[index-1].scores.java);
	if( index % 2 == 0 ){
		printf("中位数:%6.2lf\n", students[index/2+1].scores.java);
	}
	else{
		printf("中位数:%6.2lf\n", (students[index/2].scores.java + students[index/2+1].scores.java)/2);
	}
	printf("TOP5:");
	for(i = 0; i < 5; i++){
		printf(" %6.2lf ",students[i].scores.java);
	}
}

//教师菜单
void teacherMenu() {
	char ch;   //选项
	int flag = 1;
	do {
		system("cls"); //调用系统命令清屏
		printf("\n=============教务系统-    后台==============\n");
		printf("1.学生注册\n");
		printf("2.学生查询\n");
		printf("3.成绩录入\n");
		printf("4.成绩查询\n");
		printf("5.修改管理员密码\n");         //TODO: 管理员密码修改,并保存
		printf("6.成绩统计\n");       //TODO: 分类输出 各科成绩的平均分,最高分,最低分,中位数,  排名TOP5
		printf("0.退出管理端\n");
		printf("\n==============================================\n");
		printf("请输入您的选项:\n");
		scanf("%c", &ch);
		fflush(stdin);
		switch (ch) {
		case '1':
			addStudent();
			break;
		case '2':
			showStudent();
			break;
		case '3':
			recordScore();
			break;
		case '4':
			searchScore();
			break;
		case '5':
			changePassword();
			break;
		case '6':
			scoresStatistics();
			break;
		case '0':
			flag = 0;
			break;
		default:
			printf("无此选项,请确认后输入...\n");

		}
		pressKey();
	} while (flag);
		saveData();   //退出时保存数据
}

//录入成绩
void recordScore() {
	int sno;
	int site;
	char flag = 'y';
	do {
		system("cls");
		printf("请输入学号:");
		scanf("%d", &sno);
		site = findStudent(sno);
		if (site == -1) {
			printf("查无此学生:%d.\n", sno);
			return; 
		} 
		printf("请输入英语成绩:");
		scanf("%lf", &students[site].scores.english);
		printf("请输入c成绩:");
		scanf("%lf", &students[site].scores.c);
		printf("请输入java成绩:"); 
		scanf("%lf", &students[site].scores.java); 
		fflush(stdin);
		printf("继续录入吗?(y or yes)"); 
		scanf("%c", &flag);
	}while (flag == 'y' || flag == 'Y'); 
	saveData();
}

//保存录入学生数据
void saveData() {
	FILE* fp; 
	if (index <= 0) { 
		return; 
	} 
	if ((fp = fopen("stu_list", "wb")) == NULL) {
		printf("无法打开要保存的文件!"); 
		system("pause");
		return; 
	}
	fwrite(students, sizeof(struct Student), index, fp);
	fclose(fp); //TODO:     保存密码到  tpwd.txt  
}

//显示所有学生
void showStudent() {
	int i = 1;
	system("cls");
	printf("\n=========系统中所有的学生信息=========\n");   //TODO: 分屏显示    
	printf("总共有%d个学生\n",index);
	if(  index>0){   
		printf("学号\t姓名\t年龄\t省\t市\t英语\tC语言\tjava\n"); 
	}
	else{
		printf("\n系统中暂无学生数据。。。\n"); 
	} 
	for( i=0;i<index;i++){ 
		struct Student s=students[i];    //从数组中取出下标为i的元素
		printf("%d\t%s\t%d",s.sno,  s.sname, s.age );
		printf("\t%s\t%s",   s.address.province,   s.address.city  ); 
		printf("\t%5.1lf\t%5.1lf\t%5.1lf",s.scores.english, s.scores.c,s.scores.java ); 
		printf("\n");
	}    
}

//添加学生
void addStudent() {
	int i = 0; 
	time_t t;
	int newsno;
	char flag = 'y'; 
	system("cls"); 
	if (index >= N) { 
		printf("存储空间不够,请联系管理员处理。。。\n");   
		return;
	} 
	do {
		printf("\n注册第%d个学生\n", i);  //随机生成学号
		srand( (unsigned)time(&t) ); 
		newsno = rand() % 10000+1;   //局部变量
		//存到students数组中下标为index的元素,struct Student类型 的元素,放到这个元素的 sno成员 
		students[index].sno = newsno;
		printf("\n请输入学生姓名:");
		scanf("%s",students[index].sname   );      //  sname是数组名,是数组的首地址 
		printf("\n请输入学生年龄:"); 
		scanf("%d",&students[index].age);   //age是基本型 数据,要取地址 
		fflush(stdin); 
		i++; index++;   //表示数组的下标 
		printf("\n还要继续录入吗?(Y or N)");
		scanf("%c", &flag);
		fflush(stdin);
	} while (flag == 'y' || flag == 'Y');
	saveData();
	printf("\n本次总共新录入%d个学生,系统保存%d个学生\n", i, index);
}

//管理员登录
void teacherLogin() {
	char ch;
	char inputName[10];
	char inputPwd[10];
	int i;
	int j;
	for (i = 0; i < 3; i++) {
		system("cls");
		printf("请输入管理员用户名:");
		scanf("%s", inputName);     //数组名就是数组的首地址
		printf("请输入管理员密码:");
		for(j =0; (ch = getch()) != '\r' && j < 10; j++){   //TODO: 以  *  来代替密码的输入
			printf("*");
			inputPwd[j] = ch;
		}
		inputPwd[j] = '\0';
		fflush(stdin);
		if (strcmp(tname,inputName) == 0 && strcmp(tpwd, inputPwd) == 0) {
			printf("\n%s登录成功....\n", inputName);
			teacherMenu();
			return;
		}
		else {
			printf("\n%s登录失败,用户名或密码错误.\n", inputName);
			printf("%s",inputPwd);
			printf("您还有%d次机会,且行且珍惜.\n", 3-i-1);
		}
	}
}

//学生登录
void studentLogin(){   //TODO:学生登录
	char ch;   //选项
	int flag = 1;
	int flags = 0;  //学生学号姓名匹配标志
	double avgScores = 0;
	double sunScores1 = 0;
	double sumScores2 = 0;
	struct Student temp;
	char newProvince[10];
	char newCity[10];
	int inputSno = 0;
	char inputSname[10];
	int i;
	int j;
	int k;
	system("cls");
	printf("请输入学生学号:");
	scanf("%d", &inputSno);
	printf("请输入学生姓名:");
	scanf("%s", inputSname);     //数组名就是数组的首地址
	fflush(stdin);
	for(i = 0; i < index; i++){
		if ( students[i].sno == inputSno && strcmp(students[i].sname, inputSname) == 0){
			flags = 1;  //匹配成功
			break;
		}
	}
	if(flags == 0){
		printf("\n登录失败,学号或姓名错误.\n");
	}
	else {
		printf("\n%s登录成功....\n", inputSname);
		do {        
			system("cls");  //调用系统命令清屏
			printf("\n=============教务系统-  学生==============\n");
			printf("1.查看成绩\n");
			printf("2.查看名次\n");
			printf("3.查看C课程的平均分,最高分和最低分及自己的成绩名次\n");
			printf("4.修改地址\n");
			printf("0.退出\n");
			printf("\n==============================================\n");
			printf("请输入您的选项:\n");
			scanf("%c", &ch);
			fflush(stdin);
			switch (ch) {
				case '1':
					printf("英语成绩 C语言成绩 java成绩\n");
					printf("%6.1lf %6.1lf %6.1lf\n",students[i].scores.english,students[i].scores.c,students[i].scores.java);
					break;
				case '2':
					//总成绩排序
					for(k = 0; k < index-1; k++){
						for(j = 0; j < index-1-i; j++){
							sunScores1 = students[j].scores.english + students[j].scores.c + students[j].scores.java;
							sumScores2 = students[j+1].scores.english + students[j+1].scores.c + students[j+1].scores.java;
							if(sunScores1 < sumScores2){
								temp = students[j+1];
								students[j+1] = students[j];
								students[j] = temp;
							}
						}
					}
					printf("排名    姓名      总成绩\n");
					for(j = 0; j < index; j++){
						if(students[j].sno == students[i].sno){
							SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);  //高亮显示
							printf(" %d   %d  %s  %6.1lf \n", j+1,
								students[j].sno, 
								students[j].sname, 
								students[j].scores.english + students[j].scores.c + students[j].scores.java 
							);
							SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
						}
						else{
							printf(" %d   %d  %s  %6.1lf \n", j+1,
								students[j].sno, 
								students[j].sname, 
								students[j].scores.english + students[j].scores.c + students[j].scores.java 
							);
						}
					}
					break;
				case '3':
					sort_c_scores();
					for(j = 0; j < index; j++){
						avgScores += students[j].scores.c / index;
					}
					printf("C语言");
					printf("平均分:%6.1lf\n",avgScores);
					printf("最高分:%6.1lf\n",students[0].scores.c);
					printf("最低分:%6.1lf\n",students[index-1].scores.c);
					for(j = 0; j < index; j++){
						if(students[j].scores.c == students[i].scores.c){
							printf("名次:%d\n",j+1);  //输出名次
							break;
						}
					}
					break;
				case '4':
					printf("输入新的province:\n");
					scanf("%s",newProvince);
					strcpy(students[i].address.province, newProvince);
					printf("输入新的city:\n");
					scanf("%s",newCity);
					strcpy(students[i].address.city, newCity);
					printf("修改成功!");
					saveData();
					break;
				case '0':
					flag = 0;
					break;
				default:
					printf("无此选项,请确认后输入...\n");
					system("pause");
			}
		}while (flag);
		saveData();   //退出时保存数据
	}
}

void pressKey(){
	char ch;
	printf("按任意键继续...");
	scanf("%c",ch);
}

你可能感兴趣的:(C语言 学生信息管理系统(教务系统)——数组、文件实现)