Question: In C++ Please! Modify your Roman Number program to read entries from a file, one line at a time. Process each line and keep a

In C++ Please!

Modify your Roman Number program to read entries from a file, one line at a time. Process each line and keep a running total of the valid entries, separately for decimal values and for Roman Numbers. Enter the number of entries and the sums of the valid entries (in both categories) to complete this quiz. Also keep track of the number of invalid entries.

File:

I i 123 MCMLXXVII CMIX IIXXII FFFF XXXXII xlii 42 iiiiiiiiiiiiiiiiiiiiiiiii IM 2020 HELLO IMI MDCLXVI

Please answer this!

What is the sum of the ROMAN numerals in this file?

What is the sum of the DECIMAL numbers in this file?

How many INVALID ENTRIES are in the file?

Program:

#include #include #include

using namespace std; bool isValidRomanNumber(string s) { bool ret = false; regex str_expr("^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$"); if (regex_match(s, str_expr)) ret = true; return ret; }

bool isValidDecimalNumber(string s) { int i = 0, j = s.length() - 1; //function to check for spaces in string while (i < s.length() && s[i] == ' ') i++; while (j >= 0 && s[j] == ' ') j--; if (i > j) return 0;

if (i == j && !(s[i] >= '0' && s[i] <= '9')) return 0;

if (s[i] != '.' && s[i] != '+' && s[i] != '-' && !(s[i] >= '0' && s[i] <= '9')) return 0;

bool checkForDotOrExponential = false; for (i; i <= j; i++) { if (s[i] != 'e' && s[i] != '.' && s[i] != '+' && s[i] != '-' && !(s[i] >= '0' && s[i] <= '9')) return false;

if (s[i] == '.') { if (checkForDotOrExponential == true) return false; if (i + 1 > s.length()) return false; if (!(s[i + 1] >= '0' && s[i + 1] <= '9')) return false; }

else if (s[i] == 'e') { checkForDotOrExponential = true;

if (!(s[i - 1] >= '0' && s[i - 1] <= '9')) return false; if (i + 1 > s.length()) return false;

if (s[i + 1] != '+' && s[i + 1] != '-' && (s[i + 1] >= '0' && s[i] <= '9')) return false; } } return true; } int getCorrespondingDecimalOfRomanChar(char inputChar) { if (inputChar == 'I') return 1; if (inputChar == 'V') return 5; if (inputChar == 'X') return 10; if (inputChar == 'L') return 50; if (inputChar == 'C') return 100; if (inputChar == 'D') return 500; if (inputChar == 'M') return 1000;

return -1; } string convertRomanToDecimal(string s) {

int convertedDecimal = 0;

for (int i = 0; i < s.length(); i++) {

int value1 = getCorrespondingDecimalOfRomanChar(s[i]); if (i + 1 < s.length()) { int value2 = getCorrespondingDecimalOfRomanChar(s[i + 1]); if (value1 >= value2) { convertedDecimal = convertedDecimal + value1; } else { convertedDecimal = convertedDecimal + value2 - value1; i++; } } else { convertedDecimal = convertedDecimal + value1; } } return to_string(convertedDecimal); } string convertDecimalToRoman(string s) { string ret = ""; int number = std::stold(s); int listOfNumbers[] = { 1,4,5,9,10,40,50,90,100,400,500,900,1000 }; string listOfSymbols[] = { "I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M" }; int i = 12; while (number > 0) { int quotient = number / listOfNumbers[i]; number = number % listOfNumbers[i]; while (quotient--) { ret += listOfSymbols[i]; } i--; } return ret; } int main() { string inputString; bool contiLoop = true; int numberOfConvertions = 0; int numberOfRomanToDecimalConvertions = 0; int numberOfDecimalToRomanConvertions = 0; cout << "Welcome to Roman Number Conversion!" << endl; do { cout << "Please Enter a Value to Convert:"; cin >> inputString; if (inputString == "0" || inputString == "O") contiLoop = false; else {

if (isValidRomanNumber(inputString)) { string convertedVal = convertRomanToDecimal(inputString); cout << inputString << "(Roman) = " << convertedVal << "(Decimal)" << endl; numberOfRomanToDecimalConvertions++; } else if (isValidDecimalNumber(inputString)) { string convertedVal = convertDecimalToRoman(inputString); cout << inputString << "(Decimal) = " << convertedVal << "(Roman)" << endl; numberOfDecimalToRomanConvertions++; } else { cout << "I did not understand input" << inputString << endl; } } } while (contiLoop); numberOfConvertions = numberOfRomanToDecimalConvertions + numberOfDecimalToRomanConvertions; cout << numberOfConvertions << " numbers were converted (" << numberOfDecimalToRomanConvertions << "Decimal to Roman," << numberOfRomanToDecimalConvertions << " Roman to Decimal)" << endl; cout << "Thank you for Playing Roman Number Conversion! " << endl; return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!