The implementation of English-Chinese dictionary Code

Source: Internet
Author: User
Tags readfile

English-Chinese Dictionary

Design process

Read the contents of the Word file---Parse file contents--Search by input

1. First use a fixed string to receive the contents of the file read, and then transferred to the array, easy to handle later

2. Iterate through the array, add even lines as key, odd rows as value into Dictionary 1 (here refers to a class in the OC language) as a dictionary of English-Chinese, key and value in turn in Dictionary 2

As a dictionary of Chinese to English, call the Keyforvalue method to remove value. (The dictionary file content format is as follows:

#a

Trans:art. A; the letter A

#a. M.

Trans:n. Morning

#a/C

Trans:n. Current account @ current: come-and-go; Contact [Email protected] Current account

#aardvark

Trans:n. Soil Pig

#aardwolf

Trans:n. Coyote

#aasvogel

Trans:n. A kind of bald eagle

#abaci

Trans:n. Abacus

#aback

Trans:ad. Back to the back of the land

#abacus

Trans:n. Abacus

#abaft

Trans:ad. @prep to the stern. In... After

#abalone

Trans:n. Abalone

).

3. Remove all keys from the dictionary, compare the for loop with the input characters, and compare only the substring of key.

The following are the specific code implementations:

/***************************************************/

/****************-MAIN.M file-********************/

/***************************************************/

#import <Foundation/Foundation.h>

#import "Dictionary.h"

int main (int argc, const char * argv[]) {

@autoreleasepool {

printf ("************************\n");

printf ("****** Welcome to use English-Chinese dictionary *****\n");

printf ("************************\n");

Dictionary *mydic=[[dictionary alloc] init];

[Mydic ReadFile];

Char a[100]={};

Char *getuserinput=a;

Char b= ' 0 ';

Char c= ' 0 ';

while (1)

{

printf ("1.2. English to Chinese \ n");

scanf ("%c", &c);

GetChar ();

if (c== ' 2 ')

{

printf ("1. Continue query 2. Exit query \ n");

scanf ("%c", &b);

if (b== ' 1 ')

{

GetChar ();

printf ("Please enter the word: \ n");

scanf ("%s", getuserinput);

GetChar ();

NSString *input=[[nsstring alloc] initwithformat:@ "#%s", Getuserinput];

NSString *strentoch=[mydic Englishtochainese:input];

if (strentoch==null)

{

NSLog (@ "not found! Try something else!");

}else

{

Nsarray *s=[[strentoch Substringfromindex:6] componentsseparatedbystring:@ "@"];

NSString *s1=[s componentsjoinedbystring:@ "\ n"];

NSLog (@ "\n%@\n", S1);

}

}

Else

{

if (b== ' 2 ')

{

Break

}else

{

while (GetChar ()!=10) {}

printf ("Incorrect input \ n");

}

}

}else if (c== ' 1 ')

{

printf ("1. Continue query 2. Exit query \ n");

scanf ("%c", &b);

if (b== ' 1 ')

{

GetChar ();

printf ("Please enter the word: \ n");

scanf ("%s", getuserinput);

GetChar ();

NSString *input=[[nsstring alloc] initwithutf8string:getuserinput];

NSString *strentoch=[mydic Chinesetoenglish:input];

if (strentoch==null)

{

NSLog (@ "not found! Try something else!");

}else

{

NSLog (@ "\n%@\n", [Strentoch substringfromindex:1]);

}

}

Else

{

if (b== ' 2 ')

{

Break

}else

{

while (GetChar ()!=10) {}

printf ("Incorrect input \ n");

}

}

}

}

}

return 0;

}

/***************************************************/

/****************-dictionary.h file-****************/

/***************************************************/

#import <Foundation/Foundation.h>

#define PATH @ "/users/paul/desktop/dict.txt"

@interface Dictionary:nsobject

{

Nsmutabledictionary * _MYDIC;

Nsmutabledictionary * _MYDIC1;

}

-(ID) initwithmydictionary;

-(void) Setmydic: (Nsmutabledictionary *) mydic;

-(Nsdictionary *) mydic;

-(NSString *) Englishtochainese: (nsstring*) en;

-(NSString *) Chinesetoenglish: (nsstring*) ch;

-(void) readFile;

@end

/***************************************************/

/****************-DICTIONARY.M file-****************/

/***************************************************/

#import "Dictionary.h"

@implementation Dictionary

-(ID) init

{

if (Self=[super init])

{

_mydic=[[nsmutabledictionary alloc] init];

_mydic1=[[nsmutabledictionary alloc] init];

}

return self;

}

-(ID) initwithmydictionary

{

if (Self=[super init])

{

_mydic=[[nsmutabledictionary alloc] Initwithcontentsoffile:path];

}

return self;

}

-(void) readFile

{

NSString *s=[[nsstring alloc] Initwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil]; Reading the contents of a file into an array

if (s==null)

{

NSLog (@ \ \ \ \ \ \ \ "\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \" \ n

Exit (Exit_failure);

}

Nsarray *arrs=[s componentsseparatedbystring:@ "\ n"];

for (int i=0;i<[arrs count]-1;i+=2)

{

[_mydic setvalue:arrs[i+1] forkey:arrs[i];

[_mydic1 Setobject:arrs[i] forkey:arrs[i+1];

}

}

-(Nsmutabledictionary *) mydic

{

return _mydic;

}

-(void) Setmydic: (Nsmutabledictionary *) mydic

{

_mydic=[mydic Initwithcontentsoffile:path];

}

-(NSString *) Englishtochainese: (nsstring*) en

{

En=[_mydic Valueforkey:en];

Return en;

}

-(NSString *) Chinesetoenglish: (NSString *) ch

{

int index=0;

Nsarray *arr=[_mydic allvalues];

for (int k=0;k<[arr count];k++)

{

NSString *sv=[arr Objectatindex:k];

Nsarray *sa=[sv componentsseparatedbystring:@ "@"];

for (int h=0;h<[sa count]; h++)

{

Nsarray *sm=[[sa objectatindex:h] componentsseparatedbystring:@ ";"];

for (int g=0;g<[sm count]; g++)

{

if (g==0)

{

Nsarray *arrs=[[sm objectatindex:g] componentsseparatedbystring:@ ". "];

if ([ch Isequal:[arrs lastobject]])

{

Index=k;

Goto RST;

}

}else if ([ch ISEQUAL:[SM objectatindex:g]])

{

Index=k;

Goto RST;

}

}

}

}

Rst:

Ch=[_mydic1 Valueforkey:arr[index]];

return ch;

}

@end

/***************************************************/

/******************-running the sample-********************/

/***************************************************/

******************************

Welcome to use English-Chinese dictionary

******************************

1. Chinese to English translation 2.

1

1. Continue to query 2. Exit query

1

Please enter a word:

Apple

2015-08-12 21:57:48.480 mydictionary[4293:219204]

Apple

1. Chinese to English translation 2.

1

1. Continue to query 2. Exit query

1

Please enter a word:

Computer

2015-08-12 21:58:11.133 mydictionary[4293:219204]

Computer

1. Chinese to English translation 2.

2

1. Continue to query 2. Exit query

1

Please enter a word:

Computer

2015-08-12 21:58:27.470 mydictionary[4293:219204]

N. Computers; computer

1. Chinese to English translation 2.

1

1. Continue to query 2. Exit query

1

Please enter a word:

Computer

2015-08-12 21:58:41.094 mydictionary[4293:219204]

Computer

1. Chinese to English translation 2.

1

1. Continue to query 2. Exit query

1

Please enter a word:

No

2015-08-12 21:59:25.439 mydictionary[4293:219204]

Wanting

1. Chinese to English translation 2.

1

1. Continue to query 2. Exit query

1

Please enter a word:

Mouse

2015-08-12 21:59:52.569 mydictionary[4293:219204]

Mouse

1. Chinese to English translation 2.

/******************************************************/

The implementation of English-Chinese dictionary Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.