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 |
I bet mantra could make it in 5 seconds.
|
#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 |
Um, from a once over, it appears that it would read XXI as 1...
|
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:
I'll see what I can do with it. Give me a bit. |
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); } |
Actually, except for the non-initialization thing, it should work because it starts at the end and moves left.
|
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? |
*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. |
*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); } |
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 |
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 |
Quote:
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? |
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 |
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 |
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. |
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
|
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.
|
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? |
sorry about double post
BOB, do you think you can add comments here and there? |
All times are GMT -6. The time now is 10:36 AM. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.