这是我的源码,有需要的同学可以看看
#include <iostream>
#include <fstream>
using namespace std;
int LBs=0;
class BD
{
public:
char MZ[10];
int FZ,M,SL;
BD *p;
};
int HS(int a,int b)
{
int jg;
jg=a*10+b;
return jg;
}
BD* LB(ifstream Wj)
{
char jc; //用于检测输入的字符
int qh=0,i=0,sz[2]; //qh切换链表对象,i循环输入字符,sz输入的数字,jg最终结果
BD *LBt=NULL;
BD *LB1,*LB2;
LB1=LB2=new(BD);
while(1)
{
Wj.get(jc);
if(jc==37)
break;
switch (jc)
{
case '\t':
if(qh==0) LB1->MZ[i]='\0';
qh++;
i=0;
continue;
case '\n':
qh=i=0;
LB1->SL=0;
if(LBs==0)
{
LB1->p=LBt; LB2=LB1; LBt=LB1;
LB1=new(BD);
}
else
{
LB1->p=NULL;
LB2->p=LB1;
LB2=LB1;
LB1=new(BD);
}
LBs++;
continue;
default:
switch (qh)
{
case 0:
LB1->MZ[i]=jc;
i++;
break;
case 1:
sz[i]=jc-'0';
if(i==1)
LB1->FZ=HS(sz[0],sz[1]);
i++;
break;
case 2:
sz[i]=jc-'0';
if(i==1)
LB1->M=HS(sz[0],sz[1]);
i++;
break;
}
}
}
delete(LB1);
return LBt;
}
void LBsc(BD *p)
{
int i;
cout<<"-----------------------------------------------";
for(i=0;i<LBs;i++)
{
if(i%2==0) cout<<endl<<endl;
if(i<9)
{
cout<<i+1<<".---"<<p->MZ<<'('<<p->FZ<<"分"<<p->M<<"秒"<<")\t";
p=p->p;
}
else
{
cout<<i+1<<".--"<<p->MZ<<'('<<p->FZ<<"分"<<p->M<<"秒"<<")\t";
p=p->p;
}
}
cout<<endl<<endl<<"-----------------------------------------------"<<endl<<endl;
}
void CSjs(BD *p)
{
int BY,i,xh,sl;
BD *p2=p;
cout<<endl<<"请输入您的兵营数量:";
cin>>BY;
cout<<endl;
while(sl!=0)
{
p=p2;
cout<<"------------------------------------------------";
for(i=0;i<LBs;i++)
{
if(i%3==0) cout<<endl<<endl;
if(i<9)
{
cout<<i+1<<".---"<<p->MZ<<"X"<<p->SL<<"\t";
p=p->p;
}
else
{
cout<<i+1<<".--"<<p->MZ<<"X"<<p->SL<<"\t";
p=p->p;
}
}
cout<<endl<<endl<<"0.---计算并退出";
cout<<endl<<endl;
cout<<"------------------------------------------------"<<endl<<endl;
p=p2;
cout<<"请输入兵种序号<0~"<<LBs<<">:";
cin>>xh;
cout<<endl;
xh--;
if(xh==-1) break;
for(i=0;i<xh;i++)
{
p=p->p;
}
cout<<"-----"<<p->MZ<<"-----"<<endl<<"请输入数量:";
cin>>sl;
cout<<endl;
p->SL=sl;
}
}
void XHxs(BD *p)
{
int XX;
while(1)
{
cout<<"----------选项列表----------"<<endl;
cout<<"1.----------查看士兵种类信息"<<endl;
cout<<"2.--执行自动分配士兵制造计算"<<endl;
cout<<"0.----------------------退出"<<endl;
cout<<"----------------------------"<<endl;
cout<<"选项(0~2):";
cin>>XX;
switch(XX)
{
case 1:
LBsc(p);
break;
case 2:
CSjs(p);
break;
case 0:
goto muck;
default:
cout<<"请输入正确的格式<0~2>!!!"<<endl<<endl;
}
}
muck:;
}
main()
{
BD *p=NULL;
ifstream Wj("BL.ini",ios::in);
if(!Wj)
{
cerr<<"文件打开失败!"<<endl;
exit(1);
}
cout<<"进行初始化链表"<<endl;
p=LB(Wj);
cout<<"初始化成功!"<<endl;
cout<<"共有"<<LBs<<"种士兵!"<<endl<<endl;
XHxs(p);
cout<<"已退出程序!"<<endl;
Wj.close();
exit (0);
}