以下这段程序,想要求每一句程序的作用,请问有了解的吗?

#include<iostream>
#include<fstream>
#include<malloc.h>
#include<stdlib.h>
const int ERROR=0;
using namespace std;
struct LinkHash
{
LinkHash *next;
int m_nValue;
int count;
};
struct _Data
{
int Value;
int Count;
};
char *file[101]=
{};
class CHashTable
{
private:
LinkHash *HashTable[101];
public:
CHashTable();
~CHashTable();
void HashCollision(int data);
void WriteToFile();
_Data GetMaxFreq(char *filename);
};
CHashTable::CHashTable()
{

int i;
for(i=0;i<100;i++)
{
HashTable[i]=(LinkHash*)malloc(sizeof(LinkHash));
if(!HashTable[i])
exit(ERROR);
HashTable[i]->count=0;
HashTable[i]->next=NULL;
HashTable[i]->m_nValue=-1;
}
}
CHashTable::~CHashTable()
{
}
int HashFunc(int key)
{
return key%100;
}
void CHashTable::HashCollision(int data)
{
LinkHash *newNode;
LinkHash *head;
newNode=(LinkHash*)malloc(sizeof(LinkHash));
if(!newNode)
exit(ERROR);
newNode->next=NULL;
newNode->m_nValue=data;
newNode->count=0;
int p;
bool isRep=false;
p=HashFunc(data);
head=HashTable[p];
while(head->next)
{
head=head->next;
if(head->m_nValue==data)
{
head->count++;
isRep=true;
break;
}

}
if(isRep==false)
{
head->next=newNode;
head=newNode;
head->count++;
}

}
void CHashTable::WriteToFile()
{
int i;
ofstream fout;
for(i=0;i<100;i++)
{
LinkHash *p;
fout.open(file[i]);
if(HashTable[i]->next)
{
p=HashTable[i]->next;
while(p)
{
fout<<p->m_nValue<<" "<<p->count<<endl;
p=p->next;
}
}
fout.close();
fout.clear();
}
}

拉莫斯之舞
浏览 141回答 1
1回答

汪汪一只猫

#包含iostream > <#include<fstream>#include<malloc.h>#include<stdlib.h>#包括<文件包括<< # malloc。H > #包括<<程序。”const int ERROR=0;using namespace std;struct LinkHash{LinkHash *next;int m_nValue;int count;};struct _Data{int Value;int Count;};char *file[101]={};class CHashTable{private:LinkHash *HashTable[101];public:CHashTable();~CHashTable();void HashCollision(int data);void WriteToFile();_Data GetMaxFreq(char *filename);};CHashTable::CHashTable(){int i;for(i=0;i<100;i++){ HashTable[i]=(LinkHash*)malloc(sizeof(LinkHash)); if(!HashTable[i]) exit(ERROR); HashTable[i]->count=0; HashTable[i]->next=NULL; HashTable[i]->m_nValue=-1;}}CHashTable::~CHashTable(){}int HashFunc(int key){return key%100;}void CHashTable::HashCollision(int data){LinkHash *newNode;LinkHash *head;newNode=(LinkHash*)malloc(sizeof(LinkHash));if(!newNode) exit(ERROR);newNode->next=NULL;newNode->m_nValue=data;newNode->count=0;int p;bool isRep=false;p=HashFunc(data);head=HashTable[p];while(head->next){ head=head->next; if(head->m_nValue==data) { head->count++; isRep=true; break; } }if(isRep==false){ head->next=newNode; head=newNode; head->count++;}}void CHashTable::WriteToFile(){int i;ofstream fout;for(i=0;i<100;i++){ LinkHash *p; fout.open(file[i]); if(HashTable[i]->next) { p=HashTable[i]->next; while(p) { fout<<p->m_nValue<<" "<<p->count<<endl; p=p->next; } } fout.close(); fout.clear();}}linkhash linkhash我int(getmaxfreq writetofile()(101×101文件数据M下* const int误差= 0;使用命名空间std;结构linkhash { linkhash;int值;int计数;};struct { int值;int计数;};char * [ ] = {};类chashtable {私有linkhash Hashtable;公共:chashtable();~chashtable();无效hashcollision int数据;孔隙;数据char* filename);};chashtable:chashtable() {;,为(i=0;i<100;我+ +){ Hashtable [我] =()*)malloc(sizeof());如果(!Hashtable [我])退出(误差);哈希表[我] - >数= 0;哈希表[我] >下= null;哈希表[我] > M值= 1;} } chashtable::~chashtable() { } int hashfunc(int key){返回关键% 100;} void chashtable::hashcollision(int数据){ linkhash * newnode;linkhash *头;= newnode(linkhash *)malloc(sizeof(linkhash);if(!newNode) exit(ERROR);newNode->next=NULL;newNode->m_nValue=data;newNode->count=0;int p;bool isRep=false;p=HashFunc(data);head=HashTable[p];while(head->next){ head=head->next; if(head->m_nValue==data) { head->count++; isRep=true; break; } }if(isRep==false){ head->next=newNode; head=newNode; head->count++;}}void CHashTable::WriteToFile(){int i;ofstream fout;for(i=0;i<100;i++){ LinkHash *p; fout.open(file[i]); if(HashTable[i]->next) { p=HashTable[i]->next; while(p) { fout<<p->m_nValue<<" "<<p->count<<endl; p=p->next; } } fout.close(); fout.clear();}}
打开App,查看更多内容
随时随地看视频慕课网APP