数字的两种读法
数字每一位按英文读与整体按英文读
编写程序:
(1) 请求用户输入正整数,整数值应小于1000,大于0。
(2) 编写一个convertIntToWords(int value)函数,它将传递的值转换为单词,并输出单词。例如,13得到“one three”,895得到“eight nine five”。
(3) 然后编写一个新的函数convertIntToRealWords(int value),它将传递的值转换为我们真正说的单词。例如,13是“thirteen”而不是“one three”,895读作“eight hundred ninety five”,而不是“eight nine five”。
(4) 在main()函数中分别调用这两个函数
(5) 用1,12,20,123,100,102,520作为输入的正整数来测试你的程序。
/*
Name:programme4.c
Author:祁麟
Copyright:BJTU | school of software
Date:2020/10/27
Description:write a function convertIntToRealWords(int value), which
converts the passed value to words as we really say them.
*/
#include<stdio.h>
int main(void){
int number;
printf("退出请输入0");
while(1){
printf("\n请输入一个大于0小于1000的数字: ");
scanf("%d",&number);
if (number==0) break;
printf("\n这个数字读作:");
convertIntToWords(number);
printf("\n其实也可以这样读:");
convertIntToRealWords(number);
printf("\n厉害吧!\n");
}
return 0;
}
//将1~999的数字分别转换为单词
int convertIntToWords(int value){
int hundreds=0,tens=0,units=0;
hundreds=value/100;
tens=value%100/10;
units=value-100*hundreds-tens*10;
if (hundreds!=0){
switch (hundreds){
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;
}
switch (tens){
case 0:printf("zero ");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;
}
switch (units){
case 0:printf("zero");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;
}
}
if (hundreds==0&&tens!=0) {
switch (tens){
case 1:printf("one ");break;
case 2:printf("tw