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

Python08

如何用C语言来制作翻译器,第1张

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

case 1:tra1()break

case 2:tra2()break

case 3:inp()break

}

}

}

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

void main()

{

menu()

}

#include <stdio.h>

struct s_node

{

int data

struct s_node *next

}

typedef struct s_node s_list

typedef s_list *link

link operator=NULL

link operand=NULL

link push(link stack,int value)

{

link newnode

newnode=(link) malloc(sizeof(s_list))

if(!newnode)

{

printf("\nMemory allocation failure!!!")

return NULL

}

newnode->data=value

newnode->next=stack

stack=newnode

return stack

}

link pop(link stack,int *value)

{

link top

if(stack !=NULL)

{

top=stack

stack=stack->next

*value=top->data

free(top)

return stack

}

else

*value=-1

}

int empty(link stack)

{

if(stack==NULL)

return 1

else

return 0

}

int is_operator(char operator)

{

switch (operator)

{

case '+': case '-': case '*': case '/': return 1

default:return 0

}

}

int priority(char operator)

{

switch(operator)

{

case '+': case '-' : return 1

case '*': case '/' : return 2

default: return 0

}

}

int two_result(int operator,int operand1,int operand2)

{

switch(operator)

{

case '+':return(operand2+operand1)

case '-':return(operand2-operand1)

case '*':return(operand2*operand1)

case '/':return(operand2/operand1)

}

}

void main()

{

char expression[50]

int position=0

int op=0

int operand1=0

int operand2=0

int evaluate=0

printf("\nPlease input the inorder expression:")

gets(expression)

while(expression[position]!='\0'&&expression[position]!='\n')

{

if(is_operator(expression[position]))

{

if(!empty(operator))

while(priority(expression[position])<= priority(operator->data)&&

!empty(operator))

{

operand=pop(operand,&operand1)

operand=pop(operand,&operand2)

operator=pop(operator,&op)

operand=push(operand,two_result(op,operand1,operand2))

}

operator=push(operator,expression[position])

}

else

operand=push(operand,expression[position]-48)

position++

}

while(!empty(operator))

{

operator=pop(operator,&op)

operand=pop(operand,&operand1)

operand=pop(operand,&operand2)

operand=push(operand,two_result(op,operand1,operand2))

}

operand=pop(operand,&evaluate)

printf("The expression [%s] result is '%d' ",expression,evaluate)

getch()

}