Zelaron Gaming Forum

Zelaron Gaming Forum (http://zelaron.com/forum/index.php)
-   Tech Help (http://zelaron.com/forum/forumdisplay.php?f=329)
-   -   Need help (http://zelaron.com/forum/showthread.php?t=24510)

Hades-Knight 2003-11-18 07:54 PM

Need help
 
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

!King_Amazon! 2003-11-18 07:59 PM

I bet mantra could make it in 5 seconds.

Demosthenes 2003-11-18 10:50 PM

#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

WetWired 2003-11-19 10:27 AM

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.

Medieval Bob 2003-11-19 10:35 AM

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.

Medieval Bob 2003-11-19 11:41 AM

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);
}

WetWired 2003-11-19 12:02 PM

Actually, except for the non-initialization thing, it should work because it starts at the end and moves left.

Hades-Knight 2003-11-19 12:07 PM

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?

Medieval Bob 2003-11-19 12:13 PM

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

Medieval Bob 2003-11-19 12:20 PM

*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);
}

Hades-Knight 2003-11-19 12:56 PM

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 :p

Demosthenes 2003-11-19 02:34 PM

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

WetWired 2003-11-19 02:52 PM

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?

Hades-Knight 2003-11-19 03:29 PM

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

Demosthenes 2003-11-19 05:17 PM

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

Medieval Bob 2003-11-19 08:03 PM

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.

Demosthenes 2003-11-19 08:08 PM

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

Medieval Bob 2003-11-19 08:10 PM

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.

Hades-Knight 2003-11-20 04:54 PM

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?

Hades-Knight 2003-11-20 05:23 PM

sorry about double post


BOB, do you think you can add comments here and there?

Medieval Bob 2003-11-20 06:41 PM

Ugh comments suck. Can I do it tomorrow?

Hades-Knight 2003-11-21 04:15 PM

Ok its already 1 day, times up

Medieval Bob 2003-11-21 06:07 PM

Well, I don't feel like doin it now. Also, I'm not your bitch, so... if I decide to do it later, I might.

Hades-Knight 2003-11-23 04:38 PM

puh-lease can you do it now?

Hades-Knight 2003-11-24 06:32 PM

due tomorrow help with comments!!!

Hades-Knight 2003-11-24 09:05 PM

Can you add some comments and edit these to fit the criteria? kthx




#include<stdio.h>
#include <iostream.h>
int main() //Main function
{
int i; //Define variables
int previous = 0;
int num;
int total = 0;
int flag = 0;
int key = 0;
int repeat = 1;
char roman[50]; //defines roman char for input
char roman2[50]; //defines roman char for output
char pchar = 'z';

cout << "Enter the Roman numeral(in caps!!): "; //prompt for user input
cin >>roman;

for (i = 0; i < 50; i++) //count what the input value range is
{

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


/****************************Start Calculation for order of input******************/
/* If input has a value next to it, it checks to see if its more or less than first */
/* If it has less value than the one to the right, substract, if it has higher value add */
if (num == previous)
repeat = repeat + 1;
else
repeat = 1;

if (repeat == 4 && num == 1)
{

flag = 1;
}
if (repeat == 2 && num == 5)
{

flag = 1;
}
if (repeat == 4 && num == 10)
{

flag = 1;
}
if (repeat == 2 && num == 50)
{

flag = 1;
}
if (repeat == 4 && num == 100)
{

flag = 1;
}
if (repeat == 2 && num == 500)
{

flag = 1;
}

if (flag == 0)
{


if (num > previous)
{
if (key == 1)
{

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

previous = num;
num = 0;
}
else
{

i = 50;
}

}


cout<<total << endl; //Output the calculated number given that roman characters are calculated right
/*******************************START DOUBLING****************************************/
total = total * 2; //doubles the number you got from convertion
repeat = 1;

for (i = 0; i < 50; i++)
{ //Given then total in int , assign character to each int value

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') // After getting the range value calculate exact value by substracting the value character to the left
total -= 1000; //and assign character to integer value , convertion.
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];
}

cout<<roman2; //outputs the double value in roman characters

return(0);
}

WetWired 2003-11-24 09:26 PM

You could just do it yourself, you know. Ask someone from your class. And if you can't find anyone in your class who can do it, you can hope to be graded on a curve.

Medieval Bob 2003-11-24 09:32 PM

Don't know what all you want done... How's this?


#include<stdio.h>
#include <iostream.h>
int main() //Main function
{
int i; //Define variables
int previous = 0;
int num;
int total = 0;
int flag = 0;
int key = 0;
int repeat = 1;
char roman[50]; //defines roman char for input
char roman2[50]; //defines roman char for output
char pchar = 'z';

cout << "Enter the Roman numeral(in caps!!): "; //prompt for user input
cin >>roman;

for (i = 0; i < 50; i++) //count what the input value range is
{

if (roman[i] == 'M') //assigns 1000 to input M
num = 1000;
else if (roman[i] == 'D') //assigns 500 to input D
num = 500;
else if (roman[i] == 'C') //assigns 100 to input C
num = 100;
else if (roman[i] == 'L') //assigns 50 to input L
num = 50;
else if (roman[i] == 'X') //assigns 10 to input X
num = 10;
else if (roman[i] == 'V') //assigns 5 to input V
num = 5;
else if (roman[i] == 'I') //assigns 1 to input I
num = 1;
else if (roman[i] == '\0') //this is the end of input

/****************************Start Calculation for order of input******************/
/* If input has a value next to it, it checks to see if its more or less than first */
/* If it has less value than the one to the right, substract, if it has higher value add */
if (num == previous)
repeat = repeat + 1;
else
repeat = 1;

if (repeat == 4 && num == 1)
{

flag = 1;
}
if (repeat == 2 && num == 5)
{

flag = 1;
}
if (repeat == 4 && num == 10)
{

flag = 1;
}
if (repeat == 2 && num == 50)
{

flag = 1;
}
if (repeat == 4 && num == 100)
{

flag = 1;
}
if (repeat == 2 && num == 500)
{

flag = 1;
}

if (flag == 0)
{


if (num > previous)
{
if (key == 1)
{

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

previous = num;
num = 0;
}
else
{

i = 50;
}

}


cout<<total << endl; //Output the calculated number given that roman characters are calculated right
/*******************************START DOUBLING****************************************/
total = total * 2; //doubles the number you got from convertion
repeat = 1;

for (i = 0; i < 50; i++)
{ //Given then total in int , assign character to each int value

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') //I has been repeated too many times
{
roman2[i - 2] = 'V';
roman2[i - 1] = '\0';
}

if (roman2[i] == 'M') // After getting the range value calculate exact value by substracting the value character to the left
total -= 1000; //and assign character to integer value , convertion.
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];
}

cout<<roman2; //outputs the double value in roman characters

return(0);
}


All times are GMT -6. The time now is 09:46 AM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.