Honesty Rocks! truth rules.

I Need Help its about roman numbers in c/c++

HOME      >>       Programming

wicked_soul

:P hy.... i really need help... we have to make a programm that checks if input string is roman number...and i'm beginner in c/c++so i don't really know how to do this.... so if anyone can help me,please.... THANK YOU.... :lol:

osknockout

Yo. That's kind of simple. Just check if the input only has characters from the set "I, V, X, L, C, D, M". If it doesn't, then it's not integer Roman numerals.
You can set up a while loop going through a character array with several if statements checking if the characters are any of the above.

If you're trying to find out what that number IS, you can see my previous post on this topic. See if it makes sense to you.

Reply back if need anything more.


wicked_soul

Yo. That's kind of simple. Just check if the input only has characters from the set "I, V, X, L, C, D, M". If it doesn't, then it's not integer Roman numerals.You can set up a while loop going through a character array with several if statements checking if the characters are any of the above.

If you're trying to find out what that number IS, you can see my previous post on this topic. See if it makes sense to you.

Reply back if need anything more.


THANKS...but that's not enough...e.g. if i input XXXLCIM that isn't a roman number,but has characters from the set ''I,V,X,L,C,D,M''...so....?

osknockout

Alright. Then set up an character array with the stuff in series, e.g. num[0] = "I", num[1] = "V", etc.Consider the input as a string where each character is tested individually.Ok, let's see, some of the most complex statements in roman numerals:1999 - MCMXCIX (index-wise, 7-5-7-3-5-1-3)1444 - MCDXLIV (index-wise, 7-5-6-3-4-1-2)1949 - MCMXLIX (index-wise, 7-5-7-3-4-1-2)1494 - MCDXCIV (index-wise, 7-5-6-3-5-1-2)Say character n matches with index i in the array.Then character n+1 must have index less than or equal to i+2, say it has index j in the array.If j > i, then it is a standard roman numeral iff (if and only if) the preceding character wasC,X, or I - because these are the only prefix numerals, otherwise it is fine.Start from n=1 and repeat test all over the string with n++ or however you want to jump n by 1.


salamangkero

Sorry, I really shouldn't do this (you should be doing your own homework) but I couldn't resist. It's been quite a while since I've undertaken exercises like this :lol:

#include <stdio.h>#include <string.h>int main();void repeat(char c);void check(char c);char input[] = "MMDCLXVII";int indx = 0;int main() { repeat('M'); check('D'); repeat('C'); check('L'); repeat('X'); check('V'); repeat('I'); if (indx < strlen(input)) printf("Not a Roman Numeral\n"); else printf("Roman Numeral\n");}//advances the indx up to three times for this charactervoid repeat(char c) { int i; for (i=0; i<3; i++) { if(input[indx] == c) indx++; }}//advances the indx only once for this charactervoid check(char c) { if (input[indx] == c) indx++;}


osknockout

salamangkero you evil person! Why -sweet god, why- would you do homework for n00bs?

 

And what's more evil... you didn't return(0).

This merits a FAIL.

...

Actually, it's kinda nice code.

That's a very clean style you have there.


wicked_soul

salamangkero thanks a lot.... :P and osknockout i don't know how to write this statements in programm ''Then character n+1 must have index less than or equal to i+2, say it has index j in the array. If j > i, then it is a standard roman numeral iff (if and only if) the preceding character was C,X, or I.'' ?? and i'm sorry for being such boring... :D:lol:


osknockout

Hey, no prob. I'm just trying to make you do your own homework. 'cause doing comp. sci. hmwk's good for ya. :lol: And sorry for the formalistic advice also. I've been through a series of calc. and physics tests, so everything lookslike theorem proving right now...