• 面试题九宫格输入法,数字和字符串转换


    #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
    ", outstr.c_str());
        system("pause");
        return 0;
    }
  • 相关阅读:
    Jzoj5422 天才绅士少女助手克里斯蒂娜
    Jzoj5422 天才绅士少女助手克里斯蒂娜
    Jzoj5421 嘟嘟噜
    Jzoj5421 嘟嘟噜
    Jzoj5460【NOIP2017提高A组冲刺11.7】士兵训练
    Jzoj5460【NOIP2017提高A组冲刺11.7】士兵训练
    Jzoj5459【NOIP2017提高A组冲刺11.7】密室
    PAT甲级——A1046 Shortest Distance
    PAT甲级——A1045 Favorite Color Stripe
    PAT甲级——A1044 Shopping in Mars
  • 原文地址:https://www.cnblogs.com/weiyouqing/p/12536685.html
Copyright © 2020-2023  润新知