0

0

将以下内容翻译成中文:将给定的数字转换为中文的C程序

PHPz

PHPz

发布时间:2023-09-04 09:53:06

|

1505人浏览过

|

来源于tutorialspoint

转载

给定一个由数字组成的字符串,任务是将这些给定的数字转换为单词。

比如我们有一个输入“361”;那么输出应该是单词形式,即“三百六十一”。对于解决这个问题,我们需要记住数字和它所在的位置,比如个位、十位、千位等。

代码只支持0到9999的4位数字。所以输入应该在0到9999之间。

让我们考虑1,111,那么位置将会是这样:

HTTPie AI
HTTPie AI

AI API开发工具

下载

将以下内容翻译成中文:将给定的数字转换为中文的C程序

示例

Input: “1234”
Output: one thousand two hundred thirty four
Input: “7777”
Output: seven thousand seven hundred seventy seven

我们将使用的方法来解决给定的问题

  • 将输入作为字符串。
  • 为不同的值创建数组。
  • 根据输入的长度检查,决定我们将显示输出到哪些位置。
  • 根据位置显示输出。

算法

Start
   Step 1 → In function convert(char *num)
      Declare and initialize int len = strlen(num)
      If len == 0 then,
         fprintf(stderr, "empty string

") Return End If If len > 4 then, fprintf(stderr, "Length more than 4 is not supported

") Return End If Declare and initialize a char *single_digit[] = { "zero", "one", "two","three", "four","five","six", "seven", "eight", "nine"} Declare and initialize a char *tens_place[] = {"", "ten", "eleven", "twelve","thirteen", "fourteen","fifteen", "sixteen","seventeen", "eighteen", "nineteen"} Declare and Initialize a char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty","sixty", "seventy", "eighty", "ninety"} Declare and initialize char *tens_power[] = {"hundred", "thousand"} Print num If len == 1 then, Print single_digit[*num - '0'] Return End If While *num != '\0 If len >= 3 If *num -'0' != 0 Print single_digit[*num - '0'] Print tens_power[len-3] End If Decrement len by 1 End If Else If *num == '1' then, Set sum = *num - '0' + *(num + 1)- '0' Print tens_place[sum] Return End If Else If *num == '2' && *(num + 1) == '0' then, Print “twenty” Return End else If Else Set i = *num - '0' Print i? tens_multiple[i]: "" Increment num by 1 If *num != '0' then, Print single_digit[*num - '0'] End If End Else Increment num by 1 End Else End while Step 2 → In function main() Call function convert("9132") Stop

Example

#include 
#include 
#include 
//function to print the given number in words
void convert(char *num) {
   int len = strlen(num);
   // cases
   if (len == 0) {
      fprintf(stderr, "empty string

"); return; } if (len > 4) { fprintf(stderr, "Length more than 4 is not supported

"); return; } // the first string wont be used. char *single_digit[] = { "zero", "one", "two", "three", "four","five", "six", "seven", "eight", "nine"}; // The first string is not used, it is to make // array indexing simple char *tens_place[] = {"", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; // The first two string are not used, they are to make // array indexing simple char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty","sixty", "seventy", "eighty", "ninety"}; char *tens_power[] = {"hundred", "thousand"}; // Used for debugging purpose only printf("

%s: ", num); // For single digit number if (len == 1) { printf("%s

", single_digit[*num - '0']); return; } // Iterate while num is not '\0' while (*num != '\0') { // Code path for first 2 digits if (len >= 3) { if (*num -'0' != 0) { printf("%s ", single_digit[*num - '0']); printf("%s ", tens_power[len-3]); // here len can be 3 or 4 } --len; } // Code path for last 2 digits else { // Need to explicitly handle 10-19. Sum of the two digits is //used as index of "tens_place" array of strings if (*num == '1') { int sum = *num - '0' + *(num + 1)- '0'; printf("%s

", tens_place[sum]); return; } // Need to explicitely handle 20 else if (*num == '2' && *(num + 1) == '0') { printf("twenty

"); return; } // Rest of the two digit numbers i.e., 21 to 99 else { int i = *num - '0'; printf("%s ", i? tens_multiple[i]: ""); ++num; if (*num != '0') printf("%s ", single_digit[*num - '0']); } } ++num; } } int main() { convert("9132"); return 0; }

输出

nine thousand one hundred thirty two

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

3

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

1

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

5

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

7

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

30

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

3

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

2

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

3

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
进程与SOCKET
进程与SOCKET

共6课时 | 0.3万人学习

UNI-APP开发(仿饿了么)
UNI-APP开发(仿饿了么)

共32课时 | 8.7万人学习

SQLite经典教程
SQLite经典教程

共51课时 | 22.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号