Looking for help understanding how this example program flows and works. Can you
ID: 3863424 • Letter: L
Question
Looking for help understanding how this example program flows and works. Can you write comments next to each line of code explaining what it does? Why does it use a switch? why not set the roman numerals as constants instead of a switch? Thank you!!
//CPP program for converting roman into integers
#include <iostream>
#include <fstream>
#include<cctype>
#include<cstdlib>
#include<string.h>
using namespace std;
int value(char roman)
{
switch(roman)
{
case 'I':return 1;
case 'V':return 5;
case 'X':return 10;
case 'L':return 50;
case 'C':return 100;
case 'D':return 500;
case 'M':return 1000;
default : return -1;
}
}
int get(const string& input)
{
int total=0; char prev='%';
for(int i=(input.length()-1); i>=0; i--)
{
if(value(input[i])==-1)
return -1;
if(value(input[i])<total && (input[i]!=prev))
{ total -= value(input[i]);
prev = input[i];
}
else
{
total += value(input[i]);
prev = input[i];
}
}
return total;
}
int main()
{
char str[255];
int val;
ifstream in("Roman.txt");
if(!in) {
cout << "Cannot open input file. ";
return 1;
}
while(in) {
in.getline(str, 255); // delim defaults to ' '
if(in)
{
val=get(str);
if(val==-1)
cout << "Invalid character"<<endl;
else
cout << str << " = "<<val<<endl;
}
}
in.close();
return 0;
}
Explanation / Answer
//CPP program for converting roman into integers
#include <iostream>
#include <fstream>
#include<cctype>
#include<cstdlib>
#include<string.h>
using namespace std;
// Takes a roman character as input, and will return its integer equivalent,
// given the input is a valid roman character, and returns -1 otherwise.
int value(char roman)
{
switch(roman)
{
case 'I':return 1; //If the input is: I, returns 1.
case 'V':return 5; //If the input is: V, returns 5.
case 'X':return 10; //If the input is: X, returns 10.
case 'L':return 50; //If the input is: L, returns 50.
case 'C':return 100; //If the input is: C, returns 100.
case 'D':return 500; //If the input is: D, returns 500.
case 'M':return 1000; //If the input is: M, returns 1000.
default : return -1; //If neither of the above characters, returns -1.
}
}
//Takes a roman number string as input, and returns its equivalent integer as output.
int get(const string& input)
{
int total=0;
char prev='%';
for(int i=(input.length()-1); i>=0; i--) //From the last character in the input string, down till the first character.
{
if(value(input[i])==-1) //If the character in the input string is not a valid roman literal.
return -1; //Return -1.
if(value(input[i])<total && (input[i]!=prev)) //If the current literal value is less than tha total,
//and current literal is not same as the previous literal.
{
total -= value(input[i]); //Subtract the current literal value, from the total.
prev = input[i]; //Mark the current literal as prev, and move forward.
}
else //If that condition is not satisfied.
{
total += value(input[i]); //Add the current literal value, to the total.
prev = input[i]; //Mark the current literal as prev, and move forward.
}
}
return total; //After the characters in string are exhausted, return the total value.
}
int main()
{
char str[255]; //Declares an array of characters.
int val; //Declares an integer.
ifstream in("Roman.txt"); //Reads the file Roman.txt as input file stream.
if(!in) { //If file is not opened successfully.
cout << "Cannot open input file. "; //Push a message to screen.
return 1; //And stop execution.
}
while(in) { //Keep reading line by line of input from the file, till end of file is reached.
in.getline(str, 255); // delim defaults to ' '
if(in) //If read value is not end of file.
{
val=get(str); //Call the function get, which return the integer equivalent of roman string, and stores the returned value in val.
if(val==-1) //If the read string is invalid.
cout << "Invalid character"<<endl; //Show this message to screen.
else //If valid.
cout << str << " = "<<val<<endl; //Show the integer value to screen.
}
}
in.close(); //After reading all the strings from file, close the file.
return 0; //Stop execution.
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.