C语言入门 -- 数字的两种读法(2021/1/7)

这篇博客介绍了C语言编程中数字的两种英文读法。包括编写convertIntToWords函数将数字拆分成每一位英文单词,以及convertIntToRealWords函数将数字转换为常见的英文表达方式。文章提供了一个简单的测试用例,涵盖了从1到1000内的正整数。
摘要由CSDN通过智能技术生成

数字的两种读法

数字每一位按英文读与整体按英文读

编写程序:
(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
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RowdyKid

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值