高校學生學籍系統C++&mysql
/*
C++程序設計實踐教學環節任務書 一、題目:高校學籍管理系統 二、目的與要求 1. 目的: 1.掌握C++語言基本知識及其編程方法 2.掌握類和對象的基本概念與用法 3.掌握面向對象中的繼承與多態等面象對象特性 4.掌握文件流的基本操作 2. 要求 基本要求: (1)要求利用面向對象的方法以及C++的編程思想來完畢系統的設計; (2)要求在設計的過程中,建立清晰的類層次。 (3)在系統中至少要定義8個類,每一個類中要有各自的屬性和方法。 (4)在系統的設計中,要求使用面向對象的繼承與多態
*/
#include <iostream> #include <stdlib.h> #include <string> #include <winsock2.h> #include <mysql.h> #pragma comment(lib,"ws2_32") #pragma comment(lib,"libmysql") using namespace std; class Student { int id; char name[200]; char sex[100]; char min_zu[220]; char ji_guan[330]; char sheng_fen_zheng_hao[20]; char QQ[20]; char phone[20]; int score; public: void show_table(); int main_menu(); void input(); int xiu_gai(); int xiu_gai_menu(); int cha_xun(); int cha_xun_menu(); int del(); int shan_menu(); int score_tong_ji(); int score_tong_ji_menu(); }; class Xu_gai:public Student//改動類 { public: void xiu_gai_xue_hao(); void xiu_gai_xing_ming(); void xiu_gai_xing_bie(); void xiu_gai_min_zu(); void xiu_gai_ji_guan(); void xiu_gai_shen_fen_zheng_hao(); void xiu_gai_QQ_hao(); void xiu_gai_dian_hua(); void xiu_gai_cheng_ji(); }; class Cha_xun:public Student//查詢類 { public: void xue_hao_cha_xun(); void xing_ming_cha_xun(); }; class Shan_chu:public Student//刪除類 { public: void xue_hao_shan_chu(); void xing_ming_shan_chu(); }; class Score:public Student//成績統計類 { public: void cheng_ji_pai_xu(); void zui_gao_zui_di_fen_xin_xi(); void ping_jun_fen(); void ji_ge_lv(); }; void Score::ji_ge_lv()//全體學生的及格率 { Student zhao; system("pause"); system("cls"); zhao.show_table(); MYSQL mysql; mysql_init(&mysql); MYSQL_RES *resultset; MYSQL_ROW row; mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { printf("\n數據庫連接錯誤發生!"); } else { float a,b; mysql_query(&mysql,"select count(*) from xueji where score >= 450"); cout<<"及格人數:"; resultset=mysql_store_result(&mysql); if(mysql_num_rows(resultset)!=NULL) { int numFields = mysql_num_fields(resultset); while(row=mysql_fetch_row(resultset)) { cout<<row[0]; a = atoi(row[0]); cout<<endl; } } mysql_query(&mysql,"select count(*) from xueji"); cout<<"總人數:"; resultset=mysql_store_result(&mysql); if(mysql_num_rows(resultset)!=NULL) { int numFields = mysql_num_fields(resultset); while(row=mysql_fetch_row(resultset)) { cout<<row[0]; b = atoi(row[0]); cout<<endl; } } cout<<"及格率:"; cout << ((a/b)*100)<<"%" << endl<<endl; mysql_free_result(resultset); } } void Score::ping_jun_fen()//全體學生的平均分 { Student zhao; system("pause"); system("cls"); zhao.show_table(); MYSQL mysql; mysql_init(&mysql); MYSQL_RES *resultset; MYSQL_ROW row; mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { printf("\n數據庫連接錯誤發生!"); } else { float a,b; mysql_query(&mysql,"select avg(score) from xueji"); cout<<"平均分:"; resultset=mysql_store_result(&mysql); if(mysql_num_rows(resultset)!=NULL) { int numFields = mysql_num_fields(resultset); while(row=mysql_fetch_row(resultset)) { cout<<row[0]; a = atoi(row[0]); cout<<endl; } } mysql_free_result(resultset); } } void Score::zui_gao_zui_di_fen_xin_xi()//最高最低分學生的信息 { Student zhao; system("pause"); system("cls"); zhao.show_table(); MYSQL mysql; mysql_init(&mysql); MYSQL_RES *resultset; MYSQL_ROW row; mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { printf("\n數據庫連接錯誤發生!"); } else { float a,b; mysql_query(&mysql,"select max(score) from xueji"); cout<<"最高分:"; resultset=mysql_store_result(&mysql); if(mysql_num_rows(resultset)!=NULL) { int numFields = mysql_num_fields(resultset); while(row=mysql_fetch_row(resultset)) { cout<<row[0]; a = atoi(row[0]); cout<<endl; } } mysql_query(&mysql,"select min(score) from xueji"); cout<<"最低分:"; resultset=mysql_store_result(&mysql); if(mysql_num_rows(resultset)!=NULL) { int numFields = mysql_num_fields(resultset); while(row=mysql_fetch_row(resultset)) { cout<<row[0]; b = atoi(row[0]); cout<<endl; } } mysql_free_result(resultset); } } void Score::cheng_ji_pai_xu()//成績排序 { Student zhao; system("pause"); system("cls"); zhao.show_table(); MYSQL mysql; mysql_init(&mysql); MYSQL_RES *resultset; MYSQL_ROW row; mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { printf("\n數據庫連接錯誤發生!"); } else { cout<<"**************************************開始成績排名**************************************"<<endl; cout<<" 學號 "<<" 姓名 "<<"性別 "<<"民族 "<<"籍貫 "<<" 身份證 "<<" QQ "<<" 電話 "<<" 成績 "<<endl; cout<<"--------------------------------------------------------------------------------------"<<endl; mysql_query(&mysql,"select * from xueji order by score desc"); resultset=mysql_store_result(&mysql); if(mysql_num_rows(resultset)!=NULL) { //int numRows = mysql_num_rows(resultset); int numFields = mysql_num_fields(resultset); while(row=mysql_fetch_row(resultset)) { int i; for(i=0;i<numFields;i++) { cout<<" "<<row[i]<<" "; } cout<<endl; cout<<"--------------------------------------------------------------------------------------"<<endl; } } else { cout<<"\n無查詢結果!"; } mysql_free_result(resultset); } } void Shan_chu::xing_ming_shan_chu()//按姓名刪除 { Student zhao; system("pause"); system("cls"); zhao.show_table(); MYSQL mysql; mysql_init(&mysql); mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { printf("\n數據庫連接錯誤發生!"); } else { char std[220]; char na[200]; cout<<"請輸入要刪除學籍信息的學生姓名: "<<endl; cin>>na; sprintf(std,"delete from xueji where name=‘%s‘;",na); if(mysql_query(&mysql, std)) { cout<<"數據查詢錯誤發生"<<endl; } else { cout<<"刪除成功!"<<endl; } } } void Shan_chu::xue_hao_shan_chu()//按學號刪除 { Student zhao; system("pause"); system("cls"); zhao.show_table(); MYSQL mysql; mysql_init(&mysql); mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { printf("\n數據庫連接錯誤發生!"); } else { char std[220]; int na; cout<<"請輸入要刪除學籍信息的學生學號: "<<endl; cin>>na; sprintf(std,"delete from xueji where id=%d;",na); if(mysql_query(&mysql, std)) { cout<<"數據查詢錯誤發生"<<endl; } else { cout<<"刪除成功!"<<endl; } } } void Cha_xun::xing_ming_cha_xun()//姓名查詢 { system("cls"); system("pause"); MYSQL mysql; mysql_init(&mysql); MYSQL_RES *resultset; MYSQL_ROW row; mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤!"<<endl; } else { char std[200]; char n[200]; cout<<"請輸入要查的姓名:"<<endl; cin>>n; sprintf(std,"select * from xueji where name=‘%s‘;",n); if(mysql_query(&mysql, std)) { printf("\n數據查詢錯誤發生\n"); } else { resultset=mysql_store_result(&mysql); if(mysql_num_rows(resultset)!=NULL) { int numFields=mysql_num_fields(resultset);//**獲得表中的字段 while(row=mysql_fetch_row(resultset)) { int i=0; for(i=0;i<numFields;i++) { mysql_query(&mysql,"SET NAMES LATIN1"); cout<<" "<<row[i]<<" "; } cout<<endl; } } else { cout<<"顯示無果。"; } } mysql_free_result(resultset); } } void Cha_xun::xue_hao_cha_xun()//學號查詢 { system("cls"); system("pause"); MYSQL mysql; mysql_init(&mysql); MYSQL_RES *resultset; MYSQL_ROW row; mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤!"<<endl; } else { char std[200]; int n; cout<<"請輸入要查的學號:"<<endl; cin>>n; cout<<"--------------------------------------顯示查詢----------------------------------------"<<endl; cout<<" 學號 "<<" 姓名 "<<"性別 "<<"民族 "<<"籍貫 "<<" 身份證 "<<" QQ "<<" 電話 "<<" 成績 "<<endl; cout<<"--------------------------------------------------------------------------------------"<<endl; sprintf(std,"select * from xueji where id=%d;",n); if(mysql_query(&mysql, std)) { printf("\n數據查詢錯誤發生\n"); } else { resultset=mysql_store_result(&mysql); if(mysql_num_rows(resultset)!=NULL) { int numFields=mysql_num_fields(resultset);//**獲得表中的字段 while(row=mysql_fetch_row(resultset)) { int i=0; for(i=0;i<numFields;i++) { mysql_query(&mysql,"SET NAMES LATIN1"); cout<<" "<<row[i]<<" "; } cout<<endl; } cout<<"--------------------------------------------------------------------------------------"<<endl; } else { cout<<"顯示無果。"; } } mysql_free_result(resultset); } } void Xu_gai::xiu_gai_cheng_ji()//改動成績 { system("cls"); system("pause"); cout<<"顯示學生數據"<<endl; Xu_gai zhao; zhao.show_table(); MYSQL mysql; mysql_init(&mysql); mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤!"<<endl; } else { char std[200]; char m[200]; int n; cout<<"對目標學號信息進行改動:"<<endl; cin>>n; cout<<"請輸入要新的成績:"<<endl; cin>>m; sprintf(std,"update xueji set score=‘%s‘ where id=%d;",m,n); if(mysql_query(&mysql, std)) { printf("\n數據庫更改錯誤發生\n"); } else { printf("\n數據庫更改數據成功\n"); } } } void Xu_gai::xiu_gai_dian_hua()//改動電話 { system("cls"); system("pause"); cout<<"顯示學生數據"<<endl; Xu_gai zhao; zhao.show_table(); MYSQL mysql; mysql_init(&mysql); mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤!"<<endl; } else { char std[200]; char m[200]; int n; cout<<"對目標學號信息進行改動:"<<endl; cin>>n; cout<<"請輸入要新的電話:"<<endl; cin>>m; sprintf(std,"update xueji set phone=‘%s‘ where id=%d;",m,n); if(mysql_query(&mysql, std)) { printf("\n數據庫更改錯誤發生\n"); } else { printf("\n數據庫更改數據成功\n"); } } } void Xu_gai::xiu_gai_QQ_hao()//改動QQ號 { system("cls"); system("pause"); cout<<"顯示學生數據"<<endl; Xu_gai zhao; zhao.show_table(); MYSQL mysql; mysql_init(&mysql); mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤!
"<<endl; } else { char std[200]; char m[200]; int n; cout<<"對目標學號信息進行改動:"<<endl; cin>>n; cout<<"請輸入要新的QQ號:"<<endl; cin>>m; sprintf(std,"update xueji set QQ=‘%s‘ where id=%d;",m,n); if(mysql_query(&mysql, std)) { printf("\n數據庫更改錯誤發生\n"); } else { printf("\n數據庫更改數據成功\n"); } } } void Xu_gai::xiu_gai_shen_fen_zheng_hao()//改動身份證號************************************************************ { system("cls"); system("pause"); cout<<"顯示學生數據"<<endl; Xu_gai zhao; zhao.show_table(); MYSQL mysql; mysql_init(&mysql); mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤。"<<endl; } else { char std[200]; char m[200]; int n; cout<<"對目標學號信息進行改動:"<<endl; cin>>n; cout<<"請輸入要新的身份證號:"<<endl; cin>>m; sprintf(std,"update xueji set sheng_fen_zheng_hao=‘%s‘ where id=%d;",m,n); if(mysql_query(&mysql, std)) { printf("\n數據庫更改錯誤發生\n"); } else { printf("\n數據庫更改數據成功\n"); } } } void Xu_gai::xiu_gai_ji_guan()//改動籍貫 { system("cls"); system("pause"); cout<<"顯示學生數據"<<endl; Xu_gai zhao; zhao.show_table(); MYSQL mysql; mysql_init(&mysql); mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤!"<<endl; } else { char std[200]; char m[200]; int n; cout<<"對目標學號信息進行改動:"<<endl; cin>>n; cout<<"請輸入要新的籍貫:"<<endl; cin>>m; sprintf(std,"update xueji set ji_guan=‘%s‘ where id=%d;",m,n); if(mysql_query(&mysql, std)) { printf("\n數據庫更改錯誤發生\n"); } else { printf("\n數據庫更改數據成功\n"); } } } void Xu_gai::xiu_gai_min_zu()//改動民族 { system("cls"); system("pause"); cout<<"顯示學生數據"<<endl; Xu_gai zhao; zhao.show_table(); MYSQL mysql; mysql_init(&mysql); mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤!"<<endl; } else { char std[200]; char m[200]; int n; cout<<"對目標學號信息進行改動:"<<endl; cin>>n; cout<<"請輸入要新的民族:"<<endl; cin>>m; sprintf(std,"update xueji set min_zu=‘%s‘ where id=%d;",m,n); if(mysql_query(&mysql, std)) { printf("\n數據庫更改錯誤發生\n"); } else { printf("\n數據庫更改數據成功\n"); } } } void Xu_gai::xiu_gai_xing_bie()//改動性別 { system("cls"); system("pause"); cout<<"顯示學生數據"<<endl; Xu_gai zhao; zhao.show_table(); MYSQL mysql; mysql_init(&mysql); mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤!"<<endl; } else { char std[200]; char m[200]; int n; cout<<"對目標學號信息進行改動:"<<endl; cin>>n; cout<<"請輸入要新的性別:"<<endl; cin>>m; sprintf(std,"update xueji set sex=‘%s‘ where id=%d;",m,n); if(mysql_query(&mysql, std)) { printf("\n數據庫更改錯誤發生\n"); } else { printf("\n數據庫更改數據成功\n"); } } } void Xu_gai::xiu_gai_xing_ming()//改動姓名 { system("cls"); system("pause"); cout<<"顯示學生數據"<<endl; Xu_gai zhao; zhao.show_table(); MYSQL mysql; mysql_init(&mysql); mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤!"<<endl; } else { char std[200]; char m[200]; int n; cout<<"對目標學號信息進行改動:"<<endl; cin>>n; cout<<"請輸入要新的姓名:"<<endl; cin>>m; sprintf(std,"update xueji set name=‘%s‘ where id=%d;",m,n); if(mysql_query(&mysql, std)) { printf("\n數據庫更改錯誤發生\n"); } else { printf("\n數據庫更改數據成功\n"); } } } void Xu_gai::xiu_gai_xue_hao()//改動學號 { system("cls"); system("pause"); cout<<"顯示學生數據"<<endl; Xu_gai zhao; zhao.show_table(); MYSQL mysql; mysql_init(&mysql); mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤!"<<endl; } else { char std[200]; int n,m; cout<<"請輸入要改動的學號:"<<endl; cin>>n; cout<<"請輸入要新的學號:"<<endl; cin>>m; sprintf(std,"update xueji set id=%d where id=%d;",m,n); if(mysql_query(&mysql, std)) { printf("\n數據庫更改錯誤發生\n"); } else { printf("\n數據庫更改數據成功\n"); } } } int Student::xiu_gai()//定義改動函數 { cout<<"進入改動系統..."<<endl; int h; Xu_gai geng_zheng; system("pause"); system("cls"); MYSQL mysql; mysql_init(&mysql); mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤!
"<<endl; } else { while(1) { h=geng_zheng.xiu_gai_menu(); switch(h) { case 1: geng_zheng.xiu_gai_xue_hao(); break; case 2: geng_zheng.xiu_gai_xing_ming(); break; case 3: geng_zheng.xiu_gai_xing_bie(); break; case 4: geng_zheng.xiu_gai_min_zu(); break; case 5: geng_zheng.xiu_gai_ji_guan(); break; case 6: geng_zheng.xiu_gai_shen_fen_zheng_hao(); break; case 7: geng_zheng.xiu_gai_QQ_hao(); break; case 8: geng_zheng.xiu_gai_dian_hua(); break; case 9: geng_zheng.xiu_gai_cheng_ji(); break; case 0: return 0; break; default: cout<<"請選擇正確的菜單項進行操作。多謝合作!"<<endl; } } } return 0; } int Student::cha_xun()//查詢 { system("pause"); system("cls"); Cha_xun cha; MYSQL mysql; int h; mysql_init(&mysql); mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤!
"<<endl; } else { while(1) { h=cha.cha_xun_menu(); switch(h) { case 1: cha.xue_hao_cha_xun(); break; case 2: cha.xing_ming_cha_xun(); break; case 0: return 0; break; default: cout<<"請選擇正確的菜單項進行操作。多謝合作!"<<endl; } } } return 0; } int Student::del()//刪除 { system("pause"); system("cls"); Shan_chu shan; MYSQL mysql; int h; mysql_init(&mysql); mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤!
"<<endl; } else { while(1) { h=shan.shan_menu(); switch(h) { case 1: shan.xue_hao_shan_chu(); break; case 2: shan.xing_ming_shan_chu(); break; case 0: return 0; break; default: cout<<"請選擇正確的菜單項進行操作。多謝合作!"<<endl; } } } return 0; } int Student::score_tong_ji()//成績統計分析 { system("pause"); system("cls"); Score tong_ji; MYSQL mysql; int h; mysql_init(&mysql); mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤!"<<endl; } else { while(1) { h=tong_ji.score_tong_ji_menu(); switch(h) { case 1: tong_ji.cheng_ji_pai_xu(); break; case 2: tong_ji.zui_gao_zui_di_fen_xin_xi(); break; case 3: tong_ji.ping_jun_fen(); break; case 4: tong_ji.ji_ge_lv(); break; case 0: return 0; break; default: cout<<"請選擇正確的菜單項進行操作。
多謝合作!"<<endl; } } } return 0; } void Student::input()//錄入學生信息 { system("pause"); system("cls"); Student lu_ru; char std[200]; MYSQL mysql; mysql_init(&mysql); mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "student", 3306, NULL, 0)) { cout<<"數據庫連接錯誤!
"<<endl; } else { cout<<"請錄入學生信息數據"<<endl<<endl<<"錄入格式說明:輸入一項按Enter鍵進行下一項的輸入"<<endl<<endl; mysql_query(&mysql, "SET NAMES LATIN1"); cout<<"學號 "<<"姓名 "<<"性別 "<<"民族 "<<"籍貫 "<<"身份證 "<<"QQ "<<"電話 "<<"成績 "<<endl; cin>>lu_ru.id>>lu_ru.name>>lu_ru.sex>>lu_ru.min_zu>>lu_ru.ji_guan>>lu_ru.sheng_fen_zheng_hao>>lu_ru.QQ>>lu_ru.phone>>lu_ru.score; sprintf(std,"insert into xueji VALUES(%d,‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘,%d);" ,lu_ru.id,lu_ru.name,lu_ru.sex,lu_ru.min_zu,lu_ru.ji_guan,lu_ru.sheng_fen_zheng_hao,lu_ru.QQ,lu_ru.phone,lu_ru.score); mysql_query(&mysql,std); cout<<"加入成功"<<endl; } } void Student::show_table()//信息表數據的顯示 { system("cls"); MYSQL mysql; MYSQL_RES *resultset; MYSQL_ROW row; mysql_init(&mysql); mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"latin1"); if(!mysql_real_connect(&mysql,"localhost","root","123456","student",3306,NULL,0)) { cout<<"\n數據庫連接錯誤發生。"; } else { if(mysql_query(&mysql,"select * from xueji")) { cout<<"表格數據顯示失敗!"; } else { cout<<"--------------------------------------顯示信息----------------------------------------"<<endl; cout<<" 學號 "<<" 姓名 "<<"性別 "<<"民族 "<<"籍貫 "<<" 身份證 "<<" QQ "<<" 電話 "<<" 成績 "<<endl; cout<<"--------------------------------------------------------------------------------------"<<endl; resultset=mysql_store_result(&mysql); if(mysql_num_rows(resultset)!=NULL) { int numFields=mysql_num_fields(resultset);//**獲得表中的字段 while(row=mysql_fetch_row(resultset)) { int i=0; for(i=0;i<numFields;i++) { mysql_query(&mysql,"SET NAMES LATIN1"); cout<<" "<<row[i]<<" "; } cout<<endl; cout<<"--------------------------------------------------------------------------------------"<<endl; } } else { cout<<"顯示無果!"; } } mysql_free_result(resultset); } } int Student::main_menu()//主菜單 { system("pause"); system("cls"); int s; cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl <<" 1: 錄入學生信息 "<<endl <<" 2: 顯示學生信息 "<<endl <<" 3: 查找學生信息 "<<endl <<" 4: 刪除學生信息 "<<endl <<" 5: 改動學生信息 "<<endl <<" 6: 統計學生成績 "<<endl <<" 0: 退出系統 "<<endl <<" 請選擇:"; cin>>s; return s; } int Student::xiu_gai_menu()//改動菜單 { system("pause"); system("cls"); int a; cout<<endl<<endl<<endl<<endl<<endl <<" 1: 改動學號 "<<endl <<" 2: 改動姓名 "<<endl <<" 3: 改動性別 "<<endl <<" 4: 改動民族 "<<endl <<" 5: 改動籍貫 "<<endl <<" 6: 改動身份證 "<<endl <<" 7: 改動QQ號 "<<endl <<" 8: 改動手機號 "<<endl <<" 9: 改動成績 "<<endl <<" 0: 返回上一層 "<<endl <<" 請選擇:"; cin>>a; return a; } int Student::cha_xun_menu()//查詢菜單 { system("pause"); system("cls"); int k; cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl <<" 1: 按學號查找 "<<endl <<" 2: 按姓名查找 "<<endl <<" 0: 返回上一層 "<<endl <<" 請選擇:"; cin>>k; return k; } int Student::shan_menu()//刪除菜單 { system("pause"); system("cls"); int g; cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl <<" 1: 按學號刪除 "<<endl <<" 2: 按姓名刪除 "<<endl <<" 0: 返回上一層 "<<endl <<" 請選擇:"; cin>>g; return g; } int Student::score_tong_ji_menu()//成績統計菜單 { system("pause"); system("cls"); int d; cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl <<" 1: 對成績進行排序 "<<endl <<" 2: 最高和最低成績的學生信息 "<<endl <<" 3: 全體學生的平均分 "<<endl <<" 4: 及格率 "<<endl <<" 0: 返回上一層 "<<endl <<" 請選擇:"; cin>>d; return d; } int main() { int n; Student zhao; MYSQL mysql; mysql_init(&mysql); if(!mysql_real_connect(&mysql,"localhost","root","123456","student",3306,NULL,0)) { cout<<"\n數據庫連接錯誤發生!"; } else { cout<<"\n\n\n\n\n\n\n\n\n 數據庫連接成功!
\n\n\n\n\n\n\n\n"; system("PAUSE"); system("cls"); while(1) { n=zhao.main_menu(); switch(n) { case 1: zhao.input(); break; case 2: zhao.show_table(); break; case 3: zhao.cha_xun(); break; case 4: zhao.del(); break; case 5: zhao.xiu_gai(); break; case 6: zhao.score_tong_ji(); break; case 0: system("cls"); cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl; cout<<" ··········"<<endl; cout<<" ***···謝謝使用···***"<<endl; cout<<" ··········"<<endl<<endl<<endl<<endl<<endl<<endl; exit(1); break; default: cout<<"請選擇正確的菜單項進行操作。多謝合作!"<<endl; } } } return 0; }
高校學生學籍系統C++&mysql