简体   繁体   English

在 c 中将 if 语句转换为 switch 语句

[英]Converting if statements to switch statements in c

I wrote a program that will ask for an integer input (1 - 9999) and will convert the inputted integer into its corresponding word format in English.我编写了一个程序,它将要求输入 integer (1 - 9999),并将输入的 integer 转换为相应的英文单词格式。

And I'm trying to modify it so that switch statements will be used instead of if-statements (where it is applicable).我正在尝试修改它,以便使用 switch 语句而不是 if 语句(如果适用)。

Example 1: Input number: 2481 Output: two thousand four hundred eighty one例1:输入数:2481 Output:2481

Here is my code:这是我的代码:

#include<stdio.h>
#include<conio.h>

main()
{
      int num,thousands,hundreds,tens,ones;
      printf("Enter number (1-9999): ");
      scanf("%d",&num);
      //&& - check if within the range
      //|| - check if outside of the range
      if (num < 1 || num > 9999)
         printf("Invalid number.");
      else
      //if (num > 0 && num < 10000)
      {
      thousands = num / 1000;
      hundreds = num % 1000 / 100;
      tens = num % 1000 % 100 / 10;
      ones = num % 1000 % 100 % 10;
      //if (num / 1000 == 1)
      if(thousands == 1)
                   printf("one thousand ");
      if(thousands == 2)
                   printf("two thousand ");
      if(thousands == 3)
                   printf("three thousand ");
      if(thousands == 4)
                   printf("four thousand ");
      if(thousands == 5)
                   printf("five thousand ");
      if(thousands == 6)
                   printf("six thousand ");
      if(thousands == 7)
                   printf("seven thousand ");
      if(thousands == 8)
                   printf("eight thousand ");
      if(thousands == 9)
                   printf("nine thousand ");
      //if (num % 1000 / 100 == 1)
      if(hundreds == 1)
                   printf("one hundred ");
      if(hundreds == 2)
                   printf("two hundred ");
      if(hundreds == 3)
                   printf("three hundred ");
      if(hundreds == 4)
                   printf("four hundred ");
      if(hundreds == 5)
                   printf("five hundred ");
      if(hundreds == 6)
                   printf("six hundred ");
      if(hundreds == 7)
                   printf("seven hundred ");
      if(hundreds == 8)
                   printf("eight hundred ");
      if(hundreds == 9)
                   printf("nine hundred ");
      //if (num % 1000 % 100 / 10 == 1)
      if(tens == 1)
      {
              //if (num % 1000 % 100 % 10 == 0)
              if(ones == 0)
                      printf("ten ");
              if(ones == 1)
                   printf("eleven ");
              if(ones == 2)
                   printf("twelve ");
              if(ones == 3)
                   printf("thirteen ");
              if(ones == 4)
                   printf("fourteen ");
              if(ones == 5)
                   printf("fifteen ");
              if(ones == 6)
                   printf("sixteen ");
              if(ones == 7)
                   printf("seventeen ");
              if(ones == 8)
                   printf("eighteen ");
              if(ones == 9)
                   printf("nineteen ");
      }
      if(tens == 2)
                   printf("twenty ");
      if(tens == 3)
                   printf("thirty ");
      if(tens == 4)
                   printf("forty ");
      if(tens == 5)
                   printf("fifty ");
      if(tens == 6)
                   printf("sixty ");
      if(tens == 7)
                   printf("seventy ");
      if(tens == 8)
                   printf("eighty ");
      if(tens == 9)
                   printf("ninety ");
      if (tens != 1)
      {
               if(ones == 1)
                   printf("one ");
               if(ones == 2)
                   printf("two ");
               if(ones == 3)
                   printf("three ");
               if(ones == 4)
                   printf("four ");
               if(ones == 5)
                   printf("five ");
               if(ones == 6)
                   printf("six ");
               if(ones == 7)
                   printf("seven ");
               if(ones == 8)
                   printf("eight ");
               if(ones == 9)
                   printf("nine ");
      }
      
      }
      //else
          //printf("Invalid number.");
      getch();
}

However, when I tried substituting the if statements into switch statements it displays a blank.但是,当我尝试将 if 语句替换为 switch 语句时,它显示为空白。 I'm stuck at the thousands place.我被困在成千上万的地方。 I'm trying to fix it first before getting to the hundreds, tenths, and ones.我试图先修复它,然后再达到数百、十分之一和一个。 Here is what I tried:这是我尝试过的:

#include<stdio.h>
#include<conio.h>

main()
{
    int num,thousands,hundreds,tens,ones;
      printf("Enter number (1-9999): ");
      scanf("%d",&num);
      if (num < 1 || num > 9999)
         printf("Invalid number.");
      else
    {
        thousands = num / 1000;
        switch (thousands)
        {
            case '1': printf("one thousand ");
                      break;
            case '2': printf("two thousand ");
                      break;
            case '3': printf("three thousand ");
                      break;
            case '4': printf("four thousand ");
                      break;
            case '5': printf("five thousand ");
                      break;
            case '6': printf("six thousand ");
                      break;
            case '7': printf("seven thousand ");
                      break;
            case '8': printf("eight thousand ");
                      break;
            case '9': printf("nine thousand ");
                      break;
        }
    }
}

You're not comparing the same values you were before.您没有比较以前的相同值。

In your original code, you were comparing thousands against 0 , 1 , 2 , etc. In your updated code, you're comparing thousands against '0' , '1' , '2' , etc.在您的原始代码中,您将thousands012等进行比较。在更新后的代码中,您将thousands'0''1''2'等进行比较。

Use the same values you had before and you'll get the expected results.使用与之前相同的值,您将获得预期的结果。

When you write case '1': , you compare to character '1' 's value (converted to an integer), not integer;当您编写case '1':时,您将比较字符'1'的值(转换为整数),而不是 integer; you should write case 1: .你应该写case 1:

Single quotes in C are used to define single characters. C 中的单引号用于定义单个字符。 Since characters size is 1 byte in C, a char can be assigned up to 128 different values (1 byte = 8 bits = 2⁸ = from 0 to 127 ).由于 C 中的字符大小为 1 字节,因此可以为一个char分配多达 128 个不同的值(1 字节 = 8 位 = 2⁸ =从 0 到 127 )。 But how are char represented, according to that integer value?但是如何根据char值表示字符?
That is implementation-defined, but usually C uses the seven-bit US ASCII character set to represent char by default (for example, you might change the locale to have it use something different, like utf-8).这是实现定义的,但通常 C默认使用7 位 US ASCII字符集来表示char (例如,您可以更改语言环境以使其使用不同的东西,例如 utf-8)。

Back to your question, in the second snippet you're comparing the value of thousands with the ASCII values of the characters '1' , '2' , '3' , etc.回到您的问题,在第二个片段中,您将thousands位的值与字符'1''2''3'等的 ASCII 值进行比较。
That means you are not comparing thousands with the value between single quotes, but with the character's ASCII decimal value corresponding to the character, so for example, case '1': equals to case 49:这意味着您不是将thousands位与单引号之间的值进行比较,而是与字符对应的字符的 ASCII 十进制值进行比较,例如, case '1':等于case 49:

decimal -   character

[...]

49          1
50          2
51          3
52          4
53          5
54          6

[...]

Practical Example实际例子

The following program shows just what I've said:以下程序显示了我所说的内容:

#include <stdio.h>

int main(int argc, char** argv)
{
    char ch = '1';

    printf("size of a char: %ld bytes\n", sizeof(char));
    printf("character representation: %c\n", ch);
    printf("character corresponding value in ASCII table: %d\n", ch);

    return 0;
}

Output: Output:

size of a char: 1 bytes
character representation: 1
character corresponding value in ASCII table: 49

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

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