Avatar of jebreen

jebreen's solution

to Say in the C++ Track

Published at Oct 16 2019 · 0 comments
Instructions
Test suite
Solution

Given a number from 0 to 999,999,999,999, spell out that number in English.

Step 1

Handle the basic case of 0 through 99.

If the input to the program is 22, then the output should be 'twenty-two'.

Your program should complain loudly if given a number outside the blessed range.

Some good test cases for this program are:

  • 0
  • 14
  • 50
  • 98
  • -1
  • 100

Extension

If you're on a Mac, shell out to Mac OS X's say program to talk out loud. If you're on Linux or Windows, eSpeakNG may be available with the command espeak.

Step 2

Implement breaking a number up into chunks of thousands.

So 1234567890 should yield a list like 1, 234, 567, and 890, while the far simpler 1000 should yield just 1 and 0.

The program must also report any values that are out of range.

Step 3

Now handle inserting the appropriate scale word between those chunks.

So 1234567890 should yield '1 billion 234 million 567 thousand 890'

The program must also report any values that are out of range. It's fine to stop at "trillion".

Step 4

Put it all together to get nothing but plain English.

12345 should give twelve thousand three hundred forty-five.

The program must also report any values that are out of range.

Extensions

Use and (correctly) when spelling out the number in English:

  • 14 becomes "fourteen".
  • 100 becomes "one hundred".
  • 120 becomes "one hundred and twenty".
  • 1002 becomes "one thousand and two".
  • 1323 becomes "one thousand three hundred and twenty-three".

Getting Started

Make sure you have read the Installing and Running the Tests pages for C++ on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.

#if defined(EXERCISM_RUN_ALL_TESTS)

This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C++14 facilities in preference to writing your own low-level algorithms or facilities by hand. CppReference is a wiki reference to the C++ language and standard library. If you are new to C++, but have programmed in C, beware of C traps and pitfalls.

Source

A variation on JavaRanch CattleDrive, exercise 4a http://www.javaranch.com/say.jsp

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

say_test.cpp

#include "say.h"
#include "test/catch.hpp"

TEST_CASE("zero")
{
    REQUIRE("zero" == say::in_english(0ULL));
}

#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("one")
{
    REQUIRE("one" == say::in_english(1ULL));
}

TEST_CASE("fourteen")
{
    REQUIRE("fourteen" == say::in_english(14ULL));
}

TEST_CASE("twenty")
{
    REQUIRE("twenty" == say::in_english(20ULL));
}

TEST_CASE("twenty_two")
{
    REQUIRE("twenty-two" == say::in_english(22ULL));
}

TEST_CASE("sixty_nine")
{
    REQUIRE("sixty-nine" == say::in_english(69ULL));
}

TEST_CASE("one_hundred")
{
    REQUIRE("one hundred" == say::in_english(100ULL));
}

TEST_CASE("one_hundred_twenty_three")
{
    REQUIRE("one hundred twenty-three" == say::in_english(123ULL));
}

TEST_CASE("one_thousand")
{
    REQUIRE("one thousand" == say::in_english(1000ULL));
}

TEST_CASE("one_thousand_two_hundred_thirty_four")
{
    REQUIRE("one thousand two hundred thirty-four" == say::in_english(1234ULL));
}

TEST_CASE("one_million")
{
    REQUIRE("one million" == say::in_english(1000ULL*1000ULL));
}

TEST_CASE("one_million_two")
{
    REQUIRE("one million two" == say::in_english(1000ULL*1000ULL + 2ULL));
}

TEST_CASE("one_million_two_thousand_three_hundred_forty_five")
{
    REQUIRE("one million two thousand three hundred forty-five" == say::in_english(1002345ULL));
}

TEST_CASE("one_billion")
{
    REQUIRE("one billion" == say::in_english(1000ULL*1000ULL*1000ULL));
}

TEST_CASE("a_really_big_number")
{
    REQUIRE(
        "nine hundred eighty-seven billion six hundred fifty-four million "
        "three hundred twenty-one thousand one hundred twenty-three" ==
        say::in_english(987654321123ULL));
}

TEST_CASE("raises_an_error_below_zero")
{
    REQUIRE_THROWS_AS(say::in_english(-1), std::domain_error);
}

TEST_CASE("raises_an_error_for_one_trillion")
{
    REQUIRE_THROWS_AS(say::in_english(1000ULL * 1000ULL * 1000ULL * 1000ULL),
                      std::domain_error);
}
#endif

say.h

#if !defined(SAY_H)
#define SAY_H

#include <iostream>
#include <string>
#include <stdexcept>

namespace say {
  std::string in_english(long long num);
}  // namespace say

#endif // SAY_H

say.cpp

#include "say.h"

namespace say {
  static std::string zeroToNineteen[] = { "zero", "one", "two", "three", "four",
                              "five", "six", "seven", "eight", "nine", "ten",
                              "eleven", "twelve", "thirteen", "fourteen",
                              "fifteen", "sixteen", "seventeen", "eighteen",
                              "nineteen" };
  static std::string twentyToNinety[] = { "twenty", "thirty", "forty", "fifty",
                                          "sixty", "seventy", "eighty",
                                          "ninety"};

  std::string in_english(long long num)
  {
    if(num < 0)throw std::domain_error("num must be non-negative");

    if(num < 20)return zeroToNineteen[num];
    if(num < 100)return twentyToNinety[(num - num % 10)/10 - 2] +
      (num % 10 == 0 ? "" : "-" + in_english(num%10));

    if(num < 1000) return zeroToNineteen[(num - num % 100) / 100] + " hundred" +
      (num % 100 == 0 ? "" : " " + in_english(num % 100));

    if(num < 1000000)
      return in_english((num - num % 1000) / 1000) + " thousand" +
      (num % 1000 == 0 ? "" : " " + in_english(num % 1000));

    if (num < 1000000000)
      return in_english((num - num % 1000000) / 1000000) + " million" +
      (num % 1000000 == 0 ? "" : " " + in_english(num % 1000000));

    if (num < 1000000000000)
      return in_english((num - num % 1000000000) / 1000000000) + " billion" +
      (num % 1000000000 == 0 ? "" : " " + in_english(num % 1000000000));

    throw std::domain_error("num is too large!");
  }
}  // namespace say

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?