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>
- std::string
- toRoman (int value)
- {
- struct romandata_t
- {
- int value;
- char const* numeral;
- };
- struct romandata_t const romandata[] =
- { 1000, "M", 900, "CM", 500, "D", 400, "CD", 100, "C", 90, "XC", 50, "L",
- 40, "XL", 10, "X", 9, "IX", 5, "V", 4, "IV", 1, "I", 0, nullptr };
- std::string result;
- for (romandata_t const* current = romandata; current->value > 0; ++current)
- while (value >= current->value)
- {
- result += current->numeral;
- value -= current->value;
- }
- return result;
- }
- int
- main (int argc, char *argv[])
- {
- int number;
- 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;
- }
- }
- std::cout << "Your number is written as '" << toRoman (number)
- << "' in Roman numbers." << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment