大学做的一个小project,利用哈希实现单词翻译,也使用到文件流
数组
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <time.h>
using namespace std;
struct Word{
string english;
string chinese;
bool exist; //该位置是否为空
};
const int size_array=9843;
Word table[10000];
void insert_word(Word tmp){
int len=tmp.english.length();
unsigned int seed = 131;
unsigned int sum = 0;
int k=0;
while (k!=len)
{
sum = sum * seed + (tmp.english[k++]);
}
sum=sum & 0x7FFFFFFF;
sum%=size_array;
if(table[sum].exist==0) table[sum]=tmp;
else{
while(true){
if(table[sum].exist==0){
table[sum]=tmp;
break;
}
else{
sum++;
if(sum==size_array)sum=0;
}
}
}
}
string find_data(string tmp){
int len=tmp.length();
unsigned int seed = 131;
unsigned int sum = 0;
int k=0;
while (k!=len)
{
sum = sum * seed + (tmp[k++]);
}
sum=sum & 0x7FFFFFFF;
sum%=size_array;
if(table[sum].exist==0) return "Not found.";
else{
int num=sum;
if(table[sum].english==tmp) return table[sum].chinese;
sum++;
if(sum==size_array) sum=0;
while(table[sum].exist!=0&&num!=sum){
if(table[sum].english==tmp) return table[sum].chinese;
sum++;
if(sum==size_array) sum=0;
}
}
return "Not found.";
}
int main()
{
for(int i=0; i < 10000; ++i) table[i].exist=0;
clock_t start,end;
cout << "Enter the name of the dictionary file:";
char name_file1[50];
cin >> name_file1;
ifstream file1(name_file1);
//ofstream file2("hash_count.txt"); //记录单词对应哈希值的文件
string word;
//Word tmp[10000];
//int num=0;
start=clock();
while(getline(file1,word)){
int len=word.length();
int i;
Word tmp;
tmp.exist=1;
for(i=0; i < len; ++i){
if(word[i]==' ') break;
else tmp.english+=word[i];
}
for(i=i+1; i < len; ++i){
tmp.chinese+=word[i];
}
// num++;
/*int len1=tmp->english.length();
int sum=0;
for (int i = 0; i < len1; ++i){
sum+=((int)tmp.english[i]*(i+1));
}
sum %= 4171;
file2 << tmp->english << " " << sum << endl;*/ //获得单词对应的哈希值
insert_word(tmp);
}
end=clock();
cout << "Run time: " << (double)(end - start) / CLOCKS_PER_SEC << "S" << endl << endl;
int num_entry=0;
for(int i=0; i < size_array; ++i){
if(table[i].exist==true) num_entry++;
}
cout << "The size of the array is:" << size_array << endl;
cout << "The number of the entries is:" << num_entry << endl;
cout << "The load factor is:" << (double)num_entry/size_array << endl << endl;
cout << "Enter the name of the file needed to check:";
char name_file2[50];
cin >> name_file2;
ifstream file2(name_file2);
ofstream file3("result.txt");
start=clock();
while(getline(file2,word)){
file3 << word << " " << find_data(word) << endl;
}
end=clock();
cout << "Run time: " << (double)(end - start) / CLOCKS_PER_SEC << "S" << endl << endl;
file1.close();
file2.close();
file3.close(); //测试能否正确读取单词并存取到结构体
}
链表
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <time.h>
using namespace std;
const int size_list=5173;
struct Word{
string english;
string chinese;
Word *next;
Word(){
next=NULL;
}
};
Word* table[10000];
string find_data(string tmp){
int len=tmp.length();
unsigned int seed = 131;
unsigned int sum = 0;
int k=0;
while (k!=len)
{
sum = sum * seed + (tmp[k++]);
}
sum=sum & 0x7FFFFFFF;
sum %= size_list;
if(table[sum]==NULL) return "Not found.";
Word *node=table[sum];
while(node){
if(node->english==tmp) return node->chinese;
else node=node->next;
}
return "Not found.";
}
void insert_word(Word* tmp){
int len=tmp->english.length();
unsigned int seed = 131;
unsigned int sum = 0;
int k=0;
while (k!=len)
{
sum = sum * seed + (tmp->english[k++]);
}
sum=sum & 0x7FFFFFFF;
sum %= size_list;
if(table[sum]==NULL){
table[sum]=new Word();
table[sum]->english=tmp->english;
table[sum]->chinese=tmp->chinese;
}
else{
Word *node=new Word;
node->english=tmp->english;
node->chinese=tmp->chinese;
node->next=table[sum];
table[sum]=node;
}
}
int main()
{
clock_t start,end;
cout << "The size of the table is: " << size_list << endl << endl;
cout << "Enter the name of the dictionary file:";
char name_file1[50];
cin >> name_file1;
ifstream file1(name_file1);
//ofstream file2("hash_count.txt"); //记录单词对应哈希值的文件
string word;
//Word tmp[10000];
//int num=0;
for(int i=0; i < size_list; ++i) table[i]=NULL;
start=clock();
while(getline(file1,word)){
int len=word.length();
int i;
Word *tmp=new Word;
for(i=0; i < len; ++i){
if(word[i]==' ') break;
else tmp->english+=word[i];
}
for(i=i+1; i < len; ++i){
tmp->chinese+=word[i];
}
// num++;
/*int len1=tmp->english.length();
int sum=0;
for (int i = 0; i < len1; ++i){
sum+=(int)tmp->english[i];
}
sum %= 157;
file2 << tmp->english << " " << sum << endl;*/ //获得单词对应的哈希值
insert_word(tmp);
delete tmp;
}
end=clock();
cout << "Run time: " << (double)(end - start) / CLOCKS_PER_SEC << "S" << endl << endl;
cout << "Enter the name of the file needed to check:";
char name_file2[50];
cin >> name_file2;
/*Word *temp=table[111];
while(temp!=NULL){
cout << temp->english << " " << temp->chinese << endl;
temp=temp->next;
} */ //测试某一位置上所有单词
ifstream file2(name_file2);
ofstream file3("result.txt");
start=clock();
while(getline(file2,word)){
file3 << word << " " << find_data(word) << endl;
}
end=clock();
cout << "Run time: " << (double)(end - start) / CLOCKS_PER_SEC << "S" << endl << endl;
file1.close();
file2.close();
file3.close();
/*for(int i=0; i < num; ++i){
cout << tmp[i].english << " " << tmp[i].chinese << endl;
}*/ //测试能否正确读取单词并存取到结构体
}