【问题标题】:How can a char value be converted to a specific int value如何将 char 值转换为特定的 int 值
【发布时间】:2018-09-05 09:54:14
【问题描述】:

我有一个任务,我必须编写一个程序,该程序允许人们输入一个七个字母的单词并将其转换为电话号码(例如 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变量呢?

【问题讨论】:

  • 您可能想了解std::tolower(或std::toupper)。
  • 至于您当前的代码,您似乎对其中的逻辑有一种奇怪的想法。在switch 语句中使用变量one 来检查它是否是18 之间的整数值。然后检查one 是否是带有特定字母的字符。但是one 不能永远 等于1 和例如'a' 同时。

标签: c++ char type-conversion


【解决方案1】:

自从我编写任何 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....


      }

}

【讨论】:

    【解决方案2】:

    您可以使用std::map

    例如,你可以有

    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
    }
    

    或者,您可以不使用数组,而是对 chars 执行算术运算(因为它们只是小整数)。

    int char_to_dig(char c) {
      c = std::toupper(c);
      if (c < 'A' || c > 'Z') return -1;
      if (c == 'Z') return 9;
      if (c > 'R') --c;
      return ((c-'A')/3)+2;
    }
    

    这会给你像这个垫子上的数字:


    显然,有一个类似的code gold question

    【讨论】:

    • 我个人会使用std::unordered_map,因为排序并不重要,而且访问时间为 O(1)。
    • @SU3 你可以使用std::tolower(c, std::locale()) 这样上面的例子也适用于大写字母。
    • 您可以使其功能齐全,为'0'-'9'(例如c - '0')返回0-9,特殊字符'*''#'保持不变。除了来自蓝盒子的声音外,触摸板是完整的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 2017-01-18
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 2017-03-21
    相关资源
    最近更新 更多