Created
August 1, 2016 19:31
-
-
Save squgeim/e86a1f47ea5d4509cc3aed6a87b8b4d5 to your computer and use it in GitHub Desktop.
This file contains 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<stdlib.h> | |
#include<string.h> | |
#define bool int | |
#define true 1 | |
#define false 0 | |
int get_value(char roman_number) { | |
switch(roman_number) { | |
case 'I': | |
case 'i': | |
return 1; | |
case 'X': | |
case 'x': | |
return 10; | |
case 'C': | |
case 'c': | |
return 100; | |
case 'M': | |
case 'm': | |
return 1000; | |
case 'V': | |
case 'v': | |
return 5; | |
case 'L': | |
case 'l': | |
return 50; | |
case 'D': | |
case 'd': | |
return 500; | |
default: | |
printf("1: Invalid number"); | |
exit(2); | |
} | |
} | |
bool is_aux_num(int number) { | |
switch(number){ | |
case 5: | |
case 50: | |
case 500: | |
return true; | |
default: | |
return false; | |
} | |
} | |
int main(int argc, char *argv[]) { | |
char *roman_value; | |
int i; | |
int current_value; | |
int next_value; | |
int final_value = 0; | |
bool last_op_sub = false; // false == add, true == sub | |
char last_char; | |
int last_char_count; | |
int V_count = 0; | |
int L_count = 0; | |
int D_count = 0; | |
int last_additive_num = -1; | |
int last_sub_num = -1; | |
int last_sub_pos = -1; | |
if(argc != 2) { | |
printf("Invalid usage\n"); | |
printf("Usage: roman roman_numeral\n"); | |
exit(1); | |
} | |
roman_value = argv[1]; | |
// printf("%s\n", roman_value); | |
for(i = 0; i < strlen(roman_value); i++) { | |
if(V_count > 1 || L_count > 1 || D_count > 1) { | |
printf("2: Invalid number"); | |
exit(2); | |
} | |
if(last_char == roman_value[i]) { | |
last_char_count++; | |
if(last_char_count == 4) { | |
printf("3: Invalid number"); | |
exit(2); | |
} | |
} else { | |
last_char = roman_value[i]; | |
switch(last_char) { | |
case 'V': case 'v': | |
V_count++; | |
break; | |
case 'L': case 'l': | |
L_count++; | |
break; | |
case 'D': case 'd': | |
D_count++; | |
break; | |
} | |
last_char_count = 1; | |
} | |
current_value = get_value(roman_value[i]); | |
if(last_additive_num != -1 && current_value > last_additive_num) { | |
printf("4: Invalid number"); | |
exit(2); | |
} | |
if(last_sub_num != -1 && current_value >= last_sub_num && last_sub_pos != i-1) { | |
printf("7: Invalid number"); | |
exit(2); | |
} | |
if(i < strlen(roman_value) - 1) { | |
next_value = get_value(roman_value[i+1]); | |
if(current_value < next_value) { | |
if(last_op_sub == true || is_aux_num(current_value)) { | |
printf("5: Invalid number"); | |
exit(2); | |
} | |
if(current_value < next_value / 10) { | |
printf("6: Invalid number"); | |
exit(2); | |
} | |
final_value -= current_value; | |
last_sub_num = current_value; | |
last_sub_pos = i; | |
last_op_sub = true; | |
} else { | |
final_value += current_value; | |
last_additive_num = current_value; | |
last_op_sub = false; | |
} | |
} else { | |
final_value += current_value; | |
last_additive_num = current_value; | |
last_op_sub = false; | |
} | |
} | |
printf("%d", final_value); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment