2011-02-27 wcdj
问题来源:
http://topic.csdn.net/u/20110227/13/ee7a3998-5170-4c6e-b5e2-d20f5462968e.html?seed=788428640&r=71838505#r_71838505
问题描述:
实现一个电话簿的功能(电话簿的信息用结构体实现):
通过名字(区分大小写)找到相应的人的所有信息。(例如输入Li,就可以得到含有Li这个词的所有名字:LiYi,LiEr,ZhangLiang,LinKen, LingEr,, 还要找到LongKui,因为LongKui中含有L和i)
注意 :由于使用了TR1的扩展库,下面的代码要使用VS2010或sp1的VS2008才能编译。主要使用正则库中的regex_search函数来实现这个例子。
#include<iostream> #include <string> #include <regex> #include <iomanip> using namespace std; struct PhoneNameT { char name[22]; int phonenum; int postcode; char birthday[11]; }; struct LinkRec { PhoneNameT p; LinkRec *next; }; int main() { const int LEN =15; PhoneNameT n[LEN] = { {"LiYi",12387400,200240,"1990-1-1"}, {"LiEr",54741414,200240,"1989-12-9"}, {"ZhangLiang",46328821,200240,"1990-7-7"}, {"ZhangJiao",23001031,100000,"1989-11-11"}, {"ZhangZhiDong",12345643,200000,"1989-4-1"}, {"LinKen",12812818,300000,"1989-4-5"}, {"ZuoYou",52013140,100000,"1989-10-1"}, {"LongKui",10254102,200240,"1990-7-29"}, {"JiangMing",44052422,200000,"1989-4-4"}, {"JingTian",54515171,200000,"1990-4-1"}, {"GongAn",77714756,100000,"1990-5-9"}, {"GangYi",54745815,300000,"1989-8-8"}, {"HongKui",44471741,200240,"1990-7-29"}, {"GuangGang",55478155,400000,"1989-4-1"}, {"LingEr",25412623,200240,"1989-9-8"} }; cout << "通讯录内容:" << endl; cout << setiosflags(ios::left); PhoneNameT *pp = n; LinkRec *head, *rear,*a; for (int i=0; i<sizeof(n)/sizeof(PhoneNameT); ++i) cout << setw(20) << (pp+i)->name << (pp+i)->phonenum << '/t' << setw(10) << (pp+i)->postcode << '/t' << (pp+i)->birthday << endl; cout << "还需要添加几个人?" << endl; int m=0; cin >> m; PhoneNameT *NewPerson = NULL; if (m>0) { NewPerson = new PhoneNameT[m];// new 动态创建数组 head = rear=new LinkRec;// new 创建头节点 for (int i=0; i<m; ++i) { cout << "请输入第" << i+1 << "个人的信息:" << endl; cout << "姓名 电话号码 邮编 生日"<< endl; cin >> NewPerson[i].name >> NewPerson[i].phonenum >> NewPerson[i].postcode >> NewPerson[i].birthday; a = new LinkRec;// new 创建节点 a->p = NewPerson[i]; rear->next = a; rear = a; } rear->next = NULL;// 链表尾节点 } LinkRec *head1, *rear1,*a1; head1= rear1 = new LinkRec;// new 创建头节点 for (int i=0; i<LEN;++i) { a1 = new LinkRec; a1->p = n[i]; rear1->next = a1; rear1 = a1; } rear1->next=head->next;// 跳过新插入联系人链表的头结点 cout << "1.获得人物信息" << endl << "2.通过电话反查人物"<< endl << "3.寻找同城人" << endl << "4.最年轻的人" << endl; cout << "请选择方式:" << endl; int choose; cin >> choose; if (choose == 1) { char key [22]; LinkRec *h; cout << "输入关键词:" << endl; cin >> key; bool bFind = false; string str,sKey; vector<LinkRec*> lvec; tr1::regex rx; char *p = key; sKey = ".*"; while (*p) { sKey.append(1,*p);// 使用append连接单个字符到字符串 sKey += ".*"; ++p; } rx = sKey;// 保存模式匹配串 if(strcmp(key,""))// 输入关键字非空 { for (h = head1->next; h; h=h->next)// 初始化h = head1->next跳过头结点,因为头结点未初始化没有保存有效信息 { str = h->p.name; if (regex_search(str.begin(), str.end(), rx))// 使用正则regex_search函数 { bFind = true; lvec.push_back(h);// 保存匹配到的节点指针 } } } if (bFind)// 输出匹配的信息 { //cout << setiosflags(ios::left); for (vector<LinkRec*>::const_iterator iter=lvec.begin(); iter!=lvec.end();++iter) { cout << setw(20) << (*iter)->p.name << (*iter) ->p.phonenum << '/t' << setw(10) << (*iter)->p.postcode << '/t' << (*iter)->p.birthday << endl; } } else cout <<"查无此人"<<endl; } // 释放动态分配的资源 delete []NewPerson; LinkRec *p1 = head1, *p2;// 第2个链表 while (p1) { p2 = p1->next; delete p1; p1 = p2; } delete head;// 第1个链表,新插入联系人链表,删除头结点即可 return 0; }