Here is the question from Chapter 1 problem 1 that I answered: question: Write a
ID: 3638168 • Letter: H
Question
Here is the question from Chapter 1 problem 1 that I answered:question:
Write a program that converts a number entered in Roman numerals to decimal form. Your program should consist of a class, say RomanType. An object of romanType should do the following:
a. Store the number as a Roman numeral.
b. Convert and store the number into decimal form.
c. Print the number as a Roman numeral or decimal number as requested by the user. (Write 2 separate functions--one to print the number as a Roman numeral and the other to print the number as a decimal number.)
The decimal values of the Roman numerals are:
M 1000
D 500
C 100
L 50
X 10
V 5
I 1
Remember, a larger numeral preceding a smaller numeral means addition, so LX is 60. A smaller numeral preceding a larger numeral means subtraction, so XL is 40. Any place in a decimal number, such as the 1s place, the 10s place, and so on, requires from zero to 4 Roman numerals.
d. Test your program using the following Roman numerals: MCXIV, CCCLIX, and MDCLXVI.
answer:
#include<string>
using namespace std;
class romanType
{
public :
romanType( string = "" );
void setRoman( string );
void convertToDecimal();
void printRoman();
void printDecimal();
private:
string roman;
int decimal;
}; // ends class definition of romanType
#include<iostream>
#include "romanType.h"
using namespace std;
romanType::romanType( string myRoman )
{
roman = myRoman;
decimal = 0;
} // end constructor romanType
void romanType::setRoman( string myRoman )
{
roman = myRoman;
decimal = 0;
} // end function setRoman
void romanType::convertToDecimal()
{
char romans[7] = { 'M', 'D', 'C', 'L','X', 'V', 'I'};
int decimals[ 7 ] = { 1000, 500, 100,50, 10, 5, 1 };
int j, pos;
size_t len = roman.length();
// process the numeral
for ( unsigned int i = 0; i < len -1; i++ )
{
// find the roman letter
for ( pos= 0; pos < 7; pos++ )
if ( roman.at( i ) == romans[ pos ] )
break;
// check for validity of the roman letter
if ( pos< 7 )
{
// check the next roman letter's value
for ( j = 0; j < pos; j++ )
if ( roman.at( i + 1 ) == romans[ j ] )
break;
// adds or subtract the dec. val
// according to the values of j and pos
if ( j == pos )
decimal += decimals[ pos ];
else
decimal -= decimals[ pos ];
}
} // endfor
// processes the last numeralvalue
for ( j = 0; j < 7; j++ )
if (roman.at( len - 1 ) == romans[ j ] )
break;
//adds the dec. val of roman letter tothe dec. number
decimal += decimals[ j ];
} // ends functionconvertToDecimal
void romanType::printRoman()
{
cout << " The roman numeralis " << roman;
} // ends the function printRoman
void romanType::printDecimal()
{
cout << " The decimalequivalent of the "
<< "given roman numeral is " << decimal;
} // ends the function printDecimal
// Main program
#include<iostream>
#include "romanType.h"
using namespace std;
int main() // main begins the program execution
{
// lets the user know aboutthe program
cout << " Program thatconvert Roman Numeral"
<< " into decimal form.";
romanType r;
string rns[ 3 ] = { "CCCLIX","MCXIV", "MDCLXVI" };
for ( int i = 0; i < 3; i++)
{
// set the roman numeral string
r.setRoman( rns[ i ] );
// converts the roman numeral into decimal form
r.convertToDecimal();
// prints the roman numeral
r.printRoman();
// prints the decimal form of numeral
r.printDecimal();
}
cout << " ";
return 0;
}
And here's the extension question in chapter 2 problem 20 that I have no idea how to question:
a. In Programming Exercise 1 Chapter 1, we defined a class romanType to implement Roman numerals in a program. In that exercise, we also implemented a function, romanToDecimal, to convert a Roman numeral into its equivalent decimal number.
Modify the definition of the class romanType so that the data members are declared as protected. Use the class string to manipulate the strings. Furthermore, overload ther stream insertion and stream extraction operators for easy input and output. The stream insertion operator outputs the Roman numeral in the Roman format.
Also, inclue a member function, decimalToRoman, that converts the decimal number( the decimal nmber must be a positive integer) to an equivalent Roman numeral format. Write the definition of the member function decimalToRoman.
For simplicity, we assume that only the letter I can appear in front of another letter and that it appears only in front of the letters V and X. For example, 4 is represented as IV, 9 is represented as IX, 39 is represented as XXXIX, etc.
b. Derive a class extRomanType from the class romanType to do the following. In the class extRomanType, overload the arithmetic operators +, -, *, and / so that arithmetic operations can be performed on Roman numerals. Also, overload the pre- and postincrement and decrement operators as member functions of the class extRomanType.
To add(subtract, multiply, or divide) Roman numerals, add(subtract, multiply, or divide respectively) their decimal representations and then convert the result to Roman numeral format. For subtraction, if the first number is smaller than the second number, output a message saying that, "Because the first number is smaller than the second, the numbers can not be subtracted". Similarly, for division, the numerator must be larger than the denominator. Use similar conventions for the increment and decrement operators.
c. Write the definitions of the functions to overload the operators described in part b
d. Write a program to test your class extRomanType.
Answer: I have none cause I have no clue what I'm doing in this class and I'm way in over my head.
Explanation / Answer
I'm in his class--I can help only by telling this is how I start. We were assigned this yesterday and I have several dozen source codes that I am looking at, however none are going to comply with all the parameters he has placed in the program outline. If you are completely lost I don't know what to tell you, week 14 so were closing in on the end of the end. If I see some posts from you that look like you've spent the 20 to 30 hours a week I spend writing these I would be happy to shove you in the right direction. Here is all I can give you now, it's a start to get your brain started thinking about the program. The one we finished today mine was 697 lines and it took me about 26 hours, that's what it takes man. Give it a shot. thousands = number / 100; // number is the user input. hundreds = number / 100 % 10; tens = number / 10 % 10; ones = number % 10; (thousands == 1 ? roman += "M": thousands == 2 ? roman += "MM": thousands == 3 ? roman += "MMM": hundreds == 1 ? roman += "C": hundreds == 2 ? roman += "CC": hundreds == 3 ? roman += "CCC": hundreds == 4 ? roman += "CD": hundreds == 5 ? roman += "D": hundreds == 6 ? roman += "DC": hundreds == 7 ? roman += "DCC": hundreds == 8 ? roman += "DCCC": hundreds == 9 ? roman += "CM" : roman = roman); (tens == 1 ? roman += "X": tens == 2 ? roman += "XX": tens == 3 ? roman += "XXX": tens == 4 ? roman += "XL": tens == 5 ? roman += "L": tens == 6 ? roman += "LX": tens == 7 ? roman += "LXX": tens == 8 ? roman += "LXXX": tens == 9 ? roman += "XC" : roman = roman); (ones == 1 ? roman += "I": ones == 2 ? roman += "II": ones == 3 ? roman += "III": ones == 4 ? roman += "IV": ones == 5 ? roman += "V": ones == 6 ? roman += "VI": ones == 7 ? roman += "VII": ones == 8 ? roman += "VIII": ones == 9 ? roman += "IX" : roman = roman); The key to creating these functions (and in fact any function) is to work out the rules that have to be followed....So first consider the rules that Roman Numerals follow..... We can think of a common pattern with several exceptions, but thankfully even these exceptions follow patterns..... Your code will probably work reasonably well, but I suspect the instructor is looking for something that follows rules, rather than explicitly setting the values. In the simplest case (for you) you could write a function that does exactly what you do now and returns a string with the entire value. I think the professor would award more marks, however, If you created seperate functions for units, tens, hundreds etc (which you could still call from one function called from main if you liked). I suppose the biggest idea in an exercise such as this, though, is to simplify a process. Your solution, while providing the user with an answer, hasn't simplified the proccess. Hope these thoughts help.... Post again if you get stuck and need further help! Just some ideas, hope they helped.... I tackled a problem similar to this. One thing you can do is first create an integer array the size of the string holding the roman numerals. Fill each element of the int array with the value of the corresponding letter: for example, "XII" would become [10][1][1] Going from there, it's only a few steps to determine which numbers should be added and which subtracted. The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve. Please read the Posting Guidelines and particularly the Coursework Posting Guidlines. Then when you are ready post a new question in this thread. MODERATOR Do them one character at a time. Code: ( text ) 1. // Suppose the string that holds MC...VI is named RN. 2. while RN is not empty 3. { 4. switch the first character of the string RN 5. { 6. convert it to it's integer form (case 'M': value = 500; break; // etc.) 7. } 8. if the previous character is less than current character 9. { 10. subtract previous character instead of add (total -= (2*prevVal);) 11. } 12. add the value to the total (total += value;) 13. assign value to prevVal; 14. remove the first character from string RN 15. } Now this actually solves not even one function for you, but it gets you thinking about a few of them. Think about how we increment and decrement, how could you use a switch or an array for the Roman numerals? I'll check back.