I was deeply touched by the spirit of open-source recently and thought about it for a long time. Today I published an "English-Chinese dictionary" that has accumulated my code for many years.
I made an English-Chinese dictionary in pure C language because I was bored or interested. The core algorithm is the KMP quick search algorithm. Although it is a bit long, it has a simple idea (I advocate simplicity). The basic idea is: When you input an English word, if the dictionary contains this English word, chinese meanings will be found; if not included, you will be reminded to enter Chinese meanings.
It has been a long time. It doesn't mean it is running now. I will improve it slowly when I have time.
If you are interested in improving on this basis, you can also tell me where to improve. Thank you!
View plain
/**************************
Fast pattern matching --- KMP Algorithm
**************************/
# Include <stdio. h>
# Define Max size 10000
Typedef struct
{
Char ch [MAXSIZE];
Int length;
} Seqstr;
Void Getnext (Seqstr p, int next []);
Void Kmp (Seqstr t, Seqstr p, int next []);
Void InputStr (Seqstr & str );
Void OutputStr (Seqstr & str );
Void FileToStr (FILE * fp, Seqstr & t );
Int main (void)
{
Int next [30];
FILE * fp;
Seqstr p, t;
P. length = 0;
T. length = 0;
InputStr (p );
OutputStr (p );
Fp = fopen ("word.txt", "r"); // InputStr (t );
If (fp! = NULL)
{
FileToStr (fp, t );
Fclose (fp );
OutputStr (t );
Getnext (p, next );
Kmp (t, p, next );
}
Else
{
Printf ("can't open file! \ N ");
}
Return 0;
}
Void FileToStr (FILE * fp, Seqstr & str)
{
Printf ("input string from file... \ n ");
While (! Feof (fp ))
{
Str. ch [str. length] = fgetc (fp );
Str. length ++;
}
Str. ch [str. length] = '\ 0 ';
}
Void InputStr (Seqstr & str)
{
Char c;
Printf ("input a string :");
While (c = getchar ())! = '\ N ')
{
Str. ch [str. length] = c;
Str. length ++;
}
Str. ch [str. length] = '\ 0 ';
}
Void OutputStr (Seqstr & str)
{
Int I = 0;
While (str. ch [I]! = '\ 0 ')
{
Printf ("% c", str. ch [I]);
I ++;
}
Printf ("\ n ");
}
Void Kmp (Seqstr t, Seqstr p, int next [])
{
Int I, j;
Int appearTimes = 0;
Int lines = 1;
I = 0;
J = 0;
While (I <t. length)
{
While (I <t. length & j <p. length)
{
If (j =-1 | t. ch [I] = p. ch [j])
{
I ++;
J ++;
}
Else
{
J = next [j];
}
If (t. ch [I] = '\ n') // calculates the number of rows
{
Lines ++;
I ++;
}
} // While
If (j = p. length) // position of the first occurrence (return i-p.length)
{
AppearTimes ++;
Printf ("The % d times appear at line: % d \ n", appearTimes, lines );
J = 0;
}
} // While
}
Void Getnext (Seqstr p, int next [])
{
Int I, j;
Next [0] =-1;
I = 0;
J =-1;
While (I <p. length)
{
If (j =-1 | p. ch [I] = p. ch [j])
{
++ I;
++ J;
Next [I] = j;
}
Else
{
J = next [j];
}
}
For (I = 0; I <p. length; I ++)
{
Printf ("next [% d] = % 3d \ n", I, next [I]);
}
Printf ("\ n ");
}
Test data:
View plain
Good
Hi!
From Deng xiumao's blog