Zelaron Gaming Forum  
Stats Arcade Portal Forum FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read
Go Back   Zelaron Gaming Forum > The Zelaron Nexus > Science and Art > Tech Help

 
 
Thread Tools Display Modes

 
Post Need help
Reply
Posted 2003-11-18, 07:54 PM
Ok i need to write a program that takes input as roman numbers, converts it to normal(arabic) numbers and outputs the result *AND* the roman number doubled

EX:

Input X

Output

10
XX
Old
Profile PM WWW Search
Hades-Knight is neither ape nor machine; has so far settled for the in-between
 
 
Hades-Knight
 



 
Reply
Posted 2003-11-18, 07:59 PM in reply to Hades-Knight's post "Need help"
I bet mantra could make it in 5 seconds.
Old
Profile PM WWW Search
!King_Amazon! simplifies with no grasp of the basics!King_Amazon! simplifies with no grasp of the basics!King_Amazon! simplifies with no grasp of the basics!King_Amazon! simplifies with no grasp of the basics!King_Amazon! simplifies with no grasp of the basics!King_Amazon! simplifies with no grasp of the basics!King_Amazon! simplifies with no grasp of the basics
 
 
!King_Amazon!
 



 
Reply
Posted 2003-11-18, 10:50 PM in reply to Hades-Knight's post "Need help"
#include <stdio.h>

int main(void)
{
int x, now, total, i;
now = 0;
char romanNumeral[50];

printf("enter roman numeral\n");
gets(romanNumeral);

for (i = strlen(romanNumeral) - 1); i >= 0; i--)
{
if (romanNumeral[i] == 'M') {

x = 1000;

}
else if (romanNumeral[i] == 'D') {

x = 500;

}
else if (romanNumeral[i]== 'C') {

x = 100;

}
else if (romanNumeral[i] == 'L') {

x = 50;

}
else if (romanNumeral[i] == 'X') {

x = 10;

}
else if (romanNumeral[i] == 'V') {

x = 5;

}
else if (romanNumeral[i] == 'I') {

x = 1;

}

if (x >= now) {

total += x;

} else {

total -= x;

}

now = x;

}

printf("%d \n", total);

}





sorry if it's messy but i think you get the gist of it


ill see if i can do ur other 1 tomorrow

i haven't compiled 2 check for syntax errors but i think the logic should work..

and i hope u needed C...lol

Last edited by Demosthenes; 2003-11-18 at 11:09 PM.
Old
Profile PM WWW Search
Demosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to be
 
Demosthenes
 



 
Reply
Posted 2003-11-19, 10:27 AM in reply to Hades-Knight's post "Need help"
Um, from a once over, it appears that it would read XXI as 1...

Edit: 
Err, n/m. Looks like it should work for the conversion, but it doesn't do the reversal of the double.
Old
Profile PM WWW Search
WetWired read his obituary with confusionWetWired read his obituary with confusionWetWired read his obituary with confusionWetWired read his obituary with confusion
 
 
WetWired
 



 
Reply
Posted 2003-11-19, 10:35 AM in reply to Hades-Knight's post "Need help"
I don't think it works. If input is IV, it adds 1 to total (which you didn't set to zero initially which will give an error) then sets now to 1. Upon the second loop, 5 is greater than 1 so total becomes -4.

Also, your for loop
Quote:
for (i = strlen(romanNumeral) - 1); i >= 0; i--)
Is missing a "(" before strlen.

I'll see what I can do with it. Give me a bit.
D3V said:
This message is hidden because D3V is on your ignore list.
What is it they say about silence being golden?
Old
Profile PM WWW Search
Medieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzMedieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Medieval Bob
 



 
Reply
Posted 2003-11-19, 11:41 AM in reply to Hades-Knight's post "Need help"
Here's working code. I checked for invalid input such as the following:

XIr - Invalid Character
XXIVI - Invalid Sequence
XXIVV - Invalid Sequence
XXXXI - Invalid Repitition

Here's the code:
------------------------------------------------------
#include<stdio.h>

int main(void)
{
int i, previous = 0, num, total = 0, flag = 0, key = 0, error = 0, number, repeat = 1;
char roman[50];

printf("Enter the Roman numeral: ");
scanf("%s",roman);

for (i = 0; i < 50; i++)
{

if (roman[i] == 'M')
num = 1000;
else if (roman[i] == 'D')
num = 500;
else if (roman[i] == 'C')
num = 100;
else if (roman[i] == 'L')
num = 50;
else if (roman[i] == 'X')
num = 10;
else if (roman[i] == 'V')
num = 5;
else if (roman[i] == 'I')
num = 1;
else if (roman[i] == '\0')
flag = 1;
else
error = 1;

if (num == previous)
repeat = repeat + 1;
else
repeat = 1;

if (repeat == 4 && num == 1)
{
error = 1;
flag = 1;
}
if (repeat == 2 && num == 5)
{
error = 1;
flag = 1;
}
if (repeat == 4 && num == 10)
{
error = 1;
flag = 1;
}
if (repeat == 2 && num == 50)
{
error = 1;
flag = 1;
}
if (repeat == 4 && num == 100)
{
error = 1;
flag = 1;
}
if (repeat == 2 && num == 500)
{
error = 1;
flag = 1;
}

if (flag == 0)
{
if (num == 0)
error = 1;

if (num > previous)
{
if (key == 1)
{
number = i;
i = 50;
}
total = (total - (2 * previous)) + num;
key = 1;
}
else
total += num;

previous = num;
num = 0;
}
else
{
number = i - 1;
i = 50;
}

}

if (roman[number + 1] != '\0')
error = 1;

if (error == 1)
printf("Error in input.");
else
printf("%d\n",total);

return(0);
}
D3V said:
This message is hidden because D3V is on your ignore list.
What is it they say about silence being golden?
Old
Profile PM WWW Search
Medieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzMedieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Medieval Bob
 



 
Reply
Posted 2003-11-19, 12:02 PM in reply to Hades-Knight's post "Need help"
Actually, except for the non-initialization thing, it should work because it starts at the end and moves left.
Old
Profile PM WWW Search
WetWired read his obituary with confusionWetWired read his obituary with confusionWetWired read his obituary with confusionWetWired read his obituary with confusion
 
 
WetWired
 



 
Reply
Posted 2003-11-19, 12:07 PM in reply to Hades-Knight's post "Need help"
Thx bob, im changign the printfs to couts and scanf to cin, does anybody know how i can make it double the roman?


if i do :


cout << roman * 2 << end;

that will output the double but in arabic form?

Last edited by Hades-Knight; 2003-11-19 at 12:12 PM.
Old
Profile PM WWW Search
Hades-Knight is neither ape nor machine; has so far settled for the in-between
 
 
Hades-Knight
 



 
Reply
Posted 2003-11-19, 12:13 PM in reply to Hades-Knight's post "Need help"
*ERROR*

I messed up on one part. I'm editing it now, but just FYI don't use that code yet.

**edit** Nevermind. I was thinking that... nevermind what I was thinking. It's fine as is.

Anywho, I'm almost done with the printing out the value in Roman. I'm just coding it so that instead of IIII it'll do IV.
D3V said:
This message is hidden because D3V is on your ignore list.
What is it they say about silence being golden?

Last edited by Medieval Bob; 2003-11-19 at 12:17 PM.
Old
Profile PM WWW Search
Medieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzMedieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Medieval Bob
 



 
Reply
Posted 2003-11-19, 12:20 PM in reply to Hades-Knight's post "Need help"
*Double post for completion*

Done

Code:
--------------------------------------------------------------
#include<stdio.h>

int main(void)
{
int i, previous = 0, num, total = 0, flag = 0, key = 0, error = 0, number, repeat = 1;
char roman[50], roman2[50], pchar = 'z';

printf("Enter the Roman numeral: ");
scanf("%s",roman);

for (i = 0; i < 50; i++)
{

if (roman[i] == 'M')
num = 1000;
else if (roman[i] == 'D')
num = 500;
else if (roman[i] == 'C')
num = 100;
else if (roman[i] == 'L')
num = 50;
else if (roman[i] == 'X')
num = 10;
else if (roman[i] == 'V')
num = 5;
else if (roman[i] == 'I')
num = 1;
else if (roman[i] == '\0')
flag = 1;
else
error = 1;

if (num == previous)
repeat = repeat + 1;
else
repeat = 1;

if (repeat == 4 && num == 1)
{
error = 1;
flag = 1;
}
if (repeat == 2 && num == 5)
{
error = 1;
flag = 1;
}
if (repeat == 4 && num == 10)
{
error = 1;
flag = 1;
}
if (repeat == 2 && num == 50)
{
error = 1;
flag = 1;
}
if (repeat == 4 && num == 100)
{
error = 1;
flag = 1;
}
if (repeat == 2 && num == 500)
{
error = 1;
flag = 1;
}

if (flag == 0)
{
if (num == 0)
error = 1;

if (num > previous)
{
if (key == 1)
{
number = i;
i = 50;
}
total = (total - (2 * previous)) + num;
key = 1;
}
else
total += num;

previous = num;
num = 0;
}
else
{
number = i - 1;
i = 50;
}

}

if (roman[number + 1] != '\0')
error = 1;

if (error == 1)
printf("Error in input.\n");
else
printf("%d\n",total);

total = total * 2;
repeat = 1;

for (i = 0; i < 50; i++)
{

if (total >= 1)
roman2[i] = 'I';
if (total >= 5)
roman2[i] = 'V';
if (total >= 10)
roman2[i] = 'X';
if (total >= 50)
roman2[i] = 'L';
if (total >= 100)
roman2[i] = 'C';
if (total >= 500)
roman2[i] = 'D';
if (total >= 1000)
roman2[i] = 'M';
if (total == 0)
roman2[i] = '\0';

if (roman2[i] == pchar)
repeat = repeat + 1;
else
repeat = 1;

if (repeat == 4 && roman2[i] == 'I')
{
roman2[i - 2] = 'V';
roman2[i - 1] = '\0';
}

if (roman2[i] == 'M')
total -= 1000;
else if (roman2[i] == 'D')
total -= 500;
else if (roman2[i] == 'C')
total -= 100;
else if (roman2[i] == 'L')
total -= 50;
else if (roman2[i] == 'X')
total -= 10;
else if (roman2[i] == 'V')
total -= 5;
else if (roman2[i] == 'I')
total -= 1;
else if (roman2[i] == '\0')
i = 50;

pchar = roman2[i];
}

if (error != 1)
printf("%1s\n",roman2);

return(0);
}
D3V said:
This message is hidden because D3V is on your ignore list.
What is it they say about silence being golden?

Last edited by Medieval Bob; 2003-11-19 at 08:09 PM.
Old
Profile PM WWW Search
Medieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzMedieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Medieval Bob
 



 
Reply
Posted 2003-11-19, 12:56 PM in reply to Hades-Knight's post "Need help"
some questions...whats this?

"%1s\n" "%s" ???

im just gonna take off the printfs and scanf and change for cout << cin >> but will it work if remove those things?

btw can you edit it so it doesnt matter if there are errors? my teacher will know that I got help coz all the if 0 output error in input

Last edited by Hades-Knight; 2003-11-19 at 01:00 PM.
Old
Profile PM WWW Search
Hades-Knight is neither ape nor machine; has so far settled for the in-between
 
 
Hades-Knight
 



 
Reply
Posted 2003-11-19, 02:34 PM in reply to Hades-Knight's post "Need help"
sorry for any bugs...thnx 4 fixing it bob

ill do the doubling one 2day and i promise ill compile it and make sure it works

Last edited by Demosthenes; 2003-11-19 at 02:36 PM.
Old
Profile PM WWW Search
Demosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to be
 
Demosthenes
 



 
Reply
Posted 2003-11-19, 02:52 PM in reply to Hades-Knight's post "Need help"
Quote:
some questions...whats this?

"%1s\n" "%s" ???
Those indicate that the variable being passed is a char* to a null-terminated string.

Fight the system. Refuse to use cin and cout. Real life doesn't have cin and cout, so why the fuck do they teach these mangled POS classes (definition of object type classes, that is) in schools?
Old
Profile PM WWW Search
WetWired read his obituary with confusionWetWired read his obituary with confusionWetWired read his obituary with confusionWetWired read his obituary with confusion
 
 
WetWired
 



 
Reply
Posted 2003-11-19, 03:29 PM in reply to Hades-Knight's post "Need help"
Got it to work , thanx all whot ried to help me!!!!!!

btw mjordan, it was only 1 program that wa ssuposed to do all that not 2 =D
Old
Profile PM WWW Search
Hades-Knight is neither ape nor machine; has so far settled for the in-between
 
 
Hades-Knight
 



 
Reply
Posted 2003-11-19, 05:17 PM in reply to Hades-Knight's post "Need help"
well...i didnt feel like thinkin about how to do the 2nd one yesterday...lol...but basically double whatever answer u get in our number system and use the same logic backwards to get it into roman numeral...i dont know if that makes sense b/c of my crap grammar...i guess this is a thread i need grammar in...lol

but if u need me 2 i could do the 2nd part and u could modify ur program 2 do them both

but u could just make it a function and copied and pasted so it would all be one program...o well...glad u got it 2 work

did u just get the 1st part 2 work or both parts?

sorry...i know this is a crappy post but im in a rush...bbl

Last edited by Demosthenes; 2003-11-19 at 05:20 PM.
Old
Profile PM WWW Search
Demosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to be
 
Demosthenes
 



 
Reply
Posted 2003-11-19, 08:03 PM in reply to Hades-Knight's post "Need help"
Um... mjordan, the doubling is included in my program.

Also, the printf("%1s\n",roman) does the following.

It prints the string, roman. However, since the length of the string is undetermined, I simply put a 1 before the call. That means that it will print the first character of the string. If there are any characters following that, it will print them as well.

The 1 can be removed if you like as there is a null character in the string. I had it in initially for testing purposes.
D3V said:
This message is hidden because D3V is on your ignore list.
What is it they say about silence being golden?
Old
Profile PM WWW Search
Medieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzMedieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Medieval Bob
 



 
Reply
Posted 2003-11-19, 08:08 PM in reply to Hades-Knight's post "Need help"
o...i didnt read ur program yet...i thought u just corrected my mistakes but yea hades-knight told me that u did it so sorry...just scratch everything i said in my last post
Old
Profile PM WWW Search
Demosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to be
 
Demosthenes
 



 
Reply
Posted 2003-11-19, 08:10 PM in reply to Hades-Knight's post "Need help"
I made a little change in my program. I had it to where it would still double the number (invallidly) if there was an error in input. It's corrected now.
D3V said:
This message is hidden because D3V is on your ignore list.
What is it they say about silence being golden?
Old
Profile PM WWW Search
Medieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzMedieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Medieval Bob
 



 
Reply
Posted 2003-11-20, 04:54 PM in reply to Hades-Knight's post "Need help"
I meant that if you could rmeove all the error = 1; so it doesnt matter if you do an input error

i removed them but now it doesnt work, hmm


what is the flag=1 for?

Last edited by Hades-Knight; 2003-11-20 at 05:03 PM.
Old
Profile PM WWW Search
Hades-Knight is neither ape nor machine; has so far settled for the in-between
 
 
Hades-Knight
 



 
Reply
Posted 2003-11-20, 05:23 PM in reply to Hades-Knight's post "Need help"
sorry about double post


BOB, do you think you can add comments here and there?
Old
Profile PM WWW Search
Hades-Knight is neither ape nor machine; has so far settled for the in-between
 
 
Hades-Knight
 
 

Bookmarks

« Previous Thread | Next Thread »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules [Forum Rules]
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -6. The time now is 11:40 AM.
'Synthesis 2' vBulletin 3.x styles and 'x79' derivative
by WetWired the Unbound and Chruser
Copyright ©2002-2008 zelaron.com
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.