Advertisement
tattersail

Roman_int.cpp

Apr 12th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 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.  
  94. char ch;
  95. string s;
  96. is.get(ch);
  97. if (!isalpha(ch)){ is.unget(); return 0; }
  98. s = ch;
  99. while (is.get(ch) && isalpha(ch)) s += ch;
  100. is.unget();
  101.  
  102. for (Roman_int ri : roman_ones) if (s == ri.roman) return ri.arabic;
  103. error_throw(s, " is not a roman numeral (get_1s)", no_int);
  104. }
  105. //------------------------------------------------------------------------------
  106. int get_10s(istream& is)
  107. {
  108.  
  109. string s;
  110. char ch;
  111. is.get(ch);
  112. if (!isalpha(ch)){ is.unget(); return 0; }
  113. s = ch;
  114. switch (ch) {
  115. case 'X':
  116. while (ch == 'X'){
  117. is.get(ch);
  118. if (ch != 'L' && ch != 'C') is.unget();
  119. s += ch;
  120. }
  121. break;
  122. case 'L':
  123. while (is.get(ch) && ch == 'X') s += ch;
  124. is.unget();
  125.  
  126. }
  127. cout << "string10 = " << s << endl;
  128.  
  129. for (Roman_int ri : roman_tens) if (s == ri.roman){cout << "arabic10 = " << ri.arabic << endl; return ri.arabic;}// + get_1s(is);}
  130. error_throw(s, " is not a roman numeral (get_10s)", no_int);
  131. }
  132. //------------------------------------------------------------------------------
  133. int get_100s(istream& is)
  134. {
  135.  
  136. string s;
  137. char ch;
  138. is.get(ch);
  139. if (!isalpha(ch)){ is.unget(); return 0; }
  140. s = ch;
  141. switch (ch) {
  142. case 'C':
  143. while (ch == 'C'){
  144. is.get(ch);
  145. if(ch != 'D' && ch != 'M') is.unget();
  146. s += ch;
  147. }
  148. break;
  149. case 'D':
  150. while (is.get(ch) && ch == 'C') s += ch;
  151. is.unget();
  152. }
  153. cerr << "string100 = " << s << endl;
  154. for (Roman_int ri : roman_hundreds) {
  155. //if (s == ri.roman) cout << ri.roman << " " << ri.arabic << endl;
  156. if (s == ri.roman) return ri.arabic + get_10s(is);
  157. }
  158. error_throw(s, " is not a roman numeral (get_100s)", no_int);
  159.  
  160. }
  161. //------------------------------------------------------------------------------
  162. int get_1000s(istream& is)
  163. {
  164. string s;
  165. char ch;
  166. is.get(ch);
  167. s = ch;
  168. while (is.get(ch) && ch == 'M') s += ch;
  169. is.unget();
  170.  
  171. for (Roman_int ri : roman_thousands) if (s == ri.roman) return ri.arabic + get_100s(is);
  172. error_throw(s, " is not a roman numeral (get_1000s)", no_int);
  173. }
  174. //------------------------------------------------------------------------------
  175. istream& operator>>(istream& is, Roman_int& ri)
  176. {
  177. is.exceptions(is.exceptions() | ios_base::badbit);
  178. string s;
  179. is >> s;
  180. for (int x = s.size()-1; x > -1; --x) is.putback(s[x]); //return string to istream leaving copy
  181.  
  182. char ch;
  183. is.get(ch);
  184. if (ch == 'I' || ch == 'V') {
  185. is.unget();
  186. ri.arabic = get_1s(is);
  187. ri.roman = s;
  188. return is;
  189. }
  190. if (ch == 'X' || ch == 'L') {
  191. is.unget();
  192. ri.arabic = get_10s(is);
  193. ri.roman = s;
  194. return is;
  195. }
  196. if (ch == 'C' || ch == 'D') {
  197. is.unget();
  198. ri.arabic = get_100s(is);
  199. ri.roman = s;
  200. return is;
  201. }
  202. if (ch == 'M') {
  203. is.unget();
  204. ri.arabic = get_1000s(is);
  205. ri.roman = s;
  206. return is;
  207. }
  208. return is;
  209. error_throw(s, " is not a roman numeral", no_int);
  210. }
  211. //------------------------------------------------------------------------------
  212. ostream& operator<<(ostream& os, Roman_int& ri)
  213. {
  214. return os << ri.roman;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement