| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 530 人关注过本帖
标题:两段C++代码,读不懂。
取消只看楼主 加入收藏
ccnky123
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-5-14
结帖率:0
收藏
 问题点数:0 回复次数:0 
两段C++代码,读不懂。
这学期刚开始学C++,老师给了段代码要我们去上机实验,都读补懂,也不知道是书上哪些部分的知识,老师讲课喜欢穿插讲,很多没学过的内容都写进代码里了!请会的朋友用“//”标注分析,那样我才能找到对应的书中哪部分的内容,然后自学!代码如下:
part1:
#include "stdafx.h"
#include "iostream.h"
#include "string.h"
#include "malloc.h"

struct item{
    char name[20];
    long number;
};
class PhoneBook{
    int n;
    struct item *p;
    public:
        PhoneBook(int);
        ~PhoneBook();
        void setname(int,char*);
        void setnumber(char*,long);
        void print();
};
PhoneBook::PhoneBook(int x){
    n=x;
    p=(struct item*)calloc(n,sizeof(struct item));
}
PhoneBook::~PhoneBook (){
     free(p);
}
void PhoneBook::setname (int a,char* b){
    strcpy((p+a)->name ,b);
}
void PhoneBook::setnumber (char* a,long b){
    for (int i=0;i<n;i++){
        if (strcpy((p+i)->name ,a)==0)
            (p+i)->number =b;
    }
   
}
void PhoneBook::print (){
    for (int i=0;i<n;i++){
        cout<<"name="<<(p+i)->name;
        cout<<"number="<<(p+i)->number;
    }
}

int main(int argc, char* argv[])
{
    PhoneBook a(10);


    a.setname(0,"aaaa");
    a.setname(1,"bbbb");
    a.setname(2,"cccc");
    a.setname(3,"dddd");
    a.setname(4,"eeee");

    a.setnumber("aaaa",1111);
    a.setnumber("bbbb",2222);
    a.setnumber("cccc",3333);
    a.setnumber("dddd",4444);
    a.setnumber("eeee",5555);

    a.print ();

    return 0;
}
part2:
#include "stdafx.h"
#include "malloc.h"
#include "string.h"
#include "iostream.h"

struct item{
    char name[20];
    long number;
};
class PhoneBook{ //类的声明
    int current;
    int n;
    struct item *p;
    public:
        PhoneBook(int);
        ~PhoneBook();
        void setname(int,char*);
        void setnumber(int,long);

        long& operator()(char*);

        PhoneBook& operator()(int);
        void operator=(char*);
   
        long operator[](char*);
        char* operator[](long);

        void print();

};
//类的定义
long PhoneBook::operator [](char* x){
        for (int i=0;i<n;i++){
            if (strcmp((p+i)->name,x)==0)
                return (p+i)->number;
        }
}
char* PhoneBook::operator [](long x){
    for (int i=0;i<n;i++){
        if ( (p+i)->number==x)
            return (p+i)->name;
    }
}
void PhoneBook::operator =(char* a){
    strcpy((p+current)->name, a);
}
PhoneBook& PhoneBook::operator ()(int a){
    current=a;
    return *this;
}
void PhoneBook::print(){
    for (int i=0;i<n;i++)
        cout<<"name="<<(p+i)->name<<" number="<<(p+i)->number<<endl;

}
long& PhoneBook::operator ()(char* a){
    for (int i=0;i<n;i++){
        if (strcmp((p+i)->name,a)==0)
            return (p+i)->number;
    }
}
void PhoneBook::setnumber(int a,long b){
    (p+a)->number =b;
}
void PhoneBook::setname(int a,char* b){
    strcpy((p+a)->name,b);
}
PhoneBook::PhoneBook(int a){
    n=a;
    p=(struct item*)calloc(n,sizeof(struct item));
}
PhoneBook::~PhoneBook(){
    free(p);
}

//----------------------------------
int main(int argc, char* argv[])
{
    PhoneBook a(10);
    a(0)="hahaha0";
    a(1)="hahaha1";
    a(2)="hahaha2";
    a(3)="hahaha3";
    a(4)="hahaha4";
    a(5)="hahaha5";
    a(6)="hahaha6";
    a(7)="hahaha7";
    a(8)="hahaha8";
    a(9)="hahaha9";
    a("hahaha0")=12345;
    a("hahaha1")=54321;
    a("hahaha2")=1234;
    a("hahaha3")=5431;
    a("hahaha4")=1345;
    a("hahaha5")=5321;
    a("hahaha6")=1345;
    a("hahaha7")=5421;
    a("hahaha8")=13345;
    a("hahaha9")=53421;
    a.print();

    cout<<"hahaha7 's number is "<<a["hahaha7"]<<endl;
    cout<<"13345 's name is "<<a[13345]<<endl;

    return 0;
}
搜索更多相关主题的帖子: 朋友 
2011-05-14 22:09
快速回复:两段C++代码,读不懂。
数据加载中...
 
   



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

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