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 11-Dec-2006, 12:18
ShamanKingCpp ShamanKingCpp is offline
New Member
 
Join Date: Dec 2006
Posts: 3
ShamanKingCpp is on a distinguished road

Roman numerals to decimal


Plz someone help me with C++ programm!

I need enter in programm big letters... and if thay comply with Roman numerals than i need convert this Roman numeral to decimal...

example

V - 5
VII - 7
ABVT - error! It's not Roman number.
76 - error! It's not Roman number.

Plz help... i can't make myself... I don't understand C++ i just learning.

I see on forum other Roman numerals to decimal Threads, but there wasn't correct programms which works. Plz somebody write this programm here
  #2  
Old 11-Dec-2006, 13:31
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Roman numerals to decimal


Quote:
Originally Posted by ShamanKingCpp
Plz someone help me with C++ programm!
.
.
.
Plz somebody write this programm here


These are two very different requests. There are a number of people here who may be able to help you with the first request. Most will not simply write the code for you, but we like to try to help people learn to implement and debug their own programs.

Most of the time just posting an assignment and saying something like, "I don't know how to do it," will not get a helpful response. The idea is that you use what you learned in class or in some previous class to start the program. If (when) you get to a part that doesn't work the way you expect or if you write and test part of the code but are stuck for ideas about how to proceed, then.

1. Post the code that you have so far.

2. Ask specific questions about parts that you don't understand.

Like this: "When I tried to compile the program, the compiler gave the following message(s) that I didn't understand: ...

or

Like this: "The program compiled without any warning or error messages, but then I gave the program the following input:... I expected the following output:..., but the program (crashed) (gave the following wrong output..) (whatever...)"



Regards,

Dave
  #3  
Old 11-Dec-2006, 20:26
davis
 
Posts: n/a

Re: Roman numerals to decimal


Quote:
Originally Posted by ShamanKingCpp
Plz someone help me with C++ programm!

I need enter in programm big letters... and if thay comply with Roman numerals than i need convert this Roman numeral to decimal...

example

V - 5
VII - 7
ABVT - error! It's not Roman number.
76 - error! It's not Roman number.

Plz help... i can't make myself... I don't understand C++ i just learning.

I see on forum other Roman numerals to decimal Threads, but there wasn't correct programms which works. Plz somebody write this programm here

Did you take a look at:

www.gidforums.com


:davis:
  #4  
Old 12-Dec-2006, 14:21
ShamanKingCpp ShamanKingCpp is offline
New Member
 
Join Date: Dec 2006
Posts: 3
ShamanKingCpp is on a distinguished road

Re: Roman numerals to decimal


Yes i take a look at that link... but somebody with that program is not good. I little bit change it because there was things what i don't need but still in my and that programm is something wrong.

CM is 900 but the programm convert CM to 1100
XC is 90 but the programm convert XC to 110
or i write IIIDDDMMMCCCXXXIDCMX and programm convert it to 6444 ... I think it's not correctly too.

I don't realy know what problem there is! Can somebody help me with it? That program don't convert correctly from Roman to decimal. =(

PHP 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':
                    m_decimal += M;
                    break;
                case 'D':
                
                    m_decimal += D;
                    break;
                case 'C':
                
                    m_decimal += C;
                    break;
                case 'L':
                
                    m_decimal += L;
                    break;
                case 'X':
                
                    m_decimal += X;
                    break;
                case 'V':
                
                    m_decimal += V;
                    break;
                case 'I':
                
                    m_decimal += I;
                    break;
                default:
                    throw new std::out_of_range( "Error. It's not 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 << "Plz enter the big letter.";

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

    system("pause");
    return 0;
} 


  #5  
Old 12-Dec-2006, 15:32
davis
 
Posts: n/a

Re: Roman numerals to decimal


Quote:
Originally Posted by ShamanKingCpp
Yes i take a look at that link... but somebody with that program is not good. I little bit change it because there was things what i don't need but still in my and that programm is something wrong.

CM is 900 but the programm convert CM to 1100
XC is 90 but the programm convert XC to 110
or i write IIIDDDMMMCCCXXXIDCMX and programm convert it to 6444 ... I think it's not correctly too.

I don't realy know what problem there is! Can somebody help me with it? That program don't convert correctly from Roman to decimal. =(

PHP 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':
                    m_decimal += M;
                    break;
                case 'D':
                
                    m_decimal += D;
                    break;
                case 'C':
                
                    m_decimal += C;
                    break;
                case 'L':
                
                    m_decimal += L;
                    break;
                case 'X':
                
                    m_decimal += X;
                    break;
                case 'V':
                
                    m_decimal += V;
                    break;
                case 'I':
                
                    m_decimal += I;
                    break;
                default:
                    throw new std::out_of_range( "Error. It's not 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 << "Plz enter the big letter.";

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

    system("pause");
    return 0;
} 



There is no system("pause") on my system, so I didn't bother to take a look at your code. Why don't you use a simple FSM for managing the states of your deciphering?


:davis:
  #6  
Old 12-Dec-2006, 17:08
davis
 
Posts: n/a

Re: Roman numerals to decimal


Quote:
Originally Posted by ShamanKingCpp
CM is 900 but the programm convert CM to 1100
XC is 90 but the programm convert XC to 110
or i write IIIDDDMMMCCCXXXIDCMX and programm convert it to 6444 ... I think it's not correctly too.

Note that CM is not valid Roman. A smaller numeral never "starts" the number. The larger must start first, therefore:

CM is not valid
XC is not valid
IIIDDDMMMCCCXXXIDCMX is not valid

If you want 90, you will use:

LXL or
LXXXX

If you want 900, you will use:

DCD or
DCCCC

...noting that my original code didn't work properly for any of these. A reasonable choice would be to use a simple FSM and parse through the numeral appropriately using transitions in state accordingly.

The intention of my original code (other than being only 10-minutes of effort) was to give the original poster an opportunity to consider some other ways of looking at the problem through the implementation choices available in C++. It wasn't meant to be a perfected example of properly handling the problem...and I called out that it was clearly not working properly. However, it should give you (or anybody) at least an idea of how to go about writing it.


:davis:
  #7  
Old 12-Dec-2006, 21:21
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Roman numerals to decimal


With all due respect

Quote:
CM is not valid
Yes it is, it means 1000 - 100 = 900.
Quote:
XC is not valid
Yes it is, it means 100 - 10 = 90.
Quote:
IIIDDDMMMCCCXXXIDCMX is not valid
I have to agree with you there. A general rule is (in mathematical terms) a Roman numeral meaning 10^x cannot come before a numeral meaning something higher than 10^(x+1). In other words, you can have XC=90 but not IC, you'd need XCIX=99 instead. (This comes straight from Wikipedia and my experience.)

Keep in mind, Davis, that a finite state machine might be a trivial endeavor for you, but many people have no idea what FSM stands for -- you shouldn't assume and be so judgemental.
  #8  
Old 12-Dec-2006, 23:46
davis
 
Posts: n/a

Re: Roman numerals to decimal


Quote:
Originally Posted by ubergeek
Keep in mind, Davis, that a finite state machine might be a trivial endeavor for you, but many people have no idea what FSM stands for -- you shouldn't assume and be so judgemental.

Well, if I was perfect, I'd charge a LOT more! However, I pulled all of that from memory (being rather old and feable minded) rather than checking (like I probably should have done) Wikipedia or other sources.

While it may be easy to say not to be judgmental (we drop the "e" on judge), we all are always judgmental about everything. That fact gives us the ability to distinguish right from wrong and make similar comparisons about everything else...including who we let our kids play with, what web sites we surf, what channels we watch and what food products we consume...and a whole lot more.

The mere mention of an FSM for someone who doesn't know what it is suggests to me that if that person is industrious, then s/he will look it up and discover what it means. If the person lacks the motivation to determine the meaning, then there is no reason for me to explain it at any length. I'm not saying that I'm "right," but I'm not likely to change my "methods" anytime soon, either. But, thank you for your comments.


:davis:
  #9  
Old 14-Dec-2006, 07:25
ShamanKingCpp ShamanKingCpp is offline
New Member
 
Join Date: Dec 2006
Posts: 3
ShamanKingCpp is on a distinguished road

Re: Roman numerals to decimal


Plz somebody show me what code i need to change or delete! I'm just not good in english therfore i need that somebody show me the code. I don't need all c++ programm... just show what i need to change in this programm...

PHP 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':
                    m_decimal += M;
                    break;
                case 'D':
                
                    m_decimal += D;
                    break;
                case 'C':
                
                    m_decimal += C;
                    break;
                case 'L':
                
                    m_decimal += L;
                    break;
                case 'X':
                
                    m_decimal += X;
                    break;
                case 'V':
                
                    m_decimal += V;
                    break;
                case 'I':
                
                    m_decimal += I;
                    break;
                default:
                    throw new std::out_of_range( "Error. It's not 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 << "Plz enter the big letter.";

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

    system("pause");
    return 0;
} 


 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

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
help writing a roman numeral to decimal program Ichigo C++ Forum 3 23-Mar-2006 15:05
Frustrating C++ problem... Roman numerals Elsydeon C++ Forum 10 01-Sep-2005 08:37
Help with a C++ Program (Converting Roman Numerals) royrules22 C++ Forum 1 11-Jul-2005 01:01
Roman to decimal to roman SpudNuts C++ Forum 2 16-Feb-2005 20:44
Hex Result giving strange answers. Rosdahale C Programming Language 6 07-Dec-2004 21:28

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

All times are GMT -6. The time now is 21:09.


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