vitimiti

Roman numbers are nice

Jun 24th, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 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. int
  38. main (int argc, char *argv[])
  39. {
  40.   int number;
  41.   int part;
  42.   std::string roman;
  43.  
  44.   if (argc > 1)
  45.     {
  46.       number = atoi (argv[1]);
  47.       std::cout << "Received number: " << number << std::endl;
  48.  
  49.       if (number <= 0 || number >= 4000)
  50.     {
  51.       std::cout << "Please, enter a valid integer in the range [1, 3999]: ";
  52.       std::cin >> number;
  53.  
  54.       while (std::cin.fail () || number <= 0 || number >= 4000)
  55.         {
  56.           std::cout
  57.           << "Please, enter a valid integer in the range [1, 3999]: ";
  58.           std::cin.clear ();
  59.           std::cin.ignore (std::numeric_limits<std::streamsize>::max (),
  60.                    '\n');
  61.           std::cin >> number;
  62.         }
  63.     }
  64.     }
  65.   else
  66.     {
  67.       std::cout << "Please, enter a number: ";
  68.       std::cin >> number;
  69.  
  70.       while (std::cin.fail () || number <= 0 || number >= 4000)
  71.     {
  72.       std::cout << "Please, enter a valid integer in the range [1, 3999]: ";
  73.       std::cin.clear ();
  74.       std::cin.ignore (std::numeric_limits<std::streamsize>::max (), '\n');
  75.       std::cin >> number;
  76.     }
  77.     }
  78.  
  79.   if (number >= 1000)
  80.     {
  81.       part = number / 1000;
  82.  
  83.       for (int i = 0; i < part; i++)
  84.     roman += "M";
  85.  
  86.       number %= 1000;
  87.     }
  88.  
  89.   if (number >= 100)
  90.     {
  91.       part = number / 100;
  92.  
  93.       if (part == 9)
  94.     roman += "CM";
  95.       else if (part >= 5)
  96.     {
  97.       roman += "D";
  98.  
  99.       for (int i = 0; i < part - 5; i++)
  100.         roman += "C";
  101.     }
  102.       else if (part == 4)
  103.     roman += "CD";
  104.       else if (part >= 1)
  105.     for (int i = 0; i < part; i++)
  106.       roman += "C";
  107.  
  108.       number %= 100;
  109.     }
  110.  
  111.   if (number >= 10)
  112.     {
  113.       part = number / 10;
  114.  
  115.       if (part == 9)
  116.     roman += "XC";
  117.       else if (part >= 5)
  118.     {
  119.       roman += "L";
  120.  
  121.       for (int i = 0; i < part - 5; i++)
  122.         roman += "X";
  123.     }
  124.       else if (part == 4)
  125.     roman += "XL";
  126.       else if (part >= 1)
  127.     for (int i = 0; i < part; i++)
  128.       roman += "X";
  129.  
  130.       number %= 10;
  131.     }
  132.  
  133.   if (number >= 1)
  134.     {
  135.       if (number == 9)
  136.     roman += "IX";
  137.       else if (number >= 5)
  138.     {
  139.       roman += "V";
  140.  
  141.       for (int i = 0; i < number - 5; i++)
  142.         roman += "I";
  143.     }
  144.       else if (number == 4)
  145.     roman += "IV";
  146.       else if (number >= 1)
  147.     for (int i = 0; i < number; i++)
  148.       roman += "I";
  149.     }
  150.  
  151.   std::cout << "Your number is written as '" << roman << "' in Roman numbers."
  152.       << std::endl;
  153.  
  154.   return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment