#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<string>
using namespace std;
typedef struct trans
{
char eng[10];
char cha[20];
trans *next;
}trans;
typedef struct
{
char vex;
trans *link;
}hash;
hash table[10];
char orig[100]; //如果申请空间少 会影响到别的变量值
void init()
{
int n ;
ifstream in("a.txt");
in>>n;
char a[10],b[20];
trans *temp=NULL;
for(int i=0;i<n;i++)
{
table[i].link=NULL;
}
for( i=0;i<n;i++)
{
in>>a;
in>>b;
int key=(a[0]-'A')%n;
temp=new trans;
/* if(table[key].vex==NULL)
{
table[key].vex=a[0];
strcpy(temp->eng,a);
strcpy(temp->cha,b);
temp->next=table[key].link;
table[key].link=temp;
}
if(table[key].vex!=NULL&&a[0]==table[key].vex)
{
strcpy(temp->eng,a);
strcpy(temp->cha,b);
temp->next=table[key].link;
table[key].link=temp;
}
else if(table[key].vex!=NULL&&a[0]!=table[key].vex) //哈希到同一个位置 但是 首字母不同 往后移直至有空位放下
{
while(table[key].vex!=NULL)
{
key++;
}
table[key].vex=a[0];
strcpy(temp->eng,a);
strcpy(temp->cha,b);
temp->next=table[key].link;
table[key].link=temp;
}
*/
if(table[key].vex==NULL)
{
table[key].vex=a[0];
}
if(table[key].vex!=NULL&&a[0]==table[key].vex)
{
}
else if(table[key].vex!=NULL&&a[0]!=table[key].vex) //哈希到同一个位置 但是 首字母不同 往后移直至有空位放下
{
while(table[key].vex!=NULL)
{
key++;
}
table[key].vex=a[0];
}
strcpy(temp->eng,a);
strcpy(temp->cha,b);
temp->next=table[key].link;
table[key].link=temp;
}
int cnt=0;
char ch;
while((in>>ch)!=NULL)
{
// cout<<orig[cnt];
cout<<ch;
orig[cnt++]=ch; //输入带有逗号的语句
}
cout<<endl;
cout<<"翻译结果如下: "<<endl;
}
///字符串对比
//求字符串的长度 1.char* a[]="fsdfsd" ,写成数组形式 2 strlen()函数
bool cmp(char *a,char *b)
{
int lenb=strlen(b); //只求模式的长度 就可以了
for(int i=0;i<lenb;i++ )
{
if(*(a+i)!=*(b+i))
{
return false;
}
}
return true;
}
void find(char a[],int &cnt)//在表中为一个单词找到对应中文
{
int key=(a[0]-'A')%6;
while(table[key].vex!=a[0]) //如果是哈希到同一个位置的 重叠字符
{
key++; //将索引值向后直到找到 对应的link位置
}
trans *temp=table[key].link ;
while(temp!=NULL)
{
if(cmp(a,temp->eng))
{
cout<<temp->cha;
cnt=0; // //将现有的temp里的字段进行翻译
return ;
}
else temp=temp->next;
}
}
void translate()
{
int i=0;
char result[30];
char temp[5];
int cnt=0;
while(orig[i]!=NULL) //逐字的进行翻译
{
if(orig[i]>'A'&&orig[i]<'Z') //是否为大写字母
{
temp[cnt++]=orig[i];
}
else //不是字母是汉字 或符号
{
if(cnt>0)find(temp, cnt);
// 直接输出
cout<<orig[i];
}
i++;
}
}
int main(int argc, char* argv[])
{
init();
translate();
system("PAUSE");
return 0;
}