| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 463 人关注过本帖
标题:为什么这个程序的退出不能正常退出啊,而且查找不存在的名字会报错。。郁闷 ...
只看楼主 加入收藏
lxstorm
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-7-2
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
为什么这个程序的退出不能正常退出啊,而且查找不存在的名字会报错。。郁闷中。。。
#include<stdlib.h>
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
/********************************************************************************************************/
class CSalary
{
private:
    char szName[20];        //姓名
    double dlSalary;        //工资
public:
    CSalary(){    strcpy(szName,"\0");dlSalary=0.0;}
    CSalary(char *,double);
    void SetSalary(char *,double);            //置工资与姓名
    double GetSal(){return dlSalary; }       //返回工资
    int Compare(CSalary &);                    //比较姓名,供查找用,比较结果为1,0
    void Show();//显示工资
    char *GetName(){return szName;}
};

////////////////////////////////////////////////////////
void CSalary::SetSalary(char * name,double salary)
    {    for(int i=0;i<20;i++)
        {
        if(*(name+i)=='\0')
            break;
        else
            *(szName+i)=*(name+i);
        }
            *(szName+i)='\0';
            dlSalary=salary;
    }
int CSalary::Compare(CSalary &m)                    //比较本数据类中的szName与m中的szName是否相同,相同返回1,不同返回0
    {   
        if(strcmp(szName,m.szName)==0)
            return 1;
        else
            return 0;
    }
void CSalary::Show()
{
    cout<<"姓名:"<<szName<<"\t\t工资:"<<dlSalary<<endl;
}




/********************************************************************************************************/
class CNode                //定义结点类
{
private:
    CSalary *pData;            //用于指向数据类的指针,这是每个数据的不同部分
    CNode *pNext;            //指向链表的指针
public:
    CNode(){pData=0;pNext=0;}        //结点构造函数
    CNode(CNode & node)                //用于拷贝的构造函数
    {
        pData=node.pData;
        pNext=node.pNext;
    }
    void InputData(CSalary *pSal){pData=pSal;}        //输入数据
    void ShowNode(){pData->Show();}
    CSalary *GetData(){return pData;}
    friend class CList;            //定义链表类为友元类
};





/*************************************************************************************************************/
class CList                    //定义链表类
{

protected:
    CNode *pHead;            //链表头结点指针
public:
    CList(){pHead=0;}
    ~CList(){DeleteList();}
    void AddNode(CNode *pnode);            //在尾部添加结点
    void  DeleteNode(CNode *);        //删除一个指定的结点,返回该结点的指针
    CNode * LookUp(CSalary &);            //查找一个指定的数据,返回该数据所在节点在链表中的指针,若未找到返回0
    void ShowList();        //打印整个链表
    void DeleteList();        //删除整个链表
    CNode *GetListHead(){ return pHead;}        //返回链表首结点
    CNode *GetListNextNode(CNode *);            //返回链表指定结点的下一个结点
    void Insert(CNode *);            //按工资的顺序插入一个结点
};

////////////////////////////////////

void CList::AddNode(CNode * pnode)                    //在尾部添加结点
{    CNode * p;
    if(pHead==0)
    {
        pHead=pnode;
        pnode->pNext=0;
    }
    else
    {    p=pHead;
        while(p->pNext!=NULL)
        {
            p=p->pNext;
        }
            p->pNext=pnode;
            pnode->pNext=0;

    }   
}
void CList::DeleteNode(CNode *pnode)
{
    if(pnode==pHead&&pHead->pNext==0)
    {
        delete pHead->pData;
        delete pHead;
        pHead=0;
    }
    else if(pnode==pHead)
    {   
        CNode *p=pnode->pNext;
        delete pHead->pData;
        delete pHead;
        pHead=p;
    }
    else
    {
        CNode *p,*q;
        p=pHead;
        while(p != NULL && p->pNext != pnode)
        {
            p=p->pNext;
        }
        q=p->pNext;
        p->pNext=pnode->pNext;
        delete pnode->pData;
        delete pnode;
    }

}
CNode * CList::LookUp(CSalary &salary)
{   
    CNode *p=pHead;
    if (pHead == NULL)
    {   
        return NULL;

    }
    else {
        while(p->pData->Compare(salary)==0 && p!=NULL)
        {
        p=p->pNext;
        }
        if (p==NULL)
            return 0;
        return p;
    }

}
void CList::ShowList()
{    if(pHead == NULL)
        cout<<"链表为空!"<<endl;
    CNode *p=pHead;
    while (p!= NULL)
    {
        p->ShowNode();
        p=p->pNext;
    }

}
void CList::DeleteList()
{
    if(pHead == NULL)
        return;
    CNode *p,*q;
        while(p!=NULL)
        {
            delete p->pData;
            q=p;
            p=p->pNext;
            delete q;
        }
}
CNode * CList::GetListNextNode(CNode * pnode)
{
   
    return pnode->pNext;
}
void CList::Insert(CNode * pnode)
{
    if(pHead==NULL)
    {    pHead=pnode;
        pnode->pNext=NULL;
    }
    CNode *p,*q;
    p=pHead;
    if(pHead->pData->GetSal() > pnode->pData->GetSal())
    {
        pnode->pNext=pHead;
        pHead=pnode;
        return;
    }
        while(pnode->pData->GetSal() > p->pData->GetSal() && p->pNext!=NULL)
        {
            q=p;
            p=p->pNext;
        }   
        if(p->pNext!=NULL){
            pnode->pNext=p;
            q->pNext=pnode;
            }
        else{
            pnode->pNext=NULL;
            p->pNext=pnode;
            }   
}
   
   
   

/**********************************************************************************************************************/
CList SalaryList;
/*********************************************************************************************************************/

void Initial()
{    cout<<"*******************************************************************************"<<endl;
    cout<<"**                                                                           **"<<endl;
    cout<<"**                           欢迎使用工资管理系统                            **"<<endl;
    cout<<"**                                                                           **"<<endl;
    cout<<"*******************************************************************************"<<endl;
    cout<<"**                                                                           **"<<endl;
    cout<<"**                                                                           **"<<endl;
    cout<<"**                                                                           **"<<endl;
    cout<<"**---------->请选择以下功能:                                                 **"<<endl;
    cout<<"**                                                                           **"<<endl;
    cout<<"**---------->1.添加工资记录                                                  **"<<endl;
    cout<<"**                                                                           **"<<endl;
    cout<<"**---------->2.显示工资记录                                                  **"<<endl;
    cout<<"**                                                                           **"<<endl;
    cout<<"**---------->3.根据姓名查询工资数据                                          **"<<endl;
    cout<<"**                                                                           **"<<endl;
    cout<<"**---------->4.根据姓名删除工资数据                                          **"<<endl;
    cout<<"**                                                                           **"<<endl;
    cout<<"**---------->5.根据姓名修改工资数据                                          **"<<endl;
    cout<<"**                                                                           **"<<endl;
    cout<<"**---------->6.保存并退出                                                    **"<<endl;
    cout<<"**                                                                           **"<<endl;
    cout<<"*******************************************************************************"<<endl;
    cout<<"请输入你的选择:"<<endl;
}
void AddRecord(CList &SalaryList)        //将记录添加到链表中,链表是唯一的
{
    CNode *pNode;
    CSalary *pSal;
    char szName[20];
    double dlPlaceSalary;
    cout<<"输入姓名(输入0 结束): ";
    cin.getline(szName,20);
    while(strcmp(szName,"0"))
    {
        cout<<"请输入工资:\t";
        cin>>dlPlaceSalary;
        cin.ignore();
        pSal=new CSalary;        //生成新的数据类对象
        pSal->SetSalary(szName,dlPlaceSalary);        //数据类对象赋值
        pNode=new CNode;        //生成新的结点
        pNode->InputData(pSal);            //结点赋值
        SalaryList.Insert(pNode);        //结点加入链表
        cout<<"输入姓名(输入0 结束):";
        cin.getline(szName,20);
    }
    cout<<endl<<endl;
}

void Open()
{
    ifstream infile("EMP.txt");
    char name[20];
    double salary;
    while(infile>>name>>salary)
    {
        CSalary *psalary=new CSalary;
        psalary->SetSalary(name,salary);
        CNode *pnode=new CNode;
        pnode->InputData(psalary);
        SalaryList.AddNode(pnode);
    }
}
void Save()
{
    ofstream outfile("EMP.txt");
    char name[20];
    double salary;
    CNode *p;
    p=SalaryList.GetListHead();
    while(p!=NULL)
    {
        strcpy(name,p->GetData()->GetName());
        salary=p->GetData()->GetSal();
        outfile<<name<<" "<<salary<<endl;
        p=SalaryList.GetListNextNode(p);
    }
}

void ShowRecord()            
{
    SalaryList.ShowList();
}

void SearchShow()
{
    CNode *pnode;
    CSalary *pSal;
    char name[20];
    system("cls");
    cout<<"请输入要查找的姓名"<<endl;
    cin.getline(name,20);
    pSal=new CSalary;
    pSal->SetSalary(name,0);
    pnode=new CNode;
    pnode=SalaryList.LookUp(*pSal);
    if(pnode==0)
        cout<<"未找到相关数据"<<endl;
    else
        pnode->ShowNode();
        
}
void Delete()
{
    CNode *pnode;
    CSalary *pSal;
    char name[20];
    system("cls");
    cout<<"请输入要删除的姓名"<<endl;
    cin.getline(name,20);
    pSal=new CSalary;
    pSal->SetSalary(name,0);
    pnode=new CNode;
    pnode=SalaryList.LookUp(*pSal);
    if(pnode==0)
        cout<<"删除失败,未找到相关数据"<<endl;
   

}
/***********************************************************************************************************************/
void main()
{   
        int nChoice;
        char str[30];
        Open();
        while(1){
            Initial();
            cin.getline(str,30);
            nChoice=atoi(str);
            switch(nChoice){
                case 1:
                    system("cls");
                    AddRecord(SalaryList);
                    break;
                case 2:
                    system("cls");
                    ShowRecord();
                    getch();
                    break;
                case 3:
                    system("cls");
                    SearchShow();
                    getch();
                    break;
                case 4:
                    Delete();
                    break;
                case 6:
                    Save();
                    break;
                default:
                    cout<<"输入非法,请重新输入!"<<endl;
                    getch();
                    break;
                }
            if(nChoice==6)
                break;
        }
   
}
搜索更多相关主题的帖子: 工资 姓名 private include public 
2013-04-03 18:25
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:10 
case 6:
                     Save();
                     exit(1);
                     break;
这样就可以正常退出了,你只用break;不行,查找不存在的肯定要有提示的,

Maybe
2013-04-03 20:32
lxstorm
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-7-2
收藏
得分:0 
回复 2楼 邓士林
但是我在SearchShow函数中给出了找不存在的情况了啊,怎么没有用的。。直接就报错了。。
2013-04-03 21:04
快速回复:为什么这个程序的退出不能正常退出啊,而且查找不存在的名字会报错。。 ...
数据加载中...
 
   



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

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