MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / to_roman

Function to_roman

Examples/Modules/Chapter 11/Ex11_02/to_roman.cpp:4–12  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2module roman;
3
4std::string to_roman(unsigned int i)
5{
6 if (i > 3999) return {}; // 3999, or MMMCMXCIX, is the largest standard Roman numeral
7 static const std::string ms[]{ "","M","MM","MMM" };
8 static const std::string cds[]{ "","C","CC","CCC","CD","D","DC","DCC","DCCC","CM" };
9 static const std::string xls[]{ "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC" };
10 static const std::string ivs[]{ "","I","II","III","IV","V","VI","VII","VIII","IX" };
11 return ms[i / 1000] + cds[(i % 1000) / 100] + xls[(i % 100) / 10] + ivs[i % 10];
12}

Callers 2

mainFunction · 0.70
mainFunction · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected