| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1549 人关注过本帖
标题:仿照手机上的拼音输入法
只看楼主 加入收藏
kaikai
Rank: 1
等 级:新手上路
帖 子:236
专家分:0
注 册:2005-1-7
收藏
 问题点数:0 回复次数:1 
仿照手机上的拼音输入法

手机的汉字拼音输入法很'聪明',只要用数字键组合,就能够自动找到能组成拼音的字母组合。 从2代表abc,3:def,4:ghi,5:jkl,6:mno,7:pqrs,8:tuv,9:wxyz

写一个程序,对输入的数字组合,找到匹配的字母组合成拼音输出。 如果有多个匹配则按照字母顺序排列后输出。 pinyin2.txt文件,在这里下载 [attach]2923[/attach]

搜索更多相关主题的帖子: 手机 拼音 输入法 仿照 
2005-08-21 14:44
kaikai
Rank: 1
等 级:新手上路
帖 子:236
专家分:0
注 册:2005-1-7
收藏
得分:0 

#pragma warning(disable:4786)

#include <cstdio> #include <cassert> #include <vector> #include <algorithm> #include <string>

using namespace std;

class NameList { public: void init() { string a; for (int j = 0; ym[j]; j++) { a = ym[j]; mStrs.push_back(a); for (int i = 0; sm[i]; i++) { a = sm[i]; a += ym[j] ; mStrs.push_back(a); } } } void load(const char *file) { char buf[128]; char buf2[1024]; string a; mStrs.clear(); mStrGBs.clear(); FILE *fp = fopen(file,"rt"); while(fscanf(fp, "%s %s", buf, buf2) == 2) { a = buf; mStrs.push_back(a); a = buf2; mStrGBs.push_back(a); } fclose(fp); } void show() { for (vector< string >::iterator s = mStrs.begin(); s != mStrs.end(); ++s) { printf("%s ", s->c_str()); } } vector< string > mStrs; vector< string > mStrGBs; private: static const char *sm[]; static const char *ym[]; };

const char *NameList::sm[]= { "b","p","m","f","d", "t","n","l","g","k", "h","j","q","x","z", "c","s","zh","ch","sh", "r","y","w", NULL, }; const char *NameList::ym[]= { "a","ai","ao","an","ang", "o","ou","ong", "e","ei","er","en","eng", "i","iang","ian","iao","in","ing","iu","ia","ie","iong", "u","uo","uang","un","uai","uan", "ui","ue","ua", NULL, }; class Digit2PinyinConverter { public: struct StrCmpFunctor { operator () (const char *s1, const char *s2) { int len1 = strlen(s1); int len2 = strlen(s2); if (len1 != len2) return len1 < len2; return strcmp(s1, s2) < 0; } }; void init() { validStrings.clear(); vector< string >::iterator name = namelist.mStrs.begin(); while(name != namelist.mStrs.end()) { validStrings.push_back(name->c_str()); ++name; } digits = ""; sort(validStrings.begin(), validStrings.end(), StrCmpFunctor()); } /**//// @param char digit '2'..'9' void push(char digit) { if (digit < '2' || digit > '9') return; digits += digit; vector< const char * > next; vector< const char * >::iterator name = validStrings.begin(); while(name != validStrings.end()) { if (match(*name, digits.size() - 1, digit)) { next.push_back(*name); } ++name; } validStrings = next; sort(validStrings.begin(), validStrings.end(), StrCmpFunctor()); }

void show() { printf("+-------+-------+-------+\n"); printf("| |2 abc |3 def |\n"); printf("+-------+-------+-------+\n"); printf("|4 ghi |5 jkl |6 mno |\n"); printf("+-------+-------+-------+\n"); printf("|7 pqrs |8 tuv |9 wxyz |\n"); printf("+-------+-------+-------+\n"); printf("|-<prep>|0<back>|+<next>|\n"); printf("+-------+-------+-------+\n"); } string digits; vector< const char * > validStrings; static NameList namelist; private: bool match(const char *name, int pos, char digit) { assert(digit >= '0' && digit <= '9'); int len = strlen(name); if (len <= pos) return false; char d = name[pos]; const char *maplist = map[digit - '0']; for (; *maplist; maplist++) { if (d == *maplist) return true; } return false; } static const char *map[10]; }; const char *Digit2PinyinConverter::map[10]= { "", "","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz" }; NameList Digit2PinyinConverter::namelist;

int main() { char c = '0'; int page = 0; int i;

Digit2PinyinConverter converter; Digit2PinyinConverter::namelist.load("pinyin2.txt"); converter.init(); do { if (c == '\n') continue; if (c == '0') converter.init(); if (c == '+') { page++; int maxpage = (converter.validStrings.size() + 9)/10; if (page > maxpage) page = maxpage; } if (c == '-' && page > 0) page--; if (c >= '2' && c <= '9') { page = 0; converter.push(c); } system("cls"); converter.show(); for (i = 0; i < (int)converter.digits.size(); i++) printf("%c", converter.digits[i]); printf("\n"); for (i=0;i<10 && i + page * 10 < (int)converter.validStrings.size();i++) { if (strlen(converter.validStrings[i + page * 10]) > strlen(converter.digits.c_str())) break; printf("%d:%s ", i, converter.validStrings[i + page * 10]); } printf("\nPage %d, Total %d\n", page, converter.validStrings.size()); } while(scanf("%c", &c) == 1);

return 0; }


Have you visit acm.tongji. lately?
2005-08-21 14:50
快速回复:仿照手机上的拼音输入法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023438 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved