| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1917 人关注过本帖
标题:在下面的程序中想加一段 名字按首字母排序的程序,求帮忙
只看楼主 加入收藏
q593157989
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-1-5
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
在下面的程序中想加一段 名字按首字母排序的程序,求帮忙
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
int count=0;
class CData//
{
public:
CData(){};
virtual int Compare(CData &,int)=0;
virtual void Show()=0;
virtual ~CData(){};
};
class CNode
{
private:
CData *pData;
CNode *pNext;
public:
CNode(){pData=0;pNext=0;};
CNode(CNode &node)
{
pData=node.pData;
pNext=node.pNext;
}
void InputData(CData *pdata){pData=pdata;}
void ShowNode(){pData->Show();}
CData *GetData(){return pData;}
friend class CList;
};
class CList
{

CNode *pHead;
public:
CList(){pHead=0;};
~CList(){DeleteList();}
void AddNode(CNode *pnode);
CNode *DeleteNode(CNode *);
CNode *LookUp(CData &);
bool LookUpF(CData &);
void ShowList();
void DeleteList();
CNode *GetListHead(){return pHead;}
CNode *GetListNextNode(CNode *pnode);
};
CNode *CList::GetListNextNode(CNode *pnode)
{
CNode *p1=pnode;
return p1->pNext;
};
void CList::AddNode(CNode *pnode)
{
if (pHead==0)
{
pHead=pnode;
pnode->pNext=0;
return;
}
else
{
pnode->pNext=pHead;
pHead=pnode;
}
};
CNode *CList::DeleteNode(CNode *pnode)
{
CNode *p1,*p2;
p1=pHead;
while(p1!=pnode&&p1->pNext!=0)
{
p2=p1;
p1=p1->pNext;
}
if (p1==pHead)
{
pHead=pHead->pNext;
return pnode;
}
p2->pNext=p1->pNext;
return pnode;
}
CNode *CList::LookUp(CData &data)
{
CNode *p1=pHead;
while(p1)
{
if (p1->pData->Compare(data,1)==0)
return p1;
p1=p1->pNext;
}
return 0;
}
bool CList::LookUpF(CData &data)
{
bool f1=false;
CNode *p1=pHead;
while(p1)
{
if (p1->pData->Compare(data,0)==0)
{
p1->ShowNode();
f1=true;
}
p1=p1->pNext;

}
return f1;
}
void CList::ShowList()
{
CNode *p1=pHead;
while(p1)
{
p1->pData->Show();
p1=p1->pNext;
}
}
void CList::DeleteList()
{
CNode *p1,*p2;
p1=pHead;
while(p1)
{
delete p1->pData;
p2=p1;
p1=p1->pNext;
delete p2;
}
}
class CTelRecord:public CData
{
private :
char szName[20];
char szNumber[20];
char szF;
public:
CTelRecord(){strcpy(szName,"\0");strcpy(szNumber,"\0");}
CTelRecord(char *name,char *number)
{
strcpy(szName,name);
strcpy(szNumber,number);
szF=name[0];
}
void SetRecord(char *name, char *number)
{
strcpy(szName,name);
strcpy(szNumber,number);
szF=name[0];
}
int Compare(CData &,int);
void Show();
};
int CTelRecord::Compare(CData&data,int choice)
{
CTelRecord &temp=(CTelRecord &)data;
if(choice==1)
return strcmp(szName,temp.szName);
else
return (szF==temp.szF ? 0:1);
}
void CTelRecord::Show()
{
cout<<setw(15)<<szName<<setw(15)<<szNumber<<endl;
}
void AddRecord(CList &TelList)
{
CNode *pNode;
CTelRecord *pTel;
char szName[20],szNumber[20];
cout<<"请输入姓名(输入0退出,并进入系统菜单)"<<endl;
cin.getline(szName,20);
while(strcmp(szName,"0"))
{
cout<<"请输入电话号码: "<<endl;
cin.getline(szNumber,20);
pTel=new CTelRecord;
pTel->SetRecord(szName,szNumber);
pNode=new CNode;
pNode->InputData(pTel);
TelList.AddNode(pNode);
count++;
cout<<"请输入姓名(输入0退出,并进入系统菜单) "<<endl;
cin.getline(szName,20);
}
cout<<endl<<endl;
}

void DisplayRecord(CList&TelList)
{
cout<<"目前共有 "<<count<<" 条记录,具体记录如下:"<<endl;
cout<<setw(15)<<"【姓名】"<<setw(15)<<"【电话号码】"<<endl;
TelList.ShowList();
cout<<endl<<endl;
system("pause");
}

void LookUpRecord(CList&TelList)
{
CNode *pLook;
char szName[20];
cout<<"请输入您需要查找的姓名(输入0退出,并进入系统菜单)"<<endl;
cin.getline(szName,20);
while (strcmp(szName,"0"))
{
CTelRecord tele(szName,"0");
pLook=TelList.LookUp(tele);
if (pLook)
{
cout<<"在电话簿中找到"<<szName<<",内容是:"<<endl;
cout<<setw(15)<<"【姓名】"<<setw(15)<<"【电话号码】"<<endl;
pLook->ShowNode();
}
else
cout<<"在电话簿中找不到"<<szName<<","<<endl;
cout<<"请输入您需要查找的姓名(输入0退出,并进入系统菜单)"<<endl;
cin.getline(szName,20);
}
cout<<endl<<endl;
}

void DeleteRecord(CList&TelList)
{
CNode *pLook;
char szName[20];
cout<<"请输入您需要删除的姓名(输入0退出,并进入系统菜单)"<<endl;
cin.getline(szName,20);
while(strcmp(szName,"0"))
{
CTelRecord tel(szName,"0");
pLook=TelList.LookUp(tel);
if (pLook)
{
cout<<"在电话簿中找到"<<szName<<",内容是:"<<endl;
pLook->ShowNode();
cout<<"请确定是否删除此记录(Y/N)【确定删除请输入Y或y,取消删除请输入N或n】:"<<endl;
char ok;
cin>>ok;
cin.ignore();
if (ok=='Y'||ok=='y')
{
TelList.DeleteNode(pLook);
cout<<szName<<"的资料删除成功!"<<endl;
delete pLook;
count--;
}
else if(ok=='N'||ok=='n')
cout<<szName<<"的资料删除失败"<<endl;
}
else
cout<<"在电话簿中找不到"<<szName<<","<<endl;
cout<<"请输入您需要删除的姓名(输入0退出,并进入系统菜单)"<<endl;
cin.getline(szName,20);
}
cout<<endl<<endl;
}
void ModifyRecord(CList &TelList)
{
CNode *pLook;
CTelRecord *pTel;
char szName[20],szNumber[20];
cout<<"请输入您需要修改的姓名(输入0退出,并进入系统菜单)"<<endl;
cin.getline(szName,20);
while(strcmp(szName,"0"))
{
CTelRecord tel(szName,"0");
pLook=TelList.LookUp(tel);
if (pLook)
{
cout<<"在电话簿中找到"<<szName<<",内容是:"<<endl;
pLook->ShowNode();
cout<<"-----下面开始修改-----"<<endl<<"请输入修改后的姓名: "<<endl;
cin.getline(szName,20);
cout<<"请输入修改后的电话号码:"<<endl;
cin.getline(szNumber,20);
cout<<"请确定是否修改此记录(Y/N)【确定修改请输入Y或y,取消修改请输入N或n】:"<<endl;
char ok;
cin>>ok;
cin.ignore();
if (ok=='Y'||ok=='y')
{
pTel=new CTelRecord;
*pTel=tel;
pTel->SetRecord(szName,szNumber);
pLook->InputData(pTel);
cout<<szName<<"的资料修改成功!"<<endl;
}
else if(ok=='N'||ok=='n')
cout<<szName<<"的资料修改失败!"<<endl;
}
else
cout<<" 在电话簿中找不到"<<szName<<","<<endl;
cout<<" 请输入您需要修改的姓名(输入0退出,并进入系统菜单)";
cin.getline(szName,20);
}
}

void StoreFile(CList&TelList)
{
ofstream outfile("TELEPHONE.DAT",ios::binary);
if (!outfile)
{
cout<<" 数据库文件打开错误,没有将数据存入文件!\n";
return;
}
CNode *pnode;
CTelRecord *pTel;
string strName,strNumber;
pnode=TelList.GetListHead();
while(pnode)
{
pTel=(CTelRecord *)pnode->GetData();
outfile.write((char *)pTel,sizeof(CTelRecord));
pnode=TelList.GetListNextNode(pnode);
}
outfile.close();
}
void Operate(string &strChoice,CList&TelList)
{
if (strChoice=="1")
AddRecord(TelList);
else if (strChoice=="5")
DisplayRecord(TelList);

else if (strChoice=="3")
LookUpRecord(TelList);

else if (strChoice=="4")
DeleteRecord(TelList);
else if(strChoice=="2")
ModifyRecord(TelList);
else if (strChoice=="6")

StoreFile(TelList);
else cout<<"对不起,您的输入有误,请重新输入您的选择: "<<endl;
}
void LoadFile(CList &TelList)
{
fstream infile("TELEPHONE.DAT",ios::binary);
if (!infile)
{
cout<<"没有数据文件!\n\n";
return;
}
CNode *pNode;
CTelRecord *pTel;
while (!infile.eof())
{
pTel=new CTelRecord;
infile.read((char*)pTel,sizeof(CTelRecord));
pNode=new CNode;
pNode->InputData(pTel);
TelList.AddNode(pNode);
}
TelList.DeleteNode(pNode);
infile.close();
}

int main()
{
CList TelList;
system("cls");
cout<<"*******************************************************************"<<endl;
cout<<" --------------******欢迎进入电话簿管理系统******-------------\n";
cout<<"*******************************************************************"<<endl;
LoadFile(TelList);
string strChoice;
do
{ cout<<"-------------【欢迎进入系统菜单】------------- "<<endl;
cout<<" 1.添加联系人 "<<endl;
cout<<" 2.修改数据 "<<endl;
cout<<" 3.查询联系人 "<<endl;
cout<<" 4.删除联系人 "<<endl;
cout<<" 5.全部联系人 "<<endl;
cout<<" 6.退 出 "<<endl;
cout<<"【请输入您的选择】:"<<endl;
cin>>strChoice;
cin.ignore();
Operate(strChoice,TelList);
}while(strChoice!="6");
StoreFile(TelList);
cout<<"*******************************************************************"<<endl;
cout<<" ------------******欢迎再次使用电话簿管理系统******---------- "<<endl;
cout<<"*******************************************************************"<<endl;
system("pause");
return 0;
}
搜索更多相关主题的帖子: private include public return count 
2016-01-05 22:24
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9026
专家分:54030
注 册:2011-1-18
收藏
得分:10 
你这代码中都是无关的东西,谁看呀?

程序代码:
char xxx_gb2312( unsigned long word )
{
    static const unsigned long areacode[] = {
                          0xB0A1, 0xB0C5, 0xB2C1, 0xB4EE, 0xB6EA, 0xB7A2, 0xB8C1 // a b c d e f g
                        , 0xB9FE, 0xBBF7, 0xBBF7, 0xBFA6, 0xC0AC, 0xC2E8, 0xC4C3 // h i j k l m n
                        , 0xC5B6, 0xC5BE, 0xC6DA,         0xC8BB, 0xC8F6, 0xCBFA // o p q   r s t
                        , 0xCDDA, 0xCDDA, 0xCDDA,         0xCEF4, 0xD1B9, 0xD4D1 // u v w   x y z
                        , 0xF7FF };

    if( word>=areacode[0] && word<areacode[26] && (word&0xFF)>0xA0 && (word&0xFF)<0xFF )
    {
        for( int i=0; i<26; ++i )
        {
            if( word>=areacode[i] && word<areacode[i+1] )
                return 'A'+i;
        }
    }
    return '?';
}

#include <string>
std::string GetChineseSpell( const char* ptext ) // for test
{
    std::string s;

    for( const unsigned char* p=(const unsigned char*)ptext; *p; )
    {
        if( *p>0xA0 )
        {
            s += xxx_gb2312( p[0]<<8 | p[1] );
            p += 2;
        }
        else
        {
            s += xxx_gb2312( p[0] );
            p += 1;
        }
    }

    return s;
}

#include <iostream>
using namespace std;

int main( void )
{
    cout << GetChineseSpell("获取汉字的首拼字母") << endl;

    return 0;
}

2016-01-06 10:11
q593157989
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-1-5
收藏
得分:0 
回复 2楼 rjsp
怎么加到上面那一段中啊
2016-01-06 10:23
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:10 
這都不是C++程序,用C++哪有問這個問題的。C++標準庫中有現成的字符串排序算法可用,還用問怎麽排的麽。都沒説你放著string不用而用cz字符串、以及自己構造鏈表的多餘行爲了。

授人以渔,不授人以鱼。
2016-01-06 17:43
快速回复:在下面的程序中想加一段 名字按首字母排序的程序,求帮忙
数据加载中...
 
   



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

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