473,587 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting Roman numerals to integers

2 New Member
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
7 14384
daming23
2 New Member
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
joeschnell
47 New Member
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
Firecore
114 New Member
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
joeschnell
47 New Member
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
joeschnell
47 New Member
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'l l let you figure out the rest.
Good Luck
Joe
Oct 18 '07 #6
joeschnell
47 New Member
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
Doberman175
1 New Member
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

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

Similar topics

3
2114
by: | last post by:
Hello All, I'm developing a system to parse and enumerate addresses. The current obstacle is numbered streets. Does anybody know of a module already written to convert integers to their english equivalents? Example: 1ST -> FIRST SECOND -> 2ND
9
20804
by: level9 | last post by:
How can I convert roman numerals to intergers in c++ VS 6?
13
4368
by: Christopher Benson-Manica | last post by:
Inspired by a thread on clc++, I decided to try it out... #include <stdio.h> #include <stdlib.h> int main( int argc, char *argv ) { int i; int result=0; int this;
4
11757
by: Patrick Blackman | last post by:
How do you write regular numbers eg 1 23 475896 in roman numerals inC#
3
3298
by: maojecel | last post by:
guyz just want to know the simple code or program how to convert integers to roman numerals.. plz do a reply soon.. i badly need it as soon as u answer. plzzz
2
1953
by: bluedemon | last post by:
I've written the program and it works fine. but my instructor said that i can make the program short and use functions in it, but i cant really understand functions that good. so i just need a little help making the program short and use functions too. I'm posting my code here but i'm not posting the cin statements and the validating user entry...
7
6518
by: billbaitsg | last post by:
Hi, I need some help with figuring out why my program is all messed up. There are two portions two it, one that converts from roman to decimal (rom2dec) and another that converts from decimal to roman (dec2rom). int rom2dec(char rom) { //Local Variables int a = 0; //counter int dec = 0;//decimal equivalent
22
3197
by: kotlakirankumar | last post by:
please help me out the program for converting the integers to roman numerals using files in the c language from 1-5000 range send the program to my mail id ::::::: kotlakirankumar@gmail.com thank you all
3
19298
by: capoeira26 | last post by:
I have is that my entered Roman numerals work only if the numerlas are entered in large to smaller format. For exapmple if I type MMC program will correctly display 2100, but when I enter MCM it will display 2100 instead of 1900. I know that I need to come up with a way to determine what letter comes before so program can subtract it but at this...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.