Quick! Make a program that converts a decimal number to roman numerals!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 16
const int number [7] = { 1, 5, 10, 50, 100, 500, 1000};
const char *numeral[7] = {"I", "V", "X", "L", "C", "D", "M" };
void conv(char *output, int input)
{
int i, j, _temp;
for(i = 6; i >= 0; i -= 2)
{
_temp = input / number[i];
if(_temp == 4 || _temp == 9)
{
strcat(output, numeral[i ]);
strcat(output, numeral[i + 2]);
}
else if(_temp >= 5)
{
strcat(output, numeral[i + 1]);
_temp -= 5;
goto loop;
}
else
{
loop:
for(j = 0; j < _temp; ++j)
{
strcat(output, numeral[i]);
}
}
input %= number[i];
}
}
int main(int argc, char *argv[])
{
char *output = calloc(BUFSIZE, sizeof(char));
int input = atoi(argv[1]);
conv(output, input);
printf("%s\n", output);
return(0);
}
>>57106003
Git gud, mate.#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const static struct { int n; char *str; } numerals[] = {
{ 1000, "M" },
{ 900, "CM" },
{ 500, "D" },
{ 400, "CD" },
{ 100, "C" },
{ 90, "XC" },
{ 50, "L" },
{ 40, "XL" },
{ 10, "X" },
{ 9, "IX" },
{ 5, "V" },
{ 4, "IV" },
{ 1, "I" },
};
void to_roman_numerals(int n, size_t outlen, char out[static outlen])
{
for (int i = 0; n; ++i) {
size_t len = strlen(numerals[i].str);
for (; n >= numerals[i].n; n -= numerals[i].n) {
if (outlen < len + 1)
return;
strcpy(out, numerals[i].str);
out += len;
outlen -= len;
}
}
}
int main(int argc, char *argv[])
{
if (argc != 2)
return 1;
char out[50];
to_roman_numerals(atoi(argv[1]), sizeof out, out);
puts(out);
}
>>57106478
Elegant.
>>57106003
What motivation do I have if a bird isn't going to stab me??
>>57108420
You don't get this chocolate
>>57106003
what did I do wrong?
> cc romandecimals.c -o rodec
> ./rodec
> Segmentation fault
>>57106478
oh are you guys using see dager dager or sea pound or something?
>romandecimals2.c:23:5: error: ‘for’ loop initial >declarations are only allowed in C99 mode
>>57109353
>>57109393
since I cant run these, whats a decimal roman numeral anyways, (also cause this post is dubs)
program ToRoman;
uses
StrUtils;
var
n: Integer;
begin
Write('What number would you like to convert to Roman representation? ');
ReadLn(n);
WriteLn(IntToRoman(n));
end.
>his programming language doesn't do this out of the box
http://www.freepascal.org/docs-html/rtl/strutils/inttoroman.html
>>57109798
This is Free Pascal's implementation by the way (GPL-licensed). Can't copy and paste because 4chan seems to think it's spam.