#include <iostream>
#include<fstream>
#include <string>
#include <cstdlib>
#include <VECTOR>
using namespace std;
void _manager();
void _technician();
void _salesman();
findoutand(string str1)
{
string::size_type p=str1.find("&");
if(p!=string::npos)
return 1;
else
return 0;
}
findoutdoller(string str1) //查找函數
{
string::size_type p=str1.find("$");
if(p!=string::npos)
return 1;
else
return 0;
}
class base_employee
{
private:
string number; //職工號
string name; //姓名
string age; //年齡
string sex; //性別
int ranking; //排名
public:
base_employee()
{
number="NULL";
name="Noname";
age="18";
sex="mid";
ranking=0;
}
////////////////////////////////////////////////////////////////////////////////////////////////
void number_Input(string _number)
{
number=_number;
}
void name_Input(string _name)
{
name=_name;
}
void age_Input(string _age)
{
age=_age; //定義所有的輸入數據的公用接口
}
void sex_Input(string _sex)
{
sex=_sex;
}
void ranking_Input(int _ranking)
{
ranking=_ranking;
}
////////////////////////////////////////////////////////////////////////////////////////////////
string number_output()
{
return number;
}
string name_output()
{
return name;
}
string age_output() //獲取數據的接口函數
{
return age;
}
string sex_output()
{
return sex;
}
int ranking_output()
{
return ranking;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class manager:public base_employee //技術員
{
private:
string post; //職位
double weage; //工資
public:
manager() //默認構造函數
{
post="經理";
weage=0;
}
manager(string Tpost) //構造函數
{
post=Tpost;
weage=0;
}
////////////////////////////////////////////////////////////////////////////////////////////////
void weage_Input(int _weage)
{
weage=_weage; //輸入數據(工資)
}
void post_input(string _post)
{
post=_post; //輸入你的職位
}
////////////////////////////////////////////////////////////////////////////////////////////////
string post_output()
{
return post;
} //獲取數據的函數
double weage_output()
{
return weage;
}
};
class technician:public manager
{
public:
technician(string Tpost2="技術員"):manager(Tpost2) //二重繼承,並用默認實參來初始化自己的成員
{
}
};
class salesman:public manager
{
public:
salesman(string Tpost3="銷售員"):manager(Tpost3) //二重繼承,並用默認實參來初始化自己的成員
{
}
};
//經理類工資和數據錄入函數開始:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ofstream ofile;
void _manager()
{
manager mager;
string number;
string name;
string sex;
string post;
string age;
double weage;
renum1: cout<<"請輸入你的職工號"<<endl;
cin>>number;
if(findoutand(number)||findoutdoller(number))
{
cout<<"你的輸入帶有非法參數,請重新輸入!";
goto renum1;
}
number+="&";
mager.number_Input(number); //數據輸入開始
cout<<"請輸入你的姓名"<<endl;
renum2: cin>>name;
if(findoutand(name)||findoutdoller(name))
{
cout<<"你的輸入帶有非法參數,請重新輸入!";
goto renum2;
}
mager.number_Input(name);
renum3: cout<<"請輸入你的性別"<<endl;
cin>>sex;
if(findoutand(sex)||findoutdoller(sex))
{
cout<<"你的輸入帶有非法參數,請重新輸入!";
goto renum3;
}
mager.sex_Input(age);
renum4: cout<<"請輸入你的年齡"<<endl;
cin>>age;
if(findoutand(age)||findoutdoller(age))
{
cout<<"你的輸入帶有非法參數,請重新輸入!";
goto renum4;
}
mager.age_Input(age);
renum5: cout<<"請輸入你的職位"<<endl;
cin>>post;
if(findoutand(post)||findoutdoller(post))
{
cout<<"你的輸入帶有非法參數,請重新輸入!";
goto renum5;
}
mager.post_input(post);
cout<<"請輸入你的工資"<<endl;
cin>>weage;
mager.weage_Input(weage);
//char Name[20];
//strcpy(Name,name.c_str());
//strcat(Name,".txt");
//ofstream ofile("data.txt");
ofile.open("data.txt",ofstream::out | ofstream::app);
ofile<<number<<'$';
ofile<<name<<'$';
ofile<<sex<<'$';
ofile<<age<<'$';
ofile<<post<<'$';
ofile<<weage<<'$';
//ofile<<'$'<<'\n';
ofile.close();
}
void _technician()
{
technician mager;
string number;
string name;
string sex;
string age;
string post;
double weage;
retec1: cout<<"請輸入你的職工號"<<endl;
cin>>number;
if(findoutand(number)||findoutdoller(number))
{
cout<<"你的輸入帶有非法參數,請重新輸入!";
goto retec1;
}
number+="&";
mager.number_Input(number); //數據輸入開始
retec2: cout<<"請輸入你的姓名"<<endl;
cin>>name;
if(findoutand(name)||findoutdoller(name))
{
cout<<"你的輸入帶有非法參數,請重新輸入!";
goto retec2;
}
mager.number_Input(name);
retec3: cout<<"請輸入你的性別"<<endl;
cin>>sex;
if(findoutand(sex)||findoutdoller(sex))
{
cout<<"你的輸入帶有非法參數,請重新輸入!";
goto retec3;
}
mager.sex_Input(sex);
retec4: cout<<"請輸入你的年齡"<<endl;
cin>>age;
if(findoutand(age)||findoutdoller(age))
{
cout<<"你的輸入帶有非法參數,請重新輸入!";
goto retec4;
}
mager.age_Input(age);
retec5: cout<<"請輸入你的職位"<<endl;
cin>>post;
if(findoutand(post)||findoutdoller(post))
{
cout<<"你的輸入帶有非法參數,請重新輸入!";
goto retec5;
}
mager.post_input(post);
cout<<"請輸入你工作的時間"<<endl;
float time=0;
cin>>time;
weage=100*time;
mager.weage_Input(weage);
//char Name[20];
//strcpy(Name,name.c_str());
//strcat(Name,".txt");
//ofstream ofile("data.txt");
ofile.open("data.txt",ofstream::out | ofstream::app);
ofile<<number<<'$';
ofile<<name<<'$';
ofile<<sex<<'$';
ofile<<age<<'$';
ofile<<post<<'$';
ofile<<weage<<'$';
//ofile<<'$'<<'\n';
ofile.close();
}
void _salesman()
{
technician mager;
string number;
string name;
string sex;
string age;
string post;
double weage;
resa1: cout<<"請輸入你的職工號"<<endl;
cin>>number;
if(findoutand(number)||findoutdoller(number))
{
cout<<"你的輸入帶有非法參數,請重新輸入!";
goto resa1;
}
number+="&";
mager.number_Input(number); //數據輸入開始
resa2: cout<<"請輸入你的姓名"<<endl;
cin>>name;
if(findoutand(name)||findoutdoller(name))
{
cout<<"你的輸入帶有非法參數,請重新輸入!";
goto resa2;
}
mager.number_Input(name);
resa3: cout<<"請輸入你的性別"<<endl;
cin>>sex;
if(findoutand(sex)||findoutdoller(sex))
{
cout<<"你的輸入帶有非法參數,請重新輸入!";
goto resa3;
}
mager.sex_Input(sex);
resa4: cout<<"請輸入你的年齡"<<endl;
cin>>age;
if(findoutand(age)||findoutdoller(age))
{
cout<<"你的輸入帶有非法參數,請重新輸入!";
goto resa4;
}
mager.age_Input(age);
resa5: cout<<"請輸入你的職位"<<endl;
cin>>post;
if(findoutand(post)||findoutdoller(post))
{
cout<<"你的輸入帶有非法參數,請重新輸入!";
goto resa5;
}
mager.post_input(post);
cout<<"請輸入你銷售額"<<endl;
float salesmoney=0;
cin>>salesmoney;
weage=0.4*salesmoney;
mager.weage_Input(weage);
//char Name[20];
//strcpy(Name,name.c_str());
//strcat(Name,".txt");
//ofstream ofile("data.txt");
ofile.open("data.txt",ofstream::out | ofstream::app);
ofile<<number<<'$';
ofile<<name<<'$';
ofile<<sex<<'$';
ofile<<age<<'$';
ofile<<post<<'$';
ofile<<weage<<'$';
//ofile<<'$'<<'\n';
ofile.close();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
back: cout<<"請輸入您要使用的功能(1.數據錄入,2.數據統計,3.刪除數據,4.退出)"<<endl;
cout<<"請注意!輸入個人信息時,請不要輸入$以及&字符,否則將引起程序數據錯亂!"<<endl;
int chose=0;
cin>>chose;
while(chose==1)
{
int chosejob=0;
cout<<"請選擇你的職業(1.經理,2.技術員,3.銷售員,0.退出)"<<endl;
cin>>chosejob;
while(chosejob==1)
{
_manager();
break;
}
while(chosejob==2)
{
_technician();
break;
}
while(chosejob==3)
{
_salesman();
break;
}
if(chosejob==0)
break;
}
while(chose==2)
{
int count=0;
vector<manager> bowl;//用來存放數據
ifstream infile("data.txt"); //綁定文件
string a[10000]; //靜態數組存放數據
string str; //string字符串用來進行臨時中轉
//string* p=new string[]; //定義動態數組
infile>>str; //將文件中的數據存入str
//cout<<str;
int tempcount=0; //置0
int ix=0; //ix爲靜態數組下標
//string can;
//cout<<str; //數據讀入至字符串成功!
string::iterator p=str.begin();
if(str.empty())
{
cout<<"數據庫中目前找不到任何數據,請輸入一些數據後再進行統計,謝謝!"<<endl;
break;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
while(*p!=NULL)
{
manager item;
a[ix]=*p;
//cout<<a[ix]<<endl;
++ix;
++p;
if(*p=='$')
{
++p; //用以屏蔽$字符
++tempcount; //人員的內容項目計數器+1,根據$字符來判斷當前數據屬於類中的什麼部分。
for(int i=1;i<ix;i++)
{
a[0]+=a[i];
}
//cout<<a[0]<<endl;
if(tempcount==1)
{
bowl.push_back(item);
bowl[count].number_Input(a[0]); //此處的算法用途爲將文件中的數據按照一定算法放入容器內部,以供冒泡排序之用。
}
else if(tempcount==2)
{
bowl.push_back(item);
bowl[count].name_Input(a[0]);
}
else if(tempcount==3)
{
bowl.push_back(item);
bowl[count].sex_Input(a[0]);
}
else if(tempcount==4)
{
bowl.push_back(item);
bowl[count].age_Input(a[0]);
}
else if(tempcount==5)
{
bowl.push_back(item);
bowl[count].post_input(a[0]);
}
else if(tempcount==6)
{
bowl.push_back(item);
double money;
money=atof(a[0].c_str()); //字符數組的轉化
bowl[count].weage_Input(money);
++count; //人員計數器加一
tempcount=0; //由於到了最後,所以要重新置爲0
}
ix=0; //數組重置
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int j;
manager temp;
for(int i=0;i<count-1;i++)
{
for(j=0;j<count-1-i;j++)
if(bowl[j].weage_output()<bowl[j+1].weage_output())
{
temp=bowl[j]; //冒泡排序算法結束
bowl[j]=bowl[j+1];
bowl[j+1]=temp;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
cout<<"職工號"<<"|"<<"姓名"<<"|"<<"性別"<<"|"<<"年齡"<<"|"<<"崗位"<<"|"<<"工資"<<"|"<<"排名"<<endl;
for(i=0;i<count;i++)
{
bowl[i].ranking_Input(i+1);
cout<<bowl[i].number_output()<<"|"<<bowl[i].name_output()<<"|"<<bowl[i].age_output()<<"|"<<bowl[i].post_output()<<"|"<<bowl[i].weage_output()<<"|"<<bowl[i].ranking_output()<<endl;
}
chose=0;
}
while(chose==3)
{
int tempcount=0;
ifstream infile("data.txt"); //綁定文件
string arr[1000];
string del_str;
string _str;
infile>>_str;
if(_str.empty())
{
cout<<"無數據!程序即將退出!"<<endl;
break;
}
cout<<_str<<endl;//數據讀入成功!
cout<<"請輸入你要刪除的職工號"<<endl;
cin>>del_str;
del_str+="&";
string::iterator p=_str.begin();
string::iterator first=p; //記住當前p所指向的字符串(str的首字符)
string::iterator last=p; //用於尾部的指針
int ix=0;
while(*p!=NULL)
{
string tempstr; //臨時str存放的是當前指向的屬性,與del_str進行比較
arr[ix]=*p;//////////////////////////////////////////////////
++p; //將數據存進數組
++ix; ///////////////////////////////////////////////////
if(*p=='$')
{
++tempcount;
++p;
for(int i=1;i<ix;i++)
{
arr[0]+=arr[i];
}
tempstr=arr[0];
cout<<tempstr<<endl;
if(tempstr==del_str)//如果當前的臨時變量等於需要刪除的變量
{
tempcount=1; //每次找到匹配頭的時候就將計數器重新置爲1 因爲找到了職工號,所以$已經有了1個,所以置爲1而不是0
while(*p!='$'||tempcount!=6) //只要我們的$符號不爲6,或者我們的*p還不爲$,我們就繼續執行循環體中的語句。
{
++p; //一上來我們就要把指針p加一,這樣我們就能夠計算$符號的個數,來決定我們這個職工的所有屬性到底指完了沒。(☆_☆)/~~
if(*p=='$')
{
++tempcount;
}
}
last=p; //最後,我們將last指針變爲P當前所指的數據,這樣我們就完成了first和last指針的正確數據指向!
p=_str.erase(first,last+1);//調用string類型的函數來刪除兩個指針中間的值,+1是爲了刪除最後一個$。↖(^ω^)↗
}
ix=0;
first=p; //在這裏我們將first的指針指向P,否則會產生指針永遠指向頭的情況,會造成只能順序刪除而不能隨機刪除的情況,
} //這個情況debug了我一個小時,把first指針給忘了……細心很重要啊!(ー_ー)!!
}
ofstream("data.txt").close(); //清空原數據
ofile.open("data.txt",ofstream::out | ofstream::app); //重新打開文件
ofile<<_str; //將_str中的內容全部寫入文件,這樣做效率太低,不過我暫時沒什麼好辦法,希望大神能給出更完美的算法,謝謝!
chose=0; //給予跳出循環的條件,完成~!`(*∩_∩*)′
}
}
自己寫的一個企業管理程序,很簡陋,隨便看看吧
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.