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

这是我们的课程设计,如何使用C语言来制作一个翻译器。
即启动程序后,输入“如果”+回车,会出现“if”。反之输入英文则出现对应的中文翻译。并且在查询后可以继续查询,直到输入“bye”则结束程序。

写了一个简单的翻译器,只提供单词翻译,中文到英文,英文到中文都行,你需要首先进行字典录入。录入以后会自动在目录下生成一个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();

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-05-12
#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;
}
第2个回答  2010-05-09
将英文单词词库和汉字词库一一映射,匹配成对,当输入一个英文单词时,找对应的汉字解释,反之亦然。
第3个回答  2010-05-12
用数据库吧
第4个回答  2010-05-11
用一个结构体数组来做 不是很难 就是有点烦 估计

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网