Roman to arabic-----yes, again about this

This is a discussion on Roman to arabic-----yes, again about this within the C++ Programming forums, part of the General Programming Boards category; Look i have a litlle problem, and this time I0m not posting this from my home but from somewhere else,so ...

  1. #1
    Argo Argo_Jeude's Avatar
    Join Date
    Jul 2005
    Location
    Bihać, Bosnia
    Posts
    21

    Roman to arabic-----yes, again about this

    Look i have a litlle problem, and this time I0m not posting this from my home but from somewhere else,so I will not be in quick replaying here..
    Here's the problem:

    I made a function where every roman sign translates to arabic:
    M----1000
    C----100
    etc.
    and those numbers are put in some string.-(string?- I don't know the word on english).
    Then I put a for loop in wich I put several if statements which check if the number is bigger than next number, add it to the Sum,if not delete it from the sum.
    So this works pefectly for numbers like MCMXCVII--1997
    But for a numbers like this XXC it doesn't.
    I need to somehow add a if statements which checks if the number and the next number are the same.


    Further I don't know still.
    Maybe next time I post I'll put source code which I made, but if anyone has idea what am I talking about post some reply!

    Thanx in advance!

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by Argo_Jeude
    But for a numbers like this XXC it doesn't.
    That's not a valid Roman number. That should be LXXX.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    XXC is not a valid Roman Numeral so what's the problem?

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    Off the top of my head, what you must do is to traverse the string from right to left (from highest subscript to lowest). Roman numerical representation rules are quite simple. When read from right to left, if the next character is "bigger or equal" than the current one, you add, if it is smaller you subtract.

    So if your code is already working, but not for equal characters, you just need to change your if statements so that where you test for bigger then (>), you start testing for bigger than or equal (>=).

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Blah, I hated this project when I did it last semester. I did everything but the decimal to roman.

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by Mario F.
    Roman numerical representation rules are quite simple. When read from right to left, if the next character is "bigger or equal" than the current one, you add, if it is smaller you subtract.
    Well, no. Do you represent 1999 as MIM or MCMXCIC? Do you represent 45 as VL, XLV, or XLVX? Roman numerical representation rules are a bit more complex than you've stated.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    You are right. I was not considering badly formed roman numerals when I said it.

  8. #8
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Here are the rules as written by My Programming professor Mr. Gregory Shaw @ Florida International University, this was the homework assignment I had to do, it was for a roman numeral calculator so I'll eliminate the addition/subtraction of two roman numbers and just focus on the actual conversion.

    A string of numerals means that their values should be added together.

    Ex. XXX = 10 + 10 + 10 = 30, and LXI = 50 + 10 + 1 = 61.

    If a smaller value is placed before a larger one, we subtract instead of adding.

    Ex. IV = 5 - 1 = 4
    More relevant
    Here are the official rules for subtracting letters:

     Subtract only powers of ten, such as I, X, or C. Writing VL for 45 is not allowed: write XLV instead.
     Subtract only a single letter from a single numeral. Write VIII for 8, not IIX; 19 is XIX, not IXX.
     Don't subtract a letter from another letter more than ten times greater. This means that you can only subtract I from V or X, and X from L or C, so for example 1999 is MCMXCIX, and not MIM.

    Use these additional rules to convert an Arabic number to Roman numerals, again converting one digit at a time.

    Ex. 982 = 900 + 80 + 2
    = CM + LXXX + II
    = CMLXXXII
    quoted from assignmetn specifications @
    http://www.fiu.edu/~shawg/3337/cop3337.html

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    Well, I've decided to do one.

    I'm teaching myself classes and had gone trough strings and iterators before. This looked like a fun thing to put my hands on.

    Attached you can find a class that both validates and converts roman numerals to decimal. Probably a rude attempt, by many standards...

    I followed on Indigo's teacher set of rules and added a few more. Namely:

    • If subtracted before cannot subtract again same or higher (To avoid CMCM and IXCM, instead of MDCCC and CMIX)
    • Only allow I, X, C and M to repeat
    • Don't allow more than 2 repetitions (CCCC instead of CD)


    The class has no default constructor. It has to be initialized with a string. There's no public member functions besides the constructor which validates and converts the string. The class exposes:

    valid - bool, returns true if the numeral is a valid roman numeral.

    numeral - string, the roman numeral used to initialize the class

    arabic - int, the decimal conversion of the numeral if valid is TRUE, or 0 if valid is FALSE. (remember, romans had no zero representation)

    Note:
    The current rules in the class implementation don't allow for numerals above 3999. Romans had a notation for numbers higher than this. It was composed of the same letters we all know, but with a dash above them. That letter would then become its normal value times 5,000.

    So, to represent the number 4000, for instance, they would write:
    Code:
     _
    MI
    Some people repeat M several times to represent numbers above 3999. In order to keep with the historical notation, I didn't allow this on the class. As such, it can only be used for numbers between 1 and 3999.


    If you actually go to the trouble of checking this out, let me know where it can be improved.
    Attached Files Attached Files
    Last edited by Mario F.; 05-31-2006 at 05:12 PM.

  10. #10
    Argo Argo_Jeude's Avatar
    Join Date
    Jul 2005
    Location
    Bihać, Bosnia
    Posts
    21
    That's a lot of help!
    Thanx!
    I'll try to finish it now.

  11. #11
    Argo Argo_Jeude's Avatar
    Join Date
    Jul 2005
    Location
    Bihać, Bosnia
    Posts
    21
    I did it!
    It works now!
    I had a function of int type that gets char variable.
    In function i had a switch.
    'M'--->1000
    'D'--->500
    ...
    default--->0
    String of char type that gets roman numbers.
    char RimBr[11];
    String of int type ArabTemp[11] in which I put translated numbers
    1000,500,100,50,10,5,1.
    Then for loop which checks for czrrent number and next.
    VAriable Sum is set on 0 at first and if current number is bigger or equal than next it's added to the sum,else it's deleted from the sum.
    On exit cout<<Sum;
    And that's it!

    Hope this helps someone who gets in the same problem.

    As for turning the arabic number to roman, I did that long ago.
    Thanx again!
    This thread(?) is finally over!

  12. #12
    Argo Argo_Jeude's Avatar
    Join Date
    Jul 2005
    Location
    Bihać, Bosnia
    Posts
    21
    Here's the code.

    Code:
    #include<iostream>
    using namespace std;
    int main(){
    int Rim_Dec(char a);
    char RimBr[11];
    int ArapTemp[10],sum=0;
    cout<<"Enter a roman number: ";
    cin>>RimBr;
    for(int i=0;i<11;i++){
            ArapTemp[i]=Rim_Dec(RimBr[i]);
            }
    for(int i=0;i<11;i++){
            if(ArapTemp[i]>=ArapTemp[i+1]){
                         sum=sum+ArapTemp[i];
                         }
            else if(ArapTemp[i]<ArapTemp[i+1]){
                    sum=sum-ArapTemp[i];
                    }
    }         
    cout<<sum<<endl;
    system("pause");
    return 0;
    }
    int Rim_Dec(char a){
        int broj;
        switch(a){
                  case 'M': num=1000;
                  break;
                  case 'D': num=500;
                  break;
                  case 'C': num=100;
                  break;
                  case 'L': num=50;
                  break;
                  case 'X': num=10;
                  break;
                  case 'V': num=5;
                  break;
                  case 'I': num=1;
                  break;
                  default : num=0;
                  }
                  return num;
    }

    How do I make so that program checks if the number entered is right roman number?
    Maybe a function with set of rules.
    for loop that checks is there any 4 same numbers together?
    What do I need?
    Help please!?

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    Well... you didn't understand the code I posted...

    You are already doing it. That's what the function you called RimDec() does. It either returns the decimal number represented by the roman literal, or returns 0. You just need to check for 0 to know if the roman number is valid.

    As for checking for instances of the same roman literal in sequence, one way is with a counter.

    Code:
    - Declare an int and initialize it to 0
    - Declare an empty char.
    - Everytime you read a new roman literal (and starting righ with the first literal):
        - check to see if the literal is == to the char
            - If it is,
                - increment int
                - check if int == 4 (you have 4 roman literals in sequence)
                    - If it is, your roman literal is invalid...
            - If it is not,
                - assign 1 to int (in other words reset it)
                - assign the new roman literal to the char
    - loop to the next literal

    As a side note, your Rim_Dec function is declaring broj, but not using it.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    Argo Argo_Jeude's Avatar
    Join Date
    Jul 2005
    Location
    Bihać, Bosnia
    Posts
    21
    Code:
    #include<iostream>
    using namespace std;
    int Rim_Dec(char a);
    int main(){
    char RimBr[11],y;
    int ArapTemp[10],suma=0,x=0;
    cout<<"Please enter roman number: ";
    cin>>RimBr;
    
    for(int i=0;i<11;i++){
            if(RimBr[i]=='\0'){
                               break;
                               }
                               if(RimBr[i]==y){
                                               x=x+1;
                                               if(x==4){
                                                        cout<<endl<<"Not right roman number!"<<endl;
                                                        system("pause");
                                                        return 0;
                                                        }
                                               }
            else if(RimBr[i]!=y){
            x=1;
            y=RimBr[i];
            }
    }
    
    for(int i=0;i<11;i++){
            ArapTemp[i]=Rim_Dec(RimBr[i]);
            }
    
    for(int i=0;i<11;i++){
            if(ArapTemp[i]=='\0'){
                               break;
                               }
            if(ArapTemp[i]==ArapTemp[i+1]){
                                     if(ArapTemp[i+1]<ArapTemp[i+2]){
                                                               cout<<endl<<"Not right roman number!"<<endl;
                                                               system("pause");
                                                               return 0;
                                                               }
                                     else if(ArapTemp[i+1]==ArapTemp[i+2]){
                                          if(ArapTemp[i+2]<ArapTemp[i+3]){
                                                                    cout<<endl<<"Not right roman number!"<<endl;
                                                                    system("pause");
                                                                    return 0;
                                                                    }
                                          }
                                     }
    }
    for(int i=0;i<11;i++){
            if(ArapTemp[i]>=ArapTemp[i+1]){
                         suma=suma+ArapTemp[i];
                         }
            else if(ArapTemp[i]<ArapTemp[i+1]){
                    suma=suma-ArapTemp[i];
                    }
    }         
    cout<<suma<<endl;
    system("pause");
    return 0;
    }
    int Rim_Dec(char a){
        int broj;
        switch(a){
                  case 'M': broj=1000;
                  break;
                  case 'D': broj=500;
                  break;
                  case 'C': broj=100;
                  break;
                  case 'L': broj=50;
                  break;
                  case 'X': broj=10;
                  break;
                  case 'V': broj=5;
                  break;
                  case 'I': broj=1;
                  break;
                  default : broj=0;
                  }
                  return broj;
    }

    I made checking 'thing' using MarioF's code and I put another code that checks if there is more than one number that is smaller compared to next number in array.
    So that we can't enter XXC as 80,but we have to enter LXXX.

    But there is one important thing that I needhelp.

    XCM I think, should be wrong roman number.
    Does anyone knowshow to do it?
    Thanx in advance!
    Last edited by Argo_Jeude; 06-17-2006 at 06:10 AM.

  15. #15
    Argo Argo_Jeude's Avatar
    Join Date
    Jul 2005
    Location
    Bihać, Bosnia
    Posts
    21
    Quote Originally Posted by Mario F.

    As a side note, your Rim_Dec function is declaring broj, but not using it.
    So could I just write return 1000 or return 500?
    I think I could.
    Good...
    Excellent...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. roman to arabic and vise versa
    By tm` in forum C Programming
    Replies: 2
    Last Post: 12-14-2007, 07:28 AM
  2. Code won't compile! O.o
    By Coldcore in forum C Programming
    Replies: 24
    Last Post: 01-07-2006, 08:21 AM
  3. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  4. Arabic to Roman Numeral
    By jdm71488 in forum C++ Programming
    Replies: 7
    Last Post: 04-21-2004, 01:33 AM
  5. Making roman numerals into arabic numbers
    By Mule in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2003, 11:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21