Last active
December 19, 2015 06:29
-
-
Save KorbenC/5911469 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <windows.h> | |
| #define MAX_ARRAY_SIZE 7 | |
| #define BAD_NUMERAL -1 | |
| void ConvertRToD(); | |
| int GetVal( char Letter, char RomanNum[], int DecNum[] ); | |
| int main ( ) { | |
| int UserChoice = -1; | |
| printf("Welcome to Taylor's and Korben's Roman Numeral Calc\n\n"); | |
| while( UserChoice != 0 ) { | |
| printf("0. Exit\n1. Convert Roman Numeral -> Decimal\n\nChoice: "); | |
| scanf("%d", &UserChoice); | |
| switch( UserChoice ) { | |
| case 0: | |
| break; | |
| case 1: | |
| ConvertRToD(); | |
| break; | |
| default: | |
| printf("\nInvalid choice (%d)\n\n", UserChoice); | |
| break; | |
| } | |
| } | |
| printf("\n"); | |
| system("pause"); | |
| } | |
| void ConvertRToD(){ | |
| // Flush old input buffer, or fgets will be skipped, do to \n's left in the buffer. | |
| // fflush may not work on all compilers | |
| fflush(stdin); | |
| // We will use parrell arrays, not switch :) | |
| int DecimalNum[MAX_ARRAY_SIZE] = { 1, 5, 10, 50, 100, 500, 1000 }; | |
| char RomanNums [MAX_ARRAY_SIZE] = { 'I','V', 'X', 'L', 'C', 'D', 'M' }; | |
| // Vars we will use | |
| int DecimalValue = 0, RomanLen = 0; | |
| // Make the array large enough for even the biggest of RN's | |
| char RomanValue[ 15 ]; | |
| printf("\nPlease enter a roman numeral: "); | |
| // Get the input | |
| fgets( RomanValue, sizeof( RomanValue ), stdin ); | |
| // Get the size of the used array (that has our string in it) | |
| RomanLen = strlen(RomanValue) - 1; | |
| // We want to make sure there is a null term char where our string ends. | |
| // A lack of one would cause unwanted results | |
| RomanValue[RomanLen] = '\0'; | |
| // Begin loop | |
| int i = 0; | |
| for(i; i < RomanLen; i++) | |
| { | |
| // Read chars | |
| char CurrChar = RomanValue[i]; | |
| char NextChar = RomanValue[i + 1]; | |
| // Read Vals | |
| int CurrVal = GetVal( CurrChar, RomanNums, DecimalNum ); | |
| int NextVal = GetVal( NextChar, RomanNums, DecimalNum ); | |
| if( ( CurrVal == BAD_NUMERAL && CurrChar != '\0' ) || ( NextVal == BAD_NUMERAL && NextChar != '\0' ) ) | |
| { | |
| printf("\nInvalid roman numeral, returning to main menu...\n\n"); | |
| return; // Get out of this func, go back to the loop | |
| } | |
| if( NextVal > CurrVal ) | |
| DecimalValue -= CurrVal; | |
| else | |
| DecimalValue += CurrVal; | |
| } | |
| printf("\n%s in decimal is %d\n\n", RomanValue, DecimalValue); | |
| } | |
| int GetVal( char Letter, char RomanNum[], int DecNum[] ) { | |
| int i = 0; | |
| for(i; i < MAX_ARRAY_SIZE; i++ ) { | |
| if( Letter == RomanNum[i] ) | |
| return DecNum[i]; | |
| } | |
| return BAD_NUMERAL; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment