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
 
Share this Question
P: 2

This is the header file if have written so far, please make corrections and please help me with the implementation file.  //romanType.h, the specification file for the class romanType


class romanType

{

private:

int decimals;

//variable to store the decimals

char romanNumerals;

//variable to store the RomanNumerals


public:

void storeRomanNumerals(char romanNumberals);

//Function to store Roman numerals.

//Postcondition: rom = romanNumerals;


void convertRomanNumerals();

//Function to convert Roman numerals to decimals.

//Postcondition: The Roman Numeral = decimals.


void storeDecimals(int decimals);

//Function to store decimals.

//Postcondition: dec = decimals;


void printRomanNumerals();

//Function to print Roman numerals.

//Postcondition: The number is printed as Roman numerals.


void printDecimals();

//Function to print decimals.

//Postcondition: The number is printed as decimals.

};
 
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
  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?
 
P: 47

I'm in his classI 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.
 
P: 47
  //Assignment 9&10 CIS 247

//Joseph L Matzke Roman to decimal


#include "ext_roman_number.hpp"

#include <iostream>

#include <math.h>

ExtRomanNumber ExtRomanNumber::operator+(

ExtRomanNumber & rhs

)

{

return ExtRomanNumber(numberDecimal + rhs.numberDecimal);

}


ExtRomanNumber ExtRomanNumber::operator(

ExtRomanNumber & rhs

)

{

if (numberDecimal  rhs.numberDecimal < 0)

{

cout << "Because the first number is smaller than the second the number cannot be subtracted";

system("PAUSE");

exit(1);

}

return ExtRomanNumber(numberDecimal  rhs.numberDecimal);

}


ExtRomanNumber ExtRomanNumber::operator*(

ExtRomanNumber & rhs

)

{

return ExtRomanNumber(numberDecimal * rhs.numberDecimal);

}


ExtRomanNumber ExtRomanNumber::operator/(

ExtRomanNumber & rhs

)

{

return ExtRomanNumber(numberDecimal / rhs.numberDecimal);

}


ExtRomanNumber & ExtRomanNumber::operator++()

{

if (3999 == numberDecimal)

{

cout << "Number can not be incremented" << endl;

system("PAUSE");

exit(1);

}


(*this) = ExtRomanNumber(++numberDecimal);

return *this;

}


ExtRomanNumber & ExtRomanNumber::operator()

{

if (0 == numberDecimal)

{

cout << "Number can not be decremented" << endl;

system("PAUSE");

exit(1);

}


(*this) = ExtRomanNumber(numberDecimal);

return *this;

}


string getAs(

int numberDecimal,

int base

)

{

const char * digits[] = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};

if (base < 2  base > 16)

{

cout << "Illegal base" << endl;

system("PAUSE");

exit(1);

}


string result = "";


int b = 0;

while (pow(base, b) < numberDecimal)

++b;


b;


int tmp = numberDecimal;

for (int i = b; i >= 0; i)

{

result.append(

digits[tmp / ((int) pow(base, i))]

);


tmp = tmp % ((int) pow(base, i));

}


return result;

}


string ExtRomanNumber::getBin()

{

return getAs(numberDecimal, 2);

}


string ExtRomanNumber::getOct()

{

return getAs(numberDecimal, 8);

}


string ExtRomanNumber::getHex()

{

return getAs(numberDecimal, 16);

}

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
 
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
 
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.  void main()

{

const int M = 1000, D = 500, C = 100, L = 50, X = 10, V = 5, I = 1;

char numeral;

double currentValue, previousValue(1001), outputSum;


while (!ins.eof())

{

ins.get(numeral);

currentValue = numeral; // < HERE


cout<<numeral<<"  echo print numeral\n"; // echo print

cout<<currentValue<<"  echo print currentValue\n";

if (currentValue > previousValue)

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.*/


outputSum += currentValue;


previousValue = currentValue;

};

cout << outputSum << "  This is what it means.\n";

getch();

}
    Question stats  viewed: 13604
 replies: 7
 date asked: Nov 4 '06
