我編寫的類比IME

來源:互聯網
上載者:User

老師分配的一個任務,做一個類比IME

我的設計如下

有一個test.txt文字檔,作為儲存字碼和字根
啟動程式,讀取這個文字檔的資訊,把字碼和字根存入記憶體
然後先輸入字碼,顯示字根,輸入字根,就能顯示漢字了

原始碼如下

* IME程式*/
//作者:增廣賢文
#include <stdio.h>
#include <stdlib.h>

#define MAX 100

char filename[20]="test.txt";/*全域變數,檔案名稱 */
char test[MAX][MAX][MAX];/*全域變數,儲存字型檔 */

FILE * fp ;

void init(void);/*對儲存字型檔的三維數組進行初始化,每一個元素初始化'/0' */

int load(char * filename);/*將字型檔檔案的資訊存到記憶體 ,返回碼的總數 */

int print(int line);/*將打的漢字一次從顯示的控制台顯示到文字檔*/

int search_code(int line,char * str);/*根據輸入的碼,找到相應的碼,返回碼所在的行,沒有返回-1 */

int list_ph(int 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("字型檔檔案不能開啟 !");
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 ;

}

test.txt檔案如下:
a 阿 啊 呵 嗄 錒 吖 醃 錒 阿
b 不 部 本 報 本 埠 並 並 比
c 才 從 從 次 此 參 采 存 策
d 的 大 地 道 大 都 動 動 定
women 我們

輸入測試
women
1 我們
1
我們

a
1阿 2啊 3呵 4嗄 5錒 6吖 7 醃 8錒 9 阿
2

呵呵兩天做了一個囫圇的東西出來了
只要test.txt檔案足夠好,也就是編碼比較好
我作為智能全拼也是可以的

如果要做得更好,恐怕得在演算法下功夫了
比如鄰接矩陣可以最佳化字碼搜尋最佳化的速度

原始碼可以隨意複製使用,或者2次開發,最好將成果放到論壇上面大家共用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.