编写程序:
(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
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("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 (units){
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;
}
}
}
//将1~999的数字转换为英文单词
int convertIntToRealWords(int value){
int hundreds=0,tens=0,units=0;
hundreds=value/100;//得出百位上的数字
tens=value%100/10;//得出十位上的数字
units=value-100*hundreds-tens*10;//得出个位上的数字
//1到10的数
if(hundreds==0&&tens==0){
switch(units){
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&&units==0){
switch(hundreds){
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;
}
}
//10到100之间的数
if (value>10&&value<100){
switch(tens){
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("ninty ");break;
case 1:
if(units==1){
printf("eleven");
break;
}
else if (units==2){
printf("twelve");
break;
}
else if(units==3){
printf("thirteen");
break;
}
else if(units==4){
printf("forteen");
break;
}
else if(units==5){
printf("fifteen");
break;
}
else if(units==6){
printf("sixteen");
break;
}
else if(units==7){
printf("seventeen");
break;
}
else if(units==8){
printf("eighteen");
break;
}
else if(units==9){
printf("ninteen");
break;
}
else{
printf("ten");
break;
}
}
if(tens!=1){
switch(units){
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;
case 0:printf("zero");break;
}
}
}
//100~999之间的数
if (value>100&&value<1000){
if(tens==0&&units!=0){
//如果十位为零,个位不为零,如102
switch(hundreds){
case(1) :printf("one hundred and ");break;
case(2) :printf("two hundred and ");break;
case(3) :printf("three hundred and ");break;
case(4) :printf("four hundred and ");break;
case(5) :printf("five hundred and ");break;
case(6) :printf("six hundred and ");break;
case(7) :printf("seven hundred and ");break;
case(8) :printf("eight hundred and ");break;
case(9) :printf("nine hundred and ");break;
}
switch(units){
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(tens!=0&&units!=0){
//如果十位数与个位数均不为零
switch(hundreds){
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("nin hundred ");break;
case(7) :printf("one hundred ");break;
case(8) :printf("one hundred ");break;
case(9) :printf("one hundred ");break;
}
switch(tens){
case(1) :printf("and ");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){
//若十位数为1,如111
switch(units){
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;
}
}
else switch(units){
// 若十位数不为1和0,个位数不为零
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(tens!=0&&units==0){
//若十位不为零个位为0
switch(hundreds){
case(1) :printf("one hundred and ");break;
case(2) :printf("two hundred and ");break;
case(3) :printf("three hundred and ");break;
case(4) :printf("four hundred and ");break;
case(5) :printf("five hundred and ");break;
case(6) :printf("nin hundred and ");break;
case(7) :printf("one hundred and ");break;
case(8) :printf("one hundred and ");break;
case(9) :printf("one hundred and ");break;
}
switch(tens){
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("ninty");break;
}
}
}
}