• kk Exercise 6.1 Convert an integer to words


    Exercise 6-1. write a program that will prompt for and read a positive integer less than
    1,000 from the keyboard, and then create and output a string that is the value of the
    integer in words. For example, if 941 is entered, the program will create the string "Nine
    hundred and forty one".

    //
    #define __STDC_WANT_LIB_EXT1__ 1
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char *unit_words[] = {"zero", "one","two","three","four","five","six","seven","eight","nine"};
      char *teen_words[] = {"ten", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
      char *ten_words[] = {"error", "error","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
      
      
      //上面的是什么意思?
      
      char hundred[] = " hundred";
      char and[] = " and ";         //就是这个字符。 and字符串有5+1个字符,其中一个是结束字符。
      
      char value_str[50] = "";
      int value = 0;                     // Integer to be converted
      int digits[] = {0,0,0};            // 这是什么意思?digit这个数组里面有三个值?
      int i = 0;
    
      printf("Enter a positive integer less than 1000: ");
      scanf_s("%d",&value);
      if(value >= 1000)
        value = 999;
      else if(value < 1)
        value = 1;//为什么不直接显示错误,而要非执行这个区间的值
    
      while(value > 0)
      {
        digits[i++] = value % 10;//求得各位数!
        value /= 10;//十位以上的数字
      }
    
      if(digits[2] > 0)
      {
        strcat_s(value_str, sizeof(value_str), unit_words[digits[2]]);// unit_WORDS[digits[2]]这是什么意思?圆程序?
        strcat_s(value_str, sizeof(value_str), hundred);
        if(digits[1] > 0 || digits[0] > 0)
          strcat_s(value_str, sizeof(value_str), and);
      }
      if(digits[1] > 0)
      {
        if(digits[1] == 1)
          strcat_s(value_str, sizeof(value_str), teen_words[digits[0]]);
        else
        {
          strcat_s(value_str,sizeof(value_str), ten_words[digits[1]]);
          if(digits[0] > 0)
          {
            strcat_s(value_str, sizeof(value_str), " ");
            strcat_s(value_str, sizeof(value_str), unit_words[digits[0]]);
          }
        }
      }
      else
        if(digits[0] > 0)
          strcat_s(value_str, sizeof(value_str), unit_words[digits[0]]);
          
        //最终一步到位的输出,程序相当简洁,上面的构思巧妙
      printf("
    %s
    ", value_str);
      return 0;
    }
  • 相关阅读:
    Qt图像处理技术六:拉普拉斯锐化
    linux下使用动态壁纸
    qt获得当前窗口所在屏幕的大小
    linux下动态壁纸(ubuntu20.04测试)
    使用qt+网上的api做股票查看器
    在deepin环境下安装qt开发环境和dtk开发环境
    Qt图像处理技术七:轮廓提取
    Qt图像处理技术五:图像的翻转(横向,竖向)
    基于平台开发人事档案管理功能
    让VS.Net根据表结构自动生成界面和C#代码
  • 原文地址:https://www.cnblogs.com/xiaomi5320/p/4190776.html
Copyright © 2020-2023  润新知