读书人

用散列表(链地址法)求系统关键字频度

发布时间: 2012-06-15 19:37:05 作者: rapoo

用散列表(链地址法)求系统关键字频度,有一句总是出错,大家帮忙看看啊
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <math.h>
#include <windows.h>

using namespace std;
#define M 10
int hash2(string key2);
int Read2();
string keyword[9]={ "void","float","int","char","for","if","else","do","while",};
int a1[10],a2[10],a3[10],a4[10];
DWORD t1,t2,t3,t4;
double s1,s2;

typedef struct{
string KeyName2;//关键字名字
int count2; //关键字频度
}st1;
typedef struct HashNode{
st1 data;
struct HashNode *next;
}HashNode,*HashLink;
HashNode *HL;


int hash2(string key2) //链地址法,将关键词读入哈希表中
{
HashLink p,q;
int i;
i=key2[0]%10;
p=(&HL[i])->next;
while(p!=NULL)
{
if(p->data.KeyName2==key2)
{
p->data.count2++;
return 1;
}
else
p=p->next;
}
q=new HashNode;
q->data.KeyName2=key2;
q->data.count2=1;
HashLink t=&HL[i];
q->next=t->next;
t->next=q;
return 1;
}
int Read2() //链地址法读取文件
{
int j,i=0,k=0;
char ch3[100],temp3;
char ch4[100],temp4;
HL=new HashNode[M];
fstream infile1;
infile1.open("d:\\新建文件夹\\1.cpp",ios::in);
if(!infile1){
cout<<"Cannot open this file!"<<endl;
exit(1);
}
for (j=0;j<10;j++) //初始化哈希表
{
HL[j].next=NULL;
HL[j].data.count2=0;
HL[j].data.KeyName2=" ";
}


string content3;
while(!infile1.eof()){
infile1.get(temp3);
content3.append(1, temp3);
}
int start, end;
do{
start = content3.find("//");
end = content3.find('\n', start);
if(start != string::npos && end != string::npos){
content3.erase(start, end - start);
}else if(start != string::npos && end == string::npos){
content3.erase(start, content3.end - start - 1);
}
}while(start != string::npos);

for(int x = 0; x < content3.length(); x++){
temp3 = content3.at(x); //一个字符一个字符的读入
if(temp3>='a' && temp3<='z' || temp3>='A' && temp3<='Z')
{
ch3[i]=temp3;
i++;
}
else
{
if(i!=0)
{
ch3[i]='\0'; //形成一个字符串
i=0;
for(j=0;j<9;j++) //判断该字符串是否为关键词
{
if(ch3==keyword[j])
hash2(ch3); //如果是关键词,则调用hash()函数,将其存入哈希表中
}
}
}
}
infile1.close();
cout<<"文件1中的关键字和频度为:"<<endl;
for(k=0;k<10;k++)
{
HashLink p=(&HL[k])->next;
while(p!=NULL)
{
cout<<p->data.KeyName2<<" "<<p->data.count2<<endl;
p=p->next;
}
}

fstream file1; //写入文件
file1.open("d:\\新建文件夹\\OutFile1.txt",ios::out);
if(!file1)
{
cout<<"can't open file!"<<endl;
exit(1);
}
for(k=0;k<10;k++)
{
HashLink p=(&HL[k])->next;
while(p!=NULL)
{
file1<<p->data.KeyName2<<" "<<p->data.count2<<endl;
p=p->next;
}
}
file1.close();

for (j=0;j<9;j++) //按照关键词数组中的顺序,将对应关键词的频度存入数组a3中
{
for(k=0;k<10;k++)
{
HashLink p=(&HL[k])->next;
while(p!=NULL)
{
if (p->data.KeyName2==keyword[j])
a3[j]=p->data.count2;
p=p->next;
}


}
}

for (j=0;j<10;j++) //初始化哈希表
{
HL[j].next=NULL;
HL[j].data.count2=0;
HL[j].data.KeyName2=" ";
}
fstream infile2; //读入第二个文件
infile2.open("d:\\新建文件夹\\2.cpp",ios::in);
if(!infile2){
cout<<"Cannot open this file!"<<endl;
exit(1);
}


string content4;
while(!infile2.eof()){
infile2.get(temp4);
content4.append(1, temp4);
}
do{
start = content4.find("//");
end = content4.find('\n', start);
if(start != string::npos && end != string::npos){
content4.erase(start, end - start);
}else if(start != string::npos && end == string::npos){
content4.erase(start, content4.end - start - 1);
}
}while(start != string::npos);

for(int v = 0; v < content4.length(); v++){
temp4 = content4.at(v); //一个字符一个字符的读入
if(temp4>='a' && temp4<='z' || temp4>='A' && temp4<='Z')
{
ch4[i]=temp4;
i++;
}
else
{
if(i!=0)
{
ch4[i]='\0'; //形成一个字符串
i=0;
for(j=0;j<9;j++) //判断该字符串是否为关键词
{
if(ch4==keyword[j])
hash2(ch4); //如果是关键词,则调用hash()函数,将其存入哈希表中
}
}
}
}

infile2.close();
cout<<"文件2中的关键字和频度为:"<<endl;
for(k=0;k<10;k++)
{
HashLink p=(&HL[k])->next;
while(p!=NULL)
{
cout<<p->data.KeyName2<<" "<<p->data.count2<<endl;
p=p->next;
}
}
fstream file2; //将关键词和频度写入TXT文件
file2.open("d:\\新建文件夹\\OutFile2.txt",ios::out);
if(!file2)
{
cout<<"can't open file!"<<endl;
exit(1);
}
for(k=0;k<10;k++)
{
HashLink p=(&HL[k])->next;
while(p!=NULL)
{
file2<<p->data.KeyName2<<" "<<p->data.count2<<endl;
p=p->next;
}
}
file2.close();
for (j=0;j<9;j++) // //按照关键词数组中的顺序,将对应关键词的频度存入数组a4中
{
for(k=0;k<10;k++)
{
HashLink p=(&HL[k])->next;
while(p!=NULL)
{
if (p->data.KeyName2==keyword[j])
a4[j]=p->data.count2;
p=p->next;
}
}
}
int x1=0,x2=0,x3=0; // 计算相对距离的过程
for(i=0;i<10;i++)
{
if((a3[i]-a4[i])!=0)
x1=x1+(a3[i]-a4[i])*(a3[i]-a4[i]);
}
for(i=0;i<9;i++)
x2=x2+a3[i]*a3[i];
for(i=0;i<9;i++)
x3=x3+a4[i]*a4[i];
s2=sqrt(x1)/(sqrt(x2*x3));

return 1;
}



int main()
{

int no2;
while(true){
cout<<"(1)识别并统计关键字频度"<<"\n";
cout<<"(2)计算相对距离S"<<"\n";
cout<<"(3)链地址法执行时间"<<"\n";
cout<<"(4)返回主界面"<<"\n";
cout<<"\n";
cout<<"请选择您需要的服务:";
cin>>no2;
if (no2==1)
{
t3=GetTickCount();
Read2();
t4=GetTickCount();
}
if (no2==2)
{
cout<<s2<<endl;

}
if (no2==3)
{
cout<<t4-t3<<endl;

}
if (no2==4)
break;
}
return 0;
}

------解决方案--------------------


把content3.erase(start, content3.end - start - 1);修改为content3.erase(start, end - start - 1);

读书人网 >C++

热点推荐