Last active
November 9, 2015 23:58
-
-
Save tmathmeyer/d9594988618957c7cb35 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> | |
#define l(x) ((((x)>='A')&&((x)<='Z'))?((x)-'A'):((x)-'a')) | |
#define a(x) (argc[1][(x)]) | |
#define lv(x) (values[(x)]) | |
#define before(x, a, b) (fcp[(x)-'a']=(a),scp[(x)-'a']=(b)) | |
#define value(c, v) (values[(c)-'a']=(v)) | |
int main(int argv, char **argc) { | |
if (argv !=2) return puts("usage: ./roman 'MMXV'"),0; | |
int result=0,nmf=0, count=0, c=0, max=0, cbbd=99999999; | |
int fcp[26]={0}, scp[26]={0}, values[26]={0}; | |
/* FILL IN PRECEDENCE HERE */ | |
/* USE LOWERCASE */ | |
before('i', 'v', 'x'); // i can precede v or X | |
before('x', 'l', 'c'); // x can precede l or c | |
before('c', 'd', 'm'); // c can precede m or m | |
/* FILL IN VALUE HERE */ | |
value('i', 1); | |
value('v', 5); | |
value('x', 10); | |
value('l', 50); | |
value('c', 100); | |
value('d', 500); | |
value('m', 1000); | |
max = ('m'); | |
while(a(c)) { | |
if(lv(l(a(c))) > cbbd) | |
return printf("error invalid char: %c\n", a(c)), 1; | |
int v = lv(l(a(c))); | |
if(!a(c+1) || (v >= lv(l(a(c+1))))) result += (v); | |
else if(((fcp[l(a(c))]==l(a(c+1))+'a')||(scp[l(a(c))]==l(a(c+1))+'a')) | |
&& (v < lv(l(a(c+1))))) { | |
result += (lv(l(a(c+1))) - v); | |
if(lv(l(a(c+1))) > cbbd) | |
return printf("error invalid char: %c\n", a(c+1)), 1; | |
cbbd=(cbbd>(v-1)?v-1:cbbd); | |
if(c>0 && !fcp[l(a(c+1))] && l(argc[1][c-1]) == l(a(c+1))) | |
return printf("bad sequence (%c%c%c)\n",a(c+1),a(c),a(c+1)),1; | |
c++; | |
if (nmf==l(a(c)))count--; | |
} | |
else return printf("error: %c before %c\n", a(c), a(c+1)), 1; | |
if(nmf==l(a(c))) { | |
if(l(a(c))!=l(max) && count == (fcp[l(a(c))]?3:1)) | |
return printf("too many ('%c':%i)\n",l(a(c))+'A',count+1),1; | |
} else nmf=l(a(c)),count=0; | |
if(a(c))c++; | |
++count,cbbd=cbbd>v?v:cbbd; | |
} | |
printf("%i\n", result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment