當前位置:成語大全網 - 漢語詞典 - 如何用C語言做壹個翻譯器

如何用C語言做壹個翻譯器

我寫了壹個簡單的翻譯器,只提供單詞翻譯,從中文到英文,從英文到中文。妳需要先在字典裏輸入它。輸入後,目錄中會自動生成壹個dic.txt文件。

#包含“stdio.h”

#包含" stdlib.h "

#包含" string.h "

#定義文件名“dic.txt”

結構詞//字典結構

{

char中文[20];//中文

char英語[20];//英語

};

/////////////////////////////////////////////////////////////

FILE * FP//全局文件指針

file * fileopen(charfilename[])//文件打開函數

{

FILE * fp

if((fp=fopen(FileName," r))= = NULL)

{

fp=fopen(文件名,“w”);

cout & lt& lt"文件無法打開;重新創建記錄文件”;

返回FP;

}

fp=fopen(文件名,“a+”);

返回FP;

}

Void FileClose(FILE *fp) //文件關閉函數

{

if(fclose(fp)==0)

cout & lt& lt“安全關機”

其他

cout & lt& lt"文件關閉失敗"

}

////////////////////////////////////////////////////////////////

Void tra1() //中文翻譯成英文模塊。

{

FILE * fp

if((fp=fopen(FILENAME," r))= = NULL)

{

Printf("文件打開失敗!");

}

char temp Chinese[20];

word temp

Printf("請輸入中文單詞:");

scanf("%s ",temp Chinese);

while(fread(& amp;temp,sizeof(word),1,fp)==1)

{

if(strcmp(temp.chinese,tempchinese)==0)

{

Printf("中文:%s英文:% s \ n ",temp。中國人,臨時工。英語);

}

}

Printf("搜索完畢!");

file close(FP);

}

//////////////////////////////////////////////

Void tra2() //英文翻譯成中文模塊

{

FILE * fp

if((fp=fopen(FILENAME," r))= = NULL)

{

Printf("文件打開失敗!");

}

char temp English[20];

word temp

Printf("請輸入英文單詞:");

scanf("%s ",temp English);

while(fread(& amp;temp,sizeof(word),1,fp)==1)

{

if(strcmp(temp.english,tempenglish)==0)

{

Printf("中文:%s英文:% s \ n ",temp。中國人,臨時工。英語);

}

}

Printf("搜索完畢!");

file close(FP);

}

////////////////////////////////////////////////

Void inp() //字典條目模塊

{

FP=FileOpen(文件名);

word temp

Printf("請輸入英文:");

scanf("%s ",temp . English);

Printf("請輸入相應的中文:");

scanf("%s ",temp . Chinese);

fwrite(& amp;temp,sizeof(temp),1,FP);

Printf("信息添加完成");

file close(FP);

}

////////////////////////////////////////////////

Int menu() //主目錄模塊

{

int choose

而(選擇!=0)

{

printf(" \ n ");

Printf("簡單漢英翻譯系統\ n ");

Printf("1,medium-->;英語翻譯\ n ");

Printf("2,英漢翻譯\ n ");

Printf("3,字典條目\ n ");

Printf("輸入0退出系統\ n ");

Printf("請輸入:");

scanf("%d ",& amp選擇);

切換(選擇)

{

案例0:返回0;打破;

案例1:tra 1();打破;

案例二:tra 2();打破;

案例三:InP();打破;

}

}

}

///////////////////////////////////////////////////////

void main()

{

menu();

}