Guest User

Roman_int.cpp v.02

a guest
Apr 14th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. #include "Roman_int.h"
  2. #include "RomanVectors.h"
  3.  
  4. //------------------------------------------------------------------------------
  5. void error_throw(string s1, string s2, double d)
  6. {
  7. ostringstream convert;// stream used for the conversion ( output string stream )#include <sstream>
  8. convert << "ERROR: ";
  9. if (s1 != no_string) convert << s1; //insert the textual representation of s1,s2,i
  10. if (s2 != no_string) convert << s2;
  11. if (d != no_int) convert << d;
  12.  
  13. throw convert.str(); //return content of stream
  14. }
  15.  
  16. int input_stop(istream& is)
  17. {
  18. if(is.eof()) return 0;
  19. is.clear();
  20. is.unget();
  21. is.clear(ios_base::failbit);
  22. return not_a_number;
  23. error_throw("bad stream state", no_string, no_int);
  24. }
  25. //------------------------------------------------------------------------------
  26. Roman_int::Roman_int(string r, int a) :
  27. roman{ r }, arabic{ a }
  28. {
  29. }
  30. //------------------------------------------------------------------------------
  31. Roman_int::Roman_int() :
  32. roman{ not_a_roman }, arabic{ not_a_number }
  33. {
  34. }
  35. //------------------------------------------------------------------------------
  36. void vector_ones(vector<Roman_int>& v)
  37. {
  38. v.push_back(Roman_int("I", 1));
  39. v.push_back(Roman_int("II", 2));
  40. v.push_back(Roman_int("III", 3));
  41. v.push_back(Roman_int("IV", 4));
  42. v.push_back(Roman_int("V", 5));
  43. v.push_back(Roman_int("VI", 6));
  44. v.push_back(Roman_int("VII", 7));
  45. v.push_back(Roman_int("VIII", 8));
  46. v.push_back(Roman_int("IX", 9));
  47. }
  48. //------------------------------------------------------------------------------
  49. void vector_tens(vector<Roman_int>& v)
  50. {
  51. v.push_back(Roman_int("X", 10));
  52. v.push_back(Roman_int("XX", 20));
  53. v.push_back(Roman_int("XXX", 30));
  54. v.push_back(Roman_int("XL", 40));
  55. v.push_back(Roman_int("L", 50));
  56. v.push_back(Roman_int("LX", 60));
  57. v.push_back(Roman_int("LXX", 70));
  58. v.push_back(Roman_int("LXXX", 80));
  59. v.push_back(Roman_int("XC", 90));
  60. }
  61. //------------------------------------------------------------------------------
  62. void vector_hundreds(vector<Roman_int>& v)
  63. {
  64. v.push_back(Roman_int("C", 100));
  65. v.push_back(Roman_int("CC", 200));
  66. v.push_back(Roman_int("CCC", 300));
  67. v.push_back(Roman_int("CD", 400));
  68. v.push_back(Roman_int("D", 500));
  69. v.push_back(Roman_int("DC", 600));
  70. v.push_back(Roman_int("DCC", 700));
  71. v.push_back(Roman_int("DCCC", 800));
  72. v.push_back(Roman_int("CM", 900));
  73. }
  74. //------------------------------------------------------------------------------
  75. void vector_thousands(vector<Roman_int>& v)
  76. {
  77. v.push_back(Roman_int("M", 1000));
  78. v.push_back(Roman_int("MM", 2000));
  79. v.push_back(Roman_int("MMM", 3000));
  80. }
  81.  
  82. void init_vectors()
  83. {
  84. vector_thousands(roman_thousands);
  85. vector_hundreds(roman_hundreds);
  86. vector_tens(roman_tens);
  87. vector_ones(roman_ones);
  88. }
  89.  
  90. //------------------------------------------------------------------------------
  91. int get_1s(istream& is)
  92. {
  93. char ch;
  94. string s;
  95. is.get(ch);
  96. if (!isalpha(ch)){ is.unget(); return 0; }
  97. s = ch;
  98. while (is.get(ch) && isalpha(ch)) s += ch;
  99. is.unget();
  100. for (Roman_int ri : roman_ones) if (s == ri.roman) return ri.arabic;
  101. error_throw(s, " is not a roman numeral", no_int);
  102. }
  103. //------------------------------------------------------------------------------
  104. int get_10s(istream& is)
  105. {
  106. string s;
  107. char ch;
  108. is.get(ch);
  109. if (!isalpha(ch)){ is.unget(); return 0; }
  110. s = ch;
  111. switch (ch) {
  112. case roman_10:
  113. while (is.get(ch) && (ch == roman_10 || ch == roman_50 || ch == roman_100)) s += ch;
  114. is.unget();
  115. break;
  116. case roman_50:
  117. while (is.get(ch) && ch == roman_10) s += ch;
  118. is.unget();
  119. break;
  120. default: { is.unget(); return get_1s(is); }
  121. }
  122. for (Roman_int ri : roman_tens) if (s == ri.roman) return ri.arabic + get_1s(is);
  123. error_throw(s, " is not a roman numeral", no_int);
  124. }
  125. //------------------------------------------------------------------------------
  126. int get_100s(istream& is)
  127. {
  128.  
  129. string s;
  130. char ch;
  131. is.get(ch);
  132. if (!isalpha(ch)){ is.unget(); return 0; }
  133. s = ch;
  134. switch (ch) {
  135. case roman_100:
  136. while (is.get(ch) && (ch == roman_100 || ch == roman_500 || ch == roman_1000)) s += ch;
  137. is.unget();
  138. break;
  139. case roman_500:
  140. while (is.get(ch) && ch == roman_100) s += ch;
  141. is.unget();
  142. break;
  143. default: { is.unget(); return get_10s(is); }
  144. }
  145. for (Roman_int ri : roman_hundreds) if (s == ri.roman) return ri.arabic + get_10s(is);
  146. error_throw(s, " is not a roman numeral", no_int);
  147. }
  148. //------------------------------------------------------------------------------
  149. int get_1000s(istream& is)
  150. {
  151. string s;
  152. char ch;
  153. is.get(ch);
  154. s = ch;
  155. while (is.get(ch) && ch == roman_1000) s += ch;
  156. is.unget();
  157.  
  158. for (Roman_int ri : roman_thousands) if (s == ri.roman) return ri.arabic + get_100s(is);
  159. error_throw(s, " is not a roman numeral", no_int);
  160. }
  161. //------------------------------------------------------------------------------
  162. istream& operator>>(istream& is, Roman_int& ri)
  163. {
  164. is.exceptions(is.exceptions() | ios_base::badbit);
  165. string s;
  166. is >> s;
  167. for (int x = s.size()-1; x > -1; --x) is.putback(s[x]); //return string to istream leaving copy
  168.  
  169. char ch;
  170. is.get(ch);
  171. if (ch == roman_1 || ch == roman_5) {
  172. is.unget();
  173. ri.arabic = get_1s(is);
  174. ri.roman = s;
  175. return is;
  176. }
  177. if (ch == roman_10 || ch == roman_50) {
  178. is.unget();
  179. ri.arabic = get_10s(is);
  180. ri.roman = s;
  181. return is;
  182. }
  183. if (ch == roman_100 || ch == roman_500) {
  184. is.unget();
  185. ri.arabic = get_100s(is);
  186. ri.roman = s;
  187. return is;
  188. }
  189. if (ch == roman_1000) {
  190. is.unget();
  191. ri.arabic = get_1000s(is);
  192. ri.roman = s;
  193. return is;
  194. }
  195. return is;
  196. error_throw(s, " is not a roman numeral", no_int);
  197. }
  198. //------------------------------------------------------------------------------
  199. ostream& operator<<(ostream& os, Roman_int& ri)
  200. {
  201. return os << ri.roman;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment