日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

班级学生成绩管理系统——C/C++实现

發布時間:2024/1/18 c/c++ 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 班级学生成绩管理系统——C/C++实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

  • 班級學生成績管理系統——C/C++實現
    • 一、功能概述
    • 二、功能詳細說明
    • 三、代碼實現
    • 四、總結
      • 1. BUGS & QUESTIONS & ANSWERS
      • 2. SUMMARY

班級學生成績管理系統——C/C++實現

  • 項目功能基本完成,且有諸多改進,項目未完成的一兩個小BUG都標注在文章末尾了,總體沒有問題。?
  • 項目沒怎么用C++的東西,就一個string類和c++的輸入輸出,還有個new和函數傳引用。🤦🏻?♂?
  • 項目只能算是初級初級初級程序設計,沒有數據結構,沒有數據庫,沒有可視化便捷操控界面,沒有設計模式。(啥也不是😂)
  • 希望能對入門程序猿們有一些幫助,一起 stronger。💪🏻

一、功能概述

使用 C 語言完成該系統的開發,通過該系統可實現對某班級(例如 B175A5 班)學生成績信息的添加、查詢、修改、刪除、統計分析,以及數據保存、讀取等功能。



二、功能詳細說明

  • 學生成績信息錄入
    • 添加學生的成績信息到系統中,主要包括學號、姓名、各科成績。其中,學號的格式是:“班號+2 位數字”;成績要求在 0~100 之間。若輸入數據違法,則進行相應提
      示。
    • 判斷輸入的學號是否存在,若不存在,則添加到系統;否則,提示學號已存在,不
      進行添加。
    • 添加完成后,提示是否繼續添加。如果不繼續添加,則返回主菜單。
  • 學生成績信息查詢
    • 系統提供三種查詢方式,按學號,按姓名,以及顯示所有學生成績信息。
    • 如果查找成功,顯示該學生的成績信息;否則,提示未找到。
  • 學生成績信息修改
    • 系統提供兩種方式進行修改,可按姓名或學號來進行。
    • 修改時,先按照提供的信息進行查找,若查找成功,則可輸入該學生新的成績信息;否則,提示該學生不存在。
  • 學生成績信息刪除
    • 系統只提供按照學生的學號進行刪除。
    • 刪除時,首先輸入學生的學號,若查找到該生的信息,則將其從系統中刪除;否則,提示提示該學生不存在。
  • 學生成績信息統計
    • 系統提供了選擇成績統計和成績排名兩種操作。
    • 選擇成績統計時,先提示對哪門課(英語、數學、計算機)進行統計,然后提示統計方式??山y計最高分、最低分、平均分、排名、不及格的人數、優秀的人數,選擇后顯示相應的統計結果。
    • 選擇成績排序時,先提示對哪門課(英語、數學、計算機)進行成績排序,選擇后顯示相應排序結果。
  • 文件操作
    • 系統中所有的學生成績系統都以二進制形式保存在文件中。


  • 三、代碼實現

    #include <iostream> using namespace std; // #include <stdlib.h>#define MAX 50 #define TEMPSIZE 100 // 從二進制文件提取數據的中間變量 #define CODE "作者是個大帥比" // 管理員密碼/*** 1. 存儲結構*/ typedef struct {// char number[100]; // 學號 char類型數據只能直接輸入/用字符串函數,不能賦值// char name[100]; // 姓名// char sex[10]; // 性別// enum sex {男, 女}; // 性別string number; // 學號string name; // 姓名int sex; // 性別: 1--男,2--女int age; // 年齡float score[3]; // 成績 * 3 } StudentInfo; typedef struct {StudentInfo stu_array[MAX]; // 學生信息表int stu_size; // 當前學生數量 } StudentList;/*** 2. 操作函數*/ // 顯示菜單 void ShowMenu(); // 初始化學生表 bool InitStudentList(StudentList &); // 判斷學生表是否為空 bool IsEmpty(StudentList); // 添加學生信息 bool AddStudentList(StudentList &); // 顯示學生信息 bool ShowStudentList(StudentList); // 查詢學生信息 int SearchStudentListByNumber(StudentList, string); int SearchStudentListByName(StudentList, string); bool FindStudentListByIndex(StudentList, int); void FindStudentList(StudentList); // 刪除學生信息 bool DeleteStudentListByIndex(StudentList &, int); bool DeleteStudentList(StudentList &); // 修改學生信息 bool ModifyStudentListByIndex(StudentList &, int); bool ModifyStudentList(StudentList &); // 清空學生信息 bool ClearStudentList(StudentList &); // 文件讀寫操作 bool ClearStudentInfoInTxt(); bool ReadStudentInfoFromTxt(StudentList &); bool WriteStudentListToTxt(StudentList); // 學生信息統計 // 學生成績排名 bool SortStudentList(StudentList &); // 學生成績統計 bool StudentInfoStatistics(StudentList); void StudentScoreStatistics(StudentList); int CountExcellentStudents(StudentList, int); int CountFlunks(StudentList, int); float FindAverageScore(StudentList, int); float FindLowestScore(StudentList, int); float FindHighestScore(StudentList, int);/*** 2.1 菜單界面*/ void ShowMenu() {cout << "***** 歡迎來到學生信息管理系統 *****" << endl;cout << "**** 1. 添加學生信息 ****" << endl;cout << "**** 2. 顯示學生信息 ****" << endl;cout << "**** 3. 刪除學生信息 ****" << endl;cout << "**** 4. 查詢學生信息 ****" << endl;cout << "**** 5. 修改學生信息 ****" << endl;cout << "**** 6. 統計學生信息 ****" << endl;cout << "**** 7. 清空學生信息 ****" << endl;cout << "**** 8. 保存學生信息到文件 ****" << endl;// cout << "**** 9. 從文件讀取學生信息 ****" << endl;cout << "**** 0. 退出管理系統 ****" << endl;cout << "************************************" << endl; }/*** 2.2 初始化學生信息*/ bool InitStudentList(StudentList &stu_list) {stu_list.stu_size = 0; // 初始化學生表長度為0char option = 'n';cout << "開始初始化系統……\n請選擇是否需要從指定文件讀取歷史學生數據(y/n):";cin >> option;while (true){if (option == 'Y' || option == 'y'){ReadStudentInfoFromTxt(stu_list); // 從文件讀取學生信息break;}else if (option == 'N' || option == 'n'){cout << "歡迎回來!\n";system("pause");system("cls");break;}else{cout << "輸入錯誤,請重新輸入:";cin >> option;}}return true; }// 判空 bool IsEmpty(StudentList stu_list) {if (stu_list.stu_size == 0)return true;elsereturn false; }/*** 2.3 添加學生信息*/ bool AddStudentList(StudentList &stu_list) {if (stu_list.stu_size == MAX){ // 判滿cout << "學生列表已滿,無法添加!" << endl;system("pause");system("cls");return false;}else{ // 添加具體學生信息// 學號string number;cout << "請輸入學號:" << endl;while (true){cin >> number;if (SearchStudentListByNumber(stu_list, number) == -1 || SearchStudentListByNumber(stu_list, number) == -2){stu_list.stu_array[stu_list.stu_size].number = number;break;}elsecout << "學號重復,請輸入新學號:";}// 姓名string name;cout << "請輸入姓名:" << endl;cin >> name;stu_list.stu_array[stu_list.stu_size].name = name; // 利用stu_size結構體元素,在當前數組尾插入學生信息// 性別cout << "請輸入性別:" << endl;cout << "1 -- 男" << endl;cout << "2 -- 女" << endl;int sex = 0;while (true){// 簡單的輸入合法性判斷cin >> sex;if (sex == 1 || sex == 2){stu_list.stu_array[stu_list.stu_size].sex = sex;break;}cout << "輸入有誤,請重新輸入:" << endl;}// 年齡cout << "請輸入年齡:" << endl;int age = 0;while (true){// 簡單的輸入合法性判斷cin >> age;if (age > 0 && age < 150){stu_list.stu_array[stu_list.stu_size].age = age;break;}cout << "輸入有誤,請重新輸入:" << endl;}// 成績cout << "請輸入成績:" << endl;float score = 0.0;for (int i = 0; i < 3; ++i){while (true){// 簡單的輸入合法性判斷cin >> score;if (score >= 0.0 && score <= 100.0){stu_list.stu_array[stu_list.stu_size].score[i] = score;break;}cout << "輸入有誤,請重新輸入:" << endl;}}stu_list.stu_size++;cout << "學生信息添加成功!" << endl;system("pause");system("cls");return true;} }/*** 2.4 顯示學生信息*/ bool ShowStudentList(StudentList stu_list) {// if (stu_list.stu_size == 0)if (IsEmpty(stu_list)){cout << "學生列表目前為空!" << endl;system("pause");system("cls");return false;}else{cout << "學號\t\t"<< "姓名\t\t"<< "性別\t\t"<< "年齡\t\t"<< "成績1\t成績2\t成績3" << endl;for (int i = 0; i < stu_list.stu_size; ++i){cout << stu_list.stu_array[i].number << "\t\t";cout << stu_list.stu_array[i].name << "\t\t";cout << (stu_list.stu_array[i].sex == 1 ? "男" : "女") << "\t\t";cout << stu_list.stu_array[i].age << "\t\t";for (int j = 0; j < 3; ++j)cout << stu_list.stu_array[i].score[j] << "\t";cout << endl;}}system("pause");system("cls");return true; }/*** 2.5 查詢學生信息*/ int SearchStudentListByNumber(StudentList stu_list, string number) {if (IsEmpty(stu_list))return -2; // 列表為空返回 -2 (-2,-1,>0對應 3 種情況,-2最小優先級最高。)else{for (int i = 0; i < stu_list.stu_size; ++i)if (stu_list.stu_array[i].number == number)return i;return -1; // 查詢不到學生信息返回 -1} }int SearchStudentListByName(StudentList stu_list, string name) {if (IsEmpty(stu_list))return -2;else{for (int i = 0; i < stu_list.stu_size; ++i)if (stu_list.stu_array[i].name == name)return i;return -1;} }// 根據查詢到的index位序,對學生信息進行查找 bool FindStudentListByIndex(StudentList stu_list, int index) {if (index == -2){cout << "當前學生列表為空!" << endl;system("pause");system("cls");return false;}else if (index == -1){cout << "查無此人!" << endl;system("pause");system("cls");return false;}else{cout << "查找成功,學生信息如下:" << endl;cout << "學號\t\t"<< "姓名\t\t"<< "性別\t\t"<< "年齡\t\t"<< "成績\t\t" << endl;cout << stu_list.stu_array[index].number << "\t\t";cout << stu_list.stu_array[index].name << "\t\t";cout << (stu_list.stu_array[index].sex == 1 ? "男" : "女") << "\t\t";cout << stu_list.stu_array[index].age << "\t\t";for (int j = 0; j < 3; ++j)cout << stu_list.stu_array[index].score[j] << "\t";cout << endl;system("pause");system("cls");return true;} }// 查找學生信息總函數,設計查找操作的總邏輯以及對內部小函數進行調用 void FindStudentList(StudentList stu_list) {// cout << "通過輸入 學生學號 查找請按 1,通過輸入 學生姓名 查找請按 2,按其他鍵取消操作:" << endl;cout << "請選擇操作選項:\n1. 通過學生學號進行查詢\n2. 通過學生姓名進行查詢\n3. 按其他鍵取消操作\n";int select;cin >> select;while (cin.fail()){// cin輸入異常處理cin.clear();cin.ignore();cout << "輸入錯誤,請重新輸入:";cin >> select;}switch (select){case 1:{string number;cout << "請輸入待查找學生的學號:" << endl;cin >> number;int index = SearchStudentListByNumber(stu_list, number); // 查詢FindStudentListByIndex(stu_list, index);break;}case 2:{string name;cout << "請輸入待查找學生的姓名:" << endl;cin >> name;int index = SearchStudentListByName(stu_list, name); // 查詢FindStudentListByIndex(stu_list, index);break;}default:{cout << "操作取消成功!" << endl;system("pause");system("cls");return;break;}} }/*** 2.6 刪除學生信息*/ // 根據查詢到的index位序,對學生信息進行刪除 bool DeleteStudentListByIndex(StudentList &stu_list, int index) {if (index == -2){cout << "當前學生列表為空!" << endl;system("pause");system("cls");return false;}else if (index == -1){cout << "查無此人!" << endl;system("pause");system("cls");return false;}else{for (int i = index; i < stu_list.stu_size; ++i)stu_list.stu_array[i] = stu_list.stu_array[i + 1];stu_list.stu_size--;cout << "刪除成功!" << endl;system("pause");system("cls");return true;} }// 刪除學生信息總函數,設計刪除操作的總邏輯以及對內部小函數進行調用 bool DeleteStudentList(StudentList &stu_list) {// cout << "通過輸入 學生學號 刪除請按 1,通過輸入 學生姓名 刪除請按 2,按其他鍵取消操作:" << endl;cout << "請選擇操作選項:\n1. 通過學生學號進行查詢并刪除\n2. 通過學生姓名進行查詢并刪除\n3. 按其他鍵取消操作\n";int select;cin >> select;while (cin.fail()){ // cin輸入異常處理cin.clear();cin.ignore();cout << "輸入錯誤,請重新輸入:";cin >> select;}switch (select){case 1:{string number;cout << "請輸入待刪除學生的學號:" << endl;cin >> number;int index = SearchStudentListByNumber(stu_list, number); // 查詢DeleteStudentListByIndex(stu_list, index);break;}case 2:{string name;cout << "請輸入待刪除學生的姓名:" << endl;cin >> name;int index = SearchStudentListByName(stu_list, name);DeleteStudentListByIndex(stu_list, index);break;}default:{cout << "操作取消成功!" << endl;system("pause");system("cls");return false;break;}} }/*** 2.7 修改學生信息*/ // 根據查詢到的index位序,對學生信息進行修改 bool ModifyStudentListByIndex(StudentList &stu_list, int index) {if (index == -2){cout << "當前學生列表為空!" << endl;system("pause");system("cls");return false;}else if (index == -1){ // ==可不能寫成=,那就會為 真 條件,必執行此分支cout << "查無此人!" << endl;system("pause");system("cls");return false;}else{cout << "查找成功!" << endl;cout << "請輸入想要修改的信息項:\n1. 學號\n2. 姓名\n3. 性別\n4. 年齡\n5. 成績\n"<< endl;int select2;cin >> select2;switch (select2){case 1:{string number;cout << "請輸入新學號:" << endl;while (true){cin >> number;if (SearchStudentListByNumber(stu_list, number) == -1){ // 修改則該生必存在,前面的分支已經確定。stu_list.stu_array[index].number = number;break;}elsecout << "學號重復,請輸入新學號:";}break;}case 2:{string name;cout << "請輸入新姓名:" << endl;cin >> name;stu_list.stu_array[index].name = name;break;}case 3:{cout << "請輸入新性別:" << endl;cout << "1 -- 男" << endl;cout << "2 -- 女" << endl;int sex = 0;while (true){ // 簡單的輸入合法性判斷cin >> sex;if (sex == 1 || sex == 2){stu_list.stu_array[index].sex = sex;break;}cout << "輸入有誤,請重新輸入:" << endl;}break;}case 4:{cout << "請輸入新年齡:" << endl;int age = 0;while (true){ // 簡單的輸入合法性判斷cin >> age;if (age > 0 && age < 150){stu_list.stu_array[index].age = age;break;}cout << "輸入有誤,請重新輸入:" << endl;}break;}case 5:{cout << "請輸入新成績:" << endl;int score = 0;for (int i = 0; i < 3; ++i){while (true){ // 簡單的輸入合法性判斷cin >> score;if (score >= 0 && score <= 100){stu_list.stu_array[index].score[i] = score;break;}cout << "輸入有誤,請重新輸入:" << endl;}}break;}default:break;}cout << "修改成功!" << endl;system("pause");system("cls");return true;} }// 修改學生信息總函數,設計修改操作的總邏輯以及對內部小函數進行調用 bool ModifyStudentList(StudentList &stu_list) {// cout << "通過輸入 學生學號 修改請按 1,通過輸入 學生姓名 修改請按 2,按其他鍵取消操作:" << endl;cout << "請選擇操作選項:\n1. 通過學生學號進行查詢并修改\n2. 通過學生姓名進行查詢并修改\n3. 按其他鍵取消操作\n";int select;cin >> select;switch (select){case 1:{string number;cout << "請輸入待修改學生的學號:" << endl;cin >> number;int index = SearchStudentListByNumber(stu_list, number); // 查詢ModifyStudentListByIndex(stu_list, index);break;}case 2:{string name;cout << "請輸入待修改學生的姓名:" << endl;cin >> name;int index = SearchStudentListByName(stu_list, name); // 查詢ModifyStudentListByIndex(stu_list, index);break;}default:{cout << "操作取消成功!" << endl;system("pause");system("cls");return false;break;}}return true; }/*** 2.8 根據學習成績排序*/ bool SortStudentList(StudentList &stu_list) {if (stu_list.stu_size == 0){cout << "學生列表為空!" << endl;system("pause");system("cls");return false;}int n;cout << "請選擇數據項進行排序:\n1. 成績1——C語言成績\n2. 成績2——數學成績\n3. 成績3——英語成績\n";cin >> n; // 排序依據項,減1后映射到數組StudentInfo temp; // 中間變量for (int i = 0; i < stu_list.stu_size - 1; ++i){ // 冒泡排序bool flag = false;for (int j = 0; j < stu_list.stu_size - 1 - i; ++j)if (stu_list.stu_array[j].score[n - 1] > stu_list.stu_array[j + 1].score[n - 1]){temp = stu_list.stu_array[j];stu_list.stu_array[j] = stu_list.stu_array[j + 1];stu_list.stu_array[j + 1] = temp;flag = true;}if (flag == false) // 本躺遍歷后沒有進行交換,說明表已經有序,直接退出break;}cout << "排序后的學生信息:" << endl;ShowStudentList(stu_list);// system("pause"); // ShowStudentList()有系統暫停了// system("cls");return true; }/*** 2.9 清空學生信息*/ bool ClearStudentList(StudentList &stu_list) {if (stu_list.stu_size == 0){cout << "學生列表已為空!" << endl;system("pause");system("cls");return false;}cout << "請再次確認是否清空所有學生信息?(y/n)" << endl;char input;while (true){cin >> input;switch (tolower(input)){case 'y':{cout << "請輸入管理員密碼:";string code;while (true){cin >> code;if (code == CODE){stu_list.stu_size = 0; // 核心語句cout << "成功清空所有學生信息!" << endl;system("pause");system("cls");return true;}else if (code == "Q"){cout << "取消操作成功!" << endl;system("pause");system("cls");return true;}else{cout << "密碼輸入錯誤!請重新輸入(輸入 Q 取消本次操作。):";}}break;}case 'n':{cout << "取消清空操作成功!" << endl;system("pause");system("cls");return false;}default:{cout << "請輸入字母 y 或者 n:";break;}}}/*if (input == "yes" || input == "YES") {cout << "請輸入管理員密碼:";string code;while (true) {cin >> code;if (code == CODE) {stu_list.stu_size == 0;cout << "成功清空所有學生信息!" << endl;system("pause");system("cls");return true;} else {cout << "密碼輸入錯誤!請重新輸入:" << endl;}}} else {cout << "取消清空操作成功!" << endl;system("pause");system("cls");return false;}*/ }/*** 2.10 文件讀寫*/// 從文件讀取學生信息 bool ReadStudentInfoFromTxt(StudentList &stu_list) {char txt_path[50];cout << "請輸入文件路徑:";cin >> txt_path; // 輸入文件地址FILE *fp;// StudentInfo *p = (StudentInfo *)malloc(sizeof(StudentInfo) * TEMPSIZE); // c方式,為指針分配空間// StudentList stu2;// StudentInfo *p = stu2.stu_array;StudentInfo *p = new StudentInfo[TEMPSIZE]; // c++方式動態分配內存if ((fp = fopen(txt_path, "rt")) == NULL){printf("Error on open \"%s\" file!\n", txt_path);system("pause");system("cls");return false;}while (true){// 判滿if (stu_list.stu_size >= MAX){cout << "學生列表已滿,無法繼續添加!" << endl;system("pause");system("cls");return false;}// 讀出fread(p, sizeof(StudentInfo), 1, fp);if (!feof(fp)){// 打印cout << "學號\t\t"<< "姓名\t\t"<< "性別\t\t"<< "年齡\t\t"<< "成績1\t成績2\t成績3\n";cout << p->number << "\t\t";cout << p->name << "\t\t";cout << (p->sex == 1 ? "男" : "女") << "\t\t";cout << p->age << "\t\t";for (int j = 0; j < 3; ++j)cout << p->score[j] << "\t";cout << endl;// 保存stu_list.stu_array[stu_list.stu_size].number = p->number;stu_list.stu_array[stu_list.stu_size].name = p->name;stu_list.stu_array[stu_list.stu_size].sex = p->sex;stu_list.stu_array[stu_list.stu_size].age = p->age;for (int j = 0; j < 3; ++j)stu_list.stu_array[stu_list.stu_size].score[j] = p->score[j];stu_list.stu_size++; // 學生表長+1p++; // 中間變量指針后移}else{fclose(fp);cout << "讀取完畢!" << endl;system("pause");system("cls");return true;}} }// 保存學生信息到文件 bool WriteStudentListToTxt(StudentList stu_list) {if (IsEmpty(stu_list)){cout << "學生列表目前為空!無需保存。" << endl;system("pause");system("cls");return false;}else{char txt_path[50];cout << "請輸入文件路徑:";cin >> txt_path; // 輸入文件地址FILE *fp;StudentInfo *p;p = stu_list.stu_array; // 令指針指向學生表if ((fp = fopen(txt_path, "wt")) == NULL){ // 以 寫模式、二進制類型 打開文件printf("Error on open \"%s\" file!", txt_path);system("pause");system("cls");return false;}// for (int i = 0; i < stu_list.stu_size; ++i)// fwrite(p, sizeof(StudentInfo), 1, fp);fwrite(p, sizeof(StudentInfo), stu_list.stu_size, fp); // 寫入學生信息fclose(fp); // 關閉文件cout << "保存成功!" << endl;system("pause");system("cls");return true;} }// 清空文件所有學生信息 bool ClearStudentInfoInTxt() {char txt_path[50];cout << "請確認是否繼續清空文件中所有學生信息?(y/n)" << endl;char input;while (true){cin >> input;switch (tolower(input)){case 'y':{cout << "請輸入管理員密碼:";string code;while (true){cin >> code;if (code == CODE){cout << "請輸入文件路徑:";cin >> txt_path; // 輸入文件地址FILE *fp;if ((fp = fopen(txt_path, "wb")) == NULL){ // 以 wb模式 打開文件,已存在則刪除新建,不存在則建立。printf("Error on open \"%s\" file!", txt_path);system("pause");system("cls");return false;}fclose(fp);cout << "成功清空文件所有學生信息!" << endl;system("pause");system("cls");return true;}else if (code == "Q"){cout << "取消操作成功!" << endl;system("pause");system("cls");return true;}else{cout << "密碼輸入錯誤!請重新輸入(輸入 Q 取消本次操作。):";}}break;}case 'n':{cout << "取消清空操作成功!" << endl;system("pause");system("cls");return false;}default:{cout << "請輸入字母 y 或者 n:";break;}}} }/*** 2.11 學生成績統計*/ // 求第 i 門成績最高分 float FindHighestScore(StudentList stu_list, int i) {float max = stu_list.stu_array[0].score[i - 1];for (int j = 1; j < stu_list.stu_size; ++j)if (stu_list.stu_array[j].score[i - 1] > max)max = stu_list.stu_array[j].score[i - 1];return max; }// 求第 i 門成績最低分 float FindLowestScore(StudentList stu_list, int i) {float min = stu_list.stu_array[0].score[i - 1];for (int j = 1; j < stu_list.stu_size; ++j)if (stu_list.stu_array[j].score[i - 1] < min)min = stu_list.stu_array[j].score[i - 1];return min; }// 求第 i 門成績平均分 float FindAverageScore(StudentList stu_list, int i) {float avg = 0.0;for (int j = 0; j < stu_list.stu_size; ++j)avg += stu_list.stu_array[j].score[i - 1];return avg / stu_list.stu_size; }// 求不及格人數 int CountFlunks(StudentList stu_list, int i) {int count = 0;for (int j = 0; j < stu_list.stu_size; ++j)if (stu_list.stu_array[j].score[i - 1] < 60)count++;return count; }// 求優秀人數 int CountExcellentStudents(StudentList stu_list, int i) {int count = 0;for (int j = 0; j < stu_list.stu_size; ++j)if (stu_list.stu_array[j].score[i - 1] > 80)count++;return count; }// 成績統計 void StudentScoreStatistics(StudentList stu_list) {int select = 0, index = 0;cout << "成績統計開始……\n請選擇統計依據項:\n1. C語言成績\n2. 數學成績\n3. 英語成績\n";cin >> index;while (1){cout << "請選擇統計內容:\n1. 最高分\n2. 最低分\n3. 平均分\n4. 不及格人數\n5. 優秀人數\n0. 取消操作返回上級菜單\n";cin >> select;switch (select){case 1:cout << "該科目成績的最高分: " << FindHighestScore(stu_list, index) << endl;system("pause");system("cls");break;case 2:cout << "該科目成績的最低分: " << FindLowestScore(stu_list, index) << endl;system("pause");system("cls");break;case 3:cout << "該科目成績的平均分: " << FindAverageScore(stu_list, index) << endl;system("pause");system("cls");break;case 4:cout << "該科目成績不合格人數: " << CountFlunks(stu_list, index) << endl;system("pause");system("cls");break;case 5:cout << "該科目成績優秀人數: " << CountExcellentStudents(stu_list, index) << endl;system("pause");system("cls");CountExcellentStudents(stu_list, index);break;case 0:cout << "取消成功!!!" << endl;system("pause");system("cls");return;default:{cout << "輸入錯誤!請重新輸入!\n";break;}}} }// 學生信息統計總函數 bool StudentInfoStatistics(StudentList stu_list) {int select = 0;// cout << "學生信息統計開始……\n請選擇統計選項:\n1. 成績統計\n2. 成績排名\n";while (1){cout << "學生信息統計開始……\n請選擇統計選項:\n1. 成績統計\n2. 成績排名\n0. 取消操作返回主菜單\n";cin >> select;switch (select){case 1:StudentScoreStatistics(stu_list);break;case 2:SortStudentList(stu_list);break;case 0:cout << "取消成功!!!" << endl;system("pause");system("cls");return false;default:cout << "輸入錯誤!請重新輸入:";break;}}return true; }/*** 3. 主函數*/ int main() {StudentList stus_list; // 定義全局結構體變量InitStudentList(stus_list); // 初始化int select = 0;while (true){ // 死循環,只要里面設置return就行。ShowMenu();cout << "請輸入操作選項:";cin >> select;while (cin.fail()){ // cin輸入異常處理cin.clear();cin.ignore();cout << "輸入錯誤,請重新輸入:";cin >> select;}switch (select){case 1: // 1. 添加學生信息AddStudentList(stus_list);break;case 2: // 2. 顯示學生信息ShowStudentList(stus_list);break;case 3: // 3. 刪除學生信息DeleteStudentList(stus_list);break;case 4: // 4. 查詢學生信息FindStudentList(stus_list);break;case 5: // 5. 修改學生信息ModifyStudentList(stus_list);break;case 6: // 6. 統計學生信息StudentInfoStatistics(stus_list);break;case 7: // 7. 清空學生信息ClearStudentList(stus_list);ClearStudentInfoInTxt();break;case 8: // 8. 保存學生信息到文件WriteStudentListToTxt(stus_list);break;case 9: // 9. 從文件讀取學生信息ReadStudentInfoFromTxt(stus_list);break;case 0:{// 0. 退出管理系統cout << "歡迎下次使用!" << endl;exit(0);// return 0;}default:cout << "輸入錯誤,請重新輸入!" << endl;break;}}return 0; }/** 文件名后綴為 .cpp* 用 VSCode 編輯代碼的話,需要把文件改為以 GBK 模式保存打開,(下載個 Code Runner 插件會很舒服)*/



    四、總結

    1. BUGS & QUESTIONS & ANSWERS

  • 設置不能輸入重復學號number?
    • if (SearchStudentListByNumber(stu_list, number) == -1) {...}
  • int型數據輸入其他值報錯?
    • try catch
    • if(cin.fail())
  • 排序?
    • 把冒泡排序、快速排序等排序算法的ElemType換一下就行。
  • 怎么輸出格式整齊?(\t的話,某數據超過其數據類型長度,\t就不整齊了。)
    • 用 printf 進行格式化輸出。
  • 修改操作出錯,總改不了?
    • stu_list.stu_array[stu_list.stu_size].name = name; 改為 stu_list.stu_array[index]...
  • 查詢、刪除、修改時,名字重復的學生只能查出來第一個被查到的。
    • ???
  • 輸入浮點數成績直接報錯?
    • 中間變量初始化 score 的時候類型定義錯了。定義為 int 了。
  • 從文件讀取信息保存,找不到合適的解決方案?
    • 用fwrite、fread+feof
    • 用 指針變量 存儲信息,需要提前分配好空間!!!
      struct StudentInfo *p = (struct StudentInfo *)malloc(sizeof(struct StudentInfo) * TEMPSIZE);
    • 明明正常運行,學生表卻沒有改變! ==> 函數參數要加引用!!!
    • 總是多讀取一行?解決 feof() 判斷文件末尾多讀一次的方法
      • 先 if(!feof(fp)) 再 fread() 改為 先fread()再if
    • 從文件讀取學生信息可能與新輸入過的信息學號沖突

      因為只有通過了 添加學生 函數的限制條件,才能保存,所以文件內的數據都是正常合法的,只有 學號信息的唯一性 無法保證。

      • 法1:進入系統強制從文件讀取信息刷新學生表,然后再利用添加學生信息函數的判重去限制 ?
      • 法2:從文件讀取增加學號重復判斷。
    • 讀取的數據,其他信息都正常,學號、姓名是空的?而且同一次程序中讀取學生數據就沒問題,保存后退出程序,下次運行就讀不到 學號和名字。
      • (是string的問題,還是編碼問題?)???
  • 該系統主要用c語言,但是輸入輸出用了c++的cin\cout,數據類型也用了c++的string,以及函數的傳引用。對C語言學者不太好。
    • 但是,cin/cout 是真的好用!!不需要自己填寫類型。
    • string也是,比字符數組好用多了。
    • 函數傳引用,這樣修改形參實現對實參變量的修改,不用傳引用的話,只能用 指針。
  • 你能在開關中使用 OR關系 嗎?——case 'Y'||'y': ...
    • 不能!!!最好是用 tolower() 轉變為小寫,再進行 case。

  • 2. SUMMARY

  • while(true)+if&break 即可實現簡單的循環合法性判斷,用 while+switch 也可。
    該系統中我用了多種結構進行嘗試。不僅是 語法關鍵字選擇,還有輸入語句 cin 位置的區別(循環內/循環外)
  • 通過形參改變變量值 方法:
    • (1) 指針
      AddStudentList (StudentList* stu_list)+傳地址&
    • (2) 傳引用
      AddStudentList (StudentList &stu_list)
  • 學生存儲結構,對于名字學號這一類的信息,可以用
    • (1) char數組 + strcmp等字符串函數 + %s (char類型數據只能直接輸入/用字符串函數,不能直接賦值)
    • (2) string + 可直接用字符串賦值 + cin直接輸入 等便捷操作
  • 存儲結構只是結構體數組也可以,但是多一個表示當前長度的 length 屬性,會方便很多。比如清空的時候,直接讓 length=0 即可。
  • 經常重復用的代碼塊可以封裝為函數。如 xxxByIndex() 函數
  • 設置合適的提示和暫停、清屏 cout、system("pause")、system("cls")
  • 賦值語句 = 千萬不要寫成 ==
  • 想設置輸入判斷,錯誤重新輸入,就往外套 while(true),里面添個 return;無限套都可
  • 往任何地方(變量/文件)保存,就必須檢測是否已經存在。
  • break,return,exit
    • break只能中斷switch/循環,return能中斷一個函數,exit中斷一個程序。
  • 總結邏輯
    • (1) 添加學生信息

      • 判滿
      • 在表尾插入新元素
      • 當前表大小+1
      • while(true)+if,break+SearchStudentListByNumber 實現學號查重,輸入合法性判斷等
      • 輸出注意:cout << (stu_list.stu_array[i].sex == 1 ? "男" : "女");
    • (2) 顯示學生信息

      • 判空
      • 顯示學生性別信息需要用三目運算符進行簡單轉換顯示
    • (3) 刪除學生信息

      • 查詢到學生信息 SearchStudentListByNumber/SearchStudentListByName
      • 判空
      • 將 待刪學生信息 后的信息往前移一位,覆蓋 待刪學生信息
      • 表長度-1
    • (4) 查詢學生信息

      • 順序查找到學生位序 SearchStudentListByNumber/SearchStudentListByName
      • 根據返回位序信息進行相關信息顯示
    • (5) 修改學生信息

      • 查詢到學生信息,返回 index
      • 對 stu_array[index] 進行修改。直接賦值新值即可。
    • (6) 清空學生信息

      • 直接令 stu_size = 0 即可
    • (7) 排序學生信息

      • 用 冒泡排序/快速排序等排序算法 的邏輯,對學生表進行排序。

        以 學生表元素的成績元素 為大小判斷依據,以 學生表元素 為中間變量進行位置交換。

    • (0) 退出管理系統

      • exit(0)
  • 該系統也沒用到什么復雜的數據結構,就是最簡單的順序表。



    希望對大家有所幫助和啟發~
    一鍵三連!!!😎😎😎

    總結

    以上是生活随笔為你收集整理的班级学生成绩管理系统——C/C++实现的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。