您的位置:首页 > 移动开发 > 微信开发

通过手机键盘将字符串转换为数字的小程序

2011-01-04 18:28 381 查看
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{
bool br = true;
vector<string> vecPhotoNumber;
vecPhotoNumber.push_back("abc");
vecPhotoNumber.push_back("def");
vecPhotoNumber.push_back("ghi");
vecPhotoNumber.push_back("jkl");
vecPhotoNumber.push_back("mno");
vecPhotoNumber.push_back("pgrs");
vecPhotoNumber.push_back("tuv");
vecPhotoNumber.push_back("wxyz");

string strWord;
string strNumber;
char buf[2] = { 0 };
while (getline(cin, strWord))
{
strNumber.clear();
br = true;
std::transform(strWord.begin(), strWord.end(), strWord.begin(), tolower);
for (int i = 0; i < strWord.size(); i++)
{
if (!isalpha(strWord[i]))
{
br = false;
break;
}

int tmp = (int)(strWord[i] - 'a');
int reduce = 0;

// Beyond 's'.
if (tmp > 17)
{
reduce++;
}

// Beyond 'z'.
if (tmp > 24)
{
reduce++;
}

tmp = (tmp - reduce) / 3 + 2;

sprintf(buf, "%d", tmp);
strNumber += buf;
}

if (false == br)
{
cout << "Invalid inputs!" << endl;
}
else
{
cout << strNumber << endl;
}

}

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐