学生管理系统V2.0
学生管理系统V2.0
-
需求:
要求实现一个基于指针的学生成绩管理系统,具体功能如下:
- 添加学生信息:输入学号和三门成绩,存储到数组中。
- 显示所有学生信息:遍历数组,输出每个学生的学号和成绩。
- 计算每个学生的平均分和总分:遍历数组,计算每行的总分和平均分。
- 根据某科成绩排序:用户选择科目,然后按该科成绩排序,可以升序或降序。
- 查找学生信息:按学号查找,显示该生的成绩和平均分。
- 退出程序。
-
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define MAX_STUDENTS 50 // 最大学生数量
#define COURSE_NUM 3 // 课程科目数量
#define ID_LENGTH 4 // 学号长度/** 函数原型声明 **/// 添加学生信息
void addStudent(int (*scores)[COURSE_NUM], char (*ids)[ID_LENGTH], int *count);// 显示所有记录
void displayAll(int (*scores)[COURSE_NUM], char (*ids)[ID_LENGTH], int count);// 显示统计信息
void showStatistics(int (*scores)[COURSE_NUM], int count);// 学生查找
void searchStudent(int (*scores)[COURSE_NUM], char (*ids)[ID_LENGTH], int count);// 学号校验 1-校验合格,0-校验不合格
int validateId(char *id);
// 成绩排序
void scoreRanking(int (*scores)[COURSE_NUM], char (*ids)[ID_LENGTH], int count);/*** 入口函数*/
int main(int argc,char *argv[])
{int choice; // 用户菜单选择int studentCount = 0; // 当前学生的数量// 学生数据存储(二维数组)char studentIds[MAX_STUDENTS][ID_LENGTH]; // 学号数组int scores[MAX_STUDENTS][COURSE_NUM]; // 成绩二维数组do{// 系统菜单界面printf("\033[1;32m+-----------------------+\033[0m\n"); // 绿色边框printf("\033[1;32m| \033[1;33m学生成绩管理系统 v2.0\033[1;32m |\033[0m\n"); printf("\033[1;32m| \033[1;33m作者:AAA \033[1;32m|\033[0m\n"); printf("\033[1;32m+-----------------------+\033[0m\n");printf("\033[31m1. 添加学生信息\033[0m\n");printf("\033[31m2. 显示所有记录\033[0m\n");printf("\033[31m3. 查看统计信息\033[0m\n");printf("\033[31m4. 成绩排序\033[0m\n");printf("\033[31m5. 查找学生\033[0m\n");printf("\033[31m6. 退出系统\033[0m\n");printf("\n请输入您的选择: ");scanf("%d",&choice);// 选择菜单switch(choice){case 1: // 添加学生信息addStudent(scores, studentIds, &studentCount);break;case 2: // 显示所有记录displayAll(scores, studentIds, studentCount);break;case 3: // 查看统计信息showStatistics(scores, studentCount);break;case 4: // 成绩排序scoreRanking(scores, studentIds, studentCount);break;case 5: // 查找学生searchStudent(scores, studentIds, studentCount);break;case 6: // 退出系统printf("系统已退出,感谢使用!\n");break;default:printf("无效输入,请重新选择!\n");}}while(choice != 6);return 0;
}/*** 添加学生信息* @param scores 学生成绩二维数组* @param ids 学生编号二维数组* @param count 当前学生的数量*/
void addStudent(int (*scores)[COURSE_NUM], char (*ids)[ID_LENGTH], int *count)
{// 校验存储空间是否已满if(*count >= MAX_STUDENTS){printf("错误信息,存储空间已满!\n");return; // 执行return,函数提前结束}printf("\n———— 添加学生信息 ————\n");// 创建一个数组,用来存放学生学号char tempId[ID_LENGTH + 1]; // 控制台录入的字符串默认\0结尾// 学号验证do{printf("请输入4位学号:");scanf("%4s", tempId); // 从控制台输入的字符串中,截取前4位// 清空缓冲区while(getchar() != '\n'); }while(!validateId(tempId));// 检查学号是否已存在for(int i = 0; i < *count; i++){// 使用内存比较,比较两个内存块中的数据是否相等if(memcmp(ids[i], tempId, ID_LENGTH) == 0){printf("错误信息:该学号已存在!\n");return;}}// 存储学号memcpy(ids[*count], tempId, ID_LENGTH);// 输入成绩printf("请输入%d门课程成绩(0~100):\n", COURSE_NUM);for(int i = 0; i < COURSE_NUM;){printf("课程%d:", i + 1);// 非法字符校验int tempScore = scanf("%d", &scores[*count][i]);if(tempScore != 1){printf("错误心虚:成绩无效,请重新输入!\n");while(getchar() != '\n');continue;}// 输入范围校验if(scores[*count][i] < 0 || scores[*count][i] > 100){printf("错误信息:成绩无效,请重新输入!\n");continue;}i++;}// 改变studentCount的值(*count)++;printf("学生信息添加成功!\n");
}/**
* 显示所有记录
* @param scores 学生成绩二维数组
* @param ids 学生编号二维数组
* @param count 当前学生的数量
*/
void displayAll(int (*scores)[COURSE_NUM], char (*ids)[ID_LENGTH], int count)
{printf("\n—————— 学生成绩列表 ——————\n");// 校验是否存在数据if(count == 0){printf("暂无学生数据!\n");return;}// 表格 - 表头printf("%s\t%s\t%s\t%s\n","学号","语文","数学","英语");// 表格 - 数据for(int i = 0; i < count; i++){printf("%.4s\t", ids[i]); // 学号// 成绩二维数组for(int j = 0; j < COURSE_NUM; j++){printf("%d\t", *(*(scores + i) + j)); // scores[i][j]}printf("\n");}printf("\n");
}/*** 显示统计信息* @param scores 学生成绩二维数组* @param count 当前学生的数量 */
void showStatistics(int (*scores)[COURSE_NUM], int count)
{// 检验是否存在学生if(count == 0){printf("暂无学生数据!\n");return;}// 创建一个数组,用来存储每一科分总分int courseTotal[COURSE_NUM] = {0};// 创建一个数组,用来存储每一科最高分int max[COURSE_NUM] = {0};// 创建一个数组,用来存储每一科最低分int min[COURSE_NUM] = {100,100,100};// 遍历成绩,计算每一科总分,最高分,最低分for(int i = 0; i < count; i++) // 遍历得到每一个学生{for(int j = 0; j < COURSE_NUM; j++) // 遍历得到每个学生的每个科目成绩{// 获取成绩int score = scores[i][j];// 单科总分courseTotal[j] += score;// 单科最高分if(score > max[j]) max[j] = score;// 单科最低分if(score < min[j]) min[j] = score;}}// 输出信息printf("\n—————— 课程统计信息 ——————\n");char *courses[] = {"语文","数学","英语"};for(int i = 0; i < COURSE_NUM; i++){printf("%s:\n",courses[i]); // 科目printf(" 平均分:%.2f\n", (float)courseTotal[i] / count);printf(" 最高分:%d\n", max[i]);printf(" 最低分:%d\n", min[i]);}
}/*** 成绩排序* @param scores 学生成绩二维数组* @param ids 学生编号二维数组* @param count 当前学生的数量*/
void scoreRanking(int (*scores)[COURSE_NUM], char (*ids)[ID_LENGTH], int count)
{// 检验是否存在学生if(count == 0){printf("暂无学生数据!\n");return;}// 创建一个数组,用来存储每个人总分int courseTotal[MAX_STUDENTS] = {0};// 创建一个数组,用来当做成绩的副本int scores_copy[MAX_STUDENTS][COURSE_NUM];for(int i = 0; i < count; i++) {for(int j = 0; j < COURSE_NUM; j++){scores_copy[i][j] = scores[i][j];}}// 创建一个数组,用来当做学号的副本char ids_copy[MAX_STUDENTS][ID_LENGTH];for(int i = 0; i < count; i++){memcpy(ids_copy[i], ids[i], ID_LENGTH);}// 遍历成绩,计算出每个学生的总分for(int i = 0; i < count; i++) // 遍历得到每一个学生{for(int j = 0; j < COURSE_NUM; j++) // 遍历的到每个人的每科成绩{// 求出每人的总分courseTotal[i] += scores_copy[i][j];}}// 开始冒泡排序for (int i = 0; i < count; i++){for(int j = 0; j < count; j++){if (courseTotal[j] < courseTotal[j+1]){// 交换总分int tempTotal = courseTotal[j];courseTotal[j] = courseTotal[j+1];courseTotal[j+1] = tempTotal;// 交换成绩for (int k = 0; k < COURSE_NUM; k++) {int tempScore = scores_copy[j][k];scores_copy[j][k] = scores_copy[j + 1][k];scores_copy[j + 1][k] = tempScore;}// 交换学号char tempId[ID_LENGTH];memcpy(tempId, ids_copy[j], ID_LENGTH);memcpy(ids_copy[j], ids_copy[j + 1], ID_LENGTH);memcpy(ids_copy[j + 1], tempId, ID_LENGTH);}}}// 输出排序结果printf("\n—————— 成绩排序结果 ——————\n");printf("%s\t%s\t%s\t%s\t%s\t%s\n", "排名", "学号", "语文", "数学", "英语", "总分");for (int i = 0; i < count; i++) {printf("%d\t%.4s\t", i + 1, ids_copy[i]);for (int j = 0; j < COURSE_NUM; j++) {printf("%d\t", scores_copy[i][j]);}printf("%d\n", courseTotal[i]);}printf("\n");}/*** 学生查找* @param scores 学生成绩二维数组* @param ids 学生编号二维数组* @param count 当前学生的数量*/
void searchStudent(int (*scores)[COURSE_NUM], char (*ids)[ID_LENGTH], int count)
{// 创建一个数组,用来接收控制台输入的学号char targetId[ID_LENGTH + 1];printf("\n请输入要查找的学号:");scanf("%4s", targetId);while(getchar() != '\n');for(int i = 0; i < count; i++){//比较学号if(memcmp(ids[i], targetId, ID_LENGTH) == 0){printf("找到学生记录:\n");printf("学号:%.4s\n", ids[i]);printf("成绩:语文=%d 数学=%d 英语=%d\n", scores[i][0], scores[i][1],scores[i][2]);return;}}printf("未找到该学号的学生记录:\n");
}/*** 学号检验* @param id 检验的学号(数组)* @return 1-校验合格, 0-校验不合格*/
int validateId(char *id)
{char *p = id; // 不要直接操作形参,最好使用局部变量接收int len = 0;// 校验学号是否为数字while (*p && len < ID_LENGTH){// 校验输入的是否是数字if(!(*p >= '0' && *p <= '9')){printf("学号必须为数字!\n");return 0;}p++;len++;}// 校验输入的长度if (len != ID_LENGTH || *p != '\0'){printf("学号必须为4位!\n");return 0;}return 1;
}
运行结果