[英]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.