#include <iostream> #include "string.h" #include <map> using namespace std; class CTransfer { public: CTransfer(); ~CTransfer(); void statToTrans(const string src, string& outString); private: string praseStr(const string srcStr, const char filter); bool getChar(const string str, string& outC); private: map<char, string> charMap; }; CTransfer::CTransfer() { charMap.insert(make_pair<char, string>('2', "ABC")); charMap.insert(make_pair<char, string>('3', "DEF")); charMap.insert(make_pair<char, string>('4', "GHI")); charMap.insert(make_pair<char, string>('5', "JKL")); charMap.insert(make_pair<char, string>('6', "MNO")); charMap.insert(make_pair<char, string>('7', "PQRS")); charMap.insert(make_pair<char, string>('8', "TUV")); charMap.insert(make_pair<char, string>('9', "WXYZ")); } CTransfer::~CTransfer() { charMap.clear(); } void CTransfer::statToTrans(const string src, string& outString) { string srcStr = src; while (1) { size_t pos = srcStr.find(' '); if (pos != srcStr.npos) { string outChar; string subStr = srcStr.substr(0, pos); if (getChar(subStr, outChar)) { outString.append(outChar); } srcStr = srcStr.substr(pos + 1); } else { string outChar; if (getChar(srcStr, outChar)) { outString.append(outChar); } break; } } } bool CTransfer::getChar(const string str, string& outC) { for (int i = 1; i < str.length(); i++) { if (str[0] != str[i]) { return false; } } map<char, string>::iterator itor = charMap.find(str[0]); if (itor != charMap.end()) { if (str[0] == '7' || str[0] == '9') { int index = (str.length() - 1) % 4; outC = (itor->second)[index]; } else { int index = (str.length() - 1) % 3; outC = (itor->second)[index]; } return true; } return false; } string CTransfer::praseStr(const string srcStr, const char filter) { string retStr; retStr.clear(); size_t pos = srcStr.find(filter); if (pos != srcStr.npos) { retStr = srcStr.substr(0, pos); } else { retStr = ""; } return retStr; } int main() { string str = "22 33 44 55 9 88 "; string outstr; CTransfer obj; obj.statToTrans(str, outstr); printf("%s\n", outstr.c_str()); system("pause"); return 0; }