<pre name="code" class="cpp">/** 数字转换成单词,
如
1,0==》空格
2==》a或b或c
7==》p或q或r或s
*/
#include<iostream> #include<vector> using namespace std; typedef struct NUM_WORD_ { int count; char word[4]; }NUM_WORD; NUM_WORD GetWord(int num) { NUM_WORD m_word; m_word.count=0; int i=0; switch(num) { case 0: case 1: m_word.count=1; m_word.word[0]=' '; break; case 2: m_word.count=3; for(i=0;i<3;i++) m_word.word[i]='a'+i; break; case 3: m_word.count=3; for(i=0;i<3;i++) m_word.word[i]='d'+i; break; case 4: m_word.count=3; for(i=0;i<3;i++) m_word.word[i]='g'+i; break; case 5: m_word.count=3; for(i=0;i<3;i++) m_word.word[i]='j'+i; break; case 6: m_word.count=3; for(i=0;i<3;i++) m_word.word[i]='m'+i; break; case 7: m_word.count=4; for(i=0;i<4;i++) m_word.word[i]='p'+i; break; case 8: m_word.count=3; for(i=0;i<3;i++) m_word.word[i]='t'+i; break; case 9: m_word.count=4; for(i=0;i<4;i++) m_word.word[i]='w'+i; break; } return m_word; } void GetValue(char*numStr,int pos,char* wordStr) { if(pos>=strlen(numStr)) { wordStr[pos]='\0'; cout<<wordStr<<endl; return; }else{ NUM_WORD m1; int num=(int)(numStr[pos]-'0'); m1=GetWord(num); int i=0; for(i=0;i<m1.count;i++) { wordStr[pos]=m1.word[i]; GetValue(numStr,pos+1,wordStr); } } } void Output(char*numStr) { int pos=0; char*wordStr=new char[strlen(numStr)+1]; GetValue(numStr,pos,wordStr); if(wordStr!=NULL)delete []wordStr; } int main() { char*numStr="27"; Output(numStr); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-05 19:29:05