| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4055 人关注过本帖
标题:求教通讯录管理系统中的姓名按字典顺序排列
只看楼主 加入收藏
玉垒浮云
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2012-11-22
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:10 
求教通讯录管理系统中的姓名按字典顺序排列
#include<iostream>
#include<vector>
#include<string>
using namespace std;
struct information
{
    string name;
    int number;
    string address;
};
int main()
{
    vector<information>aa;
    information temp;
    int n;
    cout<<"please input the amount of pepole"<<endl;
    cin>>n;
   
    for(int i=0;i!=n;++i)
    {
        cin>>temp.name>>temp.number>>temp.address;
        aa.push_back(temp);
    }
   
   
    for(vector<information>::iterator j=aa.begin();j!=aa.end();++j)
    {
        
        cout<<(*j).name<<" "<<(*j).number<<" "<<(*j).address<<endl;
    }
    cout<<"please input name you want to look for"<<endl;

    string name1;
    cout<<"please input name1"<<endl;
    cin>>name1;
    for(vector<information>::iterator k=aa.begin();k!=aa.end();++k)
    {
        if((*k).name==name1)
            cout<<(*k).name<<(*k).number<<(*k).address<<endl;
    }
    return 0;
}
这是我的代码,缺一个按字典排序的功能,请大神指教
搜索更多相关主题的帖子: 管理系统 address include 通讯录 
2012-12-10 09:17
玉垒浮云
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2012-11-22
收藏
得分:0 
编写一个简易的通信录程序,具备输入、存储、排序、管理、查找等功能。
读入n个用户姓名、电话号码和地址,并按姓名的字典顺序排列。通过输入某一个人的姓名查找对应的电话号码和地址。如果查找的姓名不在电话簿中,则输出“查无此人”。
2012-12-10 09:20
玉垒浮云
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2012-11-22
收藏
得分:0 
如何实现按姓名的字典顺序进行排列?
2012-12-10 09:20
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
std::sort
2012-12-10 09:30
mmmmmmmmmmmm
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:8
帖 子:388
专家分:1809
注 册:2012-11-2
收藏
得分:0 
输入N个人后,循环用strcmp()函数来进行排序就可以了。

如果姓名是中文,不知道了。

我们的目标只有一个:消灭0回复!
while(1)
++money;
2012-12-10 09:39
玉垒浮云
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2012-11-22
收藏
得分:0 
回复 4楼 rjsp
能详细点吗,不会啊!!!!
2012-12-10 10:17
玉垒浮云
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2012-11-22
收藏
得分:0 
回复 5楼 mmmmmmmmmmmm
详细点,可以吗,不会啊!!!
2012-12-10 10:17
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:20 
既然你选择C++,我建议你使用C++的风格和思维方式来写代码。当然,只是建议
程序代码:
#include <iostream>
#include <string>
#include <functional>

struct information
{
    std::string name;
    int number;
    std::string address;

    explicit information( const std::string& name_="", int number_=0, const std::string& address_="" )
        : name(name_), number(number_), address(address_)
    {
    }
};

bool operator<( const information& lhs, const information& rhs )
{
    return lhs.name < rhs.name;
}

std::istream& operator>>( std::istream& is, information& inf )
{
    return is>>inf.name>>inf.number>>inf.address;
}
std::ostream& operator<<( std::ostream& os, const information& inf )
{
    return os<<inf.name<<' '<<inf.number<<' '<<inf.address;
}

#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    vector<information> infs;
    size_t n;
    cout << "please input the amount of pepole" << endl;
    cin >> n;

    // 输入
    infs.reserve( n );
    for( size_t i=0; i!=n; ++i )
    {
        information inf;
        cin >> inf;
        infs.push_back(inf);
    }

    // 输出
    for( vector<information>::const_iterator itor=infs.begin(); itor!=infs.end(); ++itor )
    {
        cout << *itor << endl;
    }

    // 排序
    std::sort( infs.begin(), infs.end() );

    cout<<"please input name you want to look for"<<endl;
    string name;
    cin >> name;
    // 二分查找
    vector<information>::const_iterator itor = lower_bound( infs.begin(), infs.end(), information(name) );
    if( itor!=infs.end() && name==itor->name )
        cout << *itor << endl;
    else
        cout << "Not found." << endl;

    return 0;
}

收到的鲜花
  • mmmmmmmmmmmm2012-12-10 13:31 送鲜花  49朵   附言:再次学习版主大神的代码 目标
2012-12-10 12:26
mmmmmmmmmmmm
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:8
帖 子:388
专家分:1809
注 册:2012-11-2
收藏
得分:0 
再次学习版主大神的代码 目标

怎么才能写出这样的代码啊

我们的目标只有一个:消灭0回复!
while(1)
++money;
2012-12-10 13:30
玉垒浮云
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2012-11-22
收藏
得分:0 
回复 8楼 rjsp
#include<iostream>
#include<vector>
#include<string>
using namespace std;
struct information
{
    string name;
    int number;
    string address;
};
int main()
{
    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<<"◆                                                                    ◆"<<endl;
    cout<<"◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆"<<endl;
   
   
   
   
    vector<information>aa;
    information temp;
    int n;
    cout<<"please input the amount of pepole"<<endl;
    cin>>n;
               
    while(true)
    {
        int choice;
        cout<<"please choose 1~5"<<endl;
        cout<<" 1.输入联系人信息"<<endl;
        cout<<" 2.查找联系人"<<endl;
        cout<<" 3.修改联系人信息"<<endl;
        cout<<" 4.显示所有联系人"<<endl;
        cout<<" 5.退出"<<endl;
        
        while(true)
        {
            cin>>choice;
            if(choice<1||choice>5)
                cout<<"input error,please input again"<<endl;
            else
                break;
        }
        
        switch(choice)
        {
        case 1:
            {
                for(int i=0;i!=n;++i)
                {
                    cout<<"Please input name number address"<<endl;
                    cin>>temp.name>>temp.number>>temp.address;
                    aa.push_back(temp);
                }
            }
            break;
            
            
        case 2:
            {
                cout<<"please input name you want to look for"<<endl;
               
                string name1;
               
                cin>>name1;
                bool flag=0;
                for(vector<information>::iterator k=aa.begin();k!=aa.end();++k)
                {
                    if((*k).name==name1)
                    {
                        cout<<(*k).name<<"  "<<(*k).number<<"  "<<(*k).address<<endl;
                        flag=1;
                    }
                }
                if(!flag)
                    cout<<"check no such person"<<endl;
                }
            break;
        case 3:
            {   
                string name2,address2;
                int number2;
                cout<<"please input name2 that you want to revise"<<endl;
                cin>>name2;
                for(vector<information>::iterator f=aa.begin();f!=aa.end();++f)
                {
                    if((*f).name==name2)
                    {
                        cout<<"Please input name number address"<<endl;
                        cin>>name2>>number2>>address2;
                        (*f).name=name2;
                        (*f).number=number2;
                        (*f).address=address2;
                        cout<<(*f).name<<" "<<(*f).number<<"  "<<(*f).address<<endl;
                    }
                }
            }
            break;
        case 4:
            {   
                string t1;
                vector<information>::iterator h;
                for ( h=aa.begin();h!=aa.end()-1;++h)
                {
                    for(vector<information>::iterator f=h+1;f!=aa.end();++f)
                    {
                        if((*h).name>(*f).name)
                        {
                            t1=(*h).name;
                            (*h).name=(*f).name;
                            (*f).name=t1;
                        }
                    }
                }
                for ( h=aa.begin();h!=aa.end();++h)
                    cout<<(*h).name<<" "<<(*h).number<<" "<<(*h).address<<endl;
               
            }
            break;
        case 5:
            cout<<"Goodbye!!!!"<<endl;
            break;
        }
    }
    return 0;
    }
   
我已经会了,虽然你的我看不懂,但还是谢谢!!!还有我用的就是c++风格啊!!!!
2012-12-11 11:07
快速回复:求教通讯录管理系统中的姓名按字典顺序排列
数据加载中...
 
   



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

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