[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Roman numerals

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 10
Thread images: 2

File: fudge_phantom.jpg (38KB, 405x405px) Image search: [Google]
fudge_phantom.jpg
38KB, 405x405px
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
>>
File: IntToRoman.png (43KB, 807x417px) Image search: [Google]
IntToRoman.png
43KB, 807x417px
>>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.
Thread posts: 10
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.