-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.c
71 lines (67 loc) · 1.36 KB
/
12.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <string.h>
char * intToRoman(int num)
{
int j, p = num;
char one[10];
char ten[10];
char hun[10];
/*char thu[20];*/
char *thu = (char *)malloc(20 * sizeof(char));
j = devi (&p, 1000);
for (int i = 0; i < j; i ++)
{
thu[i] = 'M';
}
thu[j] = '\0';
j = devi (&p, 100);
msat (j, hun, 'M', 'D', 'C');
strcat (thu, hun);
j = devi (&p, 10);
msat (j, ten, 'C', 'L', 'X');
strcat (thu, ten);
j = devi (&p, 1);
msat (j, one, 'X', 'V', 'I');
strcat (thu, one);
return thu;
}
int devi (int * p, int mun)//除数和被除数
{
int j, temp = *p;
*p = *p % mun;
j = temp / mun;
return j;
}
void msat (int j, char * ar, char x, char d, char c)
{
switch (j)
{
case 9:
ar[0] = c;
ar[1] = x;
ar[2] = '\0';
break;
case 8:
case 7:
case 6:
case 5:
ar[0] = d;
for (int i = 0; i < j - 5; i ++)
{
ar[i + 1] = c;
}
ar[j - 4] = '\0';
break;
case 4:
ar[0] = c;
ar[1] = d;
ar[2] = '\0';
break;
default:
for (int i = 0; i < j; i ++)
{
ar[i] = c;
}
ar[j] = '\0';
break;
}
}