1. Preface
Write these words, just because yesterday tangled up the most days of the day to how to separate the strings in the file, in terms of English words and translation. Finally, it was done, but the efficiency of the code execution was so low that it had to be abandoned at the end of the process. Said in the end is actually the separation of strings using the law is not the same, resulting. But it still bothers me for a long time, in fact, it is their own thinking has not been picked out, this is the core of the problem. Before the single-chip microcomputer and circuit what, their hobbies are also engaged in single-chip circuit. Now it's time to start real software programming. Always own thinking is still stuck in the process-oriented perspective, but also not a good object-oriented approach to programming. Finally, the small project is done, and some of the previous thinking has been verified, as the company's boss said before, is that we have to study the object did not understand. Learning should be constantly summed up and thinking.
2. The
Project requirements: There is a TXT document that holds a dictionary in English and translated, and now needs to use it to implement a simple English-Chinese dictionary function in the terminal input and output. That's what the demand is.
Requirements Analysis:
Objective: To realize an English-Chinese dictionary;
Function: Terminal input An English word, terminal output the translation of the word, without the word printing input error;
Research object: Before you do the problem, take a closer look at the contents of our TXT document that holds the data;
What is the law of the object?
Before you write the ideal method, talk about my initial idea.
Find the pattern:
1) English words begin with #, and then the translated content is displayed in a newline;
2) The content of the translation begins with trans:
3) Translation and the words of the respective lines;
4) English words are lowercase except trans;
5) The translation of different parts of speech uses the @ separate.
Problem Solving Ideas:
1) First to separate out the English words;
2) separating out the translation
3) put the translation and the words into a dictionary in the form of value and key respectively
3. Difficulties--how to separate?
Now I'll just stick to my code and say how I was separated at first.
-(Nsdictionary *) Getdicefromurl: (NSString *) url{nsmutabledictionary * Mydice = [[Nsmutabledictionary alloc] init]; acceptstring * acc = [[Acceptstring alloc] initwithurl:url]; NSString * mystr = [[NSString alloc] INITWITHSTRING:[ACC getString]]; Save the contents of the Dict.txt file Nsuinteger line = [acc countofsubstring:@ "#" INSTRING:MYSTR]; NSLog (@ "line =%ld", line); line = 43084NSMutableString * Mustr = [[Nsmutablestring alloc] initwithstring:mystr];for (Nsuinteger i = 0; i < 100;i + +) {nsrange r0 = [Mustr rangeofstring:@ "#"]; Nsrange r1 = [Mustr rangeofstring:@ "\ n"]; [Mustr DELETECHARACTERSINRANGE:R1]; Remove the first line break nsrange r2 = [mustr rangeofstring:@ "Trans"]; Nsrange R3 = [Mustr rangeofstring:@ "\ n"];//second newline nsstring * subStr0 = [Mustr substringwithrange:nsmakerange (R0. Location + 1, (r1.location-r0.location-1))]; Get the word part NSString * subStr1 = [Mustr substringwithrange:nsmakerange (R2.location, (R3.locatiOn-r2.location)]; The part that gets trans and its line is the translation part [Mydice setvalue:substr1 forkey:substr0];//into the dictionary [mustr deletecharactersinrange:n Smakerange (R0.location, r3.location)]; Remove the first word machine translation nsrange r4 = [Mustr rangeofstring:@ "#"];//position of the second # mustr = [[Nsmutablestring alloc] In Itwithstring:[mustr substringfromindex:r4.location]];//}return Mydice from the back part of the second # and a substring containing the second # number; }We saw that the amount of code was not much, but there was a for loop inside. Cycle 100 times, no problem, cycle 1000 times no problem, the system responds quickly. But here is the loop line = 43,084 times. And the loop inside the operation and more. This is my first separation of methods. Cause the final program can not be compiled out, into a long wait, waiting for the end I gave up. In fact, I think the above is not a problem for me, I also verify that in the case of i = 100 and 1000, there is no problem, the actual dictionary data is not a problem. If you and my head the same idea, is not wrong, you can try, because you do not try, you do not know where your problem is, without deep thinking is difficult to improve.
Later was a nudge, jumped out of the original thinking logic. Let's take a look at this better solution.
4. Look back at the demand
1) Take a look at our document that holds the dictionary data;
2) does each line end with a newline character \ n?
3) If the string in the document is separated by a newline character, and the value of the two elements adjacent to the number is saved into the array, one is the word, and the other is the translation of the word.
4) What about this method? Try not to know.
Soon the following code is in place: There are some changes in some places and above, but it does not affect the other parts.
Get file path strength, deposit in an array-(Nsdictionary *) Newdicefromurl: (NSString *) url{_mystr = [[NSString alloc] Initwithcontentsoffile: URL encoding:nsutf8stringencoding Error:nil]; Save the contents of the file in _mystr nsarray * NewArray = [_mystr componentsseparatedbystring:@ "\ n"]; The string is separated by a newline character, saved into the array nsuinteger newLine = [NewArray count]; NewLine = 86168for (int i = 0; i < newLine; i+=2) {NSString * trans = [Newarray[i + 1] substringfromindex:6];//Remove TR ans:nsstring * Word = [Newarray[i] substringfromindex:1];//Remove #nsstring * Newtrans = [trans stringbyreplacingoccurrencesofstring:@ "@" withstring:@ "\ n"]; Meet @ To change line [_mydice Setvalue:newtrans Forkey:word];} return _mydice;}
The first method inside we know that the document line = 43084, in fact, this is the number of words, not the actual number of lines, the following code in the NewLine = 86168, is the true number of this document, in fact, we already know the relationship between the two, there is no need to struggle with this problem. What's in front of you doesn't take up too much system time and memory. Mainly the content in the For loop. We used to write a microcontroller software delay program is not so written?
void delay (int time) {int i,j;for (i = 0;i < i + +) {for (j = 0; J < time; J + +);}}
Control the delay length by controlling the number of executions in the for loop? Inside the loop inside the
for (j = 0; J < time; J + +);
Is the length of delay we need to control. Let's take a look at the two methods above for the For loop and the contents of the two-segment code.
Same point
The number of cycles for two is the same. A line = 43,084 times, a newline is a multiple of line, but the cycle is i+=2, not i++, so the number of cycles is the same.
Different points
The number of code inside the loop, the contents of the two pieces of code, and the action we can see at a glance, which cycle speed is fast which is slow. I don't have to say more. The method of separating the two has said that the processing of the string is also a comment in the word code.
Terminal input
At the core of the project, we're done, and we're going to implement the terminal input section.
Before we write the code, we still look at the rules above, and the words that need to be translated are lowercase. Well, it's not difficult to implement such a terminal input.
Receive the input character, convert the string converted to OC to lowercase-(NSString *) Getinputword{char p[20] = {"}"};//Set the maximum length of the word to 20printf ("Please enter the word you want to query: \ n"); scanf ("%s", p); NSString * InStr = [[NSString alloc] Initwithutf8string:p];_word = [InStr lowercasestring];return _word;}
Why do you want to add a capital to lowercase? If our terminal input is an uppercase word, do we hint that the word is not? Obviously it is not difficult to do so.
Terminal output
Here is the output, in fact, how the output form. The simplest thing I have done.
-(void) mydice{dice * Mydice = [[Dice alloc] init];input * myinput = [[Input alloc] init]; Nsdictionary * Newdice = [Mydice newdicefromurl:@ "/users/qf/desktop/2015.07.24/Dictionary Project/dict.txt"];while (1) {NSString * s2 = [Myinput Getinputword]; NSString * result = [Newdice objectforkey:s2];if (Result! = nil) {printf ("%s\n", [[Newdice Objectforkey:s2] utf8string]);} else printf ("did not find the word you entered!\n");}}
Let's briefly explain this code:
First line: Create a Dictionary object Mydice;
Second line: Create an input object myinput;
The third line: Create a dictionary that holds the data;
Row four: while (1) cycle, we are not difficult to check a word is compiled and then query;
Line five: Save the received input in the string S2 of the newly created Declaration;
Line six: Declare a string result, save the dictionary Newdice key value key to S2 corresponding to the Value;result default value is nil, feel first to give it a good initial value;
Line seventh: Determine if result is null, if it is not empty, use printf to print the result of Vaule being converted to a C string;
Line eighth: If result is nil, the word is not found.
So the whole project is over, what is the content of the main function? is actually a line of code.
[[[Dice alloc] init] mydice];
After compiling, you can use this dictionary. Look at the results in the picture.
5. Summary
Is this small project difficult? Not hard. One step at a writing is OK. There's no such thing as finesse.
What are the points of this project used? arrays, strings, and dictionaries, but also are calls to some basic methods.
Why didn't you think of a good method at first? or their own thinking problems. Look at the point of view of the problem. I'm not sure what we're looking at.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"iOS development objective-c" homemade English-Chinese Dictionary Project