// DecimalToRoman.cpp : convert decimal number to Roman Numeral
#include <iostream>
#include <string>
#include <vector>
struct symbol {
std::string name;
int value;
};
std::vector<symbol> symbols= {
{"M",1000},
{"CM",900},
{"D",500},
{"CD",400},
{"C",100},
{"XC",90},
{"L",50},
{"XL",40},
{"X",10},
{"IX",9},
{"V",5},
{"IV",4},
{"I",1}
};
std::string dec2Roman(int n, std::vector<symbol>::iterator i = symbols.begin());
int main(int argc, char** argv)
{
if (argc == 2) {
std::cout << dec2Roman(atoi(argv[1])) << std::endl;
return EXIT_SUCCESS;
}
else {
std::cout << "Usage: " << argv[0] << " <decimal number>" << std::endl;
return EXIT_FAILURE;
}
}
std::string dec2Roman(int n, std::vector<symbol>::iterator i) {
std::string output("");
if (i != symbols.end()) {
while (n >= i->value) {
n -= i->value;
output.append(i->name);
}
if (n > 0)
output.append(dec2Roman(n, ++i));
}
return output;
}
Just trying to find the simplest way to generate roman numerals. Is there a "simpler" way ?
sample output:
===========
C:\Users\Judd>E:\CPP_Projects\DecimalToRoman\Release\DecimalToRoman.exe 1984
MCMLXXXIV
sample output:
===========
C:\Users\Judd>E:\CPP_Projects\DecimalToRoman\Release\DecimalToRoman.exe 1984
MCMLXXXIV
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.