[英]Converting if statements to switch statements in c
我编写了一个程序,它将要求输入 integer (1 - 9999),并将输入的 integer 转换为相应的英文单词格式。
我正在尝试修改它,以便使用 switch 语句而不是 if 语句(如果适用)。
例1:输入数:2481 Output:2481
这是我的代码:
#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();
}
但是,当我尝试将 if 语句替换为 switch 语句时,它显示为空白。 我被困在成千上万的地方。 我试图先修复它,然后再达到数百、十分之一和一个。 这是我尝试过的:
#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;
}
}
}
您没有比较以前的相同值。
在您的原始代码中,您将thousands
与0
、 1
、 2
等进行比较。在更新后的代码中,您将thousands
与'0'
、 '1'
、 '2'
等进行比较。
使用与之前相同的值,您将获得预期的结果。
当您编写case '1':
时,您将比较字符'1'
的值(转换为整数),而不是 integer; 你应该写case 1:
C 中的单引号用于定义单个字符。 由于 C 中的字符大小为 1 字节,因此可以为一个char
分配多达 128 个不同的值(1 字节 = 8 位 = 2⁸ =从 0 到 127 )。 但是如何根据char
值表示字符?
这是实现定义的,但通常 C默认使用7 位 US ASCII字符集来表示char
(例如,您可以更改语言环境以使其使用不同的东西,例如 utf-8)。
回到您的问题,在第二个片段中,您将thousands
位的值与字符'1'
、 '2'
、 '3'
等的 ASCII 值进行比较。
这意味着您不是将thousands
位与单引号之间的值进行比较,而是与字符对应的字符的 ASCII 十进制值进行比较,例如, case '1':
等于case 49:
decimal - character
[...]
49 1
50 2
51 3
52 4
53 5
54 6
[...]
以下程序显示了我所说的内容:
#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:
size of a char: 1 bytes
character representation: 1
character corresponding value in ASCII table: 49
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.