]> www.ginac.de Git - cln.git/blob - src/real/format-output/cl_fmt_oldroman.cc
* */*: cl_istream -> std::istream, cl_ostream -> std::ostream.
[cln.git] / src / real / format-output / cl_fmt_oldroman.cc
1 // format_old_roman().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_format.h"
8
9
10 // Implementation.
11
12 #include "cln/integer.h"
13 #include "cln/integer_io.h"
14 #include "cln/abort.h"
15
16 namespace cln {
17
18 void format_old_roman (std::ostream& stream, const cl_I& arg)
19 {
20         if (!(0 < arg && arg < 5000)) {
21                 fprint(std::cerr, "format_old_roman: argument should be in the range 1 - 4999, not ");
22                 fprint(std::cerr, arg);
23                 fprint(std::cerr, ".\n");
24                 cl_abort();
25         }
26         var uintL value = cl_I_to_UL(arg);
27         struct roman { char symbol; uintL value; };
28         static const roman scale[7] = {
29                 { 'I',    1 },
30                 { 'V',    5 },
31                 { 'X',   10 },
32                 { 'L',   50 },
33                 { 'C',  100 },
34                 { 'D',  500 },
35                 { 'M', 1000 },
36         };
37         for (int i = 6; value > 0 /* && i >= 0 */ ; i--) {
38                 var const roman * p = &scale[i];
39                 var uintL multiplicity = floor(value,p->value);
40                 value = value % p->value;
41                 while (multiplicity > 0) {
42                         fprintchar(stream,p->symbol);
43                         multiplicity--;
44                 }
45         }
46 }
47
48 }  // namespace cln