By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Converting Roman numerals to integers

P: 2
Hi,
I'm taking an online C++ class and having some difficulty understanding the assignments. Can someone please look at the code I wrote to ensure validity? I am not asking for the answer just some guidance, Iím totally lost.
The assignment is as follow:
Write a prg to convert numbers entered in Roman Numeral to decimal. Your prg should consist of a class, say romanType. An object of the type romanType should do the following:

a. Store the number as a Roman numeral
b. Convert and store the number into decimal
c. Print the number as Roman numeral or decimal number as requested by the user.

The decimals values of the Roman numerals are:
M 1000
D 500
C 100
L 50
X 10
V 5
I 1
Nov 4 '06 #1
Share this Question
Share on Google+
7 Replies


P: 2
This is the header file if have written so far, please make corrections and please help me with the implementation file.

Expand|Select|Wrap|Line Numbers
  1. //romanType.h, the specification file for the class romanType
  2.  
  3. class romanType
  4. {
  5. private:
  6.     int decimals;             
  7.                    //variable to store the decimals
  8.     char romanNumerals;      
  9.                    //variable to store the RomanNumerals
  10.  
  11. public:
  12.     void storeRomanNumerals(char romanNumberals);
  13.       //Function to store Roman numerals.
  14.       //Postcondition: rom = romanNumerals;
  15.  
  16.     void convertRomanNumerals();
  17.       //Function to convert Roman numerals to decimals.
  18.       //Postcondition: The Roman Numeral = decimals.
  19.  
  20.     void storeDecimals(int decimals);
  21.       //Function to store decimals.
  22.       //Postcondition: dec = decimals;
  23.  
  24.     void printRomanNumerals();
  25.       //Function to print Roman numerals.
  26.       //Postcondition: The number is printed as Roman numerals.
  27.  
  28.     void printDecimals();
  29.       //Function to print decimals.
  30.       //Postcondition: The number is printed as decimals.
  31. };
Nov 4 '06 #2

P: 47
hey daming, the code you've posted amounts to nothing more than what grigoletti has outlined so far as to what type of functions to use and what to name them. the //commented out sections are simply outlines from the text. when i posted this site i cautioned everyone nobody here will write code for you, however after showing a good effort they will help you find some glitches and perhaps suggest some other types of functions. haven't seen you around unless you've changed your name after getting told to read the posting directions i see many from our class receive from the moderator.
give it some effort and i'd be happy to help also if i can. later
Oct 11 '07 #3

100+
P: 114
hey daming, the code you've posted amounts to nothing more than what grigoletti has outlined so far as to what type of functions to use and what to name them. the //commented out sections are simply outlines from the text. when i posted this site i cautioned everyone nobody here will write code for you, however after showing a good effort they will help you find some glitches and perhaps suggest some other types of functions. haven't seen you around unless you've changed your name after getting told to read the posting directions i see many from our class receive from the moderator.
give it some effort and i'd be happy to help also if i can. later
So.... you are his online class teacher or something?
Oct 11 '07 #4

P: 47
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.
Oct 11 '07 #5

P: 47
Expand|Select|Wrap|Line Numbers
  1.  //Assignment 9&10   CIS 247
  2. //Joseph L Matzke   Roman to decimal
  3.  
  4. #include "ext_roman_number.hpp"
  5. #include <iostream>
  6. #include <math.h>
  7. ExtRomanNumber ExtRomanNumber::operator+(
  8.     ExtRomanNumber & rhs
  9.   )
  10. {
  11.   return ExtRomanNumber(numberDecimal + rhs.numberDecimal);
  12. }
  13.  
  14. ExtRomanNumber ExtRomanNumber::operator-(
  15.     ExtRomanNumber & rhs
  16.   )
  17. {
  18.   if (numberDecimal - rhs.numberDecimal < 0)
  19.   {
  20.     cout << "Because the first number is smaller than the second the number cannot be subtracted";
  21.     system("PAUSE");
  22.     exit(1);
  23.   }
  24.   return ExtRomanNumber(numberDecimal - rhs.numberDecimal);
  25. }
  26.  
  27. ExtRomanNumber ExtRomanNumber::operator*(
  28.     ExtRomanNumber & rhs
  29.   )
  30. {
  31.   return ExtRomanNumber(numberDecimal * rhs.numberDecimal);
  32. }
  33.  
  34. ExtRomanNumber ExtRomanNumber::operator/(
  35.     ExtRomanNumber & rhs
  36.   )
  37. {
  38.   return ExtRomanNumber(numberDecimal / rhs.numberDecimal);
  39. }
  40.  
  41. ExtRomanNumber & ExtRomanNumber::operator++()
  42. {
  43.   if (3999 == numberDecimal)
  44.   {
  45.     cout << "Number can not be incremented" << endl;
  46.     system("PAUSE");
  47.     exit(1);
  48.   }
  49.  
  50.   (*this) = ExtRomanNumber(++numberDecimal);
  51.   return *this;
  52. }
  53.  
  54. ExtRomanNumber & ExtRomanNumber::operator--()
  55. {
  56.   if (0 == numberDecimal)
  57.   {
  58.     cout << "Number can not be decremented" << endl;
  59.     system("PAUSE");
  60.     exit(1);
  61.   }
  62.  
  63.   (*this) = ExtRomanNumber(--numberDecimal);
  64.   return *this;
  65. }    
  66.  
  67. string getAs(
  68.     int numberDecimal,
  69.     int base
  70.   )
  71. {
  72.   const char * digits[] = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
  73.   if (base < 2 || base > 16)
  74.   {
  75.     cout << "Illegal base" << endl;
  76.     system("PAUSE");
  77.     exit(1);
  78.   }
  79.  
  80.   string result = "";
  81.  
  82.   int b = 0;
  83.   while (pow(base, b) < numberDecimal)
  84.     ++b;
  85.  
  86.   b--;
  87.  
  88.   int tmp = numberDecimal;
  89.   for (int i = b; i >= 0; --i)
  90.   {
  91.     result.append(
  92.         digits[tmp / ((int) pow(base, i))]
  93.       );
  94.  
  95.     tmp = tmp % ((int) pow(base, i));
  96.   }  
  97.  
  98.   return result;
  99. }
  100.  
  101. string ExtRomanNumber::getBin()
  102. {
  103.   return getAs(numberDecimal, 2);
  104. }
  105.  
  106. string ExtRomanNumber::getOct()
  107. {
  108.   return getAs(numberDecimal, 8);
  109. }
  110.  
  111. string ExtRomanNumber::getHex()
  112. {
  113.   return getAs(numberDecimal, 16);
  114. }
  115.  
Daming, this should get you going........it is one partial way I attempted to go, and it will work........I'll let you figure out the rest.
Good Luck
Joe
Oct 18 '07 #6

P: 47
If you send me 100 more lines of code added to this build I abandoned I will help you some more. I'll need to see some genuine effort. Later
Oct 18 '07 #7

P: 1
Hi,

I am doing close to the same thing as the post originator I think.

This is what I have so far. I deleted all the fstream bits cause it doesn't matter.

What I am having trouble with is converting the numeral (char) to a numerical value without using a switch()/if() statement. Is this even possible?

I have tried currentValue = 'numeral' but as you may already know this gives an error.

basically I need to know if there is any way to use the variable numeral as one of those const int I have assigned at the beginning without using a switch/if like this:

switch(numeral)
case 'M':
currentValue = 1000
.... etc.

Thanks in advance.

Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.     const int M = 1000, D = 500, C = 100, L = 50, X = 10, V = 5, I = 1;
  4.     char numeral;
  5.     double currentValue, previousValue(1001), outputSum;
  6.  
  7.     while (!ins.eof())
  8.     {
  9.         ins.get(numeral);
  10.         currentValue = numeral; // <-- HERE
  11.  
  12.         cout<<numeral<<" - echo print numeral\n"; // echo print
  13.         cout<<currentValue<<" - echo print currentValue\n";
  14.         if (currentValue > previousValue)
  15.             outputSum += -2*previousValue; /*will have already added the previous value, this subtracts that erroneous addition as well as what should have been taken away in the first place.*/
  16.  
  17.         outputSum += currentValue;
  18.  
  19.         previousValue = currentValue;
  20.     };
  21.     cout << outputSum << " - This is what it means.\n";
  22.     getch();
  23. }
Oct 23 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.