首页 > 后端开发 > C++ > 将以下内容翻译成中文:将给定的数字转换为中文的C程序

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

PHPz
发布: 2023-09-04 09:53:06
转载
1413 人浏览过

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

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

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

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

将以下内容翻译成中文:将给定的数字转换为中文的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</p><p>")
         Return
      End If
      If len > 4 then,
         fprintf(stderr, "Length more than 4 is not supported</p><p>")
         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 - &#39;0&#39;]
         Return
      End If
      While *num != &#39;\0
         If len >= 3
            If *num -&#39;0&#39; != 0
               Print single_digit[*num - &#39;0&#39;]
               Print tens_power[len-3]
            End If
               Decrement len by 1
            End If
         Else
            If *num == &#39;1&#39; then,
               Set sum = *num - &#39;0&#39; + *(num + 1)- &#39;0&#39;
               Print tens_place[sum]
               Return
            End If
            Else If *num == &#39;2&#39; && *(num + 1) == &#39;0&#39; then,
               Print &ldquo;twenty&rdquo;
               Return
            End else If
         Else
            Set i = *num - &#39;0&#39;
            Print i? tens_multiple[i]: ""
            Increment num by 1
            If *num != &#39;0&#39; then,
               Print single_digit[*num - &#39;0&#39;]
            End If
            End Else
               Increment num by 1
            End Else
            End while
   Step 2 &rarr; In function main()
      Call function convert("9132")
Stop
登录后复制

Example

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//function to print the given number in words
void convert(char *num) {
   int len = strlen(num);
   // cases
   if (len == 0) {
      fprintf(stderr, "empty string</p><p>");
      return;
   }
   if (len > 4) {
      fprintf(stderr, "Length more than 4 is not supported</p><p>");
      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("</p><p>%s: ", num);

   // For single digit number
   if (len == 1) {
      printf("%s</p><p>", single_digit[*num - &#39;0&#39;]);
      return;
   }
   // Iterate while num is not &#39;\0&#39;
   while (*num != &#39;\0&#39;) {
      // Code path for first 2 digits
      if (len >= 3) {
         if (*num -&#39;0&#39; != 0) {
            printf("%s ", single_digit[*num - &#39;0&#39;]);
            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 == &#39;1&#39;) {
            int sum = *num - &#39;0&#39; + *(num + 1)- &#39;0&#39;;
            printf("%s</p><p>", tens_place[sum]);
            return;
         }
         // Need to explicitely handle 20
         else if (*num == &#39;2&#39; && *(num + 1) == &#39;0&#39;) {
            printf("twenty</p><p>");
            return;
         }
         // Rest of the two digit numbers i.e., 21 to 99
         else {
            int i = *num - &#39;0&#39;;
            printf("%s ", i? tens_multiple[i]: "");
            ++num;
            if (*num != &#39;0&#39;)
               printf("%s ", single_digit[*num - &#39;0&#39;]);
         }
      }
      ++num;
   }
}
int main() {
   convert("9132");
   return 0;
}
登录后复制

输出

nine thousand one hundred thirty two
登录后复制

以上是将以下内容翻译成中文:将给定的数字转换为中文的C程序的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板