如何用C语言来制作翻译器

发布网友 发布时间:2022-04-23 21:36

我来回答

5个回答

热心网友 时间:2022-05-02 16:53

写了一个简单的翻译器,只提供单词翻译,中文到英文,英文到中文都行,你需要首先进行字典录入。录入以后会自动在目录下生成一个dic.txt文件。
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define FILENAME "dic.txt"
struct word //字典结构体
{
char chinese[20]; //中文
char english[20]; //英文
};
/////////////////////////////////////////////////////////////
FILE *FP; //全局文件指针
FILE * FileOpen(char FileName[]) //文件打开函数
{
FILE *fp;
if((fp=fopen(FileName,"r"))==NULL)
{
fp=fopen(FileName,"w");
cout<<"文件打开失败重新创建记录文件";
return fp;
}
fp=fopen(FileName,"a+");
return fp;
}

void FileClose(FILE *fp) //文件关闭函数
{
if(fclose(fp)==0)
cout<<"安全关闭"<<endl;
else
cout<<"文件关闭失败"<<endl;
}
////////////////////////////////////////////////////////////////
void tra1() //中文翻译成英文模块
{
FILE *fp;
if((fp=fopen(FILENAME,"r"))==NULL)
{
printf("文件打开失败!");
}
char tempchinese[20];
word temp;
printf("请输入中文单词:");
scanf("%s",tempchinese);
while(fread(&temp,sizeof(word),1,fp)==1)
{
if(strcmp(temp.chinese,tempchinese)==0)
{
printf("中文:%s 英文:%s \n",temp.chinese,temp.english);
}
}
printf("查找完毕!");
FileClose(fp);
}
//////////////////////////////////////////////
void tra2() //英文翻译成中文模块
{
FILE *fp;
if((fp=fopen(FILENAME,"r"))==NULL)
{
printf("文件打开失败!");
}
char tempenglish[20];
word temp;
printf("请输入英文单词:");
scanf("%s",tempenglish);
while(fread(&temp,sizeof(word),1,fp)==1)
{
if(strcmp(temp.english,tempenglish)==0)
{
printf("中文:%s 英文:%s \n",temp.chinese,temp.english);
}
}
printf("查找完毕!");
FileClose(fp);
}
////////////////////////////////////////////////
void inp() //字典录入模块
{
FP=FileOpen(FILENAME);
word temp;
printf("请输入英文:");
scanf("%s",temp.english);
printf("请输入对应中文:");
scanf("%s",temp.chinese);
fwrite(&temp,sizeof(temp),1,FP);
printf("信息添加完成");
FileClose(FP);
}
////////////////////////////////////////////////
int menu() //主目录模块
{
int choose;
while(choose!=0)
{
printf("\n");
printf("简易中英翻译系统\n");
printf("1、中->英翻译\n");
printf("2、英-中翻译\n");
printf("3、字典录入\n");
printf("输入0退出系统\n");
printf("请输入:");
scanf("%d",&choose);
switch(choose)
{
case 0:return 0;break;
case 1:tra1();break;
case 2:tra2();break;
case 3:inp();break;
}
}
}
///////////////////////////////////////////////////////
void main()
{
menu();

}

热心网友 时间:2022-05-02 18:11

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <locale.h>
#include <windows.h>

#define MAX_LEN 30
#define DICTIONARY "C://Dictionary.dat"

typedef struct mymay mapping;
struct mymay{
wchar_t ch[MAX_LEN];
wchar_t en[MAX_LEN];
mapping * next;
};

int addNew(wchar_t* en, wchar_t* ch, mapping* head) {
mapping *tmp = head;

if (!head) {
return 0;
}
while(tmp->next) {
if (wcscmp(tmp->ch, ch) == 0 && wcscmp(tmp->en, en) == 0) {
return 1;
}
tmp = tmp->next;
}
tmp->next = (mapping *)malloc(sizeof(mapping));
if (!tmp->next) {
return 1;
}
wcscpy(tmp->next->ch, ch);
wcscpy(tmp->next->en, en);
tmp->next->next = NULL;
return 0;
}

int createHead(wchar_t* en, wchar_t* ch, mapping **head){
*head = (mapping *)malloc(sizeof(struct mymay));
if (!(*head)) {
printf("aaa1\n");
return 1;
}
wcscpy((*head)->ch, ch);
wcscpy((*head)->en, en);
(*head)->next = NULL;
return 0;

}

int destroyList(mapping *head) {
mapping *tmp = NULL;
while(head) {
tmp = head;
head = head->next;
free(tmp);
}
return 0;
}

wchar_t* searchList(wchar_t target[], mapping *head){
mapping *tmp = head;

if (head != NULL) {
while(tmp) {
if (wcscmp(tmp->ch, target) == 0)
return tmp->en;
else if (wcscmp(tmp->en, target) == 0)
return tmp->ch;
tmp = tmp->next;
}
}
return NULL;
}

int saveDictionary(mapping *head){
FILE* fp;
mapping *tmp = head;

if ((fp = fopen(DICTIONARY, "w")) == NULL){
printf("Can not open dictionary file.\n");
return 1;
}
while (tmp) {
fwrite(tmp,sizeof(mapping),1,fp);
tmp = tmp->next;
}
fclose(fp);

return 0;
}

int loadDictionary(mapping **head){
FILE* fp;
mapping tmp;

if ((fp = fopen(DICTIONARY, "r")) == NULL){
return 1;
}
while(fread(&tmp,sizeof(mapping),1,fp)==1) {
if(!*head) {
createHead(tmp.en, tmp.ch, head);
} else {
addNew(tmp.en, tmp.ch, *head);
}
}
fclose(fp);
return 0;
}

int main(){
mapping *head = NULL;
wchar_t s1[MAX_LEN];
wchar_t s2[MAX_LEN];
wchar_t* ret = NULL;
int select;
setlocale(LC_ALL, "Chinese");

loadDictionary(&head);
for(;;) {
printf("**************MENU**************\n");
printf("1.Search words.\n");
printf("2.Insert words.\n");
printf("3.bye.\n");
printf("**************MENU**************\n");
scanf("%d",&select);
switch(select) {
case 1:
system("CLS");
printf("************1.Search words**********\n");
printf("Input the word:\n");
scanf("%ls",&s1);
if (ret = searchList(s1, head)) {
wprintf(L"The Translation is [%s].\nany key to continue\n", ret);
} else {
printf("Can not find the Translation.\nany key to continue\n");
}
getch();
break;
case 2:
system("CLS");
printf("************2.Insert words.**********\n");
printf("Input the English:\n");
scanf("%ls",s1);
printf("Input the Chinese:\n");
scanf("%ls",s2);
if (!head) {
createHead(s1, s2, &head);
} else if (!addNew(s1, s2, head)) {
printf("Insert success!\nany key to continue\n");
} else {
printf("Insert failed!\nany key to continue\n");
}
getch();
break;
case 3:
goto END;
}
system("CLS");
}
END:
saveDictionary(head);
return 0;
}

热心网友 时间:2022-05-02 19:46

将英文单词词库和汉字词库一一映射,匹配成对,当输入一个英文单词时,找对应的汉字解释,反之亦然。

热心网友 时间:2022-05-02 21:37

用数据库吧

热心网友 时间:2022-05-02 23:45

用一个结构体数组来做 不是很难 就是有点烦 估计

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com