tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,59 @@
#include <stdlib.h>
#include <stdio.h>
/*
* Writes the Roman numeral representing n into the buffer s.
* Handles up to n = 3999.
* Since C doesn't have exceptions, n = 0 causes the whole program to exit
* unsuccessfully.
* s should be have room for at least 16 characters, including the trailing
* null.
*/
void roman(char *s, unsigned int n)
{
if (n == 0)
{
fputs(stderr, "Roman numeral for zero requested.");
exit(EXIT_FAILURE);
}
#define digit(loop, num, c) \
loop (n >= num) \
{*(s++) = c; \
n -= num;}
#define digits(loop, num, c1, c2) \
loop (n >= num) \
{*(s++) = c1; \
*(s++) = c2; \
n -= num;}
digit ( while, 1000, 'M' )
digits ( if, 900, 'C', 'M' )
digit ( if, 500, 'D' )
digits ( if, 400, 'C', 'D' )
digit ( while, 100, 'C' )
digits ( if, 90, 'X', 'C' )
digit ( if, 50, 'L' )
digits ( if, 40, 'X', 'L' )
digit ( while, 10, 'X' )
digits ( if, 9, 'I', 'X' )
digit ( if, 5, 'V' )
digits ( if, 4, 'I', 'V' )
digit ( while, 1, 'I' )
#undef digit
#undef digits
*s = 0;}
int main(void)
{
char buffer[16];
unsigned int i;
for (i = 1 ; i < 4000 ; ++i)
{
roman(buffer, i);
printf("%4u: %s\n", i, buffer);
}
return EXIT_SUCCESS;
}

View file

@ -0,0 +1,38 @@
char *ToRoman(int num, char *buf, int buflen)
{
static const char romanDgts[] = "ivxlcdmVXLCDM_";
char *roman = buf + buflen;
int rdix, r, v;
*--roman = '\0'; /* null terminate return string */
if (num >= 4000000) {
printf("Number Too Big.\n");
return NULL;
}
for (rdix = 0; rdix < strlen(romanDgts); rdix += 2) {
if (num == 0) break;
v = (num % 10) / 5;
r = num % 5;
num = num / 10;
if (r == 4) {
if (roman < buf+2) {
printf("Buffer too small.");
return NULL;
}
*--roman = romanDgts[rdix+1+v];
*--roman = romanDgts[rdix];
}
else {
if (roman < buf+r+v) {
printf("Buffer too small.");
return NULL;
}
while(r-- > 0) {
*--roman = romanDgts[rdix];
}
if (v==1) {
*--roman = romanDgts[rdix+1];
}
}
}
return roman;
}

View file

@ -0,0 +1,40 @@
#include <stdio.h>
int to_roman(char *out, int n)
{
int len = 0;
if (n <= 0) return 0; /* error indication */
# define RPUT(c) if (out) out[len] = c; len++
while(n>= 1000) { n -= 1000;RPUT('M'); };
if (n >= 900) { n -= 900; RPUT('C'); RPUT('M'); };
if (n >= 500) { n -= 500; RPUT('D'); };
if (n >= 400) { n -= 400; RPUT('C'); RPUT('D'); };
while (n >= 100){ n -= 100; RPUT('C'); };
if (n >= 90) { n -= 90; RPUT('X'); RPUT('C'); };
if (n >= 50) { n -= 50; RPUT('L'); };
if (n >= 40) { n -= 40; RPUT('X'); RPUT('L'); };
while (n >= 10) { n -= 10; RPUT('X'); };
if (n >= 9) { n -= 9; RPUT('I'); RPUT('X'); };
if (n >= 5) { n -= 5; RPUT('V'); };
if (n >= 4) { n -= 4; RPUT('I'); RPUT('V'); };
while (n) { n--; RPUT('I'); };
RPUT('\0');
# undef RPUT
return len;
}
int main()
{
char buf[16];
int d = to_roman(buf, 1666);
printf("roman for 1666 is %d bytes: %s\n", d, buf);
d = 68999123;
printf("%d would have required %d bytes\n", d, to_roman(0, d));
return 0;
}