fork download
  1. #include <algorithm>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <iterator>
  5. #include <locale>
  6. #include <stdexcept>
  7. #include <string>
  8.  
  9. template <typename Value, typename To>
  10. To make_roman(Value value, To to) {
  11. if (value < 1 || 3999 < value) {
  12. throw std::range_error("int out of range for a Roman numeral");
  13. }
  14. static std::string const digits[4][10] = {
  15. { "", "M", "MM", "MMM", "", "", "", "", "", "" },
  16. { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" },
  17. { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" },
  18. { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" },
  19. };
  20. for (int i(0), factor(1000); i != 4; ++i, factor /= 10) {
  21. std::string const& s(digits[i][(value / factor) % 10]);
  22. to = std::copy(s.begin(), s.end(), to);
  23. }
  24. return to;
  25. }
  26.  
  27. class num_put
  28. : public std::num_put<char>
  29. {
  30. template <typename Value>
  31. iter_type format(iter_type to, std::ios_base& fmt, char fill,
  32. Value v) const {
  33. char buffer[16];
  34. char* end(make_roman(v, buffer));
  35.  
  36. std::streamsize len(end - buffer);
  37. std::streamsize width(std::max(fmt.width(0), len));
  38. std::streamsize fc(width - (end - buffer));
  39. switch (fmt.flags() & std::ios_base::adjustfield) {
  40. default:
  41. case std::ios_base::left:
  42. to = std::copy(buffer, end, to);
  43. to = std::fill_n(to, fc, fill);
  44. break;
  45. case std::ios_base::right:
  46. case std::ios_base::internal:
  47. to = std::fill_n(to, fc, fill);
  48. to = std::copy(buffer, end, to);
  49. }
  50. return to;
  51. }
  52. iter_type do_put(iter_type to, std::ios_base& fmt, char fill,
  53. long v) const {
  54. return this->format(to, fmt, fill, v);
  55. }
  56. iter_type do_put(iter_type to, std::ios_base& fmt, char fill,
  57. long long v) const {
  58. return this->format(to, fmt, fill, v);
  59. }
  60. iter_type do_put(iter_type to, std::ios_base& fmt, char fill,
  61. unsigned long v) const {
  62. return this->format(to, fmt, fill, v);
  63. }
  64. iter_type do_put(iter_type to, std::ios_base& fmt, char fill,
  65. unsigned long long v) const {
  66. return this->format(to, fmt, fill, v);
  67. }
  68. };
  69.  
  70. int main()
  71. {
  72. try {
  73. std::locale rloc(std::locale(), new num_put);
  74. std::ostream lout(std::cout.rdbuf());
  75. std::ostream rout(std::cout.rdbuf());
  76. (lout << std::left).imbue(rloc);
  77. (rout << std::right).imbue(rloc);
  78.  
  79. lout << "long=" << static_cast<long>(1234) << '\n'
  80. << "unsigned long=" << static_cast<unsigned long>(1234) << '\n'
  81. << "long long=" << static_cast<long long>(1234) << '\n'
  82. << "unsigned long long=" << static_cast<unsigned long long>(1234) << '\n'
  83. ;
  84.  
  85. for (int i = 59; i < 4000; i += 59) {
  86. std::cout << std::setw(4) << i << '=';
  87. lout << '\'' << std::setw(12) << i << "' ";
  88. rout << '\'' << std::setw(12) << i << "'\n";
  89. }
  90. }
  91. catch (std::exception const& ex) {
  92. std::cout << "ERROR: " << ex.what() << '\n';
  93. }
  94. }
  95.  
Success #stdin #stdout 0s 3444KB
stdin
Standard input is empty
stdout
long=MCCXXXIV
unsigned long=MCCXXXIV
long long=MCCXXXIV
unsigned long long=MCCXXXIV
  59='LIX         ' '         LIX'
 118='CXVIII      ' '      CXVIII'
 177='CLXXVII     ' '     CLXXVII'
 236='CCXXXVI     ' '     CCXXXVI'
 295='CCXCV       ' '       CCXCV'
 354='CCCLIV      ' '      CCCLIV'
 413='CDXIII      ' '      CDXIII'
 472='CDLXXII     ' '     CDLXXII'
 531='DXXXI       ' '       DXXXI'
 590='DXC         ' '         DXC'
 649='DCXLIX      ' '      DCXLIX'
 708='DCCVIII     ' '     DCCVIII'
 767='DCCLXVII    ' '    DCCLXVII'
 826='DCCCXXVI    ' '    DCCCXXVI'
 885='DCCCLXXXV   ' '   DCCCLXXXV'
 944='CMXLIV      ' '      CMXLIV'
1003='MIII        ' '        MIII'
1062='MLXII       ' '       MLXII'
1121='MCXXI       ' '       MCXXI'
1180='MCLXXX      ' '      MCLXXX'
1239='MCCXXXIX    ' '    MCCXXXIX'
1298='MCCXCVIII   ' '   MCCXCVIII'
1357='MCCCLVII    ' '    MCCCLVII'
1416='MCDXVI      ' '      MCDXVI'
1475='MCDLXXV     ' '     MCDLXXV'
1534='MDXXXIV     ' '     MDXXXIV'
1593='MDXCIII     ' '     MDXCIII'
1652='MDCLII      ' '      MDCLII'
1711='MDCCXI      ' '      MDCCXI'
1770='MDCCLXX     ' '     MDCCLXX'
1829='MDCCCXXIX   ' '   MDCCCXXIX'
1888='MDCCCLXXXVIII' 'MDCCCLXXXVIII'
1947='MCMXLVII    ' '    MCMXLVII'
2006='MMVI        ' '        MMVI'
2065='MMLXV       ' '       MMLXV'
2124='MMCXXIV     ' '     MMCXXIV'
2183='MMCLXXXIII  ' '  MMCLXXXIII'
2242='MMCCXLII    ' '    MMCCXLII'
2301='MMCCCI      ' '      MMCCCI'
2360='MMCCCLX     ' '     MMCCCLX'
2419='MMCDXIX     ' '     MMCDXIX'
2478='MMCDLXXVIII ' ' MMCDLXXVIII'
2537='MMDXXXVII   ' '   MMDXXXVII'
2596='MMDXCVI     ' '     MMDXCVI'
2655='MMDCLV      ' '      MMDCLV'
2714='MMDCCXIV    ' '    MMDCCXIV'
2773='MMDCCLXXIII ' ' MMDCCLXXIII'
2832='MMDCCCXXXII ' ' MMDCCCXXXII'
2891='MMDCCCXCI   ' '   MMDCCCXCI'
2950='MMCML       ' '       MMCML'
3009='MMMIX       ' '       MMMIX'
3068='MMMLXVIII   ' '   MMMLXVIII'
3127='MMMCXXVII   ' '   MMMCXXVII'
3186='MMMCLXXXVI  ' '  MMMCLXXXVI'
3245='MMMCCXLV    ' '    MMMCCXLV'
3304='MMMCCCIV    ' '    MMMCCCIV'
3363='MMMCCCLXIII ' ' MMMCCCLXIII'
3422='MMMCDXXII   ' '   MMMCDXXII'
3481='MMMCDLXXXI  ' '  MMMCDLXXXI'
3540='MMMDXL      ' '      MMMDXL'
3599='MMMDXCIX    ' '    MMMDXCIX'
3658='MMMDCLVIII  ' '  MMMDCLVIII'
3717='MMMDCCXVII  ' '  MMMDCCXVII'
3776='MMMDCCLXXVI ' ' MMMDCCLXXVI'
3835='MMMDCCCXXXV ' ' MMMDCCCXXXV'
3894='MMMDCCCXCIV ' ' MMMDCCCXCIV'
3953='MMMCMLIII   ' '   MMMCMLIII'