473,385 Members | 1,461 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Converting Roman numerals to integers

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 14347
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
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 100+
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
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
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
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
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
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...
9
by: level9 | last post by:
How can I convert roman numerals to intergers in c++ VS 6?
13
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
by: Patrick Blackman | last post by:
How do you write regular numbers eg 1 23 475896 in roman numerals inC#
3
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
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...
7
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...
22
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...
3
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.