實訓周做了一個比較簡單的球員管理系統,用指針,結構體實現txt文件的增刪改查,記錄一下。梳理一下,系統設計如下:
在開始代碼之前,先做一份球員名單txt文件,包含所需的各項信息,不建議放到C盤,我做的球員信息如下:
主函數:
int main()
{
int c=1;
qiuyuan p[1000];
int zhiling;
shouye();//首頁,這裏可以自己設計一下
mingling();//選擇命令
getqiuyuanshu();//讀取球員名單的行數,即球員人數
cout<<endl;
cout<<"\t"<<"請先導入球員信息,才能執行其它操作!"<<endl;
while(1)
{
menu();
cout<<"\t"<<"請輸入你想執行的功能:";
cin>>zhiling;
switch(zhiling)
{
case 1:luru(p,n);break;
case 2:qiuyuanxinxibiao(p,n);break;
case 3:chaxun(p,n);break;
case 4:zengjia(p);break;
case 5:xiugai(p,n);break;
case 6:shanchu(p);break;
case 7:fankui();break;
case 8:cout<<endl<<"\t"<<"即將退出,請稍等......";c=0;Sleep(500);exit(1);
default:cout<<"\t"<<"選擇錯誤,按任意數字鍵即可返回菜單"<<endl;
cout<<"\t";cin>>zhiling;
}
if(c==1)
{
cout<<"\n"<<"\n"<<"\t"<<"-------------------------按任意鍵返回主菜單--------------------------";
cin.clear();
cin.sync();
cin.get();
system("cls");
}
else
break;
}
return 0;
}
獲取球員人數(關鍵,即txt文件的數據行數):
int getqiuyuanshu(){
fstream fin("E:\\C++大一上學期\\球員名單.txt",ios::in);//自定義txt文件路徑,獲取球員名單總行數
if(!fin)
{
cerr<<"can not open file"<<endl;
return -1;
}
char d;
while(fin.get(d))
{
if(d=='\n')
n++;
}
fin.close();
return n;
}
mingling選擇函數
void mingling()
{
int ling;
cin>>ling;
cout<<endl;
switch(ling)
{
case 1:cout<<"\t"<<"即將進入系統......"<<endl;
Sleep(500);
system("cls");//清屏函數
login();
break;
case 2:cout<<"\t"<<"即將退出,請稍後......"<<endl;exit(1);
default:cout<<"\t"<<"選擇錯誤,請重新輸入"<<endl;
mingling();
}
}
註冊登錄login函數(爲了省事,樓主寫的註冊登錄沒有寫保存讀取賬號密碼的功能),其實就是定義了幾個變量,然後賦初值。註冊就修改這幾個變量的值,登錄就直接輸入這幾個變量的默認值。我另外一個系統的註冊登錄做了保存登錄讀取賬號密碼的功能,後期有時間再寫一下。
void login()
{
int dl;
cout<<endl<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"------------------------------------"<<endl;
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<" 1.註冊"<<endl;
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<" 2.登錄"<<endl;
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<" 3.離開"<<endl;
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"------------------------------------"<<endl;
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"(默認賬號:123456,密碼:123456)"<<endl;
cout<<endl<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<" 您選擇的是:";
cin>>dl;
switch(dl)
{
case 1:Sleep(500);system("cls");
cout<<endl;
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<" 請輸入您要註冊的手機號碼:";
cin>>tel;
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<" 設置密碼(6-14個字符):";
cin>>mima1;
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<" 確認密碼:";
cin>>mima2;
if(mima1==mima2)
{
lock=mima1;
cout<<endl;
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"註冊成功,即將跳轉到登錄界面......"<<endl;
Sleep(500);system("cls");
cout<<endl<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"請輸入登錄賬號(手機號):";
cin>>zhanghao;
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<" 密碼:";
cin>>mima;
if(zhanghao==tel&&mima==lock)
{
cout<<endl<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"登錄中,請稍候......"<<endl;
Sleep(1000);system("cls");break;
}
else
{
cout<<endl<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"密碼或賬號錯誤,即將返回......"<<endl;
Sleep(500);system("cls");login();break;
}
}
else
{
cout<<endl<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<" 兩次輸入的密碼不一致,即將返回......"<<endl;
Sleep(500);system("cls");login();break;
}
case 2:Sleep(500);system("cls");
cout<<endl;
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"請輸入登錄賬號(手機號):";
cin>>zhanghao;
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<" 密碼:";
cin>>mima;
if(zhanghao==tel&&mima==lock)
{
cout<<endl<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"登錄中,請稍候......"<<endl;
Sleep(1000);system("cls");break;
}
else
{
cout<<endl;
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"密碼或賬號錯誤,即將返回......"<<endl;
Sleep(500);system("cls");
login();break;
}
case 3:cout<<"\t\t\t"<<"即將退出,請稍候......"<<endl;exit(1);
default:cout<<"\t\t\t"<<"輸入錯誤,請重新輸入!!!"<<endl;Sleep(500);system("cls");login();
}
}
球員信息的增刪改查:
void chaxun(qiuyuan *p,int n)//查詢球員信息
{
int r;string find_name;
cout<<"\t"<<"請輸入你要查找的球員的姓名:";
cin>>find_name;
cout<<endl;
bool flag=false;
for(r=0;r<n;r++)
{ if(p[r].name==find_name)
{
cout<<"\t"<<"該球員的信息爲:"<<endl;
cout<<"\t"<<"---------------------------------------------------------------------"<<endl;
cout<<"\t"<<"號碼 姓名 球隊 位置 性別 身高 體重 球齡"<<endl;
cout<<"\t"<<p[r].x<<"\t"<<p[r].name<<"\t"<<p[r].qiudui<<"\t"<<p[r].weizhi<<"\t"<<p[r].sex<<"\t"<<p[r].shengao<<"\t"<<p[r].tizhong<<"\t"<<p[r].qiuling<<endl;
cout<<"\t"<<"---------------------------------------------------------------------"<<endl;
flag=true;
break;
}
}
if(flag==false)
cout<<"\t"<<"查無此人!"<<endl;
}
void zengjia(qiuyuan *p)//增加球員信息
{
int r;
n=n+1;
ofstream outfile;
outfile.open("E:\\C++大一上學期\\球員名單.txt",ios::out);
cout<<endl;
cout<<"\t"<<"請輸入你要增加的球員的信息:"<<endl;
cout<<"\t"<<"請輸入該球員的號碼:";
cin>>p[n-1].x;
cout<<"\t"<<"請輸入該球員的姓名:";
cin>>p[n-1].name;
cout<<"\t"<<"請輸入該球員所在的球隊:";
cin>>p[n-1].qiudui;
cout<<"\t"<<"請輸入該球員的位置:";
cin>>p[n-1].weizhi;
cout<<"\t"<<"請輸入該球員的性別:";
cin>>p[n-1].sex;
cout<<"\t"<<"請輸入該球員的身高:";
cin>>p[n-1].shengao;
cout<<"\t"<<"請輸入該球員的體重:";
cin>>p[n-1].tizhong;
cout<<"\t"<<"請輸入該球員的球齡:";
cin>>p[n-1].qiuling;
for(r=0;r<n;r++)
{
outfile<<p[r].x<<"\t"<<p[r].name<<"\t"<<p[r].qiudui<<"\t"<<p[r].weizhi<<"\t"<<p[r].sex<<"\t"<<p[r].shengao<<"\t"<<p[r].tizhong<<"\t"<<p[r].qiuling<<endl;
}
outfile.close();
cout<<"\t"<<"增加成功!";
cout<<endl;
}
void xiugai(qiuyuan *p,int n)//修改球員信息
{
int r;
string gai_name;
ofstream outfile;
outfile.open("E:\\C++大一上學期\\球員名單.txt",ios::out);
cout<<endl;
cout<<"\t"<<"請輸入你要修改的球員的姓名:";
cin>>gai_name;
bool flag=false;
for(r=0;r<n;r++)
{
if(p[r].name==gai_name)
{
cout<<"\t"<<"請重新輸入該球員的號碼:";
cin>>p[r].x;
cout<<"\t"<<"請重新輸入該球員的姓名:";
cin>>p[r].name;
cout<<"\t"<<"請重新輸入該球員的球隊:";
cin>>p[r].qiudui;
cout<<"\t"<<"請重新輸入該球員的位置:";
cin>>p[r].weizhi;
cout<<"\t"<<"請重新輸入該球員的性別:";
cin>>p[r].sex;
cout<<"\t"<<"請重新輸入該球員的身高:";
cin>>p[r].shengao;
cout<<"\t"<<"請重新輸入該球員的體重:";
cin>>p[r].tizhong;
cout<<"\t"<<"請重新輸入該球員的球齡:";
cin>>p[r].qiuling;
flag=true;
break;
}
}
if(flag==false)
cout<<"\t"<<"查無此人!"<<endl;
else if(flag==true){
for(r=0;r<n;r++)
{
outfile<<p[r].x<<"\t"<<p[r].name<<"\t"<<p[r].qiudui<<"\t"<<p[r].weizhi<<"\t"<<p[r].sex<<"\t"<<p[r].shengao<<"\t"<<p[r].tizhong<<"\t"<<p[r].qiuling<<endl;
}
outfile.close();
cout<<"\t"<<"修改成功!";
cout<<endl;
}
}
void shanchu(qiuyuan *p) //刪除球員
{
int i,r;
string shan_name;
ofstream outfile;
outfile.open("E:\\C++大一上學期\\球員名單.txt",ios::out);
cout<<endl;
cout<<"\t"<<"請輸入你要刪除的球員的姓名:";
cin>>shan_name;
cout<<endl;
bool flag=false;
for(r=0;r<n;r++)
{
if(p[r].name==shan_name)
{
for(i=r;i<n;i++)
{
p[i].x=p[i+1].x;
p[i].name=p[i+1].name;
p[i].qiudui=p[i+1].qiudui;
p[i].weizhi=p[i+1].weizhi;
p[i].sex=p[i+1].sex;
p[i].shengao=p[i+1].shengao;
p[i].tizhong=p[i+1].tizhong;
p[i].qiuling=p[i+1].qiuling;
}
n--;
flag=true;
break;
}
}
if(flag==false)
cout<<"查無此人!"<<endl;
else if(flag==true){
for(r=0;r<n;r++)
{
outfile<<p[r].x<<"\t"<<p[r].name<<"\t"<<p[r].qiudui<<"\t"<<p[r].weizhi<<"\t"<<p[r].sex<<"\t"<<p[r].shengao<<"\t"<<p[r].tizhong<<"\t"<<p[r].qiuling<<endl;
}
outfile.close();
cout<<"\t"<<"刪除成功!";
cout<<endl;
}
}
系統使用反饋函數
void fankui()
{
int wenti;
string liuyan,lxfs;
ofstream out("E:\\C++大一上學期\\球員管理系統反饋.txt",ios::app);//在cpp的bin目錄下
cout<<endl;
cout<<"\t"<<"常見問題:"<<endl;
cout<<"\t"<<"---------------------------------"<<endl;
cout<<"\t"<<"1.錄入信息時顯示輸入錯誤"<<endl;
cout<<"\t"<<"2.修改球員信息時顯示查無此人"<<endl;
cout<<"\t"<<"3.刪除球員信息時顯示查無此人"<<endl;
cout<<"\t"<<"4.其他問題"<<endl;
cout<<"\t"<<"5.離開"<<endl;
cout<<"\t"<<"---------------------------------"<<endl;
cout<<"\t"<<"請輸入您遇到的問題對應的編號:";
cin>>wenti;
switch (wenti)
{
case 1:cout<<"\t"<<"輸入請注意身高體重的範圍,規範格式。";break;
case 2:cout<<"\t"<<"請檢查輸入的球員姓名是否有誤。";break;
case 3:cout<<"\t"<<"請檢查輸入的球員姓名是否有誤。";break;
case 4:cout<<endl;
cout<<"\t"<<"請留下您在使用的過程中遇到的問題:";
cin>>liuyan;
cout<<"\t"<<"請留下您的聯繫方式以便我們聯繫您:";
cin>>lxfs;
cout<<"\t"<<"感謝您的留言,我們將會不斷改善!";
cout<<endl;
out<<endl;
out<<"\t"<<"問題:"<<liuyan<<endl;
out<<"\t"<<"聯繫方式:"<<lxfs<<endl;
out.close();
break;
case 5:cout<<endl<<"\t"<<"即將返回,請稍等......";cout<<endl;break;
default:cout<<"\t"<<"選擇錯誤,按任意數字鍵即可返回菜單"<<endl;
cout<<"\t";cin>>wenti;
}
cout<<endl;
}
以上就是球員管理系統的基本代碼,源碼在下面的鏈接。有問題的可以留言,一定會盡量回復。
球員管理系統源碼及txt文件