今天在c++中寫了一個職工管理的小項目,主要運用了vector的相關知識,vector是C++標準模板庫中的部分內 容,簡單地說,vector是個能夠存放任意類型的動態數組,能夠增加和壓縮數據。首先在程序開頭處加上#include<vector>以包含所需要的類文件vector,還有一定要加上using namespace std;它可以像普通數組一樣訪問,可以順序地向容器中填充數據,還可以動態地改變它的大小,也可以在容器中裝入自定義的數據類型,可以把一個容器的對象賦值給另外一個容器。對于vector 的數據的存入和輸出可以用v.begin()和v.end() 來得到vector開始的和結束的元素地址的指針位置。為了方便理解,舉一個例子:vector <int *> a int b = 5; a.push_back(b);//在數組的最后添加一個數據 cout<<a[0]; //最后輸出的結果為5 。
下面是自己寫的這個項目的代碼:
文件名:people.h
#ifndef PEOPLE_H#define PEOPLE_H#include <string>using namespace std;class People{public: string name; //姓名 string age; //年齡 string sex; //性別 string id; //職工號public: People(); People(string,string,string,string);};文件名:worker.h#ifndef WORKER_H#define WORKER_H#include "People.h"#include <string>using namespace std;class Worker :public People{public: string mail; //郵編 string section ; //部門 double salary; //薪資public: Worker(); Worker(string,string,string,string,string,string,double); };#endif文件名:plan.h#ifndef PLAN_H#define PLAN_H#include "worker.h"#include <functional>#include <algorithm>#include <string>#include <vector>using namespace std;class Plan{public: Plan(); void jiemian(); //主菜單界面 void find(std::vector<Worker> &ver); //查詢職工信息 void edit(std::vector<Worker> &ver); //修改職工信息 void del(std::vector<Worker> &ver); //刪除職工信息 void insert(std::vector<Worker> &ver); //添加職工信息 void st(std::vector<Worker>&ver); //按職工薪資查詢排名 void list(std::vector<Worker>&ver); //瀏覽職工信息};#endif文件名:main.cpp#include "plan.h"#include <vector>#include <iostream>using namespace std; int main(){ int temp = 1 ; char flag; vector<Worker> vecobj; Plan con; while(temp == 1) { con.jiemian(); //顯示界面 cin>>flag; cout<<endl; switch(flag) { case '1': con.insert(vecobj); break; case '2': con.del(vecobj); break; case '3': con.edit(vecobj); break; case '4': con.find(vecobj); break; case '5': con.list(vecobj); break; case '6': con.st(vecobj); break; case '0': temp=0; break ; } } return 0 ;}文件名:people.cpp#include "People.h"#include <string>using namespace std;People::People(){}People::People(string m_name,string m_age,string m_sex,string m_id){ name=m_name; age=m_age; sex=m_sex; id=m_id;}文件名:worker.cpp#include "worker.h"#include <string>using namespace std;Worker::Worker(){}Worker::Worker(string m_name,string m_age,string m_sex,string m_id,string mymail,string mysection,double mysalary):People(m_name,m_age,m_sex,m_id){ mail=mymail; section=mysection; salary=mysalary;}文件名:plan.cpp#include "plan.h"#include<windows.h>#include <string>#include <iostream>#include <vector>#include <functional>#include <algorithm>#include <time.h>using namespace std;char* mytime(){ time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); return asctime (timeinfo); }bool mysalary(Worker& wor1, Worker& wor2) //比較兩名職工薪資的大小{ return wor1.salary<wor2.salary;}Plan::Plan(){}void Plan::jiemian() //主菜單界面{ system("cls") ; system("color 5F"); cout<<endl; cout<<endl; cout<<"/t/t****************************************************"<<endl; cout<<"/t/t* "<<mytime() ; cout<<"/t/t* 請選擇您需要的操作! *"<<endl; cout<<"/t/t* (1)添加職工人員 *"<<endl; cout<<"/t/t* (2)刪除職工人員 *"<<endl; cout<<"/t/t* (3)修改職工信息 *"<<endl; cout<<"/t/t* (4)查詢 *"<<endl; cout<<"/t/t* (5)顯示所有職工個人信息 *"<<endl; cout<<"/t/t* (6)按薪資排名 *"<<endl; cout<<"/t/t* (0)退出 *"<<endl; cout<<"/t/t* 選擇相對的括號里的阿拉伯數字! *"<<endl; cout<<"/t/t****************************************************"; cout<<endl; cout<<endl; return;}void Plan::find(std::vector<Worker> &ver) //查詢職工信息{ system("cls"); string stucode; string stuname; cout<<"請輸入職工工號:"; cin>>stucode; cout<<endl ; cout<<"請輸入職工姓名"; cin>>stuname; cout<<endl ; vector<Worker>::iterator i; for(i=ver.begin(); i!=ver.end(); i++) { if( ((*i).id==stucode)&&((*i).name==stuname) ) { cout<<"職工號:"<<(*i).id<<"職工姓名:"<<(*i).name<<" 職工年齡:"<<(*i).age<<" 性別:"<<(*i).sex<<" 郵編:"<<(*i).mail <<" 部門:"<<(*i).section<<" 薪資:"<<(*i).salary<<endl; } else { cout<<"對不起 未找到您所要查詢的職工信息"; } Sleep(4000) ; } return;}void Plan::list(std::vector<Worker>&ver) //瀏覽職工個人信息{ system("cls"); vector<Worker>::iterator i; for(i=ver.begin(); i!=ver.end(); i++) { cout<<"職工號:"<<(*i).id<<"職工姓名:"<<(*i).name<<" 職工年齡:"<<(*i).age<<" 性別:"<<(*i).sex<<" 郵編:"<<(*i).mail <<" 部門:"<<(*i).section<<" 薪資:"<<(*i).salary<<endl; } cout<<endl<<endl<<endl; string name; cout<<"按任意鍵返回主界面:"; cin>>name ; return;}void Plan::edit(std::vector<Worker> &ver) //修改職工信息{ system("cls"); string stucode; string stuname; cout<<"請輸入職工工號:"; cin>>stucode; cout<<endl ; cout<<"請輸入職工姓名"; cin>>stuname; cout<<endl ; vector<Worker>::iterator i; for(i=ver.begin(); i!=ver.end(); ++i) { if( ((*i).id==stucode)&&((*i).name==stuname)) { cout<<"原來的信息:"<<endl; cout<<endl ; cout<<"職工號:"<<(*i).id<<"職工姓名:"<<(*i).name<<" 職工年齡:"<<(*i).age<<" 性別:"<<(*i).sex<<" 郵編:"<<(*i).mail <<" 部門:"<<(*i).section<<" 薪資:"<<(*i).salary<<endl; cout<<"請輸入新信息:"<<endl; cout<<endl ; cout<<"請輸入姓名:"; cin>>(*i).name; cout<<endl ; cout<<"請輸入年齡:"; cin>>(*i).age; cout<<endl ; cout<<"請輸入性別:"; cin>>(*i).sex; cout<<endl ; cout<<"請輸入職工號:"; cin>>(*i).id; cout<<endl ; cout<<"請輸入職工郵編:"; cin>>(*i).mail; cout<<endl ; cout<<"請輸入職工部門:"; cin>>(*i).section; cout<<endl ; cout<<"請輸入職工薪資:"; cin>>(*i).salary; cout<<endl ; cout<<"系統正在存儲信息,即將返回主界面......"<<endl; Sleep(3000) ; } else { cout<<"對不起 未找到您所要查詢的職工信息"; } return; }}void Plan::del(std::vector<Worker> &ver) //刪除職工信息{ system("cls"); string stucode; string stuname; cout<<"請輸入職工工號:"; cin>>stucode; cout<<endl ; cout<<"請輸入職工姓名"; cin>>stuname; cout<<endl ; vector<Worker>::iterator i; for(i=ver.begin(); i!=ver.end(); ++i) { if( ((*i).id==stucode)&&((*i).name==stuname)) { ver.erase(i); //erase是C++中String類庫中提供的一個函數 return; } } cout<<"對不起 未找到您所要查詢的職工!"<<endl; Sleep(2000) ; return;}void Plan::insert(std::vector<Worker> &ver) //添加職工信息{ system("cls"); Worker wor; string stucode; string stuname; string stusex; string stuage; string stumail; string stusection; double stusalary; cout<<"請輸入職工工號:"; cin>>stucode; cout<<endl ; cout<<"請輸入職工姓名:"; cin>>stuname; cout<<endl ; cout<<"請輸入職工年齡:"; cin>>stuage; cout<<endl ; cout<<"請輸入職工性別:"; cin>>stusex; cout<<endl ; cout<<"請輸入職工郵編:"; cin>>stumail; cout<<endl ; cout<<"請輸入職工部門:"; cin>>stusection; cout<<endl ; cout<<"請輸入職工薪資:"; cin>>stusalary; cout<<endl ; wor.id=stucode; wor.name=stuname; wor.age=stuage; wor.sex=stusex; wor.mail=stumail; wor.section=stusection; wor.salary=stusalary; ver.push_back(wor); cout<<"系統正在存儲信息,即將返回主界面......"<<endl; Sleep(3000) ; return;}void Plan::st(std::vector<Worker>&ver) //按職工薪資排名{ system("cls") ; std::sort(ver.begin(), ver.end(), mysalary); list(ver); return;}寫項目的時候,自己也遇到過不少問題,比如類的繼承和對象訪問范圍容易犯錯,關于vector的用法以及一些需要用的指針的方面,但只要將其搞懂或者上網查一些資料,明白其具體含義及用法后,寫起來就會很方面,vector著實很方面,根本不需要你去考慮你想存放數據的大小,很方便,對于這個項目,自己準備再用文件功能將其能保存到本地更好的實現各個功能!
新聞熱點
疑難解答
圖片精選