Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Roman_int.h"
- #include "RomanVectors.h"
- //------------------------------------------------------------------------------
- void error_throw(string s1, string s2, double d)
- {
- ostringstream convert;// stream used for the conversion ( output string stream )#include <sstream>
- convert << "ERROR: ";
- if (s1 != no_string) convert << s1; //insert the textual representation of s1,s2,i
- if (s2 != no_string) convert << s2;
- if (d != no_int) convert << d;
- throw convert.str(); //return content of stream
- }
- int input_stop(istream& is)
- {
- if(is.eof()) return 0;
- is.clear();
- is.unget();
- is.clear(ios_base::failbit);
- return not_a_number;
- error_throw("bad stream state", no_string, no_int);
- }
- //------------------------------------------------------------------------------
- Roman_int::Roman_int(string r, int a) :
- roman{ r }, arabic{ a }
- {
- }
- //------------------------------------------------------------------------------
- Roman_int::Roman_int() :
- roman{ not_a_roman }, arabic{ not_a_number }
- {
- }
- //------------------------------------------------------------------------------
- void vector_ones(vector<Roman_int>& v)
- {
- v.push_back(Roman_int("I", 1));
- v.push_back(Roman_int("II", 2));
- v.push_back(Roman_int("III", 3));
- v.push_back(Roman_int("IV", 4));
- v.push_back(Roman_int("V", 5));
- v.push_back(Roman_int("VI", 6));
- v.push_back(Roman_int("VII", 7));
- v.push_back(Roman_int("VIII", 8));
- v.push_back(Roman_int("IX", 9));
- }
- //------------------------------------------------------------------------------
- void vector_tens(vector<Roman_int>& v)
- {
- v.push_back(Roman_int("X", 10));
- v.push_back(Roman_int("XX", 20));
- v.push_back(Roman_int("XXX", 30));
- v.push_back(Roman_int("XL", 40));
- v.push_back(Roman_int("L", 50));
- v.push_back(Roman_int("LX", 60));
- v.push_back(Roman_int("LXX", 70));
- v.push_back(Roman_int("LXXX", 80));
- v.push_back(Roman_int("XC", 90));
- }
- //------------------------------------------------------------------------------
- void vector_hundreds(vector<Roman_int>& v)
- {
- v.push_back(Roman_int("C", 100));
- v.push_back(Roman_int("CC", 200));
- v.push_back(Roman_int("CCC", 300));
- v.push_back(Roman_int("CD", 400));
- v.push_back(Roman_int("D", 500));
- v.push_back(Roman_int("DC", 600));
- v.push_back(Roman_int("DCC", 700));
- v.push_back(Roman_int("DCCC", 800));
- v.push_back(Roman_int("CM", 900));
- }
- //------------------------------------------------------------------------------
- void vector_thousands(vector<Roman_int>& v)
- {
- v.push_back(Roman_int("M", 1000));
- v.push_back(Roman_int("MM", 2000));
- v.push_back(Roman_int("MMM", 3000));
- }
- void init_vectors()
- {
- vector_thousands(roman_thousands);
- vector_hundreds(roman_hundreds);
- vector_tens(roman_tens);
- vector_ones(roman_ones);
- }
- //------------------------------------------------------------------------------
- int get_1s(istream& is)
- {
- char ch;
- string s;
- is.get(ch);
- if (!isalpha(ch)){ is.unget(); return 0; }
- s = ch;
- while (is.get(ch) && isalpha(ch)) s += ch;
- is.unget();
- for (Roman_int ri : roman_ones) if (s == ri.roman) return ri.arabic;
- error_throw(s, " is not a roman numeral (get_1s)", no_int);
- }
- //------------------------------------------------------------------------------
- int get_10s(istream& is)
- {
- string s;
- char ch;
- is.get(ch);
- if (!isalpha(ch)){ is.unget(); return 0; }
- s = ch;
- switch (ch) {
- case 'X':
- while (ch == 'X'){
- is.get(ch);
- if (ch != 'L' && ch != 'C') is.unget();
- s += ch;
- }
- break;
- case 'L':
- while (is.get(ch) && ch == 'X') s += ch;
- is.unget();
- }
- cout << "string10 = " << s << endl;
- for (Roman_int ri : roman_tens) if (s == ri.roman){cout << "arabic10 = " << ri.arabic << endl; return ri.arabic;}// + get_1s(is);}
- error_throw(s, " is not a roman numeral (get_10s)", no_int);
- }
- //------------------------------------------------------------------------------
- int get_100s(istream& is)
- {
- string s;
- char ch;
- is.get(ch);
- if (!isalpha(ch)){ is.unget(); return 0; }
- s = ch;
- switch (ch) {
- case 'C':
- while (ch == 'C'){
- is.get(ch);
- if(ch != 'D' && ch != 'M') is.unget();
- s += ch;
- }
- break;
- case 'D':
- while (is.get(ch) && ch == 'C') s += ch;
- is.unget();
- }
- cerr << "string100 = " << s << endl;
- for (Roman_int ri : roman_hundreds) {
- //if (s == ri.roman) cout << ri.roman << " " << ri.arabic << endl;
- if (s == ri.roman) return ri.arabic + get_10s(is);
- }
- error_throw(s, " is not a roman numeral (get_100s)", no_int);
- }
- //------------------------------------------------------------------------------
- int get_1000s(istream& is)
- {
- string s;
- char ch;
- is.get(ch);
- s = ch;
- while (is.get(ch) && ch == 'M') s += ch;
- is.unget();
- for (Roman_int ri : roman_thousands) if (s == ri.roman) return ri.arabic + get_100s(is);
- error_throw(s, " is not a roman numeral (get_1000s)", no_int);
- }
- //------------------------------------------------------------------------------
- istream& operator>>(istream& is, Roman_int& ri)
- {
- is.exceptions(is.exceptions() | ios_base::badbit);
- string s;
- is >> s;
- for (int x = s.size()-1; x > -1; --x) is.putback(s[x]); //return string to istream leaving copy
- char ch;
- is.get(ch);
- if (ch == 'I' || ch == 'V') {
- is.unget();
- ri.arabic = get_1s(is);
- ri.roman = s;
- return is;
- }
- if (ch == 'X' || ch == 'L') {
- is.unget();
- ri.arabic = get_10s(is);
- ri.roman = s;
- return is;
- }
- if (ch == 'C' || ch == 'D') {
- is.unget();
- ri.arabic = get_100s(is);
- ri.roman = s;
- return is;
- }
- if (ch == 'M') {
- is.unget();
- ri.arabic = get_1000s(is);
- ri.roman = s;
- return is;
- }
- return is;
- error_throw(s, " is not a roman numeral", no_int);
- }
- //------------------------------------------------------------------------------
- ostream& operator<<(ostream& os, Roman_int& ri)
- {
- return os << ri.roman;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement