如何将char值转换为特定的int值

时间:2018-03-27 05:53:29

标签: c++ char type-conversion

我有一项任务,我必须制作一个程序,允许一个人输入七个字母的单词并将其转换为电话号码(例如1-800-PAINTER至1-800-724-6837)。我试图让每个字母转换成一个特定的数字输出给用户,每个字母对应于电话键盘上的数字(所以a,A,b,B或c,C等于1,即,更多信息:https://en.wikipedia.org/wiki/Telephone_keypad)。

目前我已将其设置为使输入字的每个字母分别代表一个,两个,三个,四个,五个,六个或七个的char变量。然后,使用switch和if语句,想法是将char转换为xtwo = 2,xthree = 3等的int变量。然而,这并不起作用。有更好的方法吗?

代码示例(直到第一个开关,但主要是它的重复模式):

int main()
{
    char one, two, three, four, five, six, seven;

    cout << "Enter seven letter word (1-800-***-****): " << "\n";

    cin >> one >> two >> three >> four >> five >> six >> seven;
    int xtwo = 2; int xthree = 3; int xfour = 4; int xfive = 5; int xsix = 6; int xseven = 7; int xeight = 8;
    int xnine = 9;

    switch (one)
    {
    case 1:
        if (one == 'a' || one == 'b' || one == 'c' || one == 'A' || one == 'B' || one == 'C')
        {
            one = xtwo;
        }
        break;
    case 2:
        if (one == 'd' || one == 'e' || one == 'f' || one == 'D' || one == 'E' || one == 'F')
        {
            one = xthree;
        }
        break;
    case 3:
        if (one == 'g' || one == 'h' || one == 'l' || one == 'G' || one == 'H' || one == 'L')
        {
            one = xfour;
        }
        break;
    case 4:
        if (one == 'j' || one == 'k' || one == 'l' || one == 'J' || one == 'K' || one == 'L')
        {
            one = xfive;
        }
        break;
    case 5:
        if (one == 'm' || one == 'n' || one == 'o' || one == 'M' || one == 'N' || one == 'O')
        {
            one = xsix;
        }
        break;
    case 6:
        if (one == 'p' || one == 'q' || one == 'r' || one == 's' || one == 'P' || one == 'Q' || one == 'R' || one == 'S')
        {
            one = xseven;
        }
        break;
    case 7:
        if (one == 't' || one == 'u' || one == 'v' || one == 'T' || one == 'U' || one == 'V')
        {
            one = xeight;
        }
        break;
    case 8:
        if (one == 'w' || one == 'x' || one == 'y' || one == 'z' || one == 'W' || one == 'X' || one == 'Y' || one == 'Z')
        {
            one = xnine;
        }
        break;
    }

因此,实质上,如何将字母的char变量转换为特定的int变量?

2 个答案:

答案 0 :(得分:5)

您可以使用std::map

例如,您可以

ArrayList<ArchiveIssueModel> list = new ArrayList<>();

ArrayAdapter<ArchiveIssueModel> spinnerArrayAdapter =
        new ArrayAdapter<ArchiveIssueModel>(getActivity(),
                android.R.layout.simple_spinner_item, list);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_year.setAdapter(spinnerArrayAdapter);

// What it means?
sp_year.setSelection(spinnerArrayAdapter.getPosition(year));

然后

std::map<char,int> char_to_dig {
  {'a',1}, {'b',1}, {'c',1},
  {'d',2}, {'e',2}, {'f',2}
};

会给你char_to_dig['a']

或者,您可以编写一个执行映射的函数。有点像这样:

1

或者,不是使用数组,而是可以对int char_to_dig(char c) { static const char _c[] = "abcdefghi"; static const int _i[] = { 1,1,1,2,2,2,3,3,3 }; for (unsigned i=0; i<9; ++i) { if (_c[i]==c) return _i[i]; } return -1; // some value to signal error } 执行算术运算(因为它们只是小整数)。

char

这会给你这样的数字:

enter image description here

显然,有类似的code gold question

答案 1 :(得分:0)

自从我编写任何c / c ++代码以来,已经有好几年了,我甚至没有安装编译器来测试......但是这应该让你开始走上正轨 检查功能和语法......一切都在我脑海中。需要检查。 //

int numArray[7];
char inputStr[10]; 
cout << " give me 7 characters";
cin >> input;

/*
use a for loop to read the string letter by letter (a string in c is an 
array of characters)
convert the characters to uppercase 
fall through case statements for each group of letters
assing value to output array to do wiht as you like.
*/


for(i=0; i < 7; i++){
    inputStr[i] = toupper(inputStr[i]);
    switch(input[i]){
      case 'A':
      case 'B':
      case 'C':
        numArray[i] = 2;
        break;
      case 'D':
      case 'E':
      case 'F':
        numArray[i] = 3;
        break;

    and so on and so foth....


      }

}