WHat this program does , is to convert roman numericals to a decimal. all the cr
ID: 3557181 • Letter: W
Question
WHat this program does , is to convert roman numericals to a decimal. all the credits of this code go to christopherganga123 . I need the commenting. Functions, members, classes what they do etc
the code :
Roman.h
#ifndef ROMANTYPE_H
#define ROMANTYPE_H
int const SIZE = 10;
class RomanType
{
private:
char input[SIZE];
int place[SIZE];
public:
void setNumber(char *roman){
strcpy(input, roman);}
void setOutput(int *roman){
place[SIZE] = *roman;}
void convert();
int Sum();
int output();
};
void RomanType::convert(){
for(int count = 0;count <= SIZE;count++){
if(input[count] == 'I'){
place[count] = 1;
continue;}
else if(input[count] == 'V'){
place[count] = 5;
continue;}
else if(input[count] == 'X'){
place[count] = 10;
continue;}
else if(input[count] == 'L'){
place[count] = 50;
continue;}
else if(input[count] == 'C'){
place[count] = 100;
continue;}
else if(input[count] == 'D'){
place[count] = 500;
continue;}
else if(input[count] == 'M'){
place[count] = 1000;
continue;}}
};
int RomanType::output()
{
int total = 0;
for(int count = 0;count <= SIZE;count++)
{
if(place[count + 1] <= place[count])
{
total += place[count];
continue;
}
else
{
total -= place[count];
}
return total;
}
};
int RomanType::Sum(){
int value = 0;
for(int count = 0;count <= SIZE;count++){
if(input[count + 1] <= input[count]){
if(input[count] == 'I'){
value += 1;
continue;}
if(input[count] == 'V'){
value += 5;
continue;}
if(input[count] == 'X'){
value += 10;
continue;}
if(input[count] == 'L'){
value += 50;
continue;}
if(input[count] == 'C'){
value += 100;
continue;}
if(input[count] == 'D'){
value += 500;
continue;}
if(input[count] == 'M'){
value += 1000;
continue;}}
else{
if(input[count] == 'I'){
value -= 1;
continue;}
else if(input[count] == 'V'){
value -= 5;
continue;}
else if(input[count] == 'X'){
value -= 10;
continue;}
else if(input[count] == 'L'){
value -= 50;
continue;}
else if(input[count] == 'C'){
value -= 100;
continue;}
else if(input[count] == 'D'){
value -= 500;
continue;}
else if(input[count] == 'M'){
value -= 1000;
continue;}
}
}
return value;
};
#endif
Converter_Function.cpp
/*This program takes a roman numeral and converts it
to a whole number using a class.*/
#include<iostream>
#include<cstring>
#include"RomanType.h"
using namespace std;
int main()
{
char input[SIZE];
char choice;
RomanType Output;
do{
//Heading
cout<<" ROMAN NUMERAL CONVERTER ";
cout<<" Please double check your input! ";
cout<<" If the sequence is not valid you will get incorrect data! ";
cout<<"Enter a roman numeral to be converted: ";
cin>>input;
cout<<" ROMAN NUMERAL CONVERTER ";
cout<<"Your sequence is ----> "<<input<<" ";
Output.setNumber(input);
Output.convert();
cout<<" "<<Output.output()<<" ";
cout<<"Press Y to enter more Roman Numerals: ";
cin>>choice;
}while(choice == 'y' || choice == 'Y');
return 0;
}
Explanation / Answer
RomanType.h
#ifndef ROMANTYPE_H
#define ROMANTYPE_H
int const SIZE = 10;
class RomanType
{
//declaring private variables and functions
private:
char input[SIZE];
int place[SIZE];
//declaring public variables and functions
public:
//copies the roman numerial into input[] array
void setNumber(char *roman){
strcpy(input, roman);}
void setOutput(int *roman){
place[SIZE] = *roman;}
void convert();
int Sum();
int output();
};
//The below function converts the given roman input into their respective decimal values
//It uses for loop for iteration
//if and else-if statement are used to check the given input roman decimal.
// After checking the input it stores the given input value into place[] array
// Continue statement is used to stop the if-else statement after the roman decimal value is found
void RomanType::convert(){
for(int count = 0;count <= SIZE;count++){
if(input[count] == 'I'){
place[count] = 1;
continue;}
else if(input[count] == 'V'){
place[count] = 5;
continue;}
else if(input[count] == 'X'){
place[count] = 10;
continue;}
else if(input[count] == 'L'){
place[count] = 50;
continue;}
else if(input[count] == 'C'){
place[count] = 100;
continue;}
else if(input[count] == 'D'){
place[count] = 500;
continue;}
else if(input[count] == 'M'){
place[count] = 1000;
continue;}}
};
//Calculates the total value of the numerial
//This function returns the total value of the roman numerial
int RomanType::output()
{
int total = 0;
for(int count = 0;count <= SIZE;count++)
{
if(place[count + 1] <= place[count])
{
total += place[count];
continue;
}
else
{
total -= place[count];
}
return total;
}
};
//checkes if 2st input value is less than or equal to 1st input value
//if yes then their value is added to the value variable
//if no then their value is subracted in the value variable
int RomanType::Sum(){
int value = 0;
for(int count = 0;count <= SIZE;count++){
if(input[count + 1] <= input[count]){
if(input[count] == 'I'){
value += 1;
continue;}
if(input[count] == 'V'){
value += 5;
continue;}
if(input[count] == 'X'){
value += 10;
continue;}
if(input[count] == 'L'){
value += 50;
continue;}
if(input[count] == 'C'){
value += 100;
continue;}
if(input[count] == 'D'){
value += 500;
continue;}
if(input[count] == 'M'){
value += 1000;
continue;}}
else{
if(input[count] == 'I'){
value -= 1;
continue;}
else if(input[count] == 'V'){
value -= 5;
continue;}
else if(input[count] == 'X'){
value -= 10;
continue;}
else if(input[count] == 'L'){
value -= 50;
continue;}
else if(input[count] == 'C'){
value -= 100;
continue;}
else if(input[count] == 'D'){
value -= 500;
continue;}
else if(input[count] == 'M'){
value -= 1000;
continue;}
}
}
return value;
};
#endif
Convertor_function.cpp
/*This program takes a roman numeral and converts it
to a whole number using a class.*/
#include<iostream>
#include<cstring>
#include"RomanType.h"
using namespace std;
int main()
{
char input[SIZE];
char choice;
RomanType Output;
do{
//Heading
cout<<" ROMAN NUMERAL CONVERTER ";
cout<<" Please double check your input! ";
cout<<" If the sequence is not valid you will get incorrect data! ";
cout<<"Enter a roman numeral to be converted: ";
cin>>input;
cout<<" ROMAN NUMERAL CONVERTER ";
cout<<"Your sequence is ----> "<<input<<" ";
// calls setNumber function in RomanType.h
Output.setNumber(input);
//calss convert function in RomanType.h
Output.convert();
//calls output function in RomanType.h
//Prints the value of the roman numerical
cout<<" "<<Output.output()<<" ";
//If the user want to enter another numerical the allows the user to enter another numerical by entering y
cout<<"Press Y to enter more Roman Numerals: ";
cin>>choice;
}while(choice == 'y' || choice == 'Y');
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.