简体   繁体   English

c++ 和 static_cast 的新功能<char> ()</char>

[英]New to c++ and static_cast<char>()

So I'm in college right now and I have this lab that wants me to make a code that inputs an integer and outputs a corresponding letter, I was able to get the code to work but it wants me to use static_cast() for 1-9 " Write a program that prompts the user to input an integer between 0 and 35. The prompt should say Enter an integer between 0 and 35:所以我现在在上大学,我有这个实验室要我编写一个输入 integer 并输出相应字母的代码,我能够让代码工作但它要我使用 static_cast() for 1 -9 " 编写一个程序,提示用户输入一个介于 0 到 35 之间的 integer。提示应该说输入一个介于 0 到 35 之间的 integer:

If the number is less than or equal to 9, the program should output the number;如果数字小于或等于9,程序应该是output这个数字; otherwise, it should output:否则,它应该是 output:

A for 10 B for 11 C for 12. . A 为 10 B 为 11 C 为 12。 . . and Z for 35. (Hint: For numbers >= 10, calculate the ACSII value for the corresponding letter and convert it to a char using the cast operator, static_cast().)" Z 代表 35。(提示:对于 >= 10 的数字,计算相应字母的 ACSII 值,并使用强制转换运算符 static_cast() 将其转换为字符。)”

and here is what I have.这就是我所拥有的。

 #include <iostream>

using namespace std;

int main() 
{
 int num;

 cout << "Enter an integer between 0 and 35: ";
 cin >> num;
 cout << endl;

 switch(num)
 {
   case 0:
    cout << "0";
      break;
   case 1:
    cout << "1";
      break;
   case 2:
    cout << "2";
      break;
   case 3:
    cout << "3";
      break;
   case 4:
    cout << "4";
      break; 
   case 5:
    cout << "5";
      break;
   case 6:
    cout << "6";
      break;
   case 7:
    cout << "7";
      break;
   case 8:
    cout << "8";
      break;
   case 9:
    cout << "9";
      break;
   case 10:
    cout << "A";
    break;
   case 11:
    cout << "B";
   case 12:
    cout << "C";
   case 13:
    cout << "D";
   case 14:
    cout << "E";
   case 15:
    cout << "F";
   case 16:
    cout << "G";
   case 17:
    cout << "H";
   case 18:
    cout << "I";
   case 19:
    cout << "J";
   case 20:
    cout << "K";
   case 21:
    cout << "L";
   case 22:
    cout << "M";
   case 23:
    cout << "N";
   case 24:
    cout << "O";
   case 25:
    cout << "P";
   case 26:
    cout << "Q";
   case 27:
    cout << "R";
   case 28:
    cout << "S";
   case 29:
    cout << "T";
   case 30:
    cout << "U";
   case 31:
    cout << "V";
   case 32:
    cout << "W";
   case 33:
    cout << "X";
   case 34:
    cout << "Y";
   case 35:
    cout << "Z";
 }
    // Write your main here
    return 0;
}

Here's a quick improvement to you code.这是对您的代码的快速改进。 Rather than have 36 case statements, use a table.与其使用 36 个 case 语句,不如使用一个表。

const char table[] = "0123456789ABCDEFGHUJKLMNOPQRSTUVWXYZ";

if ((num >= 0) && (num <= 35))
{
    cout << table[num];
}

But I suspect your professor wants you to make use of the fact that chars are integers to and can be added with an offset to get another char.但是我怀疑您的教授希望您利用字符是整数并且可以添加偏移量以获得另一个字符这一事实。 (eg 'A' + 1 == 'B' ). (例如'A' + 1 == 'B' )。 And all character literals like '4' and 'G' have ordinal values that match their values on the ascii char.并且所有字符文字,如“4”和“G”,都具有与其在 ascii 字符上的值相匹配的序数值。 (eg 'A' is 65 and '0' is 48) (例如,'A' 是 65,'0' 是 48)

So I think what they are getting at is this:所以我认为他们的意思是:

if ((num >= 0) && (number <= 9))
{
    int ord = '0' + num;
    cout << static_cast<char>(ord) << endl;
}
else if ((num>= 10) && (num <= 35))
{
    int ord = 'A' + num - 10;
    cout << static_cast<char>(ord) << endl;
}

Notice that I'm using character literals in single quotes ( 'A' ) instead of string literals in double quotes like "A" .请注意,我在单引号 ( 'A' ) 中使用字符文字,而不是像"A"这样的双引号中的字符串文字。 You can effectively "add" chars together.您可以有效地将字符“添加”在一起。 Math operations applied to strings have a quite different effect.应用于字符串的数学运算具有完全不同的效果。

Pedantic comment on my answer.对我的回答的迂腐评论。 Technically, the C standard only stipulates that digit chars ( '0' to '9' ) are aligned sequentially in ordinal values.从技术上讲,C 标准仅规定数字字符( '0''9' )按顺序值顺序对齐。 Ancient encodings like EBCIDIC didn't have the letters in sequential order.像 EBCIDIC 这样的古代编码没有按顺序排列的字母。 But your prof called out "ascii", so you're in the clear.但是你的教授叫了“ascii”,所以你没有问题。

The ideal solution here is to build a time machine and get the ASCII developers to put 9 adjacent to A in order to simplify the logic required.这里理想的解决方案是构建一个时间机器并让 ASCII 开发人员将 9 放在 A 旁边,以简化所需的逻辑。 :) :)

What you have with the big switch is unsatisfactory for the test.你有什么大开关是不能令人满意的测试。 I recommend looking at the ASCII table and observing the decimal numbers that represent each character.我建议查看ASCII 表并观察代表每个字符的十进制数字。

For example, 65 is 'A', 66 is 'B'.例如,65 是“A”,66 是“B”。 static_cast<char>(65) will output 'A'. static_cast<char>(65)将 output 'A'。 With a little bit of math, you can manipulate the input number to add it to 65. A trick in C/C++ is that you can add to the character value directly, such as 'A' + offset .只需一点数学知识,您就可以操纵输入数字将其加到 65。C/C++ 中的一个技巧是您可以直接将其加到字符值上,例如'A' + offset

Try to get your resulting code as simple as possible!尝试让您的结果代码尽可能简单!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM