GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Mar-2006, 02:09
Ichigo Ichigo is offline
New Member
 
Join Date: Feb 2006
Posts: 18
Ichigo is on a distinguished road
Exclamation

help writing a roman numeral to decimal program


Hi guys! okay, so I must write a program that converts a number that was entered in Roman numerals to normal numbers. The program has to impliment a class that should do the following: 1. store the number as a roman numeral, 2. convert and store the number into a decimal, and 3. print the number as a roman numeral or normal number, as requested by the user.
and just as bg info, im using the microsoft visual C++ 6.0 compiler...

and just as a reminder on roman numerals (for ppl who are rusty like me and dont remember the roman numerals, lol):
M=1000, D=500, C=100, L=50, X=10, V=5, I=1

CPP / C++ / C Code:
#include <iostream>
using namespace std;


class romanType{
public:
	void roman();
	int convert();
	void print();
	void get();
	
private:
	int M, D, C, L, X, V, I;
	char romanNumeral;
};

void romanType::roman(){
	M = 1000;
	D = 500;
	C = 100;
	L = 50;
	X = 10;
	V = 5;
	I = 1;
}


int romanType::convert(){
	if (romanNumeral = M){
		cout << 1000;
	}else if(romanNumeral = D){
		cout << 500;
	}else if(romanNumeral = C){
		cout << 100;
	}else if(romanNumeral = L){
		cout << 50;
	}else if(romanNumeral = X){
		cout << 10;
	}else if(romanNumeral = V){
		cout << 5;
	}else if(romanNumeral = I){
		cout << 1;
	}

	return romanNumeral;
}


void romanType::print(){
	cout << romanNumeral << endl;
}

void romanType::get(){


}

int main(){
	char romanNumeral;
	cout << "Welcome to the Roman numeral to decimal converter!\nPlease enter a number in Roman numerals to be converted: ";
	cin >> romanNumeral;

	//print();
return 0;
}
  #2  
Old 23-Mar-2006, 06:56
davis
 
Posts: n/a

Re: help writing a roman numeral to decimal program


Quote:
Originally Posted by Ichigo
Hi guys! okay, so I must write a program that converts a number that was entered in Roman numerals to normal numbers. The program has to impliment a class that should do the following: 1. store the number as a roman numeral, 2. convert and store the number into a decimal, and 3. print the number as a roman numeral or normal number, as requested by the user.
and just as bg info, im using the microsoft visual C++ 6.0 compiler...

and just as a reminder on roman numerals (for ppl who are rusty like me and dont remember the roman numerals, lol):
M=1000, D=500, C=100, L=50, X=10, V=5, I=1


I wrote a bit to help get you started. Please note that it works "some what," but doesn't (yet) contain the necessary logic for properly processing Roman numerals when a "smaller" is between two largers. EG:

MCM = 1900 (not 2100, which is what my code will produce!)

I figured that I can put forth a bit of code and you can add the simple hack needed to figure this out. Let me know if you have troubles with it. Also, I didn't test my code with VC++, as I'm using Linux and GCC.

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;


class RomanNumeral
{
public:
    const static int M = 1000;
    const static int D = 500;
    const static int C = 100;
    const static int L = 50;
    const static int X = 10;
    const static int V = 5;
    const static int I = 1;

    RomanNumeral( const int decimal ) :
    m_roman( "" ),
    m_decimal( ((decimal > 0) ? decimal : 0) ) 
    {
	if( decimal > 0 )
	{
	    int i = decimal;
	    while( i > 0 )
	    {
		if( ( i - M ) >= 0 )
		{
		    m_roman += "M";
		    i -= M;
		    continue;
		}
		if( ( i - D ) >= 0 )
		{
		    m_roman += "D";
		    i -= D;
		    continue;
		}
		if( ( i - C ) >= 0 )
		{
		    m_roman += "C";
		    i -= C;
		    continue;
		}
		if( ( i - L ) >= 0 )
		{
		    m_roman += "L";
		    i -= L;
		    continue;
		}
		if( ( i - X ) >= 0 )
		{
		    m_roman += "X";
		    i -= X;
		    continue;
		}
		if( ( i - V ) >= 0 )
		{
		    m_roman += "V";
		    i -= V;
		    continue;
		}
		if( ( i - I ) >= 0 )
		{
		    m_roman += "I";
		    i -= I;
		    continue;
		}
	    }
	}
	else
	{
	    m_roman = "0";
	}
    }

    RomanNumeral( const std::string& string ) :
    m_roman( ((string.size() > 0 ) ? string : "0" ) ),
    m_decimal( 0 )
    {
	int i = 0;
	while( i < (int)string.size() )
	{
	    char c = string[i++];
	    switch( c )
	    {
		case 'M':
		case 'm':
		    m_decimal += M;
		    break;
		case 'D':
		case 'd':
		    m_decimal += D;
		    break;
		case 'C':
		case 'c':
		    m_decimal += C;
		    break;
		case 'L':
		case 'l':
		    m_decimal += L;
		    break;
		case 'X':
		case 'x':
		    m_decimal += X;
		    break;
		case 'V':
		case 'v':
		    m_decimal += V;
		    break;
		case 'I':
		case 'i':
		    m_decimal += I;
		    break;
		default:
		    throw new std::out_of_range( "Not a valid Roman numeral!" );
		    break;
	    }
	}
    }	
	
    int getDecimal()
    {
	return m_decimal;
    }
    void setDecimal( const int decimal );
    const std::string& getRoman()
    {
	return m_roman;
    }
protected:
    std::string m_roman;
    int m_decimal;
};

int main()
{
    cout << "Welcome to the Roman numeral to decimal converter!\nPlease enter a number in Roman numerals to be converted: ";
    
    std::string roman;
    cin >> roman;
    try
    {
	RomanNumeral rn( roman );
	cout << "Roman Numeral: " << roman << " Decimal: " << rn.getDecimal() << endl;
    }
    catch( exception* ex )
    {
	cout << roman << " " << ex->what() << endl;
    }

    cout << "Now enter a decimal number to be converted to Roman numerals: ";
    int decimal;
    cin >> decimal;
    try
    {
	RomanNumeral rn( decimal );
	cout << "Decimal number: " << decimal << " Roman: " << rn.getRoman() << endl;
    }
    catch( ... )
    {
	cout << "error during processing...too bad, I'm outta here!" << endl;
    }

    return 0;
}


...note that I made some fairly "radical" design changes to your code. Also, I added the ability to convert decimal to Roman, which seemed natural for the class. Notice also that some functionality is "stubbed out," in that I didn't implement "setDecimal," which would need a refactoring of the ctors to call a protected init operation that would "dynamically" re-initialize the class members based on the calls, setDecimal and/or setRoman, which, the latter, I didn't even stub out...as I started to reach my ~10-minute threshold for responses

Of some interest, you should note that I modified all of the silly private members MDCLXVI to const static ints, which is strongly preferred.

HTH...


:davis:
  #3  
Old 23-Mar-2006, 13:40
Ichigo Ichigo is offline
New Member
 
Join Date: Feb 2006
Posts: 18
Ichigo is on a distinguished road

Re: help writing a roman numeral to decimal program


aahhh, thank you very much!
i am having one strange problem though, it says for the const static ints:
"illegal pure syntax, must be '=0' " and
"pure specifier can only be specified for functions"
  #4  
Old 23-Mar-2006, 14:05
davis
 
Posts: n/a

Re: help writing a roman numeral to decimal program


Quote:
Originally Posted by Ichigo
aahhh, thank you very much!
i am having one strange problem though, it says for the const static ints:
"illegal pure syntax, must be '=0' " and
"pure specifier can only be specified for functions"

That is a problem with VC++ 6.0. Just move the initialization of the members out of the class and into global scope:

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;

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


class RomanNumeral
{
public:
    RomanNumeral( const int decimal ) :
    m_roman( "" ),
    m_decimal( ((decimal > 0) ? decimal : 0) ) 
    {
        if( decimal > 0 )
        {
            int i = decimal;
            while( i > 0 )
            {
                if( ( i - M ) >= 0 )
                {
                    m_roman += "M";
                    i -= M;
                    continue;
                }
                if( ( i - D ) >= 0 )
                {
                    m_roman += "D";
                    i -= D;
                    continue;
                }
                if( ( i - C ) >= 0 )
                {
                    m_roman += "C";
                    i -= C;
                    continue;
                }
                if( ( i - L ) >= 0 )
                {
                    m_roman += "L";
                    i -= L;
                    continue;
                }
                if( ( i - X ) >= 0 )
                {
                    m_roman += "X";
                    i -= X;
                    continue;
                }
                if( ( i - V ) >= 0 )
                {
                    m_roman += "V";
                    i -= V;
                    continue;
                }
                if( ( i - I ) >= 0 )
                {
                    m_roman += "I";
                    i -= I;
                    continue;
                }
            }
        }
        else
        {
            m_roman = "0";
        }
    }

    RomanNumeral( const std::string& string ) :
    m_roman( ((string.size() > 0 ) ? string : "0" ) ),
    m_decimal( 0 )
    {
        int i = 0;
        while( i < (int)string.size() )
        {
            char c = string[i++];
            switch( c )
            {
                case 'M':
                case 'm':
                    m_decimal += M;
                    break;
                case 'D':
                case 'd':
                    m_decimal += D;
                    break;
                case 'C':
                case 'c':
                    m_decimal += C;
                    break;
                case 'L':
                case 'l':
                    m_decimal += L;
                    break;
                case 'X':
                case 'x':
                    m_decimal += X;
                    break;
                case 'V':
                case 'v':
                    m_decimal += V;
                    break;
                case 'I':
                case 'i':
                    m_decimal += I;
                    break;
                default:
                    throw new std::out_of_range( "Not a valid Roman numeral!" );
                    break;
            }
        }
    }

    int getDecimal()
    {
        return m_decimal;
    }
    void setDecimal( const int decimal );
    const std::string& getRoman()
    {
        return m_roman;
    }
protected:
    std::string m_roman;
    int m_decimal;
};

int main()
{
    cout << "Welcome to the Roman numeral to decimal converter!\nPlease enter a number in Roman numerals to be converted: ";

    std::string roman;
    cin >> roman;
    try
    {
        RomanNumeral rn( roman );
        cout << "Roman Numeral: " << roman << " Decimal: " << rn.getDecimal() << endl;
    }
    catch( exception* ex )
    {
        cout << roman << " " << ex->what() << endl;
    }

    cout << "Now enter a decimal number to be converted to Roman numerals: ";
    int decimal;
    cin >> decimal;
    try
    {
        RomanNumeral rn( decimal );
        cout << "Decimal number: " << decimal << " Roman: " << rn.getRoman() << endl;
    }
    catch( ... )
    {
        cout << "error during processing...too bad, I'm outta here!" << endl;
    }

    return 0;
}



...you'll also need to fix the following:

Code:
Welcome to the Roman numeral to decimal converter! Please enter a number in Roman numerals to be converted: MCM Roman Numeral: MCM Decimal: 2100 Now enter a decimal number to be converted to Roman numerals: 1900 Decimal number: 1900 Roman: MDCCCC

...note that MCM = 1900 and even though MDCCCC does equal 1900, MCM is the "proper" way to use Roman numerals. It is the whole VIIII versus IX thing or IIII versus IV. The "convention" is that if there is no larger number in front, then to use the "more verbose" method.

VIIII would be preferable to IX, but VIV would be preferrable to VIIII. It is the "smaller between two largers" issue.

The convention basically states use the fewest symbols to convey the final number such that:

VII = 7

...is preferred over:

VIIIV = 7

...both are "mathematically" correct if implemented, but in reality, if someone enters a 7 in decimal, it should spit out VII and not VIIIV.

You should never find a smaller starting out with a larger after it.

CD = illegal (400?)
CCCC = 400 (proper)

...even though CD is two symbols compared to the four.

You ought to have some fun working out that algorithm


:davis:
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help writing a program that reads from a file and converts it to binary. edd_y_123 C Programming Language 3 18-Dec-2005 10:21
Frustration...Roman Numeral Program SpyD3r C++ Forum 14 13-Nov-2005 20:02
ROMAN Numeral Pt. 2, Floating Point Exception SpyD3r C++ Forum 1 13-Nov-2005 00:02
Roman to decimal to roman SpudNuts C++ Forum 2 16-Feb-2005 19:44
Hex Result giving strange answers. Rosdahale C Programming Language 6 07-Dec-2004 20:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:58.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.