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("\n%s\n", value_str);
  return 0;
}
时间: 2024-10-10 14:07:30

kk Exercise 6.1 Convert an integer to words的相关文章

java.lang.UnsupportedOperationException: Can&#39;t convert to integer: type=0x3

前几天在编写一个自定义控件的时候,出现了这个这个异常: Caused by: java.lang.UnsupportedOperationException: Can't convert to integer: type=0x3 然后定位到代码中的时候,发现是自定义控件中引用资源的时候报的错误,代码片段如下: int iconId = mTypedArray.getInteger(R.styleable.TitleBar_icon, 0); iconIv.setImageResource(ico

Exercise 2.1 Convert inches to yards, feet, and inches

Exercise 2-1. Write a program that prompts the user to enter a distance in inches andthen outputs that distance in yards, feet, and inches. (For those unfamiliar with imperialunits, there are 12 inches in a foot and 3 feet in a yard.) #include <stdio

Hex string convert to integer with stringstream

#include <sstream>#include <iostream>int main() { unsigned int x; std::stringstream ss; ss << std::hex << "FF"; ss >> x; // output it as a signed type std::cout << static_cast<int>(x) << std::endl;

Exercise 2-1.

Exercise 2-1. Write a program that prompts the user to enter a distance in inches andthen outputs that distance in yards, feet, and inches. (For those unfamiliar with imperialunits, there are 12 inches in a foot and 3 feet in a yard.) //ME #include<s

Integer 缓存

public class StringAndIntegerTest { public static void main(String[] args) { boolean b = (new Integer(1) == Integer.valueOf("1")); System.out.println(b); } } 点击查看结果 false Integer 源码 /* * Copyright (c) 1994, 2013, Oracle and/or its affiliates. Al

Bit Twiddling Hacks

http://graphics.stanford.edu/~seander/bithacks.html Bit Twiddling Hacks By Sean Eron Anderson[email protected] Individually, the code snippets here are in the public domain (unless otherwise noted) — feel free to use them however you please. The aggr

logstash 添加nginx日志

选择需求分类废话少说直接上图 第一张图: 2.此图搭配的日志格式是: log_format main '$remote_addr - $remote_user [$time_local] $http_host $request_method "$uri" "$query_string" ' '$status $body_bytes_sent "$http_referer" $upstream_status $upstream_addr $requ

elk系统搭建并收集nginx日志-主要步骤

一)简介 elk系统是一套目前较为流行的日志收集分析系统,主要由elasticserch,logstash,kibana三部分组成,其中elasticsearch负责数据的存储,logstash负责日志的收集过滤,kibana负责日志的可视化部分.整个工作流程为logstash收集日志,过滤后输出并保存到elasticsearch中,最后用户通过kibana从elasticsearch中读取数据并处理.本文中日志收集引入filebeat收集日志,logstash监听在5000端口并接受fileb

Python3内置函数——bin

先上英文文档: bin(x) Convert an integer number to a binary string prefixed with "0b". The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer. Some examples: >>>