A task assigned by the instructor for a simulated Input Method
My design is as follows:
There is a test.txt text file, which is used as the storage code and word root.
Start the program, read the information of the text file, and store the character code and word root to the memory.
Then, enter the font code to display the font root and the font root to display the Chinese characters.
The source code is as follows:
* Input method program */
// Author: Zeng xianwen
# Include <stdio. h>
# Include <stdlib. h>
# Deprecision max 100
Char filename [20] = "test.txt";/* global variable, file name */
Char test [Max] [Max] [Max];/* global variable, storage font */
File * FP;
Void Init (void);/* initialize the 3D array of the storage font library, and initialize each element '/0 '*/
Int load (char * filename);/* Save the information of the font file to the memory, total number of returned codes */
Int print (INT line);/* display the Chinese character once from the displayed console to the text file */
Int search_code (INT line, char * Str);/* locate the corresponding code based on the entered code. The row where the returned code is located does not return-1 */
Int list_ph (INT Code);/* lists the position of the font Root Based on the location of the Code */
Int main ()
{
// Int I = 0;
// Int J = 0;
Int line = 0;
Init ();
Line = load (filename );
Print (line );
Getchar ();
System ("pause ");
Return 0;
}
Void Init (void)
{
Int I = 0, j = 0, K = 0;
For (I = 0; I <Max; I ++)
For (j = 0; j <Max; j ++)
For (k = 0; k <Max; k ++)
Test[J] [k] = '/0 ';
}
Int load (char * filename)
{
Int I = 0, j = 0, K = 0;
Char ch;
If (FP = fopen (filename, "R") = NULL)
{
Printf ("the font file cannot be opened! ");
Getchar ();
Exit (0 );
}
While (CH = fgetc (FP ))! = EOF)
{
If (Ch! = '')
{
Test[J] [k] = CH;
K ++;
}
If (CH = '')
{
Test[J] [k] = '/0 ';
J ++;
K = 0;
}
If (CH = '/N ')
{
I ++;
J = 0;
K = 0;
}
}
Fclose (FP );
Return I;
}
Int print (INT line)
{
Int choose = 0;
Int code = 0;
Int Total = 0;
Char STR [100];
Loop:
Gets (STR );
// Printf ("% s/n", STR );
Code = search_code (line, STR );
// Printf ("% s/n", test [Code] [0]);
If (code! =-1)
Total = list_ph (CODE );
Else
Goto loop;
Loop2:
Scanf ("% d", & choose );
If (choose! = 0 & choose <= total & choose> = 0)
Printf ("% s", test [Code] [Choose]);
Else
Goto loop2;
Return 1;
}
Int search_code (INT line, char * Str)
{
Int I = 0;
For (I = 0; I <= line; I ++)
{
If (strcmp (STR, test[0]) = 0)
Return I;
}
Return-1;
}
Int list_ph (INT code)
{
Int J = 0;
For (j = 1; test [Code] [J] [0]! = '/0'; j ++)
{
Printf ("% d. % s", J, test [Code] [J]);
}
Return J;
}
The test.txt file is as follows:
Ah ah
B Does not report this port and
C is used from this parameter
D. All major geographies are dynamic.
Women we
Input test
Women
1. We
1
We
A
1 AH 2 Ah 3 Ah 4 ah 5 AH 6 Ah 7 pickled 8 Ah 9
2
Ah
Oh, two days ago, I made a dirty copy.
Only the test.txt file is good enough, that is, the encoding is better.
I can also work as a smart player.
If you want to do better, I'm afraid you have to work on algorithms.
For example, the adjacent matrix can optimize the speed of character/code search.
Source code can be copied and used at will, or developed twice. It is best to share the results on the forum.