问题是要编写一个程序,要求用户输入 2 位数字,然后为其打印英文单词。假设您输入“41”,printf 函数会打印出“四十一”。我几乎完成了程序,但坚持将 11-19 位数字转换为相应的单词。我使用一个 switch 语句打印第一个数字,第二个 switch 语句打印第二个数字。看一下代码:
#include <stdio.h>
int main(void)
{
int first_digit, second_digit;
printf("Enter two digits: ");
scanf("%1d%1d",&first_digit,&second_digit);
switch(first_digit % 10) {
case 1: printf("ten"); 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;
}
switch(second_digit % 10) {
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;
}
return 0;
}