這篇文章主要爲大家詳細介紹了C語言實現簡單的翻譯功能,具有一定的參考價值,感興趣的小夥伴們可以參考一下
本文實例爲大家分享了C語言實現翻譯功能的具體代碼,供大家參考,具體內容如下
#include<stdio.h> #define number 100 struct date { char chinese[30]; char English[30]; }a[number]; void copy(char *str1, char *str2, int counst)//將值賦給trans { int i = 0; if (str2 == ' ' || str2 == '\0') { return; } while (counst--) { *str1++ = *str2++; } return; } int panduan(char *d, char *f)//判斷是否相等 { char *str1 = d, *str2 = f; int xc=0; while (!(xc = *str2 - *str1)&&*str1) { str1++; str2++; } if (xc != 0) { return 0; } return 1; } char* ry( struct date a[number],char *wordtemp,int x) { for (int i = 0;i<x; i++) { if (panduan(a[i].English, wordtemp)) { return a[i].chinese; } } return "NULL"; } int main() { char b; int x; int exit=0; char str[50]; printf("[請先輸入英文單詞,再輸入拼音,中間以空格隔開,當輸入*時結束]\n"); for (int i=0;; i++) { printf("第[%d]組:\t", i+1); scanf("%[^' ']", a[i].English); scanf("%c", &b); scanf("%[^\n]", a[i].chinese); scanf("%c", &b); if (*a[i].English == '*'&&*a[i].chinese == '*') { x = i; break; } } printf("[請輸入你所要翻譯的內容]\n"); scanf("%[^\n]", str); printf("|翻譯的結果爲|:\n"); for (int i = 0;; i++) { if (str[i] != ' ') { char Trans[100]; int n = 0; if(str[i]!=' ') { while (!(str[i] == ' ' || str[i] == '\0')) { n++; i++; if (str[i] == '\0') { exit = 1; } } copy(Trans, &str[i - n], n); Trans[n] = '\0'; printf("[%s]", ry(a,Trans,x)); } if (exit) { break; } } } system("pause"); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。