當前位置:成語大全網 - 新華字典 - 怎麽用C++編寫出壹個英漢字典程序?

怎麽用C++編寫出壹個英漢字典程序?

#include<iostream>

using namespace std;

#include<string.h>

#define N 1024

int nWord=2;

char h[N][21]={"妳好","再見",0};

char y[N][21]={"hello","bye",0};

void ReadFile() { FILE *fp; char buffer[21]; int i;

nWord=0;

if ( fp=fopen("c:\\h.txt","r") ) {

fgets(buffer,20,fp); sscanf(buffer,"%d",&nWord);

for ( i=0;i<nWord;i++ ) fgets(fp,20,h[i]);

fclose(fp);

} else printf("無法打開漢語短語文件\n");

if ( fp=fopen("c:\\y.txt","r") ) {

fgets(buffer,20,fp);

for ( i=0;i<nWord;i++ ) fgets(fp,20,y[i]);

fclose(fp);

} else printf("無法打開英語短語文件\n");

}

void main() { unsigned char s[20]; int i;

ReadFile();

cin>>s;

if ( s[0]>127 ) for ( i=0;i<nWord;i++ ) if ( strcmp(s,h[i])==0 ) cout<<y[i]<<endl;

else for ( i=0;i<nWord;i++ ) if ( strcmp(s,y[i])==0 ) cout<<h[i]<<endl;

}

兩個文件存放到c盤根目錄:

h.txt : 第1行寫短語個數,後面每行壹個短語,行數不要超過N,短語字符數不要超過20,否則改程序

2

妳好

再見

y.txt: 第1行寫短語個數,後面每行壹個短語,行數不要超過N,短語字符數不要超過20,否則改程序

2

hello

bye