|
- #include<iostream>
- #include<string>
- #include<fstream>
- #include<WinSock.h>
- #include<mysql.h>
- #include<conio.h>
- #pragma comment(lib,"C:\\MySQL\\lib\\opt\\libmysql.lib")
- using namespace std;
- void Str_to_double(double temp,string &str)
- {
- //values1表示整數部分,values2表示小數部分
- int values1 = int(temp);
- int values2 = (temp-values1) * 100;//values2最多為十位數
- int *a;
- a = new int[10];
- for(int i = 0;(values1 / 10) != 0;i++)
- {
- a[i] = values1 % 10;
- values1 /= 10;
- }
- a[i] = values1;
- for(int j = i;j >= 0;j--)
- str += a[j] + 48;
- str += ".";
- int one,two;
- one = values2 / 10;
- two = values2 % 10;
- str += one + 48;
- str += two + 48;
- delete[] a;//釋放內存
- }
- bool InitDatabase(MYSQL &mydata)
- {
- //初始化數據庫
- if(0==mysql_library_init(0, NULL, NULL))
- cout << "初始化數據庫成功--" << endl;
- else
- {
- cout << "mysql_library_init() failed!" << endl;
- return 0;
- }
- //初始化數據結構
- if(NULL!=mysql_init(&mydata))
- cout << "初始化數據結構成功--" << endl;
- else
- {
- cout << "mysql_init() failed!" << endl;
- return false;
- }
- //初始化字符集
- if(0==mysql_options(&mydata,MYSQL_SET_CHARSET_NAME, "gbk"))
- cout << "初始化字符集成功--" << endl;
- else
- {
- cout << "mysql_options() failed!" << endl;
- return false;
- }
- //鏈接數據庫
- if(NULL!=mysql_real_connect(&mydata,"localhost","root","123456","teacher_salary_system",0,NULL,0))
- cout << "鏈接數據庫成功--" << endl;
- else
- {
- cout << "mysql_real_connect() failed!" << endl;
- return false;
- }
- string sqlstr;
- sqlstr = "create table if not exists Teacher_data";
- sqlstr += "(";
- sqlstr += "Number int unsigned not null auto_increment primary key," ;//教師號
- sqlstr += "Name varchar(10) not null,";//姓名
- sqlstr += "Sex varchar(10) not null,";//性別
- sqlstr += "Company varchar(100) not null,";//單位名稱
- sqlstr += "Address varchar(100) not null,";//家庭住址
- sqlstr += "Telephone varchar(20) not null,";//聯系電話
- sqlstr += "Basic_sal double unsigned not null,";//基本工資
- sqlstr += "Sub_sal double unsigned not null,";//津貼
- sqlstr += "Life_sal double unsigned not null,";//生活補貼
- sqlstr += "Tele_mon double unsigned not null,";//電話費
- sqlstr += "Elec_water_mon double unsigned not null,";//水電費
- sqlstr += "House_mon double unsigned not null,";//房租
- sqlstr += "Income_tax double unsigned not null,";//所得稅
- sqlstr += "Health_mon double unsigned not null,";//衛生費
- sqlstr += "Gongji_mon double unsigned not null,";//公積金
- sqlstr += "Got_sal double unsigned not null,";//應發工資
- sqlstr += "Total_spend double unsigned not null,";//合計扣款
- sqlstr += "Real_sal double unsigned not null";//實發工資
- sqlstr += ");";
- if(0 == mysql_query(&mydata,sqlstr.c_str()))
- {
- cout << "初始化Teacher_data成功!" << endl << "按任意鍵繼續--";
- getch();
- return true;
- }
- else
- {
- cout << "mysql() 創建Teacher_data失敗!" << endl;
- return false;
- }
- }
- bool Input(MYSQL &mysql) //用引用
- {
- while(1)
- {
- system("CLS");
- string data = "insert into Teacher_data values(";
- string temp;
- cout << "--按提示進行操作--" << endl;
- cout << "請輸入(下同)教師編號:" << endl;
- cin >> temp;
- data += temp + ",";
- cout << "教師姓名:" << endl;
- cin >> temp;
- data += "'" + temp + "',";
- cout << "教師性別:" << endl;
- cin >> temp;
- data += "'" + temp + "',";
- cout << "單位名稱:" << endl;
- cin >> temp;
- data += "'" + temp + "',";
- cout << "家庭住址:" << endl;
- cin >> temp;
- data += "'" + temp + "',";
- cout << "聯系電話:" << endl;
- cin >> temp;
- data += "'" + temp + "',";
- cout << "基本工資:" << endl;
- double temp1;
- cin >> temp1;
- Str_to_double(temp1,data);
- data += ",";
- cout << "津貼:" << endl;
- double temp2;
- cin >> temp2;
- Str_to_double(temp2,data);
- data += ",";
- cout << "生活補貼:" << endl;
- double temp3;
- cin >> temp3;
- Str_to_double(temp3,data);
- data += ",";
- cout << "電話費:" << endl;
- double temp4;
- cin >> temp4;
- Str_to_double(temp4,data);
- data += ",";
- cout << "水電費:" << endl;
- double temp5;
- cin >> temp5;
- Str_to_double(temp5,data);
- data += ",";
- cout << "房租:" << endl;
- double temp6;
- cin >> temp6;
- Str_to_double(temp6,data);
- data += ",";
- cout << "所得稅:" << endl;
- double temp7;
- cin >> temp7;
- Str_to_double(temp7,data);
- data += ",";
- cout << "衛生費:" << endl;
- double temp8;
- cin >> temp8;
- Str_to_double(temp8,data);
- data += ",";
- cout << "公積金:" << endl;
- double temp9;
- cin >> temp9;
- Str_to_double(temp9,data);
- data += ",";
- double temp10 = temp1 + temp2 + temp3;//應發工資
- Str_to_double(temp10,data);
- data += ",";
- double temp11 = temp4 + temp5 + temp6 + temp7 +temp8 + temp9;//合計扣款
- Str_to_double(temp11,data);
- data += ",";
- double temp12 = temp10 - temp11;//實發工資
- cout << "實發工資:" << temp12 << endl;
- Str_to_double(temp12,data);
- data += ");";
- if(0 == mysql_query(&mysql,data.c_str()))//只能接收二進制文件
- cout << "插入成功!" << endl;
- else
- {
- cout << "mysql_query() failed!" << endl;
- cout << data.c_str() << endl;
- cout << "按任意鍵繼續--" << endl;
- getch();
- return false;
- }
- cout << "輸入成功,是否繼續輸入?Y/N" << endl;
- string judge;
- cin >> judge;
- if(!(judge == "Y" || judge == "y"))
- return false;
- }
- }
- void Show(MYSQL &mydata,string model)
- {
- string sqlstr;
- if(model == "all")
- sqlstr = "select * from Teacher_data order by Number" ;
- else if (model == "search")
- {
- cout << "**************************" << endl;
- cout << " ****請選擇查詢的方式**** " << endl;
- cout << " ****1、編號查詢**** " << endl;
- cout << " ****2、姓名查詢**** " << endl;
- cout << " ****3、性別查詢**** " << endl;
- cout << " ****4、單位名稱查詢****" << endl;
- cout << " ****5、家庭住址查詢****" << endl;
- cout << " ****6、聯系電話查詢****" << endl;
- int i;//用于選擇查詢方式
- string p;//輸入查詢的內容
- cin >> i;
- while(i > 6 || i < 1)
- {
- cout << "無該查詢方式,請重新輸入--" << endl;
- cin >> i;
- }
- switch (i)
- {
- case 1:
- cout << "請輸入查詢的教師編號:" << endl;
- cin >> p;
- sqlstr = "select * from Teacher_data where Number = " + p + ";";
- break;
- case 2:
- cout << "請輸入查詢的教師姓名:" << endl;
- cin >> p;
- sqlstr = "select * from Teacher_data where name = '" + p + "';";
- break;
- case 3:
- cout << "請輸入查詢的教師性別:" << endl;
- cin >> p;
- sqlstr = "select * from Teacher_data where sex = '" + p + "';";
- break;
- case 4:
- cout << "請輸入查詢教師的單位名稱:" << endl;
- cin >> p;
- sqlstr = "select * from Teacher_data where company = '" + p + "';";
- break;
- case 5:
- cout << "請輸入查詢教師的家庭住址:" << endl;
- cin >> p;
- sqlstr = "select * from Teacher_data where address = '" + p + "';";
- break;
- case 6:
- cout << "請輸入查詢教師的聯系電話:" << endl;
- cin >> p;
- sqlstr = "select * from Teacher_data where telephone = '" + p + "';";
- break;
- default:
- break;
- }
- }
- system("CLS");
- MYSQL_RES *result = NULL;
- if(0 == mysql_query(&mydata,sqlstr.c_str()))
- {
- //一次性取得數據集
- result = mysql_store_result(&mydata);
- //取得并打印行數
- int rowcount = mysql_num_rows(result);
- cout << "row count: " << rowcount << endl;
- //取得并打印各字段的名稱
- unsigned int fieldcount = mysql_num_fields(result);
- MYSQL_FIELD *field = NULL;
- for (unsigned int i = 0; i < fieldcount; i++)
- {
- field = mysql_fetch_field_direct(result, i);
- cout << field->name << "\t";
- }
- cout << endl;
-
- //打印各行
- MYSQL_ROW row = NULL;
- row = mysql_fetch_row(result);
- while (NULL != row)
- {
- for (int i = 0; i < fieldcount; i++)
- {
- cout << row[i] << "\t";
- }
- cout << endl;
- row = mysql_fetch_row(result);
- }
- cout << "按任意鍵繼續--" << endl;
- getch();
- }
- else
- {
- cout << "退出系統--" <<endl;
- exit(0);
- }
- }
- bool Delete(MYSQL &mydata)
- {
- system("CLS");
- string sqlstr;
- cout << "請輸入要刪除信息的教師號--" << endl;
- string i;
- cin >> i;
- sqlstr += "delete from Teacher_data where Number = " + i + ";";
- if(0 == mysql_query(&mydata,sqlstr.c_str()))
- {
- cout << "刪除成功--" << endl << "按任意鍵繼續--";
- getch(); //便于查找語法錯誤,下同
- return true;
- }
- else
- {
- cout << "刪除失敗!請檢查您輸入的ID是否存在" << endl;
- cout << sqlstr.c_str() << endl;
- getch();
- return false;
- }
- }
- bool Modify(MYSQL &mydata)
- {
- system("CLS");
- string sqlstr;
- cout << "請輸入要修改信息的教師號--" << endl;
- string i;
- cin >> i;
- sqlstr = "select * from Teacher_data where Number = " + i + ";" ;
- MYSQL_RES *result = NULL;//結果集
- MYSQL_FIELD *field = NULL; //字段名指針
- MYSQL_ROW row = NULL;//行
- if(0 == mysql_query(&mydata,sqlstr.c_str()))
- {
- result = mysql_store_result(&mydata);//取得全部結果
- unsigned int fieldcount = mysql_num_fields(result); //字段名數量
- int rowcount = mysql_num_rows(result);//取得行數
- for(int j = 0; j < fieldcount; j++)
- {
- //打印字段名稱
- field = mysql_fetch_field_direct(result,j);
- cout << field -> name << "\t";
- }
- cout << endl;
- row = mysql_fetch_row(result);//檢測結果的下一行
- while(row != NULL)
- {
- for(int k = 0; k < fieldcount; k++)
- {
- cout << row[k] << "\t";//輸出每一行的字段
- }
- row = mysql_fetch_row(result);//往下面加一行
- cout << endl;
- }
- }
- sqlstr = "update Teacher_data set ";
- cout << "請輸入要修改的字段名稱:" << endl;
- string temp;//記錄當前用戶輸入的字段
- cin >> temp;
- sqlstr += temp;
- cout << "請輸入要修改的內容:" << endl;
- cin >> temp;
- sqlstr += " = '" + temp + "'";
- sqlstr += " where Number = ";
- sqlstr += i + ";";
- if(0 == mysql_query(&mydata,sqlstr.c_str()))
- {
- cout << "修改成功--" << endl;
- getch();
- return true;
- }
- else
- {
- cout << "mysql_query() modify failed!" << endl;
- cout << sqlstr.c_str() << endl;
- getch();
- return false;
- }
- }
- void menu()
- {
- cout<<"**************************"<<endl;
- cout<<"****教師工資管理系統****"<<endl;
- cout<<" ***1、輸入教師信息*** "<<endl;
- cout<<" ***2、修改教師信息*** "<<endl;
- cout<<" ***3、刪除教師信息*** "<<endl;
- cout<<" ***4、瀏覽教師信息*** "<<endl;//將全部教師顯示到屏幕上
- cout<<" ***5、查找教師信息*** "<<endl;//查找某一教師的所有信息
- cout<<" ***0、安全退出系統*** "<<endl;
- cout<<"-----------------------"<<endl;
- cout<<endl;
- }
- int main(void)
- {
- MYSQL mydata;
- if( !InitDatabase(mydata))
- {
- cout << "初始化失敗" << endl;
- getch();
- exit(0);
- }
- while(1)
- {
- system("CLS");
- menu();
- int select_opreation;
- cin >> select_opreation;
- while(select_opreation > 5 || select_opreation < 0)
- {
- cout << "輸入有誤請重新輸入!" <<endl;
- cin >> select_opreation;
- }
- switch(select_opreation)
- {
- case 1:
- Input(mydata);
- break;
- case 2:
- Modify(mydata);
- break;
- case 3:
- Delete(mydata);
- break;
- case 4:
- Show(mydata,"all");
- break;
- case 5:
- Show(mydata,"search");
- break;
- case 0:
- cout<<"系統退出!!!"<<endl;
- exit(0);
- default:
- break;
- }
- cin.clear();//重置cin輸入狀態
- cin.sync(); //清楚緩沖區沒有讀取的信息
- }
- return 0;
- }
復制代碼
|
-
-
teacher.docx
2018-7-4 13:06 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
17.13 KB, 下載次數: 2, 下載積分: 黑幣 -5
完整
評分
-
查看全部評分
|