学生信息管理系统 c

学生信息管理系统分成四个模块: 管理员, 教师, 学生, 退出系统

每个模块都需要实现不同的功能.

列如教师模块实现了: 输入学生信息, 显示学生信息, 删除学生信息, 修改学生信息, 插入学生信息

学生模块实现了:单科成绩查询, 总成绩查询

//main.cpp 模块

#include<iostream>
#include"taaer.h"
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

using namespace std;

#define MAX_COUNT 1000

#define RECORDER_PER_PAGE 10

struct student {
	int num; //学号
	char name[16];
	char professional[16]; //专业
	int cLang; //C 语言
	int algo; //计算材料学
	int database; //原子物理
	int electricity; // 电动力学
};

struct student stu[MAX_COUNT];
int currentCount = 0;


void init() {  //初始化
	char cmd[128];
	//errno_t  err;

	//mode con lines=%d cols=%d ,这段代码控制控制台的大小
	sprintf_s(cmd, "mode con lines=%d cols=%d",  WIN_HEIGHT, WIN_WIDTH);
	system(cmd); //cmd 输入到 控制台中
	system("color 1f");

	memset(stu, 0, sizeof(stu));   //清空数组

	//FILE* fp = NULL;
	//err =  fopen_s(&fp, "acc.txt", "rb");
	FILE* fp = fopen("acc.txt", "rb");
	if (fp  == NULL) {
		//printf("文件不存在!\n");
		currentCount = 0;
		return;
	}
	int i = 0;
	while (!feof(fp)) {  //检测流上的文件结束符 ,文件未结束就继续执行
		int ret = fread(&stu[i],sizeof(struct student), 1, fp);  //从文件中读取数据

		if (ret == 1) {   //读取成功
			i++;
		}
	}
	currentCount = i;
}

void waitConfirm() {
	rewind(stdin); // rewind,清空缓存区
	getch(); //即从控制台读取一个字符,但不显示在屏幕上
}

void menu() {  //菜单界面
	system("cls");   //清空控制台

	printTableHead(MENU_WIDTH);
	printTableMidInfo(MENU_WIDTH, "学生信息管理系统");
	printTableMidInfo(MENU_WIDTH, "");

	const char* subMenus[] = {
	"1.管理员   ",
	"2.教师     ",
	"3.学生     ",
	"0.退出系统 "
	};
	int count = sizeof(subMenus) / sizeof(subMenus[0]);

	for (int i = 0; i < count; i++) {
		printTableMidInfo(MENU_WIDTH, subMenus[i]);
	}

	printTableMidInfo(MENU_WIDTH, "");
	printTableTail(MENU_WIDTH);
	printMidInfo("请选择(0-3): ");

}


struct student inputInfo() {
	struct student s;

	rewind(stdin); //清空输入缓存区
	printf("学号:");
	scanf_s("%d", &s.num);
	rewind(stdin);
	printf("姓名:");
	scanf_s("%s", s.name, 16);
	rewind(stdin);
	printf("专业:");
	scanf_s("%s", s.professional,16);
	rewind(stdin);
	printf("C语言:");
	scanf_s("%d", &s.cLang);
	rewind(stdin);
	printf("计算材料学:");
	scanf_s("%d", &s.algo);
	rewind(stdin);
	printf("原子物理:");
	scanf_s("%d", &s.database);
	rewind(stdin);
	printf("电动力学:");
	scanf_s("%d", &s.electricity);
	rewind(stdin);
	//s.sum = s.cLang + s.algo + s.database + s.electricity;

	return s;
}

int searchStu(int snum) {  //判断学号是否存在
	for (int i = 0; i < currentCount; i++) {
		if (stu[i].num == snum) {
			return i;
		}
	}
	return -1;
}

bool save() {  //信息输入文件
	FILE* fp = fopen("acc.txt", "wb");
	if (fp == NULL) {
		fclose(fp);   //关闭流
		return false;
	}
	for (int i = 0; i < currentCount; i++) {
		if (fwrite(&stu[i], sizeof(struct student), 1, fp) != 1) {
			fclose(fp);
			return false;
		}
	}
	fclose(fp);
	return true;
}


void input() {  //输入功能
	char str[16];
	struct student s;

	while (1) {
		system("cls");
		printf("输入学生信息(y/n):");
		rewind(stdin); //清空输入缓存区
		scanf_s("%s", str,16);
		if (strcmp(str, "Y") != 0 && strcmp(str, "y") != 0) {  //输入Y\y 进行输入
			break;
		}
		s = inputInfo();
		if (searchStu(s.num) >= 0) {   //判断学号是否存在
			printf("学号[%d] 已经存在!\n", s.num);
			waitConfirm();
			continue;
		}
		stu[currentCount++] = s;
		if (!save()) {
			printf("保存失败!\n");
		}
		else {
			printf("保存成功!\n");
		}
		waitConfirm();
	}
	printf("\n 结束输入!\n");
}



void Tinterface() {   //教师操作界面
	system("cls");   //清空控制台

	printTableHead(MENU_WIDTH);
	printTableMidInfo(MENU_WIDTH, "教师操作界面");
	printTableMidInfo(MENU_WIDTH, "");

	const char* Tinterfaces[] = {
	"1.输入学生信息",
	"2.显示学生信息",
	"3.删除学生信息",
	"4.修改学生信息",
	"5.插入学生信息",
	"0.退出        "
	};
	int count = sizeof(Tinterfaces) / sizeof(Tinterfaces[0]);

	for (int i = 0; i < count; i++) {
		printTableMidInfo(MENU_WIDTH, Tinterfaces[i]);
	}

	printTableMidInfo(MENU_WIDTH, "");	
	printTableTail(MENU_WIDTH);
	printMidInfo("请选择(0-5): ");

}

void Ainterface() {   //管理员操作界面
	system("cls");   //清空控制台

	printTableHead(MENU_WIDTH);
	printTableMidInfo(MENU_WIDTH, "管理员操作界面");
	printTableMidInfo(MENU_WIDTH, "");

	const char* Ainterfaces[] = {
	"1.教师身份",
	"2.学生身份",
	"0.退出    "
	};
	int count = sizeof(Ainterfaces) / sizeof(Ainterfaces[0]);

	for (int i = 0; i < count; i++) {
		printTableMidInfo(MENU_WIDTH, Ainterfaces[i]);
	}

	printTableMidInfo(MENU_WIDTH, "");
	printTableTail(MENU_WIDTH);
	printMidInfo("请选择(0-2): ");


}

void Sinterface() {   //学生操作界面
	system("cls");   //清空控制台

	printTableHead(MENU_WIDTH);
	printTableMidInfo(MENU_WIDTH, "学生操作界面");
	printTableMidInfo(MENU_WIDTH, "");

	const char* subMenus[] = {
		"1.单科成绩查询",
	    "2.总成绩查询  ",
		"0.退出        "
	};
	int count = sizeof(subMenus) / sizeof(subMenus[0]);

	for (int i = 0; i < count; i++) {
		printTableMidInfo(MENU_WIDTH, subMenus[i]);
	}

	printTableMidInfo(MENU_WIDTH, "");
	printTableTail(MENU_WIDTH);
	printMidInfo("请选择(0-3): ");


}

void insert() {   //插入学生信息
	int snum;
	system("cls");
	printf("请输入要插入的位置(学号):");
	scanf("%d", &snum);
	int destIndex = searchStu(snum);
	if (destIndex < 0) {
		printf("没有这名学生,插入位置错误!\n");
		return;
	}
	struct student t = inputInfo();
	int i = searchStu(t.num);
	if (i >= 0) {
		printf("学号[%d]已经存在! \n", t.num);
		return;
	}
	for (int j = currentCount - 1; j > destIndex; j--) {
		stu[j + 1] = stu[j];
	}
	stu[destIndex + 1] = t;
	currentCount++;
	if (save()) {
		printf("插入成功!\n");
	}
	else {
		printf("保存文件失败!\n");
	}
}

void modify()  {  //修改学生信息
	int snum;
	system("cls");

	printf("请输入要修改的学生的学号: ");
	scanf_s("%d", &snum);
	rewind(stdin);

	int i = searchStu(snum);   //判断是否有这个学号
	if (i < 0) {
		printf("没有找到这名学生!\n");
		return;
	}
	printf("找到了这名学生, 可以修改他的信息!\n");
	printf("姓名:");
	scanf("%s", stu[i].name);
	rewind(stdin);
	printf("专业:");
	scanf_s("%d", &stu[i].professional);
	rewind(stdin);
	printf("C 语言:");
	scanf_s("%d", &stu[i].cLang);
	rewind(stdin);
	printf("计算材料学:");
	scanf_s("%d", &stu[i].algo);
	rewind(stdin);
	printf("原子物理:");
	scanf_s("%d", &stu[i].database);
	rewind(stdin);
	printf("电动力学:");
	scanf_s("%d", &stu[i].professional);
	rewind(stdin);

	//stu[i].sum = stu[i].cLang + stu[i].algo + stu[i].database +  stu[i].professional;
	if (save()) {
		printf("修改成功!\n");
	}
	else {
		printf("保存文件失败!\n");
	}
}



void del() {    //删除学生功能
	FILE* fp;
	int snum = 0;
	char str[16] = "";
	system("cls");
	printf("请输入学号:");
	scanf("%d", &snum);
	int i = searchStu(snum);
	if (i < 0) {
		printf("没有找到这名学生!\n");
		return;
	}
	printf("找到这条记录,是否删除?(y/n)");
	scanf("%s", str);
	if (strcmp(str, "Y") == 0 || strcmp(str, "y") == 0) {
		for (int j = i; j < currentCount; j++) {
			stu[j] = stu[j + 1];
		}
		currentCount--;
		if (save()) {
			printf("删除成功!\n");
		}
		else {
			printf("保存文件失败!\n");
		}
	}
	else {
		printf("取消删除!\n");
	}
}



// 表头信息
char head[][COL_LEN_MAX] = { "学号", "姓名","专业","C语言","计算材料","原子物理","电动力学","总分"};

char heas[][COL_LEN_MAX] = { "学号","姓名","专业","c语言","计算材料","原子物理","电动力学"};
void showPage(int startIndex, int endIndex) {
	if (endIndex >= currentCount) {
		endIndex = currentCount - 1;
	}
	if (endIndex - startIndex + 1 > RECORDER_PER_PAGE) {
		endIndex = startIndex + RECORDER_PER_PAGE - 1;
	}
	char row[7][COL_LEN_MAX];
	system("cls");
	printTableHead(TABLE_WIDTH, 7);
	printTableRow(TABLE_WIDTH, heas, sizeof(heas) / sizeof(heas[0]));  //打印到这出了问题
	printTableMidLine(TABLE_WIDTH, 7);
	for (int i = startIndex; i <= endIndex; i++) {
		sprintf(row[0], "%d", stu[i].num);
		sprintf(row[1], "%s", stu[i].name);
		sprintf(row[2], "%s", stu[i].professional);
		sprintf(row[3], "%d", stu[i].cLang);
		sprintf(row[4], "%d", stu[i].algo);
		sprintf(row[5], "%d", stu[i].database);
		sprintf(row[6], "%d", stu[i].electricity);
		printTableRow(TABLE_WIDTH, row, 7);
		if (i < endIndex) {
			printTableMidLine(TABLE_WIDTH, 7);
		}
		else {
			printTableTail(TABLE_WIDTH, 7);
		}
	}
}


void show() {   //显示学生信息
	system("cls");

	if (currentCount == 0) {  //判断有没有学生信息
		printf("还没有学生信息!\n");
		return;
	}

	int pageCount = (currentCount + RECORDER_PER_PAGE - 1) / RECORDER_PER_PAGE;  //学生信息的页数
	char buff[64];

	for (int i = 0; i < pageCount; i++) {
		showPage(i * RECORDER_PER_PAGE, (i + 1) * RECORDER_PER_PAGE - 1);
		sprintf(buff, "共%d 页 第[%d]页", pageCount, i + 1);
		printMidInfo(buff);  //打印共%d 页 第[%d]页
		if (i < pageCount - 1) {
			waitConfirm();
		}
	}
}
/*
void order() {  //学生成绩排名
	if (currentCount == 0) {
		printf("还没有学生记录!\n");
		return;
	}

	for (int i = 0; i < currentCount - 1; i++) {
		for (int j = i + 1; j < currentCount; j++) {
			if (stu[i].sum < stu[j].sum) {
				struct student t = stu[i];
				stu[i] = stu[j];
				stu[j] = t;
			}
		}
	}
	if (!save()) {  //保存文件
		printf("排序后,保存文件失败!\n");
	}
	else {
		show();
	}
}*/

void OnlyDranch() {  //单科成绩查询
	system("cls");
	int num;

	if (currentCount == 0) {
		printf("还没有学生记录!\n");
		return;
	}

	printf("请输入要查询的学生的学号: ");
	scanf_s("%d", &num);

	int i = searchStu(num);
	if (i < 0) {
		printf("没有找到这名学生!\n");
		return;
	}

	char ro[7][COL_LEN_MAX];

	printTableHead(TABLE_WIDTH, 7);
	printTableRow(TABLE_WIDTH, heas, sizeof(heas) / sizeof(heas[0]));  
	printTableMidLine(TABLE_WIDTH, 7);

	
	sprintf(ro[0], "%d", stu[i].num);
	sprintf(ro[1], "%s", stu[i].name);
	sprintf(ro[2], "%s", stu[i].professional);
	sprintf(ro[3], "%d", stu[i].cLang);
	sprintf(ro[4], "%d", stu[i].algo);
	sprintf(ro[5], "%d", stu[i].database);
	sprintf(ro[6], "%d", stu[i].electricity);
	printTableRow(TABLE_WIDTH, ro, 7);
	
	//printTableMidLine(TABLE_WIDTH, 7);
	
	printTableTail(TABLE_WIDTH, 7);

	

}



void Teachers() {  //教师功能
	Tinterface();   //教师操作界面

	int n1;
	scanf_s("%d", &n1);
	rewind(stdin);

	while (1) {
		switch (n1){
		case 1:  //输入学生信息
			input();
			break;
		case 2:  //显示学生信息
			show(); 
			break;
		case 3: //删除学生信息
			del();
			break;
		case 4:  //修改学生信息
			modify();
			break;
		case 5:  //插入学生信息
			insert();
			break;
		default:
			return;
		}
		break;
	}

}

void administrator() {  //管理员功能
	Ainterface();   //管理员操作界面

	int n2;
	scanf_s("%d", &n2);

	while (1) {
		switch (n2) {
		case 1:  //教师身份
			printf("允许使用教师身份\n");
			printf("返回主菜单......");
		case 2:  //学生身份
			printf("允许使用学生身份\n");
			printf("返回主菜单.....");
		default:
			return;
		}
	}
}

void students() {  //学生功能
	Sinterface();   //学生操作界面

	int n3;
	scanf_s("%d", &n3);

	while (1) {
		switch (n3) {
		case 1:  //单科成绩查询
			OnlyDranch();
			break;
		case 2: //总成绩查询
			show();
			//TheTotal();
			break;
		default:
			return;
		}
		break;
	}

}


int main(void) {

	init();   //初始化  

	menu();  //菜单界面
	
	int n;
	scanf_s("%d", &n);
	rewind(stdin);

	while (1) {
		switch (n) {
		case 1:  //管理员
			administrator();
			break;
		case 2:  //教师
			//sum:
			Teachers();
			break;
		case 3:  //学生
			//menu:
			students();
			break;
		default:return 1;  //退出系统
		}

		waitConfirm();
		menu();

		rewind(stdin); // 清空输入缓冲区
		scanf_s("%d", &n);

	}

	return 0;
}
//haaer.h   模块

#pragma once

// 窗口宽度
#define WIN_WIDTH 80
// 窗口高度
#define WIN_HEIGHT 25

#define TABLE_WIDTH 78

#define MENU_WIDTH  50
#define COL_LEN_MAX 64

void printTableHead(int tableWidth);
void printTableTail(int tableWidth);
void printTableRow(int tableWidth, char cols[][COL_LEN_MAX], int n);
void printTableHead(int tableWidth, int cols);
void printTableTail(int tableWidth, int cols);
void printTableMidLine(int tableWidth, int cols);
void printTableMidInfo(int tableWidth, const char* str);
void printMidInfo(const char* str);

//taaer.cpp

#include <stdio.h>
#include <string.h>
#include "taaer.h"

void printTableHead(int tableWidth) {
	int margin = (WIN_WIDTH - tableWidth) / 2;
	for (int i = 0; i < margin; i++) printf(" ");
	printf("┌");
	for (int i = 0; i < tableWidth - 2; i++)printf("─");
	printf("┐\n");
}



void printTableTail(int tableWidth) {
	int margin = (WIN_WIDTH - tableWidth) / 2;
	for (int i = 0; i < margin; i++) printf(" ");
	printf("└");
	for (int i = 0; i < tableWidth - 2; i++)printf("─");
	printf("┘\n");
}


void printTableRow(int tableWidth, char cols[][COL_LEN_MAX], int n) {
	int margin = (WIN_WIDTH - tableWidth) / 2;
	for (int i = 0; i < margin; i++) printf(" ");

	int colWidth = (tableWidth - (n + 1)) / n;

	for (int i = 0; i < n; i++) {
		printf("│");

		int leftSpan = (colWidth - strlen(cols[i])) / 2;
		int rightSpan = colWidth - leftSpan - strlen(cols[i]);

		for (int j = 0; j < leftSpan; j++) printf(" ");
		printf("%s", cols[i]);
		for (int j = 0; j < rightSpan; j++) printf(" ");
	}

	printf("│\n");
}


void printTableHead(int tableWidth, int cols) {
	int margin = (WIN_WIDTH - tableWidth) / 2;
	int spanLen = (tableWidth - (cols + 1)) / cols;
	for (int i = 0; i < margin; i++) printf(" ");

	printf("┌");
	for (int i = 0; i < cols; i++) {
		for (int j = 0; j < spanLen; j++) {
			printf("─");
		}

		if (i < cols - 1) {
			printf("┬");
		}
		else {
			printf("┐");
		}
	}
	printf("\n");
}

void printTableTail(int tableWidth, int cols) {
	int margin = (WIN_WIDTH - tableWidth) / 2;
	int spanLen = (tableWidth - (cols + 1)) / cols;
	for (int i = 0; i < margin; i++) printf(" ");

	//└─┴─┘
	printf("└");
	for (int i = 0; i < cols; i++) {
		for (int j = 0; j < spanLen; j++) {
			printf("─");
		}

		if (i < cols - 1) {
			printf("┴");
		}
		else {
			printf("┘");
		}
	}
	printf("\n");
}

void printTableMidLine(int tableWidth, int cols) {
	int margin = (WIN_WIDTH - tableWidth) / 2;
	int spanLen = (tableWidth - (cols + 1)) / cols;
	for (int i = 0; i < margin; i++) printf(" ");

	printf("├");
	for (int i = 0; i < cols; i++) {
		for (int j = 0; j < spanLen; j++) {
			printf("─");
		}

		if (i < cols - 1) {
			printf("┼");
		}
		else {
			printf("┤");
		}
	}
	printf("\n");
}


void printTableMidInfo(int tableWidth, const char* str) {
	int margin = (WIN_WIDTH - tableWidth) / 2;
	for (int i = 0; i < margin; i++) printf(" ");
	printf("│");

	int len1 = (tableWidth - 2 - strlen(str)) / 2;
	for (int i = 0; i < len1; i++) printf(" ");

	printf("%s", str);

	int len2 = tableWidth - 2 - len1 - strlen(str);
	for (int i = 0; i < len2; i++) printf(" ");

	printf("│\n");
}

void printMidInfo(const char* str) {
	int margin = (WIN_WIDTH - strlen(str)) / 2;
	for (int i = 0; i < margin; i++) printf(" ");
	printf("%s", str);
}

注意  需要把项目属性 中   的 SDL检查   更改成 

不然会报错

如果有不明白的地方,去 哔哩哔哩->奇牛学院    

老师讲课十分容易听懂 , 常以 生活中的有趣事 讲解 难懂的技术

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值