vitimiti

Romans

Jun 24th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. /*
  2.  * The MIT License (MIT)
  3.  *
  4.  * Copyright (c) 2015 Víctor Matía Rodríguez <[email protected]>
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  7.  * of this software and associated documentation files (the "Software"), to deal
  8.  * in the Software without restriction, including without limitation the rights
  9.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10.  * copies of the Software, and to permit persons to whom the Software is
  11.  * furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included in
  14.  * all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22.  * THE SOFTWARE.
  23.  */
  24.  
  25. /*
  26.  * Main.cpp
  27.  *
  28.  *  Created on: 25 de jun. de 2015
  29.  *      Author: Víctor Matía Rodríguez
  30.  */
  31.  
  32. #include <cstdlib>
  33. #include <iostream>
  34. #include <limits>
  35. #include <string>
  36.  
  37. std::string
  38. toRoman (int value)
  39. {
  40.   struct romandata_t
  41.   {
  42.     int value;
  43.     char const* numeral;
  44.   };
  45.   struct romandata_t const romandata[] =
  46.     { 1000, "M", 900, "CM", 500, "D", 400, "CD", 100, "C", 90, "XC", 50, "L",
  47.     40, "XL", 10, "X", 9, "IX", 5, "V", 4, "IV", 1, "I", 0, nullptr };
  48.  
  49.   std::string result;
  50.  
  51.   for (romandata_t const* current = romandata; current->value > 0; ++current)
  52.     while (value >= current->value)
  53.       {
  54.     result += current->numeral;
  55.     value -= current->value;
  56.       }
  57.  
  58.   return result;
  59. }
  60.  
  61. int
  62. main (int argc, char *argv[])
  63. {
  64.   int number;
  65.  
  66.   if (argc > 1)
  67.     {
  68.       number = atoi (argv[1]);
  69.       std::cout << "Received number: " << number << std::endl;
  70.  
  71.       if (number <= 0 || number >= 4000)
  72.     {
  73.       std::cout << "Please, enter a valid integer in the range [1, 3999]: ";
  74.       std::cin >> number;
  75.  
  76.       while (std::cin.fail () || number <= 0 || number >= 4000)
  77.         {
  78.           std::cout
  79.           << "Please, enter a valid integer in the range [1, 3999]: ";
  80.           std::cin.clear ();
  81.           std::cin.ignore (std::numeric_limits<std::streamsize>::max (),
  82.                    '\n');
  83.           std::cin >> number;
  84.         }
  85.     }
  86.     }
  87.   else
  88.     {
  89.       std::cout << "Please, enter a number: ";
  90.       std::cin >> number;
  91.  
  92.       while (std::cin.fail () || number <= 0 || number >= 4000)
  93.     {
  94.       std::cout << "Please, enter a valid integer in the range [1, 3999]: ";
  95.       std::cin.clear ();
  96.       std::cin.ignore (std::numeric_limits<std::streamsize>::max (), '\n');
  97.       std::cin >> number;
  98.     }
  99.     }
  100.  
  101.   std::cout << "Your number is written as '" << toRoman (number)
  102.       << "' in Roman numbers." << std::endl;
  103.  
  104.   return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment