Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 Víctor Matía Rodríguez <[email protected]>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- /*
- * Main.cpp
- *
- * Created on: 25 de jun. de 2015
- * Author: Víctor Matía Rodríguez
- */
- #include <cstdlib>
- #include <iostream>
- #include <limits>
- #include <string>
- int
- main (int argc, char *argv[])
- {
- int number;
- int part;
- std::string roman;
- if (argc > 1)
- {
- number = atoi (argv[1]);
- std::cout << "Received number: " << number << std::endl;
- if (number <= 0 || number >= 4000)
- {
- std::cout << "Please, enter a valid integer in the range [1, 3999]: ";
- std::cin >> number;
- while (std::cin.fail () || number <= 0 || number >= 4000)
- {
- std::cout
- << "Please, enter a valid integer in the range [1, 3999]: ";
- std::cin.clear ();
- std::cin.ignore (std::numeric_limits<std::streamsize>::max (),
- '\n');
- std::cin >> number;
- }
- }
- }
- else
- {
- std::cout << "Please, enter a number: ";
- std::cin >> number;
- while (std::cin.fail () || number <= 0 || number >= 4000)
- {
- std::cout << "Please, enter a valid integer in the range [1, 3999]: ";
- std::cin.clear ();
- std::cin.ignore (std::numeric_limits<std::streamsize>::max (), '\n');
- std::cin >> number;
- }
- }
- if (number >= 1000)
- {
- part = number / 1000;
- for (int i = 0; i < part; i++)
- roman += "M";
- number %= 1000;
- }
- if (number >= 100)
- {
- part = number / 100;
- if (part == 9)
- roman += "CM";
- else if (part >= 5)
- {
- roman += "D";
- for (int i = 0; i < part - 5; i++)
- roman += "C";
- }
- else if (part == 4)
- roman += "CD";
- else if (part >= 1)
- for (int i = 0; i < part; i++)
- roman += "C";
- number %= 100;
- }
- if (number >= 10)
- {
- part = number / 10;
- if (part == 9)
- roman += "XC";
- else if (part >= 5)
- {
- roman += "L";
- for (int i = 0; i < part - 5; i++)
- roman += "X";
- }
- else if (part == 4)
- roman += "XL";
- else if (part >= 1)
- for (int i = 0; i < part; i++)
- roman += "X";
- number %= 10;
- }
- if (number >= 1)
- {
- if (number == 9)
- roman += "IX";
- else if (number >= 5)
- {
- roman += "V";
- for (int i = 0; i < number - 5; i++)
- roman += "I";
- }
- else if (number == 4)
- roman += "IV";
- else if (number >= 1)
- for (int i = 0; i < number; i++)
- roman += "I";
- }
- std::cout << "Your number is written as '" << roman << "' in Roman numbers."
- << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment