| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 554 人关注过本帖
标题:求问我的这个程序的查询功能为什么这么鸡肋,根本找不到到底哪里出错了?求 ...
只看楼主 加入收藏
G梦
Rank: 1
等 级:新手上路
帖 子:48
专家分:7
注 册:2012-12-27
结帖率:88.89%
收藏
已结贴  问题点数:20 回复次数:6 
求问我的这个程序的查询功能为什么这么鸡肋,根本找不到到底哪里出错了?求指正
附上代码:#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;
 }
搜索更多相关主题的帖子: include public number 
2013-09-06 13:38
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:7 
编译错误还是 运行不到你想要的结果?
请明确问题

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-09-06 14:51
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:7 
统一二楼意见
估计又是哪个做作业的,上网搜到代码,运行不了,来求助的哩...

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-09-06 15:36
G梦
Rank: 1
等 级:新手上路
帖 子:48
专家分:7
注 册:2012-12-27
收藏
得分:0 
回复 3楼 peach5460
请看看好,到底是不是网上搜的
2013-09-06 15:54
G梦
Rank: 1
等 级:新手上路
帖 子:48
专家分:7
注 册:2012-12-27
收藏
得分:0 
回复 2楼 yuccn
就是创建文件将其导入之后,再利用查询的功能时不能找到相应的记录
2013-09-06 15:55
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:0 
以下是引用G梦在2013-9-6 15:55:21的发言:

就是创建文件将其导入之后,再利用查询的功能时不能找到相应的记录


你这样说还是不够具体的,人家不见到你导入的文件是什么文件(空文件?)
别人不知道你输入了什么,需要查询什么,或者期望结果是什么,这些问题都没有明确,人家看这样的问题也是很郁闷的

你自己下断点调试下咯

@test

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-09-06 15:59
G梦
Rank: 1
等 级:新手上路
帖 子:48
专家分:7
注 册:2012-12-27
收藏
得分:0 
回复 6楼 yuccn
导入的是一个知过能改个人信息的文件,包括职工的工号,姓名,所在部门,工资级别,性别等东西; 查找的时候就是按照编号来查询的
2013-09-06 21:14
快速回复:求问我的这个程序的查询功能为什么这么鸡肋,根本找不到到底哪里出错了 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040878 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved