求问我的这个程序的查询功能为什么这么鸡肋,根本找不到到底哪里出错了?求指正
附上代码:#include<iostream>#include<iomanip>
#include<fstream>
#include<cstring>
#include<cstdlib>
using namespace std;
class CWorker;
void Read(CWorker &wor);
istream&operator>>(istream&is,CWorker &wor);
class CWorker
{
public:
CWorker(){}
CWorker(char*number,char*name,char*birth,char*Bumen,int GZjb,char*pho,char sex='M')
{
strncpy(this->number,number,9);
strncpy(this->name,name,20);
strncpy(this->birth,birth,9);
strncpy(this->Bumen,Bumen,20);
this->GZjb=GZjb;
strncpy(this->pho,pho,6);
this->sex=sex;
}
void SetData(char ch='Y') //输入职工信息
{
cout<<"~~~~~~~~从键盘输入职工信息~~~~~~~~"<<endl;
while(ch!='N')
{
cout<<"工号:";
cin>>number;
cout<<"姓名:";
cin>>name;
cout<<"出生年月:";
cin>>birth;
cout<<"所在部门:";
cin>>Bumen;
cout<<"工资级别:";
cin>>GZjb;
cout<<"电话:";
cin>>pho;
cout<<"性别:";
cin>>sex;
cout<<"是否开始输入?请选择Y or N:";
cin>>ch;
}
}
char* GetNumber()
{
return number;
}
void Show(int n)
{
static bool isHead=false;
//输出表头
if(!isHead){
if(n>0)
cout<<setw(3)<<"序号";
cout<<setw(8)<<"工号"<<setw(8)<<"姓名"<<setw(10)<<"出生年月"<<setw(10)<<"所在部门"
<<setw(10)<<"工资级别"<<setw(8)<<"电话"<<setw(8)<<"性别"<<endl;
isHead=true;
}
if(n>0) cout<<setw(2)<<n;
cout<<setw(11)<<number<<setw(8)<<name<<setw(8)<<birth<<setw(10)<<Bumen<<setw(8)<<GZjb<<setw(12)<<pho<<setw(6)<<sex<<endl;
}
friend istream& operator>>(istream&is,CWorker &wor);
private :
char number[9];
char name[20];
char birth[9];
char Bumen[10];
char pho[5];
int GZjb;
char sex;
};
istream& operator>>(istream&is,CWorker &wor)
{
is>>wor.number; is>>wor.name; is>>wor.birth;
is>>wor.Bumen; is>>wor.GZjb; is>>wor.pho;
is>>wor.sex ;
return is;
}
class CNode //结点类
{
friend class CList;
public:
CWorker GetData()
{
return data;
}
CNode* GetNext()
{
return next;
}
private:
CWorker data; //数据域
CNode *next; //指针域
};
class CList //链表类
{
public:
CList()
:pHead(NULL),pCur(NULL)
{}
~CList()
{
CNode *p=pHead;
while(pHead)
{
pHead=p->next;
delete p;
p=pHead;
}
}
void SetCurToHead() //将当前指针定位到头指针的位置
{
pCur=pHead;
}
CNode* cha(char strNumber[100]) //按编号查询
{
// Read(wor);
CNode *p=pHead;
// CNode *pHead=NULL;
while(p)
{
if(strcmp(p->data.GetNumber(),strNumber)==0)
{
cout<<"UU"<<endl;
return p;
}
else
p=p->next;
}
CNode* find=p;
if(find)
{
find->GetData().Show(1);
}
else
cout<<"未找到此记录!"<<endl;
return NULL;
}
void operator+=(CWorker &wor) //重载链表的+=操作,使得文件数据录入链表
{
CNode*b=new CNode;
b->data=wor;
//添加到链表中
CNode*p=pHead;
if(NULL==p)
{
pHead=b; b->next=NULL; //将b作为第一个结点
pCur=pHead;
}
else
{ //找到链表的最后一个结点
while(p->next!=NULL) p=p->next ;
p->next=b; b->next=NULL;
}
}
private:
CNode *pHead;
CNode *pCur;
CWorker wor;
};
void filedao()
{
CWorker wor;
cout<<"~~~~~~~从文件导入记录~~~~~~~~~"<<endl;
fstream file1; //定义一个fstream类的对象用于读
file1.open("职工个人信息.txt",ios::in); //用成员函数open打开文件
if(!file1)
{
cout<<"职工个人信息.txt不能打开!"<<endl;
exit(1);
}
fstream file2; //定义一个fstream类的对象用于写
file2.open("职工个人信息2.txt",ios::out);
if(!file2)
{
cout<<"职工个人信息.txt不能创建!"<<endl;
file1.close();
exit(1);
}
char ch;
while(!file1.eof()) //判断文件结尾
{
file1.read(&ch,1); //使用成员函数read和write读写文件
cout<<ch;
file2.write(&ch,1);
}
file2.close(); //文件使用结束后及时关闭
file1.close();
cout<<endl;
}
int main()
{
int n;
CWorker wor;
CList list;
cout<<"==========职工信息管理系统============"<<endl;
cout<<"请选择要操作的指令号:"<<endl;
cout<<" 1.从文件导入记录:"<<endl;
cout<<" 2.从键盘输入记录:"<<endl;
cout<<" 3.按编号查找编辑记录:"<<endl;
cout<<" 4.退出"<<endl;
cout<<"================endl=================="<<endl;
cout<<"请选择:";
cin>>n;
if(n>4||n<1)
{
cout<<"输入错误!请重新输入:";
cin>>n;
}
while(n>=1&&n<4)
{
while(n!=4)
{
switch(n)
{
case 1: filedao(); break;
case 2: wor.SetData(); break;
case 3:
cout<<"~~~~~~~~按编号查找记录~~~~~~~~~"<<endl;
char strNum[100];
cout<<"请输入想要查询的编号:";
cin>>strNum;
fstream file1;
//cout<<"11"<<endl;
file1.open("职工个人信息.txt",ios::in); //用成员函数open打开文件
cout<<"2"<<endl;
if(!file1)
{
cout<<"职工个人信息.txt不能打开!"<<endl;
exit(1);
}
while(file1>>wor) //将文件录入wor中
{
cout<<"UU"<<endl;
list+=wor; //将录入wor中的文件录入链表中
}
file1.close();
list.cha(strNum); //查询
cout<<"是否进行编辑?请选择Y/N:";
char str;
cin>>str;
if(str=='Y')
{
CWorker wor1;
cout<<"请重新设置该位职工的个人信息";
wor1.SetData();
cout<<"修改成功!";
}
else if(str=='N')
{
exit(1);
}
else
{
cout<<"输入错误!"<<endl;
}
cout<<"没有找到!"<<endl;
return NULL;
break;
}
cout<<"==========职工信息管理系统============"<<endl;
cout<<" 请选择要操作的指令号:"<<endl;
cout<<" 1.从文件导入记录:"<<endl;
cout<<" 2.从键盘输入记录:"<<endl;
cout<<" 3.按编号查找记录:"<<endl;
cout<<" 4.退出"<<endl;
cout<<"================endl=================="<<endl;
cout<<"请重新选择:";
cin>>n;
}
}
return 0;
}