visual c++ - c++ Converting roman numerals to decimals -


this program part of exam took, had write. got far , couldn't anywhere. here prompt:"write test function todecimal() converts roman numeral such mmlxvii it's decimal number representation. use main() test function. todecimal() function should have 2 arguments, string array of roman numerals , helper function. helper function return numeric value of each of letters used in roman numbers. convert string arguments so: @ first 2 characters,if first larger, convert first , add summation, call conversion function again second value , add both. if first character lesser second subtract first second, , add result conversion of string. without validation convert strings "ic". validate string arguement, if there error, call error processing function. provide @ least 2 error processing functions , test todecimal() each. 1 adking user correct, other may correct it."

i,x,c,m cannot repeated more 3 times in succession, d,l,v, can never repeated in succession.i can subtracted v , x,x can subtracted l , c, c can subtracted d , m. v, l, , d can never subtracted.

i've lost 2 days worth of sleep on this, tried writing hundreds of different ways using , breaking rules. closest i've got on it.

#include <iostream> #include <string> #include <map> #include <algorithm> #include <cstring> using namespace std;  bool checker(string roman); // adds each value of roman numeral int todecimal(string, bool* (*function)(string)); int convert(string roman, int i);  int main(){     string roman;     cout << "this program takes roman numeral user enters converts decimal notation." << endl;     cout << "enter roman numeral: ";     cin >> roman;     transform(roman.begin(), roman.end(), roman.begin(), toupper);     cout << roman << " equal " << todecimal(roman,  *checker(roman)) << endl; }  bool checker(string roman){     int length = roman.length();     (int count = 0; count < length; count++){         string sub = roman.substr(count, count);         if(sub != "i" || sub != "v" || sub != "x" || sub != "l" || sub != "c" || sub != "d" || sub != "m"){             cout << "error. try again"<< endl;             return false;         }         else if(convert(roman, count) == convert(roman, count-1) && convert(roman, count) == convert(roman, count+1)){             if (convert(roman,count) == 1 || convert(roman,count) == 10 || convert(roman,count) == 100 || convert(roman,count) == 1000)                 if(convert(roman, count-1) == convert(roman, count-2) || convert(roman, count+1) == convert(roman, count+2)){                     cout << "error try again" << endl;                     return false;                 }             else if (convert(roman,count) == 5 || convert(roman,count) == 50 || convert(roman,count) == 500){                 cout << "error try again" << endl;                     return false;             }             else return true;          }                }     return true; }  int todecimal(string s, bool*(checker) (string roman)){     /**map<char, int> roman;     roman['m'] = 1000;     roman['d'] = 500;     roman['c'] = 100;     roman['l'] = 50;     roman['x'] = 10;     roman['v'] = 5;     roman['i'] = 1;*/     checker(s);     int res = 0;     (int = 0; < s.length() - 1; ++i){         int num = convert(s,i);         res += num;         /**if (roman[s[i]] < roman[s[i+1]])             res -= roman[s[i]];         else             res += roman[s[i]];     }     res += roman[s[s.size()-1]];*/}     return res; }  int convert(string roman, int i){     enum romans {i = 1, v = 5, x = 10, l = 50, c = 100, d = 500, m = 1000};     int num = 0;     char c = roman[0];      switch(c){         case 'm':              num = m; break;         case 'd':                if(i + 1 != roman.size() && roman[i+1] == 'm'){                 num = m - d;break;             }             else                 num = d; break;         case 'c':              if(i + 1 != roman.size() && roman[i+1] == 'm' || roman[i+1] == 'd'){                 if(roman[i+1] == 'm') num = m - c; break;                 if(roman[i+1] == 'd') num = d - c; break;             }             else                 num = c; break;         case 'l':             if(i + 1 != roman.size() && roman[i+1] == 'm' || roman[i+1] == 'd' || roman[i+1] == 'c'){                 if(roman[i+1] == 'm') num = m - l; break;                 if(roman[i+1] == 'd') num = d - l; break;                 if(roman[i+1] == 'c') num = c - l; break;                 }             else                 num = l; break;         case 'x':              if(i + 1 != roman.size() && roman[i+1] == 'm' || roman[i+1] == 'd' || roman[i+1] == 'c'|| roman[i+1] == 'l'){                 if(roman[i+1] == 'm') num = m - x; break;                 if(roman[i+1] == 'd') num = d - x; break;                 if(roman[i+1] == 'c') num = c - x; break;                 if(roman[i+1] == 'l') num = c - x; break;             }                 num = x; break;         case 'v':             if(i + 1 != roman.size() && roman[i+1] == 'm' || roman[i+1] == 'd' || roman[i+1] == 'c'|| roman[i+1] == 'l' || roman[i+1] == 'x'){                 if(roman[i+1] == 'm') num = m - v; break;                 if(roman[i+1] == 'd') num = d - v; break;                 if(roman[i+1] == 'c') num = c - v; break;                 if(roman[i+1] == 'l') num = l - v; break;                 if(roman[i+1] == 'x') num = x - v; break;             }                 num = v; break;         case 'i':             if ( + 1 != roman.size() && roman[i + 1] != 'i'){                 if(roman[i+1] == 'm') num = m - i; break;                 if(roman[i+1] == 'd') num = d - i; break;                 if(roman[i+1] == 'c') num = c - i; break;                 if(roman[i+1] == 'l') num = l - i; break;                 if(roman[i+1] == 'x') num = x - i; break;             }                 num =1; break;     }     return num; } 

** have added of people on here. edit show progress/congress.

this code use convert roman (smaller 3999) integer. may check if works larger numbers.

int romantoint(string s) {     map<char, int> roman;     roman['m'] = 1000;     roman['d'] = 500;     roman['c'] = 100;     roman['l'] = 50;     roman['x'] = 10;     roman['v'] = 5;     roman['i'] = 1;      int res = 0;     (int = 0; < s.size() - 1; ++i)     {         if (roman[s[i]] < roman[s[i+1]])             res -= roman[s[i]];         else             res += roman[s[i]];     }     res += roman[s[s.size()-1]];     return res; } 

hope you.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -