fixed decimal to roman

This commit is contained in:
Krishna Vedala
2020-05-27 18:22:57 -04:00
parent 7e875baab7
commit a8bdbbda35

View File

@@ -1,97 +1,76 @@
//This Programme Converts a given decimal number in the range [0,4000) /**
//to both Lower case and Upper case Roman Numeral * @file
* @brief This Programme Converts a given decimal number in the range [0,4000)
* to both Lower case and Upper case Roman Numeral
*/
#include <cmath> #include <cmath>
#include <cstdio> #include <cstdio>
#include <string> #include <cstring>
#include <iostream> #include <iostream>
using namespace std;
//This functions fills a string with character c, n times and returns it /** This functions fills a string with character c, n times and returns it
string fill(char c, int n) * @note This can probably be replace by `memcpy` function.
{ */
string s = ""; std::string fill(char c, int n) {
while (n--) std::string s = "";
s += c; while (n--) s += c;
return s; return s;
} }
//to convert to lowercase Roman Numeral /** to convert to lowercase Roman Numeral
// the function works recursively * the function works recursively
string tolowerRoman(int n) */
{ std::string tolowerRoman(int n) {
if (n < 4) if (n < 4) return fill('i', n);
return fill('i', n); if (n < 6) return fill('i', 5 - n) + "v";
if (n < 6) if (n < 9) return std::string("v") + fill('i', n - 5);
return fill('i', 5 - n) + "v"; if (n < 11) return fill('i', 10 - n) + "x";
if (n < 9) if (n < 40) return fill('x', n / 10) + tolowerRoman(n % 10);
return string("v") + fill('i', n - 5); if (n < 60) return fill('x', 5 - n / 10) + 'l' + tolowerRoman(n % 10);
if (n < 11)
return fill('i', 10 - n) + "x";
if (n < 40)
return fill('x', n / 10) + tolowerRoman(n % 10);
if (n < 60)
return fill('x', 5 - n / 10) + 'l' + tolowerRoman(n % 10);
if (n < 90) if (n < 90)
return string("l") + fill('x', n / 10 - 5) + tolowerRoman(n % 10); return std::string("l") + fill('x', n / 10 - 5) + tolowerRoman(n % 10);
if (n < 110) if (n < 110) return fill('x', 10 - n / 10) + "c" + tolowerRoman(n % 10);
return fill('x', 10 - n / 10) + "c" + tolowerRoman(n % 10); if (n < 400) return fill('c', n / 100) + tolowerRoman(n % 100);
if (n < 400) if (n < 600) return fill('c', 5 - n / 100) + 'd' + tolowerRoman(n % 100);
return fill('c', n / 100) + tolowerRoman(n % 100);
if (n < 600)
return fill('c', 5 - n / 100) + 'd' + tolowerRoman(n % 100);
if (n < 900) if (n < 900)
return string("d") + fill('c', n / 100 - 5) + tolowerRoman(n % 100); return std::string("d") + fill('c', n / 100 - 5) +
if (n < 1100) tolowerRoman(n % 100);
return fill('c', 10 - n / 100) + "m" + tolowerRoman(n % 100); if (n < 1100) return fill('c', 10 - n / 100) + "m" + tolowerRoman(n % 100);
if (n < 4000) if (n < 4000) return fill('m', n / 1000) + tolowerRoman(n % 1000);
return fill('m', n / 1000) + tolowerRoman(n % 1000);
return "?"; return "?";
} }
//to convert to uppercase Roman Numeral /** to convert to uppercase Roman Numeral
// the function works recursively * the function works recursively
string toupperRoman(int n) */
{ std::string toupperRoman(int n) {
if (n < 4) if (n < 4) return fill('I', n);
return fill('I', n); if (n < 6) return fill('I', 5 - n) + "V";
if (n < 6) if (n < 9) return std::string("V") + fill('I', n - 5);
return fill('I', 5 - n) + "V"; if (n < 11) return fill('I', 10 - n) + "X";
if (n < 9) if (n < 40) return fill('X', n / 10) + toupperRoman(n % 10);
return string("V") + fill('I', n - 5); if (n < 60) return fill('X', 5 - n / 10) + 'L' + toupperRoman(n % 10);
if (n < 11)
return fill('I', 10 - n) + "X";
if (n < 40)
return fill('X', n / 10) + toupperRoman(n % 10);
if (n < 60)
return fill('X', 5 - n / 10) + 'L' + toupperRoman(n % 10);
if (n < 90) if (n < 90)
return string("L") + fill('X', n / 10 - 5) + toupperRoman(n % 10); return std::string("L") + fill('X', n / 10 - 5) + toupperRoman(n % 10);
if (n < 110) if (n < 110) return fill('X', 10 - n / 10) + "C" + toupperRoman(n % 10);
return fill('X', 10 - n / 10) + "C" + toupperRoman(n % 10); if (n < 400) return fill('C', n / 100) + toupperRoman(n % 100);
if (n < 400) if (n < 600) return fill('C', 5 - n / 100) + 'D' + toupperRoman(n % 100);
return fill('C', n / 100) + toupperRoman(n % 100);
if (n < 600)
return fill('C', 5 - n / 100) + 'D' + toupperRoman(n % 100);
if (n < 900) if (n < 900)
return string("D") + fill('C', n / 100 - 5) + toupperRoman(n % 100); return std::string("D") + fill('C', n / 100 - 5) +
if (n < 1100) toupperRoman(n % 100);
return fill('C', 10 - n / 100) + "M" + toupperRoman(n % 100); if (n < 1100) return fill('C', 10 - n / 100) + "M" + toupperRoman(n % 100);
if (n < 4000) if (n < 4000) return fill('M', n / 1000) + toupperRoman(n % 1000);
return fill('M', n / 1000) + toupperRoman(n % 1000);
return "?"; return "?";
} }
//main function /** main function */
int main() {
int main()
{
int n; int n;
cout << "\t\tRoman numbers converter\n\n"; std::cout << "\t\tRoman numbers converter\n\n";
cout << "Type in decimal number between 0 up to 4000 (exclusive): "; std::cout << "Type in decimal number between 0 up to 4000 (exclusive): ";
cin >> n; std::cin >> n;
cout << n << " in Upper Roman Numerals is " << toupperRoman(n) << "\n"; std::cout << n << " in Upper Roman Numerals is " << toupperRoman(n) << "\n";
cout << n << " in Lower Roman Numerals is " << tolowerRoman(n) << "\n"; std::cout << n << " in Lower Roman Numerals is " << tolowerRoman(n) << "\n";
return 0; return 0;
} }