From bbfb9da1a1eab35be828284ebddf44f5919a2669 Mon Sep 17 00:00:00 2001 From: Dmitry Kokorin Date: Wed, 23 Mar 2016 19:48:51 +0300 Subject: [PATCH] roman_numbers: initial commit --- cpp/roman-numerals/CMakeLists.txt | 59 ++++++++++++++ cpp/roman-numerals/README.md | 79 ++++++++++++++++++ cpp/roman-numerals/roman_numerals_test.cpp | 95 ++++++++++++++++++++++ 3 files changed, 233 insertions(+) create mode 100644 cpp/roman-numerals/CMakeLists.txt create mode 100644 cpp/roman-numerals/README.md create mode 100644 cpp/roman-numerals/roman_numerals_test.cpp diff --git a/cpp/roman-numerals/CMakeLists.txt b/cpp/roman-numerals/CMakeLists.txt new file mode 100644 index 0000000..56a3675 --- /dev/null +++ b/cpp/roman-numerals/CMakeLists.txt @@ -0,0 +1,59 @@ +# Get the exercise name from the current directory +get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME) + +# Basic CMake project +cmake_minimum_required(VERSION 2.8.11) + +# Name the project after the exercise +project(${exercise} CXX) + +# Locate Boost libraries: unit_test_framework, date_time and regex +set(Boost_USE_STATIC_LIBS ON) +set(Boost_USE_MULTITHREADED ON) +set(Boost_USE_STATIC_RUNTIME OFF) +find_package(Boost 1.55 REQUIRED COMPONENTS unit_test_framework date_time regex) + +# Enable C++11 features on gcc/clang +if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)") + set(CMAKE_CXX_FLAGS "-std=c++11") +endif() + +# Configure to run all the tests? +if(${EXERCISM_RUN_ALL_TESTS}) + add_definitions(-DEXERCISM_RUN_ALL_TESTS) +endif() + +# Get a source filename from the exercise name by replacing -'s with _'s +string(REPLACE "-" "_" file ${exercise}) + +# Implementation could be only a header +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp) + set(exercise_cpp ${file}.cpp) +else() + set(exercise_cpp "") +endif() + +# Include a test helper header if it exists +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/require_equal_containers.h) + set(test_helper require_equal_containers.h) +else() + set(test_helper "") +endif() + +# Build executable from sources and headers +add_executable(${exercise} ${file}_test.cpp ${test_helper} ${exercise_cpp} ${file}.h) + +# We need boost includes +target_include_directories(${exercise} PRIVATE ${Boost_INCLUDE_DIRS}) + +# We need boost libraries +target_link_libraries(${exercise} ${Boost_LIBRARIES}) + +# Tell MSVC not to warn us about unchecked iterators in debug builds +if(${MSVC}) + set_target_properties(${exercise} PROPERTIES + COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS) +endif() + +# Run the tests on every build +add_custom_command(TARGET ${exercise} POST_BUILD COMMAND ${exercise}) diff --git a/cpp/roman-numerals/README.md b/cpp/roman-numerals/README.md new file mode 100644 index 0000000..6eca5d8 --- /dev/null +++ b/cpp/roman-numerals/README.md @@ -0,0 +1,79 @@ +# Roman Numerals + +Write a function to convert from normal numbers to Roman Numerals: e.g. + +The Romans were a clever bunch. They conquered most of Europe and ruled +it for hundreds of years. They invented concrete and straight roads and +even bikinis. One thing they never discovered though was the number +zero. This made writing and dating extensive histories of their exploits +slightly more challenging, but the system of numbers they came up with +is still in use today. For example the BBC uses Roman numerals to date +their programmes. + +The Romans wrote numbers using letters - I, V, X, L, C, D, M. (notice +these letters have lots of straight lines and are hence easy to hack +into stone tablets). + +``` + 1 => I +10 => X + 7 => VII +``` + +There is no need to be able to convert numbers larger than about 3000. +(The Romans themselves didn't tend to go any higher) + +Wikipedia says: Modern Roman numerals ... are written by expressing each +digit separately starting with the left most digit and skipping any +digit with a value of zero. + +To see this in practice, consider the example of 1990. + +In Roman numerals 1990 is MCMXC: + +1000=M +900=CM +90=XC + +2008 is written as MMVIII: + +2000=MM +8=VIII + +See also: http://www.novaroma.org/via_romana/numbers.html + +## Getting Started + +Make sure you have read the [getting started with C++](http://help.exercism.io/getting-started-with-cpp.html) +page on the [exercism help site](http://help.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](http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd). +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. + +```C++ +#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++11 facilities in preference to writing your own +low-level algorithms or facilities by hand. [CppReference](http://en.cppreference.com/) +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](http://www.slideshare.net/LegalizeAdulthood/c-traps-and-pitfalls-for-c-programmers). + +## Source + +The Roman Numeral Kata [view source](http://codingdojo.org/cgi-bin/wiki.pl?KataRomanNumerals) diff --git a/cpp/roman-numerals/roman_numerals_test.cpp b/cpp/roman-numerals/roman_numerals_test.cpp new file mode 100644 index 0000000..a95f0fb --- /dev/null +++ b/cpp/roman-numerals/roman_numerals_test.cpp @@ -0,0 +1,95 @@ +#include "roman_numerals.h" +#define BOOST_TEST_MAIN +#include + +BOOST_AUTO_TEST_CASE(one_yields_I) +{ + BOOST_REQUIRE_EQUAL("I", roman::convert(1)); +} + +#if defined(EXERCISM_RUN_ALL_TESTS) +BOOST_AUTO_TEST_CASE(two_yields_II) +{ + BOOST_REQUIRE_EQUAL("II", roman::convert(2)); +} + +BOOST_AUTO_TEST_CASE(three_yields_III) +{ + BOOST_REQUIRE_EQUAL("III", roman::convert(3)); +} + +BOOST_AUTO_TEST_CASE(four_yields_IV) +{ + BOOST_REQUIRE_EQUAL("IV", roman::convert(4)); +} + +BOOST_AUTO_TEST_CASE(five_yields_V) +{ + BOOST_REQUIRE_EQUAL("V", roman::convert(5)); +} + +BOOST_AUTO_TEST_CASE(six_yields_VI) +{ + BOOST_REQUIRE_EQUAL("VI", roman::convert(6)); +} + +BOOST_AUTO_TEST_CASE(nine_yields_IX) +{ + BOOST_REQUIRE_EQUAL("IX", roman::convert(9)); +} + +BOOST_AUTO_TEST_CASE(twenty_seven_yields_XXVII) +{ + BOOST_REQUIRE_EQUAL("XXVII", roman::convert(27)); +} + +BOOST_AUTO_TEST_CASE(forty_eight_yields_XLVIII) +{ + BOOST_REQUIRE_EQUAL("XLVIII", roman::convert(48)); +} + +BOOST_AUTO_TEST_CASE(fifty_nine_yields_LIX) +{ + BOOST_REQUIRE_EQUAL("LIX", roman::convert(59)); +} + +BOOST_AUTO_TEST_CASE(ninety_three_yields_XCIII) +{ + BOOST_REQUIRE_EQUAL("XCIII", roman::convert(93)); +} + +BOOST_AUTO_TEST_CASE(one_hundred_forty_one_yields_CXLI) +{ + BOOST_REQUIRE_EQUAL("CXLI", roman::convert(141)); +} + +BOOST_AUTO_TEST_CASE(one_hundred_sixty_three_yields_CLXIII) +{ + BOOST_REQUIRE_EQUAL("CLXIII", roman::convert(163)); +} + +BOOST_AUTO_TEST_CASE(four_hundred_two_yields_CDII) +{ + BOOST_REQUIRE_EQUAL("CDII", roman::convert(402)); +} + +BOOST_AUTO_TEST_CASE(five_hundred_seventy_five_yields_DLXXV) +{ + BOOST_REQUIRE_EQUAL("DLXXV", roman::convert(575)); +} + +BOOST_AUTO_TEST_CASE(nine_hundred_eleven_yields_CMXI) +{ + BOOST_REQUIRE_EQUAL("CMXI", roman::convert(911)); +} + +BOOST_AUTO_TEST_CASE(one_thousand_twenty_four_yields_MXXIV) +{ + BOOST_REQUIRE_EQUAL("MXXIV", roman::convert(1024)); +} + +BOOST_AUTO_TEST_CASE(three_thousand_yields_MMM) +{ + BOOST_REQUIRE_EQUAL("MMM", roman::convert(3000)); +} +#endif