哈希表结构求解百度语言翻译机

本文介绍了一种使用哈希表实现字符串翻译的算法,包括初始化哈希表、读取输入文件、处理字符串和输出翻译结果的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
 

 
#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值