Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,472 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,924 people online right now. Registration is fast and FREE... Join Now!




Roman Numeral Conversion using C

 
Reply to this topicStart new topic

Roman Numeral Conversion using C

ronkareiva
5 Feb, 2008 - 08:35 PM
Post #1

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 2

At this point I am only looking for help in determing which string function I should to take information entered from a user and to put it in a string. I need the string to only accept the letters that represent roman numerals (I, V, X, L, C, D, M) and exclude all other values. I would like to use the fgets function but I cannot figure out how to get to exclude all the other letters. If I cannot use the fgets function I am assuming I would use the scanf function to read and store until it reaches an invalide character or the end of the line. Is this correct?

Any help would be great. I have not wrote any code yet since I am not sure where to start at this point.

This is to be written using C and not C++

Thank you,

Ron Kareiva
User is offlineProfile CardPM
+Quote Post

#include<wmx010>
RE: Roman Numeral Conversion Using C
6 Feb, 2008 - 08:41 AM
Post #2

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
http://www.dreamincode.net/forums/showtopic22135.htm
User is offlineProfile CardPM
+Quote Post

selloorhari
RE: Roman Numeral Conversion Using C
7 Feb, 2008 - 05:05 AM
Post #3

D.I.C Head
**

Joined: 7 Feb, 2008
Posts: 66


My Contributions


This is my reply to your question... Check my code below....

#include<stdio.h>
#include<stdlib.h>
void main()
{

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


This post has been edited by selloorhari: 7 Feb, 2008 - 06:59 AM
User is offlineProfile CardPM
+Quote Post

shreshth91
RE: Roman Numeral Conversion Using C
7 Feb, 2008 - 06:25 AM
Post #4

New D.I.C Head
*

Joined: 7 Feb, 2008
Posts: 4


My Contributions
QUOTE(selloorhari @ 7 Feb, 2008 - 06:05 AM) *



This is my reply to your question... Check my code below....

#include<stdio.h>
#include<stdlib.h>
void main()
{

// M =1000, D = 500 , C = 100 , L = 50 , X = 10 , V = 5 , I = 1 ;
/* Program by K.Sri Hari */
/* Please mail your opinions to me */

int num,temp,a,i ;

clrscr() ;
printf ( " \n Enter the Number " ) ;
scanf ( "%u", &num ) ;
temp = num ;

while( temp <= 0 || temp >= 5000)
{
printf ( "\n The Number cannot be ROMANIZED" ) ;
getch() ;
exit(0) ;
}

printf ( "\n The Roman letter Representation is : " ) ;
if ( num >= 1000 )
{
a = num / 1000 ;
for ( i = 0 ; i < a ; i++ )
printf ( "M" ) ;
num = num - ( a* 1000 ) ;
}

if ( num>=500 )
{
if ( num>=900 )
{
printf( "CM" ) ;
num = num -900 ;
}
else
{
printf ("D" ) ;
num = num - 500 ;
}
}

if ( num >= 100 )
{
a = num / 100 ;
for ( i = 0 ; i < a ; i++ )
printf ( "C" ) ;
num = num - ( a* 100 ) ;
}

if ( num >= 50 )
{
if ( num >= 90 )
{
printf ( "XC" ) ;
num = num - 90 ;
}

else
{
printf ( "L" ) ;
num = num - 50 ;
}
}

if ( num >= 10 )
{
a = num / 10 ;
for ( i = 0 ; i < a ; i++ )
printf ( "X" );
num = num - (a*10) ;
}

if ( num >= 5 )
{
if ( num >= 9 )
{
printf ( "IX" ) ;
num = num - 9 ;
}

else
{
printf ( "V" ) ;
num = num - 5 ;
}
}

if ( num >= 1 )
{
a = num / 1 ;
for ( i = 0 ; i < a ; i++ )
printf ( "I" );
num = num - (a*1) ;
}

getch() ;
}



First of all, I think it's d.i.c. policy to not write do other people's work for them.
Also, if I'm not mistaken, the question is Roman system --> decimal system, not the other way around.

User is offlineProfile CardPM
+Quote Post

selloorhari
RE: Roman Numeral Conversion Using C
7 Feb, 2008 - 06:31 AM
Post #5

D.I.C Head
**

Joined: 7 Feb, 2008
Posts: 66


My Contributions
QUOTE(shreshth91 @ 7 Feb, 2008 - 07:25 AM) *

QUOTE(selloorhari @ 7 Feb, 2008 - 06:05 AM) *



This is my reply to your question... Check my code below....

#include<stdio.h>
#include<stdlib.h>
void main()
{

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

}



First of all, I think it's d.i.c. policy to not write do other people's work for them.
Also, if I'm not mistaken, the question is Roman system --> decimal system, not the other way around.


OK give me some time let me try to write the ROMAN System --> decimal

This post has been edited by selloorhari: 7 Feb, 2008 - 06:58 AM
User is offlineProfile CardPM
+Quote Post

shreshth91
RE: Roman Numeral Conversion Using C
7 Feb, 2008 - 06:39 AM
Post #6

New D.I.C Head
*

Joined: 7 Feb, 2008
Posts: 4


My Contributions
QUOTE(selloorhari @ 7 Feb, 2008 - 07:31 AM) *

QUOTE(shreshth91 @ 7 Feb, 2008 - 07:25 AM) *

QUOTE(selloorhari @ 7 Feb, 2008 - 06:05 AM) *



This is my reply to your question... Check my code below....

#include<stdio.h>
#include<stdlib.h>
void main()
{
...
}



First of all, I think it's d.i.c. policy to not write do other people's work for them.
Also, if I'm not mistaken, the question is Roman system --> decimal system, not the other way around.


OK give me some time let me try to write the ROMAN System --> decimal


Didn't you read the first remark??
You're NOT SUPPOSED TO GIVE THEM THE CODE. It's against d.i.c. policy!
User is offlineProfile CardPM
+Quote Post

selloorhari
RE: Roman Numeral Conversion Using C
7 Feb, 2008 - 06:56 AM
Post #7

D.I.C Head
**

Joined: 7 Feb, 2008
Posts: 66


My Contributions
QUOTE(shreshth91 @ 7 Feb, 2008 - 07:39 AM) *

QUOTE(selloorhari @ 7 Feb, 2008 - 07:31 AM) *

QUOTE(shreshth91 @ 7 Feb, 2008 - 07:25 AM) *

QUOTE(selloorhari @ 7 Feb, 2008 - 06:05 AM) *



This is my reply to your question... Check my code below....

#include<stdio.h>
#include<stdlib.h>
void main()
{
...
}



First of all, I think it's d.i.c. policy to not write do other people's work for them.
Also, if I'm not mistaken, the question is Roman system --> decimal system, not the other way around.


OK give me some time let me try to write the ROMAN System --> decimal


Didn't you read the first remark??
You're NOT SUPPOSED TO GIVE THEM THE CODE. It's against d.i.c. policy!


OK SORRY FOR THAT.. HERE AFTER I WON'T
thanks for reminding my mistake...


User is offlineProfile CardPM
+Quote Post

ronkareiva
RE: Roman Numeral Conversion Using C
7 Feb, 2008 - 09:06 PM
Post #8

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 2

Thanks for the help so far. At this point I have gotten most of it writtend but I am jammed. I am able to read the roman numeral and input it as a string9which is required for the assignment). After that I took the appropriate values and added them to a parallel array(also required for the assignment. I.E. if the value of str[0] is V then ary[0] is 5. My problem is the conversion. I cannot get it to properly convert all the time. The code I have below will convert correctly unless there is two or more consecutive numbers that are the same and the number following them is larger. For example IV converts to 4 but IIV converts to V instead of 3. let me know if anyone can help with this part.

Thank you,

Ron Kareiva





#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


int main (void)
{
char str[20];
int ary[20];
int check;
int count;
int decimal;


while (true)
{
count = 0;
decimal = 0;
for (int i = 0; i < 20; i++)
{
str[i] = NULL;
ary[i] = 0;
}

printf ("Please enter a Roman Numeral of up to 20 characters to be converted\n");
printf ("to an Integer value.\n");
fgets(str, sizeof (str), stdin);
check = strspn(str, "IVXLCDM");
count = strlen (str);


if (check != count - 1)
{
printf("Your input is not a Roman Numeral.\n");
printf("Roman Numerals only contain the following case sensitive characters:\n");
printf("I,V,X,L,C,D,M\n");
printf("All other characters are invalid including the lower case equivilants.\n");
printf("Please try again by entering a properly formatted Roman Numeral\n\n\n\n");
}
else
{

for (int i = 0; i < count - 1; i++)
{
if (str[i] == 'I')
ary[i] = 1;
if (str[i] == 'V')
ary[i] = 5;
if (str[i] == 'X')
ary[i] = 10;
if (str[i] == 'L')
ary[i] = 50;
if (str[i] == 'C')
ary[i] = 100;
if (str[i] == 'D')
ary[i] = 500;
if (str[i] == 'M')
ary[i] = 1000;
}

for (int i = 0; i < count - 1; i++)
{

if (ary[i] >= ary[i + 1])
decimal += ary[i];
if (ary[i] < ary[i + 1])
decimal -= ary[i];

}
printf("The value of your Roman Numeral is %d", decimal);
printf("%\n\n");
}
}
}
User is offlineProfile CardPM
+Quote Post

selloorhari
RE: Roman Numeral Conversion Using C
8 Feb, 2008 - 06:09 AM
Post #9

D.I.C Head
**

Joined: 7 Feb, 2008
Posts: 66


My Contributions
QUOTE(ronkareiva @ 7 Feb, 2008 - 10:06 PM) *

Thanks for the help so far. At this point I have gotten most of it writtend but I am jammed. I am able to read the roman numeral and input it as a string9which is required for the assignment). After that I took the appropriate values and added them to a parallel array(also required for the assignment. I.E. if the value of str[0] is V then ary[0] is 5. My problem is the conversion. I cannot get it to properly convert all the time. The code I have below will convert correctly unless there is two or more consecutive numbers that are the same and the number following them is larger. For example IV converts to 4 but IIV converts to V instead of 3. let me know if anyone can help with this part.

Thank you,

Ron Kareiva





#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


int main (void)
{
char str[20];
int ary[20];
int check;
int count;
int decimal;


while (true)
{
count = 0;
decimal = 0;
for (int i = 0; i < 20; i++)
{
str[i] = NULL;
ary[i] = 0;
}

printf ("Please enter a Roman Numeral of up to 20 characters to be converted\n");
printf ("to an Integer value.\n");
fgets(str, sizeof (str), stdin);
check = strspn(str, "IVXLCDM");
count = strlen (str);


if (check != count - 1)
{
printf("Your input is not a Roman Numeral.\n");
printf("Roman Numerals only contain the following case sensitive characters:\n");
printf("I,V,X,L,C,D,M\n");
printf("All other characters are invalid including the lower case equivilants.\n");
printf("Please try again by entering a properly formatted Roman Numeral\n\n\n\n");
}
else
{

for (int i = 0; i < count - 1; i++)
{
if (str[i] == 'I')
ary[i] = 1;
if (str[i] == 'V')
ary[i] = 5;
if (str[i] == 'X')
ary[i] = 10;
if (str[i] == 'L')
ary[i] = 50;
if (str[i] == 'C')
ary[i] = 100;
if (str[i] == 'D')
ary[i] = 500;
if (str[i] == 'M')
ary[i] = 1000;
}

for (int i = 0; i < count - 1; i++)
{

if (ary[i] >= ary[i + 1])
decimal += ary[i];
if (ary[i] < ary[i + 1])
decimal -= ary[i];

}
printf("The value of your Roman Numeral is %d", decimal);
printf("%\n\n");
}
}
}



CODE
SIR Your code is nice......


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 02:37PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month