Ian Clarke:
> I would advocate that people start to email each other with random data,
> just for the hell of it.
> 
> Allow me to begin, these are hex numbers generated by radioactive
> decay... of course, if they were encrypted plans for the destruction of 
> the world - nobody would know... :

LXXII CI CXIV CI XXXII XCVII CXIV CI XXXII XCVII XXXII XCIX CXI CXVII
CXII CVIII CI XXXII XCVII C CXVIII XCVII CX XCIX CI C XXXII CXV CXVI
CI CIII XCVII CX CXI CIII CXIV XCVII CXII CIV CV XCIX XXXII CXII CXIV
CXI CIII CXIV XCVII CIX CXV XXXII CXVI CXI XXXII CIV CI CVIII CXII XLVI
XXXII LXXXIV CIV CI CXXI X CIV XCVII CXVIII CI CX XXXIX CXVI XXXII XCVIII
CI CI CX XXXII XCIX CXIV XCVII XCIX CVII CI C XXXII CXXI CI CXVI XLIV
XXXII CXV CXI XXXII CXVI CIV CI CXXI XXXII CIX CXVII CXV CXVI XXXII XCVIII
CI XXXII CIII CXI CXI C XLVI X

gcc -Wall -O2 -DROMANIZE -o romanize romanize.c
gcc -Wall -O2 -o deromanize romanize.c
echo "Hello, Mr. Bin Laden." | ./romanize | ./deromanize

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

struct {
        char symbol;
        int size;
} units[] = {
        {'M', 1000},
        {'D',  500},
        {'C',  100},
        {'L',   50},
        {'X',   10},
        {'V',    5},
        {'I',    1},
        {}
};

#ifdef ROMANIZE

static void romanize(unsigned int n, char *s)
{
        int i;
        char *p = s;
        for (i = 0; units[i].symbol; i++) {
                int j, x = n / units[i].size;
                memset(p, units[i].symbol, x);
                p += x;
                n -= x * units[i].size;
                j = i + 1;
                if (units[j].symbol && (units[j].size == 500
                                || units[j].size == 50 || units[j].size == 5))
                        j++;
                if (units[j].symbol && n >= units[i].size - units[j].size
                                && units[j].size * 10 >= units[i].size) {
                        *p++ = units[j].symbol;
                        *p++ = units[i].symbol;
                        n -= units[i].size - units[j].size;
                }
        }
        *p = 0;
}

int main(int argc, char **argv)
{
        int c, n = 0;
        char b[1024];
        while ((c = getchar()) != EOF) {
                char t;
                romanize(c, b);
                if ((n += strlen(b) + 1) > 68)
                        t = '\n', n = 0;
                else t = ' ';
                fputs(b, stdout), putchar(t);
        }
        return 0;
}

#else

static inline int num_size(char c)
{
        int i;
        for (i = 0; units[i].symbol; i++)
                if (units[i].symbol == c)
                        return units[i].size;
        return -1;
}

static int deromanize(char *s)
{
        int ret = 0, i, l = strlen(s);
        for (i = 0; i < l; i++) {
                int tmp, n = num_size(s[i]);
                if (n < 0)
                        return -1;
                if (i+1 < l && (tmp = num_size(s[i+1])) > n)
                        ret += tmp - n, i++;
                else
                        ret += n;
        }
        return ret;
}

int main(int argc, char **argv)
{
        int c, i = 0;
        char b[32];
        while ((c = getchar()) != EOF) {
                b[i++] = c;
                if (i == sizeof(b))
                        goto err;
                if (c == ' ' || c == '\n' || c == '\t') {
                        int n;
                        b[i-1] = 0;
                        n = deromanize(b);
                        if (n < 0)
                                goto err;
                        putchar(n);
                        i = 0;
                }
        }
        return 0;
err:    b[sizeof(b)-1] = 0;
        fprintf(stderr, "Invalid roman numeral: %s\n", b);
        return 1;
}

#endif

_______________________________________________
Chat mailing list
[EMAIL PROTECTED]
http://lists.freenetproject.org/mailman/listinfo/chat

Reply via email to