/*
第2種情報処理技術者試験 平成6年度・春期・午後 問13
BohYoh Shibata PREPARATION
*/
#include <stdio.h>
#include <string.h>
#define BUFSIZE 80
#define TRUE 1
#define FALSE 0
int rdxcnv(char *, unsigned long *);
int numchk(char *, char *);
main()
{
char buff[BUFSIZE];
int rv = TRUE;
unsigned long mem = 0L;
enum {DEC, HEX, OCT } radix = DEC ;
for (;;) {
fputs(">", stdout);
fgets(buff, BUFSIZE, stdin);
buff[strlen(buff) - 1] = '\0' ; /* 改行文字添削 */
if (strcmp(buff, "END") == 0)
break;
else if (strcmp(buff, "DEC") == 0)
radix = DEC;
else if (strcmp(buff, "HEX") == 0)
radix = HEX;
else if (strcmp(buff, "OCT") == 0)
radix = OCT;
else
rv = rdxcnv(buff, &mem) ;
if (rv == FALSE) {
fputs("EOOR!!!\n", stdout);
rv = TRUE;
} else {
switch (radix) {
case DEC:
fprintf(stdout, "%lu DEC\n", mem);
break;
case HEX:
fprintf(stdout, "%lX HEX\n", mem);
break;
case OCT:
fprintf(stdout, "%lo OCT\n", mem);
break;
}
}
}
}
int rdxcnv(char *buff, unsigned long *memp)
{
int rv;
if (strlen(buff) < 2)
return FALSE ;
switch (buff[0]) {
case 'D':
rv = numchk(buff+1, "0123456789");
if (rv == TRUE)
sscanf( buff + 1 , "%lu", memp );
break;
case 'H':
rv = numchk(buff+1, "0123456789ABCDEF");
if (rv == TRUE)
sscanf( buff + 1 , "%lX", memp );
break;
case 'O':
rv = numchk(buff+1, "01234567");
if (rv == TRUE)
sscanf( buff + 1 , "%lo", memp );
break;
default:
rv = FALSE;
}
return rv;
}
int numchk(char *buff, char *base)
{
int m, n;
for (m = 0; buff[m] != '\0'; m++) {
for (n = 0; base[n] != '\0'; n++)
if (buff[m] == base[n])
break;
if ( base[n] == '\0' )
return FALSE;
}
return TRUE;
}