能否将C语言数字转英文程序中的if(tens!=1)改为switch语句?
将整数转英文单词程序中的if语句替换为switch语句的方案
问题描述
原本计划用if-else编写接收1-9999整数并转换为对应英文单词的程序,现已改为使用switch语句实现,当前代码运行正常,但希望将代码中的if (tens != 1)语句替换为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; hundreds = num % 1000 / 100; tens = num % 1000 % 100 / 10; ones = num % 1000 % 100 % 10; 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; } switch(hundreds) { case 0: break; case 1: printf(" one hundred"); break; case 2: printf(" two hundred"); break; case 3: printf(" three hundred"); break; case 4: printf(" four hundred"); break; case 5: printf(" five hundred"); break; case 6: printf(" six hundred"); break; case 7: printf(" seven hundred"); break; case 8: printf(" eight hundred"); break; case 9: printf(" nine hundred"); break; } switch(tens) { { case 1: { switch(ones) { case 0: printf(" ten");break; case 1: printf(" eleven"); break; case 2: printf(" twelve"); break; case 3: printf(" thirteen"); break; case 4: printf(" fourteen"); break; case 5: printf(" fifteen"); break; case 6: printf(" sixteen"); break; case 7: printf(" seventeen"); break; case 8: printf(" eighteen"); break; case 9: printf(" nineteen"); break; } break; } break; } case 2: printf(" twenty"); break; case 3: printf(" thirty"); break; case 4: printf(" forty"); break; case 5: printf(" fifty"); break; case 6: printf(" sixty"); break; case 7: printf(" seventy"); break; case 8: printf(" eighty"); break; case 9: printf(" ninety"); break; } if (tens != 1) { switch(ones) { case 0: break; case 1: printf(" one"); break; case 2: printf(" two"); break; case 3: printf(" three"); break; case 4: printf(" four"); break; case 5: printf(" five"); break; case 6: printf(" six"); break; case 7: printf(" seven"); break; case 8: printf(" eight"); break; case 9: printf(" nine"); break; } } } getch(); }
解决方案
完全可以将if (tens != 1)替换为switch语句,利用switch匹配tens的所有可能取值(0-9),仅在tens不为1的分支中处理个位数字的输出,tens=1时直接跳过即可。
替换后的代码片段如下:
// 替换原有的if (tens != 1)块 switch(tens) { case 1: // 十位为1时已处理过10-19的英文,此处无需处理个位 break; case 0: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: switch(ones) { case 0: break; case 1: printf(" one"); break; case 2: printf(" two"); break; case 3: printf(" three"); break; case 4: printf(" four"); break; case 5: printf(" five"); break; case 6: printf(" six"); break; case 7: printf(" seven"); break; case 8: printf(" eight"); break; case 9: printf(" nine"); break; } break; }
额外优化建议
- 原代码中
tens的switch块存在多余的嵌套大括号,可删除以简化代码结构; - C标准中
main函数应声明为int main()并返回0,符合规范; <conio.h>是DOS时代的头文件,在现代编译器中可能不被支持,可考虑移除getch(),改用return 0;结束程序。
内容的提问来源于stack exchange,提问作者yourfriendlyneighborhoodcat
相关产品推荐
相关产品推荐

