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

1. 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. 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.

3. XXC is not a valid Roman Numeral so what's the problem?

4. 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. Blah, I hated this project when I did it last semester. I did everything but the decimal to roman.

6. 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.

7. You are right. I was not considering badly formed roman numerals when I said it.

8. 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. 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.

10. That's a lot of help!
Thanx!
I'll try to finish it now.

11. 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. 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. 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.

14. 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!

15. 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...

Popular pages Recent additions