标题:求助,我的排序功能为什么不能实现
只看楼主
cosbin
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-5-24
结帖率:0
已结贴  问题点数:20 回复次数:2 
求助,我的排序功能为什么不能实现
自己从网上找了一段代码自己试着修改了以下,加了个排序,但运行时老是出现“遇到问题需要关闭”不知是为什么。希望各位大虾帮忙下,不胜感激...

#include<iostream>
#include<string> //控制字符串的头文件
using namespace std;
class tong //建立同学录的一个类
{
public:
    tong(); //构造函数
    tong(char n[20],char se[20],char ag[20],char te[100],char qq[100]);
    ~tong();
    void Set();//添加
    void Delete();//删除
    void Alter();//修改
    void List();//显示
    void RearchN();
    void RearchT();
    void RearchQ();
    void paixu();
private:
    char name[20];char age[20];char telphone[100];char QQ[100];char sex[20];
   
};

tong::tong(){} //构造函数
tong::tong(char n[20],char se[20],char ag[20],char te[100],char qq[100])
{
    strcpy(name, n);
    strcpy(sex, se);
    strcpy(age, ag);
    strcpy(telphone, te);
    strcpy(QQ, qq);
}
tong::~tong(){}

tong*s[50];int i=0;int j=0;
char name[20];char age[20];char telphone[100];char QQ[100];char sex[20];

void tong::Set()
{
    cout<<"请输入姓名:"<<endl;  
    cin>>name;  
    cout<<"请输入性别:"<<endl;
    cin>>sex;
    cout<<"请输入年龄:"<<endl;  
    cin>>age;  
    cout<<"电话号码:"<<endl;  
    cin>>telphone;  
    cout<<"QQ号码:"<<endl;  
    cin>>QQ;  
    j++;  
    s[i]=new tong(name,sex,age,telphone,QQ);  
    i++;  
}



void tong:: Delete() //(2)删除人员(Delete)
{  
    cout<<"请输入您要删除的学生姓名:"<<endl;  
    cin>>name;  
    for(int h=0;h<i;h++)  
    {  
        if(strcmp(name,s[h]->name)==0)  
        {  
            i--;  
            s[h]=s[h+1];  
            h++;  
        }
        else cout<<"您要求删除的对象本来就不存在!请检查输入的正确性!";  
    }
}


void tong::Alter() //(3)修改数据(Alter)
{  
    cout<<"请输入您要修改的学生的姓名:"<<endl;  
    cin>>name;  
    for(int h=0;h<i;h++)  
    {  
        if(strcmp(name,s[h]->name)==0)  
        { char se[20];char ag[20];char qq[100];char te[100];
        cout<<"请输入性别:"<<endl;
        cin>>se;
        cout<<"请输入年龄:"<<endl;  
        cin>>ag;  
        cout<<"电话号码:"<<endl;  
        cin>>te;
        cout<<"QQ号码:"<<endl;
        cin>>qq;  
        strcpy(s[h]->sex,se);  
        strcpy(s[h]->age,ag);  
        strcpy(s[h]->telphone,te);
        strcpy(s[h]->QQ,qq);
        cout<<"数据修改成功!";  
        }
        else cout<<"您要修改的学生本来就不存在!请检查重新输入!"<<endl;
    }
}  


void tong::List() //(4)显示记录(List)
{  
    cout<<"本系统所有通讯录的数据如下:"<<endl;  
    if(i==0)  
        cout<<"管理系统中没有录入数据或者数据已经被删除!"<<endl;  
    for(int k=0;k<i;k++)  
    {  
        cout<<"姓名:"<<s[k]->name<<" "<<  
            "性别:"<<s[k]->sex<<" "<<"年龄:"<<s[k]->age  
            <<" "<<"电话号码:"<<s[k]->telphone<<" "<<"QQ号码:"
            <<s[k]->QQ<<endl;  
    }  
}  

void tong::RearchN()
{
    if(i==0)
        cout<<"管理系统中没有录入数据或者数据已经被删除!"<<endl;
    cout<<"请输入你要查询的学生姓名"<<endl;
    cin>>name;
    for(int j=0;j<i;j++)
    {
        if(strcmp(name,s[j]->name)==0)
        {
            cout<<"您要查询的学生是:"<<s[j]->name<<endl;
            cout<<"性别: "<<s[j]->sex<<" "<<"年龄: "<<s[j]->age<<" "<<"电话号码:"<<s[j]->telphone
                <<" "<<"QQ号码:"<<s[j]->QQ<<endl;
        }
        
        else
            cout<<"对不起!您要查询的学生不存在!"<<endl;
    }
}  

void tong::RearchT()
{
//    char n;
    if(i==0)
        cout<<"管理系统中没有输入数据!"<<endl;
    cout<<"请输入你要查询的学生的电话"<<endl;
    cin>>telphone;
    for(int j=0;j<i;j++)
    {
        if(strcmp(s[j]->telphone,telphone)==0)
        {
            cout<<"您要查询的学生是:"<<s[j]->name<<endl;
            cout<<" 性别: "<<s[j]->sex<<" "<<"年龄: "<<s[j]->age<<" "<<"电话号码:"<<s[j]->telphone
                <<" "<<"QQ号码:"<<s[j]->QQ<<endl;
        }
        else cout<<"对不起!您要查询的学生不存在!"<<endl;  
    }
   
}

void tong::RearchQ()
{
    char n[100];
    if(i==0)
        cout<<"管理系统中没有输入数据!"<<endl;
    cout<<"请输入你要查询的学生的QQ"<<endl;
    cin>>n;
    for(int j=0;j<i;j++)
    {
        if(strcmp(s[j]->QQ,n)==0)
        {
            cout<<"您要查询的学生是:"<<s[j]->name<<endl;
            cout<<" 性别: "<<s[j]->sex<<" "<<"年龄: "<<s[j]->age<<" "<<"电话号码:"<<s[j]->telphone
                <<" "<<"QQ号码:"<<s[j]->QQ<<endl;
        }
        else cout<<"对不起!您要查询的学生不存在!"<<endl;  
    }
}

void tong::paixu()
{
    if(i==0)
        cout<<"管理系统中没有输入数据!"<<endl;
        for(int l=0;l<i-1;l++)
    {
        for(int k=0;k<i-l;k++)
        {
            if(strcmp(s[k]->telphone,s[k+1]->telphone)>0)
            {
                char name[20];char age[20];char telphone[100];char QQ[100];char sex[20];
               
                strcpy(telphone,s[k]->telphone);
                strcpy(s[k]->telphone,s[k+1]->telphone);
                strcpy(s[k+1]->telphone,telphone);
               
                strcpy(name,s[k]->name);
                strcpy(s[k]->name,s[k+1]->name);
                strcpy(s[k+1]->name,name);
               
               /* strcpy(address,st[j].address);
                strcpy(st[j].address,st[j+1].address);
                strcpy(st[j+1].address,address);*/
               
                strcpy(age,s[k]->age);
                strcpy(s[k]->age,s[k+1]->age);
                strcpy(s[k+1]->age,age);
               
                strcpy(QQ,s[k]->QQ);
                strcpy(s[k]->QQ,s[k+1]->QQ);
                strcpy(s[k+1]->QQ,QQ);
                 
                strcpy(sex,s[k]->sex);
                strcpy(s[k]->sex,s[k+1]->sex);
                strcpy(s[k+1]->sex,sex);
            }
        }
    }
    for(int f=0;f<i;f++)
    {
        int x=0;
        if(strcmp(s[f]->telphone,"0"))
        {
            cout<<s[f]->name<<endl;
            cout<<" 性别: "<<s[f]->sex<<" "<<"年龄: "<<s[f]->age<<" "<<"电话号码:"<<s[f]->telphone
                <<" "<<"QQ号码:"<<s[f]->QQ<<endl;
            x=1;
        }
        if(x==1)
        {
            cout<<endl;
            cout<<"-----------------------------------------------------------------------------"<<endl;
        }
    }
}
void caozuo(int p)
{ tong t;
switch(p)
{
case 0:t.Set();break;
case 1:t.Set();break;
case 2:t.Delete();break;
case 3:t.Alter();break;
case 4:t.List();break;
case 5:t.RearchN();break;
case 6:t.RearchT();break;
case 7:t.RearchQ();break;
case 8:t.paixu();break;
case 9:exit(0);break;
}
}



int main()
{  
    char w;
    do{
        cout<<"请输入操作代码(0 通讯录录入/1 增加人员/2 删除人员/3 修改数据/4 显示记录/ 5 按姓名查询/6 按电话查询/7 按QQ查询/8 按电话排序/9 退出)"<<endl;  
        int a;cin>>a; caozuo(a);
        cout<<"您想继续进行其他操作吗?(y/n)"<<endl;  
        cin>>w;
    }while(w=='y');
   
   
   
    return 0;  
} //利用while语句又回到了前面的循环
搜索更多相关主题的帖子: 不胜感激 include 同学录 public 字符串 
2010-05-24 19:48
rwyangguang
Rank: 2
等 级:论坛游民
帖 子:23
专家分:46
注 册:2009-7-7
得分:20 
参考看看   
建议:
1.在查找时如果为0   可以让程序结束   这些我没改
2.那个排序也可以换换
#include<iostream>
#include<string> //控制字符串的头文件
using namespace std;
class tong //建立同学录的一个类
{
public:
    tong(); //构造函数
    tong(char n[20],char se[20],char ag[20],char te[100],char qq[100]);
    ~tong();
    void Set();//添加
    int  Delete();//删除
    void Alter();//修改
    void List();//显示
    void RearchN();
    void RearchT();
    void RearchQ();
    void paixu();
private:
    char name[20];
    char age[20];
    char telphone[100];
    char QQ[100];
    char sex[20];
   
};

tong::tong(){} //构造函数
tong::tong(char n[20],char se[20],char ag[20],char te[100],char qq[100])
{
    strcpy(name, n);
    strcpy(sex, se);
    strcpy(age, ag);
    strcpy(telphone, te);
    strcpy(QQ, qq);
}
tong::~tong(){}

tong *s[50];
int i=0;
int j=0;
/*char name[20];
char age[20];
char telphone[100];
char QQ[100];
char sex[20]; */

void tong::Set()
{
    cout<<"请输入姓名:"<<endl;  
    cin>>name;  
    cout<<"请输入性别:"<<endl;
    cin>>sex;
    cout<<"请输入年龄:"<<endl;  
    cin>>age;  
    cout<<"电话号码:"<<endl;  
    cin>>telphone;  
    cout<<"QQ号码:"<<endl;  
    cin>>QQ;  
    j++;  
    s[i]=new tong(name,sex,age,telphone,QQ);  
    i++;  
}



int tong::Delete() //(2)删除人员(Delete)
{  
    cout<<"请输入您要删除的学生姓名:"<<endl;  
    cin>>name;  
    for(int h=0;h<i;h++)  
    {  
        if(strcmp(name,s[h]->name)==0)  
            break;
    }
    if(h==i)
    {
        cout<<"您所输的学生部存在"<<endl;
        return 0;
    }
    for(;h<i-1;h++)
        s[h]=s[h+1];
    return 1;
}



void tong::Alter() //(3)修改数据(Alter)
{  
    cout<<"请输入您要修改的学生的姓名:"<<endl;  
    cin>>name;  
    for(int h=0;h<i;h++)  
    {  
        if(strcmp(name,s[h]->name)==0)  
        { char se[20];char ag[20];char qq[100];char te[100];
        cout<<"请输入性别:"<<endl;
        cin>>se;
        cout<<"请输入年龄:"<<endl;  
        cin>>ag;  
        cout<<"电话号码:"<<endl;  
        cin>>te;
        cout<<"QQ号码:"<<endl;
        cin>>qq;  
        strcpy(s[h]->sex,se);  
        strcpy(s[h]->age,ag);  
        strcpy(s[h]->telphone,te);
        strcpy(s[h]->QQ,qq);
        cout<<"数据修改成功!";  
        }
     }
    if(h==i)
    {
        cout<<"您所输的学生部存在"<<endl;
    }
}  


void tong::List() //(4)显示记录(List)
{  
    cout<<"本系统所有通讯录的数据如下:"<<endl;  
    if(i==0)  
        cout<<"管理系统中没有录入数据或者数据已经被删除!"<<endl;  
    for(int k=0;k<i;k++)  
    {  
        cout<<"姓名:"<<s[k]->name<<" "
            <<"性别:"<<s[k]->sex<<" "
            <<"年龄:"<<s[k]->age  <<" "
            <<"电话号码:"<<s[k]->telphone<<" "
            <<"QQ号码:" <<s[k]->QQ<<endl;  
    }  
}  

void tong::RearchN()
{
    if(i==0)
        cout<<"管理系统中没有录入数据或者数据已经被删除!"<<endl;
    cout<<"请输入你要查询的学生姓名"<<endl;
    cin>>name;
    for(int j=0;j<i;j++)
    {
        if(strcmp(name,s[j]->name)==0)
        {
            cout<<"您要查询的学生是:"<<s[j]->name<<endl;
            cout<<"性别: "<<s[j]->sex<<" "<<"年龄: "<<s[j]->age<<" "<<"电话号码:"<<s[j]->telphone
                <<" "<<"QQ号码:"<<s[j]->QQ<<endl;
        }
    }
    if(j==i)
    {
        cout<<"您所输的学生部存在"<<endl;
    }
}  

void tong::RearchT()
{
//    char n;
    if(i==0)
        cout<<"管理系统中没有输入数据!"<<endl;
    cout<<"请输入你要查询的学生的电话"<<endl;
    cin>>telphone;
    for(int j=0;j<i;j++)
    {
        if(strcmp(s[j]->telphone,telphone)==0)
        {
            cout<<"您要查询的学生是:"<<s[j]->name<<endl;
            cout<<" 性别: "<<s[j]->sex<<" "<<"年龄: "<<s[j]->age<<" "<<"电话号码:"<<s[j]->telphone
                <<" "<<"QQ号码:"<<s[j]->QQ<<endl;
        }

    }
        if(j==i)
    {
        cout<<"您所输的学生部存在"<<endl;
    }
   
}

void tong::RearchQ()
{
    char n[100];
    if(i==0)
        cout<<"管理系统中没有输入数据!"<<endl;
    cout<<"请输入你要查询的学生的QQ"<<endl;
    cin>>n;
    for(int j=0;j<i;j++)
    {
        if(strcmp(s[j]->QQ,n)==0)
        {
            cout<<"您要查询的学生是:"<<s[j]->name<<endl;
            cout<<" 性别: "<<s[j]->sex<<" "<<"年龄: "<<s[j]->age<<" "<<"电话号码:"<<s[j]->telphone
                <<" "<<"QQ号码:"<<s[j]->QQ<<endl;
        }
    }
        if(j==i)
    {
        cout<<"您所输的学生部存在"<<endl;
    }
}

void tong::paixu()
{
    if(i==0)
        cout<<"管理系统中没有输入数据!"<<endl;
        for(int l=0;l<i-1;l++)
    {
        for(int k=0;k<i-l-1;k++)
        {
            if(strcmp(s[k]->telphone,s[k+1]->telphone)>0)
            {
                char name[20];char age[20];char telphone[100];char QQ[100];char sex[20];
               
                strcpy(telphone,s[k]->telphone);
                strcpy(s[k]->telphone,s[k+1]->telphone);
                strcpy(s[k+1]->telphone,telphone);
               
                strcpy(name,s[k]->name);
                strcpy(s[k]->name,s[k+1]->name);
                strcpy(s[k+1]->name,name);
               
               /* strcpy(address,st[j].address);
                strcpy(st[j].address,st[j+1].address);
                strcpy(st[j+1].address,address);*/
               
                strcpy(age,s[k]->age);
                strcpy(s[k]->age,s[k+1]->age);
                strcpy(s[k+1]->age,age);
               
                strcpy(QQ,s[k]->QQ);
                strcpy(s[k]->QQ,s[k+1]->QQ);
                strcpy(s[k+1]->QQ,QQ);
                 
                strcpy(sex,s[k]->sex);
                strcpy(s[k]->sex,s[k+1]->sex);
                strcpy(s[k+1]->sex,sex);
            }
        }
    }

    for(int f=0;f<i;f++)
    {
        int x=0;
        if(strcmp(s[f]->telphone,"0"))
        {
            cout<<s[f]->name<<endl;
            cout<<" 性别: "<<s[f]->sex<<" "
                <<"年龄: "<<s[f]->age<<" "
                <<"电话号码:"<<s[f]->telphone <<" "
                <<"QQ号码:"<<s[f]->QQ<<endl;
            x=1;
        }
        if(x==1)
        {
            cout<<endl;
            cout<<"-----------------------------------------------------------------------------"<<endl;
        }
    }
}
void caozuo(int p)
{ tong t;
switch(p)
{
case 0:t.Set();break;
case 1:t.Set();break;
case 2:t.Delete();break;
case 3:t.Alter();break;
case 4:t.List();break;
case 5:t.RearchN();break;
case 6:t.RearchT();break;
case 7:t.RearchQ();break;
case 8:t.paixu();break;
case 9:exit(0);break;
}
}



int main()
{  
    char w;
    do{
        cout<<"请输入操作代码(0 通讯录录入/1 增加人员/2 删除人员/3 修改数据/4 显示记录/ 5 按姓名查询/6 按电话查询/7 按QQ查询/8 按电话排序/9 退出)"<<endl;  
        int a;cin>>a; caozuo(a);
        cout<<"您想继续进行其他操作吗?(y/n)"<<endl;  
        cin>>w;
    }while(w=='y');
   
   
   
    return 0;  
} //利用while语句又回到了前面的循环
2010-05-24 20:41
cosbin
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-5-24
得分:0 
谢谢了........太感谢了。帮了大忙啦
2010-05-25 12:36



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-307997-1-1.html




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

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