| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 784 人关注过本帖
标题:类中的数组,求赐教啊
只看楼主 加入收藏
Tlife
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-9-3
结帖率:66.67%
收藏
已结贴  问题点数:2 回复次数:6 
类中的数组,求赐教啊
#include<iostream>
#include<string>
using namespace std;
enum place{home,office,fax,cell,pager};
class Phone
{
private:
    int cc;
    int ac;
    int num;
    place type;
public:
    Phone(int c=0,int a=0, int n=0, place t=(place) 0):cc(c),ac(a),num(n),type(t){}
    void set();
    void print();
    friend void find(Phone &, int );
};

/*Phone :: Phone
{    cc=0;
    ac=0;
    num=0;
    type=(place)0;
}
*/
void Phone:: set()
{
    int i;
    cout<<"Please enter the phone number (Country code Area code Phone number and Type)"<<endl;
    cin>>cc>>ac>>num>>i;
    type=(place) i;
}

void Phone:: print()
{
    cout<<"Country code"<<'\t'<<"Area code"<<'\t'<<"Phone number"<<'\t'<<"Type"<<endl;
    cout<<cc<<'\t'<<ac<<'\t'<<num<<'\t';
    switch(type)
    {
    case 0: cout<<"Home"<<endl;
    case 1: cout<<"Office"<<endl;
    case 2: cout<<"Fax"<<endl;
    case 3: cout<<"Cell"<<endl;
    case 4: cout<<"Pager"<<endl;
    default:cout<<"Type Wrong"<<endl;
    }


}

void find( Phone &p ,int num)
{
    int i,j=0;
    for(i=0;i<3;i++)
    {
        if(p[i].num == num)
        {
            cout<<"Found the same number"<<endl;
            p[i].print();
            j=1;
            break;
        }
    }
   
   if(!j) cout<<"No phone number match"<<endl;
}

int main()
{
    Phone  p[3]={Phone(),Phone(),Phone()};
    int i,num
    for(i=1;i<3;i++)
    {
        p[i].set();
        p[i].print();
    }
    cout<<"Input the number you want to find"<<endl;
    cin>>num;
    find(p,num);
    return 0;
}
本程序主要的功能是输入电话号码(其中有区号还有类型),可以进行查询与更改。
我运用的是数组,但是为什么说我 if(p[i].num == num) 这句话有问题,有没有哪位高手能帮我一下啊?数组在类中是这样用的吧,还有存在一些原则性的错误?
搜索更多相关主题的帖子: private include office public 
2011-10-27 13:46
Tlife
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-9-3
收藏
得分:0 
这个问题自己解决了,但是又有新问题出现了
class Phone
{
private:
    。。。
public:
   。。。。
    friend void equal(Phone p[]);
};

void equal(Phone p[])
{
    int i,j=0;
    for(i=0;i<3;i++)
    {
        if(p[i].num==p[i+1].num)
        {
            cout<<"Equal number"<<endl;
            j=1;
            p[i].print();
        }
    }
    if(!j) cout<<"There is no equal phone number"<<endl;
}
int main()
{
    Phone  p[4]={Phone(),Phone(12345678,(place) 2),Phone(),Phone()};
    int i,num;
    p[0].print();
    p[1].print();
    for(i=2;i<4;i++)
    {
        p[i].set();
        p[i].print();
    }
    equal(p);
    cout<<"Input the number you want to find"<<endl;
    cin>>num;
    find(p,num);
    return 0;
}
又做了些改动(其他和原来的都一样),但是现在说我的equal函数出问题的了 if(p[i].num==p[i+1].num) ?这个为什么
2011-10-27 14:37
鑫乐源
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:27
专家分:150
注 册:2011-10-20
收藏
得分:1 
程序代码:
#include<iostream>
#include<string>
using namespace std;
enum place{home,office,fax,cell,pager};
class Phone
{
private:
    int cc;
    int ac;
    int num;
    place type;
public:
    Phone(int c=0,int a=0, int n=0, place t=(place) 0):cc(c),ac(a),num(n),type(t){}
    void set();
    void print();
    friend void equal(Phone p[]);
};

/*Phone :: Phone
{    cc=0;
    ac=0;
    num=0;
    type=(place)0;
}
*/
void Phone:: set()
{
    int i;
    cout<<"Please enter the phone number (Country code Area code Phone number and Type)"<<endl;
    cin>>cc>>ac>>num>>i;
    type=(place) i;
}

void Phone:: print()
{
    cout<<"Country code"<<'\t'<<"Area code"<<'\t'<<"Phone number"<<'\t'<<"Type"<<endl;
    cout<<cc<<'\t'<<ac<<'\t'<<num<<'\t';
    switch(type)
    {
    case 0: cout<<"Home"<<endl;
    case 1: cout<<"Office"<<endl;
    case 2: cout<<"Fax"<<endl;
    case 3: cout<<"Cell"<<endl;
    case 4: cout<<"Pager"<<endl;
    default:cout<<"Type Wrong"<<endl;
    }


}

void equal(Phone p[])
{
    int i,j=0;
    for(i=0;i<3;i++)
    {
        if(p[i].num==p[i+1].num)
        {
            cout<<"Equal number"<<endl;
            j=1;
            p[i].print();
        }
    }
    if(!j) cout<<"There is no equal phone number"<<endl;
}

int main()
{
    Phone  p[3]={Phone(),Phone(),Phone()};
    int i,num;
    for(i=1;i<3;i++)
    {
        p[i].set();
        p[i].print();
    }
    cout<<"Input the number you want to find"<<endl;
    cin>>num;
    equal(p);
    return 0;
}

不要和编译器比智商。
2011-10-27 22:32
lucky563591
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:765
专家分:2103
注 册:2009-11-18
收藏
得分:1 
Phone &p改为Phone *p
2011-10-28 09:17
YueWuSS
Rank: 2
等 级:论坛游民
帖 子:15
专家分:96
注 册:2011-10-29
收藏
得分:1 
回复 楼主 Tlife
//修改后程序如下
//修改部分已加红色

#include<iostream>
 #include<string>
 using namespace std;
 enum place{home,office,fax,cell,pager};
 class Phone
 {
 private:
     int cc;
     int ac;
     int num;
     place type;
 public:
     Phone(int c=0,int a=0, int n=0, place t=(place) 0):cc(c),ac(a),num(n),type(t){}
     void set();
     void print();
     friend void find(Phone [], int );    //引用传递不能用 p[i] 操作, 应改为指针,Phone *,或 Phone []
};
 
/*Phone :: Phone
 {    cc=0;
     ac=0;
     num=0;
     type=(place)0;
 }
 */
 void Phone:: set()
 {
     int i;
     cout<<"Please enter the phone number (Country code Area code Phone number and Type)"<<endl;
     cin>>cc>>ac>>num>>i;
     type=(place) i;
 }
 
void Phone:: print()
 {
     cout<<"Country code"<<'\t'<<"Area code"<<'\t'<<"Phone number"<<'\t'<<"Type"<<endl;
     cout<<cc<<"\t\t"<<ac<<"\t\t"<<num<<"\t\t";   //打印不对齐
     switch(type)
     {
     case 0: cout<<"Home"<<endl; break;   //少了 break;语句
     case 1: cout<<"Office"<<endl; break;
     case 2: cout<<"Fax"<<endl;   break;
     case 3: cout<<"Cell"<<endl;  break;
     case 4: cout<<"Pager"<<endl;  break;
     default:cout<<"Type Wrong"<<endl;
     }
 

}
 
void find( Phone p[] ,int num)  //引用传递不能用 p[i] 操作, 应改为指针,Phone *,或 Phone []
{
     int i,j=0;
     for(i=0;i<3;i++)
     {
         if( (p+i)->num == num)
        {
             cout<<"Found the same number"<<endl;
             p[i].print();
             j=1;
             break;
         }
     }
     
   if(!j) cout<<"No phone number match"<<endl;
 }
 
int main()
 {
     Phone  p[3]={Phone(),Phone(),Phone()};
     int i,num;             //缺少“;”(在“for”的前面)
     for(i=1;i<3;i++)
     {
         p[i].set();
         p[i].print();
     }
     cout<<"Input the number you want to find"<<endl;
     cin>>num;
     find(p,num);
     return 0;
 }
 
2011-10-29 12:12
晴天一阵
Rank: 2
等 级:论坛游民
帖 子:40
专家分:24
注 册:2011-5-21
收藏
得分:1 
学习了
2011-11-02 18:44
Tlife
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-9-3
收藏
得分:0 
回复 4楼 lucky563591
真是谢谢啊把我的问题解决了!
2011-11-05 20:37
快速回复:类中的数组,求赐教啊
数据加载中...
 
   



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

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