Nadesłany przez Tomasz Lubiński, 09 lutego 2007 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
rzymskie.c:
//Konwersja liczby arabskie <-> rzymskie
//www.algorytm.org
//(c)2006 Tomasz Lubinski
#include "stdio.h"
#define ROMAN_N 7
unsigned int arabic[ROMAN_N] = {1000, 500, 100, 50, 10, 5, 1};
char roman[ROMAN_N] = {'M', 'D', 'C', 'L', 'X', 'V', 'I'};
// Converts arabic <number> to roman <result>
// Returns <result> or NULL, if an ERROR occurs.
char *arabic2roman(char *result, int number)
{
int i = 0, //position in arabic and roman arrays
j = 0; //position in result
if ((number > 3999) || (number <= 0))
{
return NULL;
}
while ((number) && (i < ROMAN_N))
{
if(number >= arabic[i])
{
number -= arabic[i];
result[j++] = roman[i];
}
else if ((i%2 == 0) &&
(i<ROMAN_N-2) && // 9xx condition
(number >= arabic[i] - arabic[i+2]) &&
(arabic[i+2] != arabic[i] - arabic[i+2]))
{
number -= arabic[i] - arabic[i+2];
result[j++] = roman[i+2];
result[j++] = roman[i];
i++;
}
else if ((i%2 == 1) &&
(i<ROMAN_N-1) && //4xx condition
(number >= arabic[i] - arabic[i+1]) &&
(arabic[i+1] != arabic[i] - arabic[i+1]))
{
number -= arabic[i] - arabic[i+1];
result[j++] = roman[i+1];
result[j++] = roman[i];
i++;
}
else
{
i++;
}
}
result[j++] = 0;
return result;
}
// Converts roman <number> to arabic
// Returns <result> or -1, if an ERROR occurs.
int roman2arabic(char *number)
{
int i = 0, //position in arabic and roman arrays
j = 0, //position in number
result = 0,
length = 0;
length = strlen(number);
while ((j<length) && (i<ROMAN_N))
{
if(number[j] == roman[i])
{
result += arabic[i];
j++;
}
else if ((i%2 == 0) &&
(i<ROMAN_N-2) && // 9xx condition
(j<length-1) &&
(number[j] == roman[i+2]) &&
(number[j+1] == roman[i]))
{
result += arabic[i] - arabic[i+2];
j += 2;
i++;
}
else if ((i%2 == 1) &&
(i<ROMAN_N-1) && //4xx condition
(j<length-1) &&
(number[j] == roman[i+1]) &&
(number[j+1] == roman[i]))
{
result += arabic[i] - arabic[i+1];
j += 2;
i++;
}
else
{
i++;
}
}
//there was an error during conversion
if (i == ROMAN_N)
{
result = -1;
}
return result;
}
int main(void)
{
char roman[50];
arabic2roman(roman, 1981);
printf("%d = %s\n", roman2arabic(roman), roman);
arabic2roman(roman, 1);
printf("%d = %s\n", roman2arabic(roman), roman);
arabic2roman(roman, 3);
printf("%d = %s\n", roman2arabic(roman), roman);
arabic2roman(roman, 4);
printf("%d = %s\n", roman2arabic(roman), roman);
arabic2roman(roman, 5);
printf("%d = %s\n", roman2arabic(roman), roman);
arabic2roman(roman, 6);
printf("%d = %s\n", roman2arabic(roman), roman);
arabic2roman(roman, 45);
printf("%d = %s\n", roman2arabic(roman), roman);
arabic2roman(roman, 68);
printf("%d = %s\n", roman2arabic(roman), roman);
return 0;
}


arabic[i+2] != arabic - arabic[i+2]
co to ma niby sprawdzać?