C語言綜合案例:學生成績管理系統
需求
1.存儲最多50名學生的信息(不使用結構體)
2.每個學生包含:
- 學號(字符數組)
- 姓名(字符數組)
- 3門課程成績(一維數組)
3.實現功能菜單:
- 添加學生信息
- 顯示所有學生信息
- 計算學生平均分
- 查找最高分科目
- 退出系統
代碼
/*************************************************************************> File Name: demo.c> Author: 張扣> Description:C語言綜合案例:學生成績管理系統> Created Time: 2025年02月27日 星期四 21時10分38秒************************************************************************/#include <stdio.h>
#include <string.h>//常量定義
#define MAX_STU 50 //定義學生數量
#define NAME_LEN 20 //名字的最大長度
#define ID_LEN 8 //學號的最大長度
#define COURSE_NUM 3 //課程數量//全局數組實現數據存儲
char stu_id[MAX_STU][ID_LEN]; //學號二維數組
char stu_name[MAX_STU][NAME_LEN]; //姓名二維數組
int scores[MAX_STU][COURSE_NUM]; //成績二維數組
int stu_count = 0; //當前實際學生數/***輸入校驗成績*course 課程名稱*/
int get_valid_score(const char* course)//const修飾的變量,值不能改變
{int score;while(1){printf("請輸入%s成績(0~100):",course);//校驗:1.校驗輸入的數據格式 2.校驗成績的范圍是否正確if(scanf("%d",&score == 1 && score >= 0 && score <= 100)){return score;}//輸入有誤while(getchar() != '\n'); //清空輸入緩沖區printf("成績輸入有誤!\n");}
}/***添加學生信息*/
void add_student()
{//判斷數組是否已經存滿if(stu_count >= MAX_STU){printf("存儲已滿!\n");return;//函數返回,后續代碼不再執行}printf("\n---添加第%d個學生---\n",stu_count + 1);//輸入學號printf("請輸入學號:");scanf("%s",stu_id[stu_count]);//輸入姓名printf("請輸入姓名:");getchar();scanf("%[^\n]",stu_name[stu_count]);//%[^\n]匹配除了\n以外的輸入//輸入各科成績scores[stu_count][0] = get_valid_score("語文");scores[stu_count][1] = get_valid_score("數學");scores[stu_count][2] = get_valid_score("英語");stu_count++;
}/***顯示所有學生信息*/
void show_all()
{if(stu_count == 0){printf("暫無學生信息!\n");return;//跳出函數,后續代碼不執行}//格式化輸出信息printf("\n%-12s%-20s%-12s%-12s%-12s\n","學號","姓名","語文","數學","英語");for(int i = 0; i < stu_count; i++){//獲取學號和姓名printf("%-12s%-20s",stu_id[i],stu_name[i]);for(int j = 0; j < COURSE_NUM; j++){printf("%-12d",scores[i][j]);}printf("\n");}printf("\n");
}/***計算平均分*/
void calc_average()
{if(stu_count == 0){printf("暫無數據!\n");return;}char target_id[ID_LEN];printf("請輸入要查詢的學號:");scanf("%s",target_id);for(int i = 0; i < stu_count; i++){if(strcmp(stu_id[i],target_id) == 0){float sum = 0;for(int j = 0; j < COURSE_NUM; j++){sum += scores[i][j];}printf("平均分:%.2f\n",sum/COURSE_NUM);return;}}printf("未找到該學生!\n");
}/***查找最高分科目*/
void find_max()
{int max_score = -1;char max_course[20];char max_stu[NAME_LEN];for(int i = 0; i < stu_count; i++){for(int j = 0; j < COURSE_NUM; j++){if(scores[i][j] > max_score){max_score = scores[i][j];//最高成績對應的姓名strcpy(max_stu,stu_name[i]);switch(j){case 0:strcpy(max_course,"語文");break;case 1:strcpy(max_course,"數學");break;case 2:strcpy(max_course,"英語");break;}}}}if(max_score != -1){printf("最高分記錄:%s的%s獲得%d分\n",max_stu,max_course,max_score);}else{printf("暫無數據!\n");}
}int main(int argc,char *argv[])
{//定義一個變量,用來接收用戶的輸入int choice;while(1){//設計頭printf("\n=== 學生成績管理系統 v1.0 ===\n");printf("1.添加學生信息\n");printf("2.顯示所有學生信息\n");printf("3.查看平均分\n");printf("4.查看最高分\n");printf("5.退出系統\n");printf("請選擇操作:");//過濾非數字int result = scanf("%d",&choice);//該判斷的作用,檢測是否輸入的是整數,輸入的數據匹配scanf() == 1,不匹配scanf() != 1if(result != 1){//如果輸入的不是數字,就執行這個判斷//清空輸入緩沖區while(getchar() != '\n');printf("輸入有誤!\n");continue;}//過濾掉1~5以外的數字//校驗switch(choice){case 1://添加學生信息add_student();break;case 2://顯示所有學生信息show_all();break;case 3://查看平均分calc_average();break;case 4://查找最高分find_max();break;case 5://退出系統printf("系統已退出!\n");return 0;default:printf("無效選項!\n");}}return 0;
}