#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include<math.h> #include<io.h> #include <stdlib.h> using namespace std; //定義學生節點 typedef struct studentNode{ string number; string name; int math; int english; int chinese; struct studentNode *next; }*student; int countOfStudent; FILE * fp; void ShowAllStudentInfo(student& studentLink); //輸入學生信息 void InputStudentInfo(student& studentLink){ cout << "************歡迎進入學生信息創建菜單*************" << endl; cout << "1.重新創建" << endl; cout << "2.引用已有數據" << endl; int choice = 0; int choice1 = 0; studentLink->next = nullptr; cin >> choice; switch (choice){ case 1:{ cout << "請依次輸入學號,姓名,數學成績,英語成績,語文成績:" << endl; countOfStudent = 0; while (1){ student stu = new studentNode; countOfStudent = countOfStudent + 1; cin >> stu->number >> stu->name >> stu->math >> stu->english >> stu->chinese; cout << stu->number << stu->name << stu->math << stu->english << stu->chinese; //頭插法 stu->next = studentLink->next; studentLink->next = stu; cout << "1.繼續輸入 2.已完成" << endl; cin >> choice1; if (choice1 == 2) { //輸入完成后將學生信息寫入文件 fp = fopen("output\\studentFile.txt", "w"); fprintf(fp, "%d\n", countOfStudent); student p = studentLink->next; while (p){ fprintf(fp, "%s %s %d %d %d \n", p->number.c_str(), p->name.c_str(), p->math, p->english, p->chinese); cout << p->number << p->name << p->math << p->english << p->chinese; p = p->next; } fclose(fp); break; } else{ cout << "請依次輸入學號,姓名,數學成績,英語成績,語文成績:" << endl; } } break; } case 2: //到指定文件夾讀取數據 int exists = 0; if (!access("output\\studentFile.txt", 0)) exists = 1; char a[20]; char b[30]; if (exists) { fp = fopen("output\\studentFile.txt", "rt"); fscanf(fp, "%d", &countOfStudent); for (int i = 0; i<countOfStudent; i++) { student stu = new studentNode; fscanf(fp, "%s %s %d %d %d\n", b, a, &stu->math, &stu->english, &stu->chinese); stu->number = b; stu->name = a; //頭插法 stu->next = studentLink->next; studentLink->next = stu; } cout << "已加載完成!" << "已加載" << countOfStudent << "條數據!" << endl; //cout<<stu1.name<<stu1.number<<stu1.chinese<<stu1.math<<stu1.english<<endl; } else{ cout << "暫無本地數據,請手動創建!" << endl; } fclose(fp); break; } } //顯示所有學生信息 void ShowAllStudentInfo(student& studentLink){ cout << "************歡迎進入學生信息瀏覽菜單*************" << endl; student p = studentLink->next; cout << "學號: " << "姓名: " << "數學: " << "英語: " << "語文: " << endl; while (p){ cout << p->number << " " << p->name << " " << p->math << " " << p->english << " " << p->chinese << endl; p = p->next; } } //數據的排序,並顯示 void SortAllStudentInfo(student& studentLink){ cout << "************歡迎進入學生信息順序顯示菜單*************" << endl; int choice = 0; cout << "1 按學號升序顯示 2 按學號降序顯示 " << endl; cout << "請輸入您的選擇:" << endl; cin >> choice; switch (choice){ case 1:{ student p;//循環指示變量,用於指向無序表的當前待插入元素 student q;//q和r用於對已有有序序列進行循環遍歷 student r; student u;//臨時變量,用於控制循環 p = studentLink->next; studentLink->next = nullptr; while (p){ //每次循環開始前將指針指向最開始的地方 r = studentLink; q = studentLink->next; while ((q != nullptr) && q->number < p->number){ r = q; q = q->next; } //保存下一個需插入的點 u = p->next; //保存好了之后進行插入 p->next = r->next; r->next = p; //插入完成之后,將p指回未排序序列 p = u; } { } { fp = fopen("output\\studentAscend.txt", "w"); cout << "************學生信息升序瀏覽*************" << endl; student p = studentLink->next; cout << "學號: " << "姓名: " << "數學: " << "英語: " << "語文: " << endl; while (p){ cout << p->number << " " << p->name << " " << p->math << " " << p->english << " " << p->chinese << endl; fprintf(fp, "%s %s %d %d %d \n", p->number.c_str(), p->name.c_str(), p->math, p->english, p->chinese); p = p->next; } } fclose(fp); break; } case 2:{ student p, q, r, u; p = studentLink->next; studentLink->next = nullptr; while (p){ r = studentLink; q = studentLink->next; while ((q != nullptr) && q->number>p->number){ r = q; q = q->next; } u = p->next; p->next = r->next; r->next = p; p = u; } { fp = fopen("output\\studentDescend.txt", "w"); cout << "************學生信息降序瀏覽*************" << endl; student p = studentLink->next; cout << "學號: " << "姓名: " << "數學: " << "英語: " << "語文: " << endl; while (p){ cout << p->number << " " << p->name << " " << p->math << " " << p->english << " " << p->chinese << endl; fprintf(fp, "%s %s %d %d %d \n", p->number.c_str(), p->name.c_str(), p->math, p->english, p->chinese); p = p->next; } } fclose(fp); break; } } } //學生信息查詢 void SearchStudentInfo(student& studentLink){ cout << "************歡迎進入學生信息查詢菜單*************" << endl; while (1) { cout << "1.按照學號查詢。 2.按照姓名查詢" << endl; int choice = 0; int choice1 = 0; cin >> choice; switch (choice){ case 1: { student p = studentLink->next; cout << "1.按照學號精確查詢。 2.按照學號模糊查詢" << endl; cin >> choice1; switch (choice1){ case 1: { cout << "請輸入精確學號:" << endl; string num; cin >> num; bool flag = false; while (p){ if (p->number == num) { flag = true; cout << "學號: " << "姓名: " << "數學: " << "英語: " << "語文: " << endl; cout << p->number << " " << p->name << " " << p->math << " " << p->english << " " << p->chinese << endl; break; } p = p->next; } if (!flag) { cout << "當前學號不存在" << endl; } break; } case 2: { cout << "請輸入前綴學號:" << endl; string num; bool flag = false; cin >> num; while (p){ if (p->number.find(num, 0) == 0) { if (!flag) { cout << "學號: " << "姓名: " << "數學: " << "英語: " << "語文: " << endl; }; cout << p->number << " " << p->name << " " << p->math << " " << p->english << " " << p->chinese << endl; flag = true; } p = p->next; } if (!flag) { cout << "當前學號不存在" << endl; } break; } } break; } case 2: { student p = studentLink->next; cout << "1.按照姓名精確查詢。 2.按照姓氏模糊查詢" << endl; cin >> choice1; switch (choice1){ case 1: { cout << "請輸入精確姓名:" << endl; string name; cin >> name; bool flag = false; while (p){ if (p->name == name) { cout << "學號: " << "姓名: " << "數學: " << "英語: " << "語文: " << endl; cout << p->number << " " << p->name << " " << p->math << " " << p->english << " " << p->chinese << endl; flag = true; break; } p = p->next; } if (!flag) { cout << "當前姓名不存在" << endl; } break; } case 2: { cout << "請輸入前綴姓氏:" << endl; string name; bool flag = false; cin >> name; while (p){ if (p->name.find(name, 0) == 0) { if (!flag) { cout << "學號: " << "姓名: " << "數學: " << "英語: " << "語文: " << endl; }; cout << p->number << " " << p->name << " " << p->math << " " << p->english << " " << p->chinese << endl; flag = true; } p = p->next; } if (!flag) { cout << "當前姓氏不存在" << endl; } break; } } break; } } int choice3 = 0; cout << "1.繼續查詢。 2.查詢完成" << endl; cin >> choice3; if (choice3 == 2) { break; } } } //對學生信息進行修改 void ModifyStudentInfo(student& studentLink){ cout << "************歡迎進入學生信息修改菜單*************" << endl; fp = fopen("output\\studentFile.txt", "rt"); fscanf(fp, "%d", &countOfStudent); fclose(fp); while (1){ cout << "1.添加 2.刪除 3.修改" << endl; int choice = 0; cout << "請輸入您想要的操作:" << endl; cin >> choice; switch (choice){ case 1: { cout << "請依次輸入學號,姓名,數學成績,英語成績,語文成績:" << endl; while (1){ student stu = new studentNode; countOfStudent = countOfStudent + 1; cin >> stu->number >> stu->name >> stu->math >> stu->english >> stu->chinese; cout << stu->number << stu->name << stu->math << stu->english << stu->chinese; //頭插法 stu->next = studentLink->next; studentLink->next = stu; cout << "1.繼續輸入 2.已完成" << endl; int choice1 = 0; cout << "請輸入選擇:" << endl; cin >> choice1; if (choice1 == 2) { break; } } break; } case 2: { cout << "請輸入學生學號" << endl; string number1; cin >> number1; bool flag = false; student p = studentLink; student p1 = p->next; while (p){ if (p1) { if (p1->number == number1) { cout << "find" << endl; student p2 = p1; p->next = p2->next; free(p2); flag = true; countOfStudent--; } } p = p->next; if (p) { p1 = p->next; } } if (!flag) { cout << "無此學生!" << endl; } break; } case 3: { cout << "請輸入需修改學生的學號:" << endl; string number; cin >> number; bool flag = false; student p = studentLink->next; while (p){ if (p->number == number) { flag = true; cout << "1 修改學號 2 修改姓名 3 修改數學成績 4 修改英語成績 5 修改英語成績" << endl; int cho = 0; cin >> cho; switch (cho){ case 1:{ cout << "請輸入新的學號:" << endl; string num; cin >> num; p->number = num; break; } case 2:{ cout << "請輸入新的姓名:" << endl; string name; cin >> name; p->name = name; break; } case 3:{ cout << "請輸入新的數學成績:" << endl; int num; cin >> num; p->math = num; break; } case 4:{ cout << "請輸入新的英語成績:" << endl; int num; cin >> num; p->english = num; break; } case 5:{ cout << "請輸入新的語文成績:" << endl; int num; cin >> num; p->chinese = num; break; } } } p = p->next; } if (!flag) { cout << "該學號不存在" << endl; } break; } } cout << "1 繼續修改 2 修改完成" << endl; int choice7 = 0; cin >> choice7; if (choice7 == 2) { student p = studentLink->next; cout << "sssssss" << endl; //輸入完成后將學生信息寫入文件 fp = fopen("output\\studentFile.txt", "w"); fprintf(fp, "%d\n", countOfStudent); while (p){ fprintf(fp, "%s %s %d %d %d \n", p->number.c_str(), p->name.c_str(), p->math, p->english, p->chinese); p = p->next; } fclose(fp); break; } } } //數據統計功能 void StatisticStudentInfo(student& studentLink){ cout << "1 學生平均分及總分 2 每門課程平均分機最高分最低分" << endl; int choice = 0; cout << "請輸入您的選擇:" << endl; cin >> choice; switch (choice){ case 1: { //計算每個學生的平均分和總分 fp = fopen("output\\studentAverageScoreAndTotal.txt", "wb"); student p = studentLink->next; int avg = 0; int total = 0; cout << "學號: " << "姓名: " << "平均分: " << "總分: " << endl; while (p){ avg = (p->math + p->english + p->chinese) / 3; total = p->math + p->english + p->chinese; fprintf(fp, "%s %s %ld %ld\n", p->number.c_str(), p->name.c_str(), avg, total); cout << p->number << " " << p->name << " " << avg << " " << total << endl; p = p->next; } fclose(fp); break; } case 2: { int mathMax = 0; int mathMin = 65535; int mathAvg = 0; int englishMax = 0; int englishMin = 65535; int englishAvg = 0; int chineseMax = 0; int chinesMin = 65535; int chineseAvg = 0; int countOfM = 0; student p = studentLink->next; while (p){ //遍歷數學成績 if (p->math>mathMax){ mathMax = p->math; } if (p->math<mathMin){ mathMin = p->math; } mathAvg += p->math; if (p->english>englishMax) { englishMax = p->english; } if (p->english<englishMin) { englishMin = p->english; } englishAvg += p->english; if (p->chinese >chineseMax) { chineseMax = p->chinese; } if (p->chinese<chinesMin) { chinesMin = p->chinese; } chineseAvg += p->chinese; countOfM++; p = p->next; } fp = fopen("output\\studentMath.txt", "wb"); fprintf(fp, "數學: 最高分:%d 最低分: %d 平均分: %d\n", mathMax, mathMin, mathAvg / countOfM); cout << "數學: " << "最高分: " << mathMax << " 最低分: " << mathMin << " 平均分: " << mathAvg / countOfM << endl; fclose(fp); fp = fopen("output\\studentEglish.txt", "wb"); fprintf(fp, "英語: 最高分:%d 最低分: %d 平均分: %d\n", englishMax, englishMin, englishAvg / countOfM); cout << "英語: " << "最高分: " << englishMax << " 最低分: " << englishMax << " 平均分: " << englishAvg / countOfM << endl; fclose(fp); fp = fopen("output\\studentChinese.txt", "wb"); fprintf(fp, "語文: 最高分:%d 最低分: %d 平均分: %d\n", chineseMax, chinesMin, chineseAvg / countOfM); cout << "語文: " << "最高分: " << chineseMax << " 最低分: " << chinesMin << " 平均分: " << chineseAvg / countOfM << endl; fclose(fp); break; } } } int main(){ cout << "****************************歡迎來到學生信息管理系統***************************" << endl; cout << "1.輸入學生信息" << endl; cout << "2.查詢所有學生信息" << endl; cout << "3.排序顯示學生信息" << endl; cout << "4.查詢某一個學生具體信息" << endl; cout << "5.插入新信息,修改信息,刪除信息" << endl; cout << "6.數據統計" << endl; cout << "7.退出" << endl; int choice = 0; student studentLink = new studentNode; //studentLink->next = nullptr; while (1){ cout << "主菜單---請輸入您的選擇:"; cin >> choice; switch (choice){ case 1: InputStudentInfo(studentLink); break; case 2: ShowAllStudentInfo(studentLink); break; case 3: SortAllStudentInfo(studentLink); break; case 4: SearchStudentInfo(studentLink); break; case 5: ModifyStudentInfo(studentLink); break; case 6: StatisticStudentInfo(studentLink); break; case 7: exit(0); break; } } getchar(); return 0; }