读书人

新人求对C++代码注释和说明!解决办法

发布时间: 2012-06-19 14:45:20 作者: rapoo

新人求对C++代码注释和说明!
[code=C/C++][/code]

//#include "iostream.h"
#include <iostream>
#include "string.h"
#include "fstream"
#define NULL 0
unsigned int key;
unsigned int key2;
int *p;
struct node //建节点
{
char name[8],address[20];
char num[11];
node * next;
};

typedef node* pnode;
typedef node* mingzi;
node **phone;
node **nam;
node *a;

using namespace std; //使用名称空间

void hash(char num[11]) //哈希函数
{
int i = 3;
key=(int)num[2];

while(num[i]!=NULL)
{
key+=(int)num[i];
i++;
}
key=key%20;
}

void hash2(char name[8]) //哈希函数
{
int i = 1;
key2=(int)name[0];
while(name[i]!=NULL)
{
key2+=(int)name[i];
i++;
}
key2=key2%20;
}

node* input() //输入节点
{
node *temp;
temp = new node;
temp->next=NULL;
cout<<"输入姓名:"<<endl;
cin>>temp->name;
cout<<"输入地址:"<<endl;
cin>>temp->address;
cout<<"输入电话:"<<endl;
cin>>temp->num;
return temp;
}

int apend() //添加节点
{
node *newphone;
node *newname;
newphone=input();
newname=newphone;
newphone->next=NULL;
newname->next=NULL;
hash(newphone->num);
hash2(newname->name);
newphone->next = phone[key]->next;
phone[key]->next=newphone;
newname->next = nam[key2]->next;
nam[key2]->next=newname;
return 0;
}

void create() //新建节点
{
int i;
phone=new pnode[20];
for(i=0;i<20;i++)
{
phone[i]=new node;
phone[i]->next=NULL;
}
}
void create2() //新建节点
{
int i;
nam=new mingzi[20];
for(i=0;i<20;i++)
{
nam[i]=new node;
nam[i]->next=NULL;
}
}
void list() //显示列表
{
int i;
node *p;
for(i=0;i<20;i++)
{
p=phone[i]->next;
while(p)
{
cout<<p->name<<'_'<<p->address<<'_'<<p->num<<endl;
p=p->next;
}
}
}
void list2() //显示列表
{
int i;
node *p;
for(i=0;i<20;i++)
{
p=nam[i]->next;
while(p)
{
cout<<p->name<<'_'<<p->address<<'_'<<p->num<<endl;
p=p->next;
}
}
}

void find(char num[11]) //查找用户信息
{
hash(num);
node *q=phone[key]->next;
while(q!= NULL)
{
if(strcmp(num,q->num)==0)
break;
q=q->next;
}
if(q)
cout<<q->name<<"_" <<q->address<<"_"<<q->num<<endl;
else cout<<"无此记录"<<endl;
}
void find2(char name[8]) //查找用户信息
{
hash2(name);
node *q=nam[key2]->next;
while(q!= NULL)


{
if(strcmp(name,q->name)==0)
break;
q=q->next;
}
if(q)
cout<<q->name<<"_" <<q->address<<"_"<<q->num<<endl;
else cout<<"无此记录"<<endl;
}


void save() //保存用户信息
{
int i;
node *p;
for(i=0;i<20;i++)
{
p=phone[i]->next;
while(p)
{
fstream iiout("out.txt", ios::out);
iiout<<p->name<<"_"<<p->address<<"_"<<p->num<<endl;
p=p->next;
}
}
}

void menu() //菜单
{

cout<<"0.添加记录"<<endl;
cout<<"3.查找记录"<<endl;
cout<<"2.姓名散列"<<endl;
cout<<"4.号码散列"<<endl;
cout<<"5.清空记录"<<endl;
cout<<"6.保存记录"<<endl;
cout<<"7.退出系统"<<endl;
}

int main()
{
char num[11];
char name[8];

create();
create2() ;

int sel;
while(1)
{
menu();
cin>>sel;
if(sel==3)
{ cout<<"9号码查询,8姓名查询"<<endl;
int b;
cin>>b;
if(b==9)
{ cout<<"请输入电话号码:"<<endl;
cin >>num;
cout<<"输出查找的信息:"<<endl;
find(num);
}
else
{ cout<<"请输入姓名:"<<endl;
cin >>name;
cout<<"输出查找的信息:"<<endl;
find2(name);}
}
if(sel==2)
{ cout<<"姓名散列结果:"<<endl;
list2();
}
if(sel==0)
{ cout<<"请输入要添加的内容:"<<endl;
apend();
}
if(sel==4)
{ cout<<"号码散列结果:"<<endl;
list();
}
if(sel==5)
{ cout<<"列表已清空:"<<endl;
create();
create2();
}
if(sel==6)
{ cout<<"通信录已保存:"<<endl;
save();
}
if(sel==7) return 0;
}
return 0;

}




[解决办法]
大致注释了一下,整体上应该是将用户输入的数据分别根据号码和名字存储在利用hash进行排序的hash表中,但是里面append里面有逻辑错误,并且内存没有合理释放过。

C/C++ code
//#include "iostream.h"#include <iostream>#include "string.h"#include "fstream"#define NULL 0unsigned int key;unsigned int key2;int *p;struct node //建节点{    char name[8],address[20];    char num[11];    node * next;};typedef node* pnode;typedef node* mingzi;node **phone;node **nam;node *a;using namespace std; //使用名称空间void hash(char num[11]) //哈希函数{    int i = 3;    key=(int)num[2];    while(num[i]!=NULL)    {        key+=(int)num[i];        i++;    }    key=key%20;}void hash2(char name[8]) //哈希函数{    int i = 1;    key2=(int)name[0];    while(name[i]!=NULL)    {        key2+=(int)name[i];        i++;    }    key2=key2%20;}/*获取用户输入信息*/node* input() //输入节点{    node *temp;    temp = new node;    temp->next=NULL;    cout<<"输入姓名:"<<endl;    cin>>temp->name;    cout<<"输入地址:"<<endl;    cin>>temp->address;    cout<<"输入电话:"<<endl;    cin>>temp->num;    return temp;}int apend() //添加节点{    node *newphone;    node *newname;    newphone=input();       //根据用户输入生成新的phone节点    newname=newphone;       //newname指针指向newphone    newphone->next=NULL;    //与input中的代码重复了    newname->next=NULL;     //newname与newphone指向同一内存,此句重复    hash(newphone->num);    //计算号码的散列值    hash2(newname->name);   //计算mingzi的散列值    newphone->next = phone[key]->next;  //将newphone插入phone[key]的索引为1的位置    phone[key]->next=newphone;    newname->next = nam[key2]->next;    //newname插入nam[key]的索引为1的位置,由于newname和newphone指针相同所以会改变newphone->next = ...的效果    nam[key2]->next=newname;    return 0;}/*创建能够存储20个key的hash表*/void create() //新建节点{    int i;    phone=new pnode[20];  //生成20node指针    for(i=0; i<20; i++)   //生成20个node对象    {        phone[i]=new node;        phone[i]->next=NULL;    }}/*创建能够存储20个key的hash表*/void create2() //新建节点{    int i;    nam=new mingzi[20];    for(i=0; i<20; i++)    {        nam[i]=new node;        nam[i]->next=NULL;    }}/*输出所有的phone*/void list() //显示列表{    int i;    node *p;    for(i=0; i<20; i++)    {        p=phone[i]->next;        while(p)    //输出每个链表的所有内容        {            cout<<p->name<<'_'<<p->address<<'_'<<p->num<<endl;            p=p->next;        }    }}/*输出所有的nam*/void list2() //显示列表{    int i;    node *p;    for(i=0; i<20; i++)    {        p=nam[i]->next;        while(p)    //输出每个链表的所有内容        {            cout<<p->name<<'_'<<p->address<<'_'<<p->num<<endl;            p=p->next;        }    }}/*根据号码查找用户*/void find(char num[11]) //查找用户信息{    hash(num);                  //计算号码的散列值    node *q=phone[key]->next;    while(q!= NULL)    {        if(strcmp(num,q->num)==0)   //找到了,结束            break;        q=q->next;    }    if(q)        cout<<q->name<<"_" <<q->address<<"_"<<q->num<<endl;    else cout<<"无此记录"<<endl;}/*根据姓名查找用户*/void find2(char name[8]) //查找用户信息{    hash2(name);        //计算姓名的散列值    node *q=nam[key2]->next;    while(q!= NULL)    {        if(strcmp(name,q->name)==0)//找到了,结束            break;        q=q->next;    }    if(q)        cout<<q->name<<"_" <<q->address<<"_"<<q->num<<endl;    else cout<<"无此记录"<<endl;}/*保存phone中的所有信息*/void save() //保存用户信息{    int i;    node *p;    for(i=0; i<20; i++)    {        p=phone[i]->next;        while(p)        {            fstream iiout("out.txt", ios::out);            iiout<<p->name<<"_"<<p->address<<"_"<<p->num<<endl;            p=p->next;        }    }}/*显示菜单*/void menu() //菜单{    cout<<"0.添加记录"<<endl;    cout<<"3.查找记录"<<endl;    cout<<"2.姓名散列"<<endl;    cout<<"4.号码散列"<<endl;    cout<<"5.清空记录"<<endl;    cout<<"6.保存记录"<<endl;    cout<<"7.退出系统"<<endl;}int main(){    char num[11];    char name[8];    create();      //创建20个phone    create2() ;    //创建20个mingzi    int sel;    while(1)    {        menu();         //显示菜单        cin>>sel;       //获取用户选择        if(sel==3)      //如果是查找记录        {            cout<<"9号码查询,8姓名查询"<<endl;            int b;            cin>>b;            if(b==9)    //号码查询            {                cout<<"请输入电话号码:"<<endl;                cin >>num;                cout<<"输出查找的信息:"<<endl;                find(num);      //根据号码查找            }            else        //姓名查询            {                cout<<"请输入姓名:"<<endl;                cin >>name;                cout<<"输出查找的信息:"<<endl;                find2(name);    //根据姓名查找            }        }        if(sel==2)      //姓名散列        {            cout<<"姓名散列结果:"<<endl;            list2();        }        if(sel==0)      //添加记录        {            cout<<"请输入要添加的内容:"<<endl;            apend();        }        if(sel==4)      //根据号码的散列值,依次输出记录        {            cout<<"号码散列结果:"<<endl;            list();        }        if(sel==5)      //重新生成,原来生成的没销毁,有内存泄漏        {            cout<<"列表已清空:"<<endl;            create();            create2();        }        if(sel==6)  //按照号码的散列顺序,将记录输出至文件        {            cout<<"通信录已保存:"<<endl;            save();        }        if(sel==7) return 0;    }    return 0;} 

读书人网 >C++

热点推荐