Question
Hello Experts! I would appreciate some help on this program with two things: 1.) My program is supposed to convert both roman numerals to decimals
Hello Experts! I would appreciate some help on this program with two things:
1.) My program is supposed to convert both roman numerals to decimals and decimals to roman numerals. The problem is that my program converts lowercase roman numerals to -1. What can I change so that it will convert correctly?
2.) I also need help with another addition to the code the assignment is as follows:
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.
Thank you for the help! The language is C++ and I cannot use the bits library.
My Code:
#include
// creating useful arrays for converting decimal to roman int num[] = { 1,4,5,9,10,40,50,90,100,400,500,900,1000 }; string sym[] = { "I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M" };
// declared the useful function for this program // function declared to check if string is valid decimal number bool isValidDecimalNumber(string s); // function declared to check if string is valid roman number bool isValidRomanNumber(string s); // function declared to convert roman to decimal string convertRomanToDecimal(string s); // function declared to convert decimal to roman string convertDecimalToRoman(string s);
// function value defined to get the value of particular roman character int value(char r) { if (r == 'I') return 1; if (r == 'V') return 5; if (r == 'X') return 10; if (r == 'L') return 50; if (r == 'C') return 100; if (r == 'D') return 500; if (r == 'M') return 1000;
return -1; }
// main method int main() { cout << "Salve! Welcome to Roman Number Conversion!" << endl; // variable declared string input; int numberConversions = 0;
// running the testcases till user exits by entering 0 or O while (true) { // taking input of string from user cout << "Please enter a value to convert: " << endl; cin >> input;
// checking if user entered "0" or "O" if (input == "0" || input == "O") { // since user entered "0" or "O", thus we are printed the number of conversion did and exiting cout << numberConversions << " numbers were converted." << endl; cout << "Thank you for using Roman Number Conversion. Vale!" << endl; return 0; }
// checking if user has entered a decimal string or a roman string // case when string is valid decimal number if (isValidDecimalNumber(input)) { int decimalNumber = stoi(input); cout << decimalNumber << " (Decimal) is " << convertDecimalToRoman(input) << " (Roman)" << endl; numberConversions++; } // case when string is valid roman number else if (isValidRomanNumber(input)) { cout << input << " (Roman) = " << convertRomanToDecimal(input) << " (Decimal)" << endl; numberConversions++; } // case when user entered invalid string, which is neither roman nor decimal else { cout << "Sorry, I did not understand input " << input << endl; } } }
// function defined to check if string is valid decimal or not bool isValidDecimalNumber(string s) { // check if the string represents a valid decimal number for (int i = 0; i < s.length(); i++) { if (!isdigit(s[i])) { return false; } } return true; }
// function defined to check if string is valid roman number or not bool isValidRomanNumber(string s) { // checking if each of the character in string, belongs to the valid roman characters for (int r = 0; r < s.length(); r++) { s[r] = toupper(s[r]);
switch (s[r]) { case 'I': break; case 'V': break; case 'X': break; case 'L': break; case 'C': break; case 'D': break; case 'M': break; default: return false; } } return true; }
// function defined to convert roman to decimal string convertRomanToDecimal(string s) { // declared variable to store the decimal number int res = 0;
// traversing over each of the character in the roman string for (int i = 0; i < s.length(); i++) { int s1 = value(s[i]);
if (i + 1 < s.length()) { int s2 = value(s[i + 1]);
// case when value of current symbol is greater then value of next symbol if (s1 >= s2) { res = res + s1; } // otherwise else { res = res + s2 - s1; i++; } } else { res = res + s1; } } // returning the final decimal value return to_string(res); }
// function defined to convert decimal to roman string convertDecimalToRoman(string s) { int number = stoi(s); string res = ""; int i = 12; // checking each of the digit in th decimal number while (number > 0) { int div = number / num[i]; number = number % num[i]; //finding the suitable roman symbol, and adding it to final result while (div--) { res += sym[i]; } i--; } // returning answer return res; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started