C :: Aufgabe #82 :: Lösung #2
2 Lösungen
#82
Jahreszahlenkonverter für römische Schreibweise
Anfänger - C
von BlackBird321
- 04.06.2015 um 22:36 Uhr
Bitte schreibe ein Programm, welches eine einzugebende Jahreszahl in eine römische Schreibweise umwandelt.
Beispiel:
1995 = MCMXCV
2015 = MMXV
Beispiel:
1995 = MCMXCV
2015 = MMXV
#2
von Gnom (550 Punkte)
- 04.05.2020 um 20:51 Uhr
#include <stdio.h>
char* ConvertA2R(int a) {
static char buf[20];
char *p = buf;
while (a >= 1000) {*p++ = 'M'; a-=1000;}
if (a >= 900) {*p++ = 'C'; *p++ = 'M'; a-=900;}
if (a >= 500) {*p++ = 'D'; a-=500;}
if (a >= 400) {*p++ = 'C'; *p++ = 'D'; a-=400;}
while (a >= 100) {*p++ = 'C'; a-=100;}
if (a >= 90) {*p++ = 'X'; *p++ = 'C'; a-=90;}
if (a >= 50) {*p++ = 'L'; a-=50;}
if (a >= 40) {*p++ = 'X'; *p++ = 'L'; a-=40;}
while (a >= 10) {*p++ = 'X'; a-=10;}
if (a >= 9) {*p++ = 'I'; *p++ = 'X'; a-=9;}
if (a >= 5) {*p++ = 'V'; a-=5;}
if (a >= 4) {*p++ = 'I'; *p++ = 'V'; a-=4;}
while (a >= 1) {*p++ = 'I'; a-=1;}
return buf;
}
int main(void) {
unsigned int year;
printf("Geben Sie eine Jahreszahl ein: ");
scanf("%u", &year);
printf("Die arabische Zahl %d entspricht der römischen Zahl %s\n\n\n", year, ConvertA2R(year));
return 0;
}Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1
