學生信息管理系統包括了輸入、查詢、修改、刪除等操作。
代碼:
#define MAX 1000//最大存儲人數 //個人信息 typedef struct Student { char num[10];//編號 char name[15];//姓名 int chinese;//語文 int english;//英語 int math;//數學 int physical;//物理 int chemical;//化學 int biology;//生物 int Lgrade;//平時成績 int Fgrade;//期末成績 float ave;//總評成績 }Stu; typedef struct GradeManage { Stu stu[MAX ]; int count; }Tele, * ptele; void initiate(ptele ptele);//啓動 void Add(ptele ptele);//輸入學生人數,最多輸入1000個 void Search(ptele ptxl);//查找學生人數,按姓名查找 void Display(ptele ptxl);//顯示學生通訊錄中所有人 void Modify(ptele ptele);//修改學生某一人的信息 void Delete(ptele ptxl);//刪除某一人的信息 void Save(ptele ptxl);//保存學生信息到文件中 void menu();//菜單 void initiate(ptele ptele) { ptele->count = 0; } static int hanshu(ptele ptele, const char * name) { int i = 0; for (i = 0; i < ptele ->count; i++) { if (strcmp(name , ptele->stu[i].name) == 0) { return i; } } return -1; } void Add(ptele ptele) { if (ptele ->count == MAX) { printf( "錄入學生已滿!\n" ); return; } cout<< "學號:"; cin>> ptele->stu[ptele ->count].num; cout << "姓名:"; cin>> ptele->stu[ptele ->count].name; cout << "語文:"; cin >> ptele->stu[ptele ->count].chinese; cout << "英語:"; cin >> ptele->stu[ptele ->count].english; cout << "數學:"; cin >> ptele->stu[ptele ->count].math; cout << "物理:"; cin >> ptele->stu[ptele ->count].physical; cout << "化學:"; cin >> ptele->stu[ptele ->count].chemical; cout << "生物:"; cin >> ptele->stu[ptele ->count].biology; cout << "平時成績:" ; cin >> ptele->stu[ptele ->count].Lgrade; cout << "期末成績:" ; cin >> ptele->stu[ptele ->count].Fgrade; ptele->stu[ptele ->count].ave = (ptele->stu[ptele ->count].Lgrade*0.3) + (ptele->stu[ptele ->count].Fgrade*0.7); ptele->count++; printf( "錄入成功!\n" ); } void Search(ptele ptxl) { int ret = 0; char name[10]; cout<< "請輸入要查詢學生的姓名:" ; cin>>name; ret = hanshu( ptxl, name); if (ret == -1) { cout << "沒有要找的人!" << endl; return; } else { ptxl->stu[ptxl ->count].ave = (ptxl->stu[ptxl ->count].Lgrade*0.3) + (ptxl->stu[ptxl ->count].Fgrade*0.7); cout << "學號\t姓名 語文 英語 數學 物理 化學 生物 平時成績 期末成績 總評成績" << endl; cout << ptxl->stu[ret].num << " " << ptxl->stu[ret].name << " " << ptxl->stu[ret].chinese << " " << ptxl ->stu[ret].english << " " << ptxl->stu[ret].math << " " << ptxl->stu[ret].physical << " " << ptxl ->stu[ret].chemical << " " << ptxl->stu[ret].biology << " " << ptxl->stu[ret].Lgrade << " " << ptxl->stu[ret].Fgrade << " " << ptxl->stu[ret].ave <<endl; } } void Modify(ptele ptele) { int ret = 0; char name[10]; cout<< "請輸入要修改人的姓名:" ; cin>>name; ret = hanshu( ptele, name); if (ret == -1) { cout << "不存在要修改的人!" << endl; return; } else { cout << "學號:"; cin >> ptele->stu[ret].num; cout << "姓名:"; cin >> ptele->stu[ret].name; cout << "語文:"; cin >> ptele->stu[ret].chinese; cout << "英語:"; cin >> ptele->stu[ret].english; cout << "數學:"; cin >> ptele->stu[ret].math; cout << "物理:"; cin >> ptele->stu[ret].physical; cout << "化學:"; cin >> ptele->stu[ret].chemical; cout << "生物:"; cin >> ptele->stu[ret].biology; cout << "平時成績:" ; cin >> ptele->stu[ret].Lgrade; cout << "期末成績:" ; cin >> ptele->stu[ret].Fgrade; } ptele->stu[ptele ->count].ave = (ptele->stu[ptele ->count].Lgrade*0.3) + (ptele->stu[ptele ->count].Fgrade*0.7); cout << "修改成功!" << endl; } void Delete(ptele ptxl) { int ret = 0; int j = 0; char name[10]; cout << "請輸入要刪除學生的姓名:" << endl; cin>>name; ret = hanshu( ptxl, name); if (ret == -1) { cout << "沒有要刪除的學生!" << endl; return; } else { for (j = ret; j < ptxl ->count - 1; j++) { ptxl->stu[j] = ptxl ->stu[j + 1]; } } ptxl->count--; cout << "刪除學生成功!" << endl; } void Save(ptele ptxl) { FILE* fp; int i = 0; if ((fp = fopen("manage.txt" , "ab+")) == NULL) { cout << "不能打開" << endl;; exit( EXIT_SUCCESS); } fprintf(fp, "學號\t姓名 語文 英語 數學 物理 化學 生物 平時成績 期末成績 總評成績" ); fprintf(fp, "\r\n"); for (i = 0; i < ptxl ->count; i++) { fprintf(fp, "%2s %s %d %d %d %d %d %d %d %d %f\n", ptxl->stu[i].num, ptxl ->stu[i].name, ptxl->stu[i].chinese, ptxl ->stu[i].english, ptxl->stu[i].math, ptxl ->stu[i].physical, ptxl->stu[i].chemical, ptxl ->stu[i].biology, ptxl->stu[i].Lgrade, ptxl ->stu[i].Fgrade, ptxl->stu[i].ave); fprintf(fp, "\r\n"); } fclose(fp); cout << "保存到文件中成功!" << endl; } void Display(ptele ptxl) { int i = 0; cout << "學號\t姓名 語文 英語 數學 物理 化學 生物 平時成績 期末成績 總評成績" << endl; for (i = 0; i < ptxl ->count; i++) { cout << ptxl->stu[i].num << " " << ptxl->stu[i].name << " " << ptxl->stu[i].chinese << " " << ptxl ->stu[i].english << " " << ptxl->stu[i].math << " " << ptxl->stu[i].physical << " " << ptxl ->stu[i].chemical << " " << ptxl->stu[i].biology << " " << ptxl->stu[i].Lgrade << " " << ptxl->stu[i].Fgrade << " " << ptxl->stu[i].ave << endl; } } void menu() { cout << "*******************************************************" << endl; cout << "* 學生成績管理系統 " << endl; cout << "*******************************************************" << endl; cout << "設計人:高鐸 班級:信息143 學號:201412030305" << endl; cout << "*******************************************************" << endl; cout << "* 1 錄入學生信息 " << endl; cout << "* 2 顯示學生信息 " << endl; cout << "* 3 查詢學生信息 " << endl; cout << "* 4 修改學生信息 " << endl; cout << "* 5 刪除學生信息 " << endl; cout << "* 6 保存學生信息 " << endl; cout << "* 0 退出 " << endl; cout << "*******************************************************" << endl; } #define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; #include "Studdent.h" int main() { Tele ptele; int input = 1; initiate(&ptele); system( "color a"); while (input) { menu(); cout << "\t\t請選擇自己的操作<0~5>:" ; cin >> input; switch (input) { case 1:Add(&ptele); break; case 2:Display(&ptele); break; case 3:Search(&ptele); break; case 4:Modify(&ptele); break; case 5:Delete(&ptele); break; case 6:Save(&ptele); break; case 0:exit(EXIT_SUCCESS ); break; } } return 0; }