⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shiyan.c

📁 关于t9输入法的详细代码
💻 C
字号:
#include<reg1210.h>
#include <stdio.h>
#include<string.h>
#include"PY_mb.h"
#include"51t9py_indexa.h"
#define CNTLQ      0x11
#define CNTLS      0x13
#define DEL        0x7F
#define BACKSPACE  0x08
#define CR         0x0D
#define LF         0x0A

unsigned char cpt9PY_Mblen;
struct t9PY_index code  * cpt9PY_Mb[16];

unsigned char T9PY_Get_Match_PY_MB(char *p_PadInput,struct t9PY_index code  ** List_match_PY_Mb)
{
    struct t9PY_index *p_PY_CurrenIndex,*p_PY_LastIndex,*p_PY_TempIndex;
    unsigned char i,j,cInputStrLength;
	unsigned char    T9PY_Match_NUM=0; //完全匹配拼音数量
    j=0;                                        //j为匹配最大值
	if(*p_PadInput=='\0')return(0);       //如果输入空字符返回0//

    cInputStrLength=strlen(p_PadInput);     //获取输入拼音串长度//
 

    p_PY_CurrenIndex=&(t9PY_index2[0]);                        //首索引地址赋值,p_PY_CurrenIndex为当前拼音索引地址
    p_PY_LastIndex=t9PY_index2+sizeof(t9PY_index2)/sizeof(t9PY_index2[0]);//最后索引地址之后的地址(作下一语句比较用)
	while(p_PY_CurrenIndex < p_PY_LastIndex)  //遍历字母索引表.或者使用语句: while((p_PY_CurrenIndex->t9PY_T9[0])!='\n')                       
	{
        for(i=0;i<cInputStrLength;i++)
        {
               if(*(p_PadInput+i)!=*((*p_PY_CurrenIndex).t9PY_T9+i))    //检查是否匹配
            {
                if (i+1 > j) 
                {
                    j=i+1;                    //j为不匹配字符串中前面字符匹配的最大值,例如:被比较字符串(键盘输入的字符串)为985,相比较的2个字符串(码表索引里面的字符串)为983和953,则j=2
                    p_PY_TempIndex=p_PY_CurrenIndex;
                }
                break;                        //不匹配,退出//
            }            
        }
        if((i==cInputStrLength) && (T9PY_Match_NUM<16))    //匹配,最多16组.匹配并不是指相比较的字符长短一样,不是完全匹配
        {
		
            List_match_PY_Mb[T9PY_Match_NUM]=p_PY_CurrenIndex;//
            T9PY_Match_NUM++;
        }
        p_PY_CurrenIndex++;
    }
     if(j!=cInputStrLength)                 //不匹配但最多匹配字符的1组
        List_match_PY_Mb[0]=p_PY_TempIndex;//
    return (T9PY_Match_NUM);                //输出完全匹配组数,0为无果而终//
}
void t9PY_Test(void)
{
    bit PYEnter=0;
    bit HZok=0;
    unsigned char temp;
//    unsigned char temp2;
    unsigned char t9PYn=0;
    char idata inline[16]={0x00};
    idata char chinese_word[3]="  ";
    char tempchar,Add=0,i=0;
    struct t9PY_index *cpTemp;
//    cpTemp=t9PY_index2;
    printf ("\n按键 /:上一拼音 *:下一拼音 .和空格及回车键:输入状态和选字状态切换\n");         //
    printf ("请按键:2-abc 3-def 4-ghi 5-jkl 6-mno 7-pqrs 8-tuv 9-wxyz \n");          //
    while(!HZok)
    {
        tempchar=getchar();
        switch (tempchar)
        {
//            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                  if (~PYEnter)
                 {
                    inline[i]=tempchar;
                    i++;
                    Add=0;
                   cpt9PY_Mblen=T9PY_Get_Match_PY_MB(inline,cpt9PY_Mb);
                }
                break;
            case '/':
                if (t9PYn >0) t9PYn --;
                break;
            case '*':
                t9PYn ++;
                if (t9PYn >=cpt9PY_Mblen) t9PYn --;
                break;
            case '-':
                if (Add >= 19) Add -=19;
                break;
            case '=':
            case '+':
                if (Add < strlen((*cpTemp).PY_mb) -19 )Add +=19;
                break;
            case BACKSPACE:
                if (i>0) i--;
                inline[i]=0x00;
                Add=0;
                cpt9PY_Mblen=T9PY_Get_Match_PY_MB(inline,cpt9PY_Mb);
                break;
            case '.':                        //输入状态和选字状态切换
            case ' ':
            case '\n':
                PYEnter ^=1;
                break;
            default     :
                break;
        }

        printf ("                                               \n");
          if (PYEnter)
         {
           printf ("Choise");
              cpTemp=cpt9PY_Mb[t9PYn];
            if((cpTemp != PY_mb_space) && (tempchar>='1') && (tempchar<='9'))
            {
                HZok=1;
                t9PYn=0;
                printf ("                                                 \n");
			  	printf ("Target of Chinese word you had selected :  ");
                chinese_word[0]=*((*cpTemp).PY_mb+(Add/2)*2+(tempchar-'1')*2+1);//Add除于2后又乘于2的作用是为了获得偶数(因为拼音码表首字符是'@'而不是汉字)
                chinese_word[1]=*((*cpTemp).PY_mb+(Add/2)*2+(tempchar-'1')*2+2);
                   printf (chinese_word);//显示最后选择的一个汉字
                   printf ("\n");
            }
            else
            {
			printf ("\n\n  The spell of your Selection : ");
                printf ((*(cpTemp)).PY);//输出你选择的拼音
               printf ("\n\n\n press NUM keys to select,or press '+' and '-' for other \n"); 
                   printf ((*cpTemp).PY_mb+(Add/2)*2+1);//显示你选择拼音对应的汉字,供给选择
                   printf ("\n1 2 3 4 5 6 7 8 9\n");
            }
        }
        else 
        {
            printf ("Spell  ");
            for (temp=t9PYn;temp<cpt9PY_Mblen;temp++)
            {
                cpTemp=cpt9PY_Mb[temp];
//                    temp2=((strlen((*(cpTemp)).PY_mb)-Add)/2);
//                    printf ("%2bd:%02bd:",temp,temp2);
                    printf (":");
                    printf ((*(cpTemp)).PY);
//                       printf ((*(cpTemp)).PY_mb+Add);
//                printf ("\n");
            }
//               printf ("\n");
        }
    }
}
void main (void) 
{
   unsigned char temp;
    temp = 0x00;
    SCON0  = 0x50;                /* SCON: mode 1, 8-bit UART, enable rcvr      */
    TMOD |= 0x20;                /* TMOD: timer 1, mode 2, 8-bit reload        */
    TH1   = 253;                   /* TH1:  reload value for 9600 baud @ 11.0592MHz */
    TL1  =  253;
	TR1   = 1;                       /* TR1:  timer 1 run                          */
    TI    = 1;                          /* TI:   set TI to send first char of UART    */
    PCON=0x00;
while(1)
    {  
        t9PY_Test();
    }

//temp=getchar();
//printf("换\n",temp);
//while(1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -