-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumconv2.c
281 lines (248 loc) · 4.28 KB
/
numconv2.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* numconv2.c
* Converts Roman numerals to Arabic, and vice versa
* Jamie Schlessinger
* August 13, 2012
*/
#include <stdio.h>
void arabtoroman(void);
void romantoarab(void);
int main(void)
{
char entry;
char cc;
system("clear");
printf("\n******************************\n");
printf("Welcome to Numeral Conversion!\n");
do
{
printf("Please make a selection from the following:\n");
printf("1) Convert Roman numerals to Arabic numerals\n");
printf("2) Convert Arabic numerals to Roman numerals\n");
printf("X) Exit the program\n");
printf("Your selection: ");
switch (entry = getchar())
{
case '1':
while (1)
{
cc = getchar();
if (cc == '\n')
break;
}
romantoarab();
break;
case '2':
while (1)
{
cc = getchar();
if (cc == '\n')
break;
}
arabtoroman();
break;
case 'X':
getchar();
printf("Goodbye!\n");
return 0;
case 'x':
getchar();
printf("Goodbye!\n");
return 0;
default:
while (1)
{
cc = getchar();
if (cc == '\n')
break;
}
printf("Invalid menu choice. Please enter 1, 2, or X.\n");
system("sleep 1");
}
} while (entry != 'X');
return 0;
}
/* arabtoroman.c -- converts Arabic numerals to Roman numerals */
void arabtoroman(void)
{
int xx, ii;
printf("Enter a positive integer no greater than 10000: ");
scanf("%d", &xx);
printf("\n");
if (xx > 10000)
{
printf("Error: number too large!\n");
getchar();
system("sleep 1");
return;
}
system("sleep 0.5");
printf("Your number in Roman numerals is: ");
while (xx >= 1000)
{
printf("M");
xx -= 1000;
}
if (xx >= 900)
{
printf("CM");
xx -= 900;
}
if (xx >= 500)
{
printf("D");
xx -= 500;
}
if (xx >= 400)
{
printf("CD");
xx -= 400;
}
while (xx >= 100)
{
printf("C");
xx -= 100;
}
if (xx >= 90)
{
printf("XC");
xx -= 90;
}
if (xx >= 50)
{
printf("L");
xx -= 50;
}
if (xx >= 40)
{
printf("XL");
xx -= 40;
}
while (xx >= 10)
{
printf("X");
xx -= 10;
}
if (xx >= 9)
{
printf("IX");
xx -= 9;
}
if (xx >= 5)
{
printf("V");
xx -= 5;
}
if (xx >= 4)
{
printf("IV");
xx -= 4;
}
while (xx > 0)
{
printf("I");
xx -= 1;
}
getchar();
printf("\n\n");
system("sleep 0.5");
}
/* romantoarab.c -- converts Roman numerals to Arabic */
#include <stdio.h>
void romantoarab(void)
{
char roman[40];
char *proman;
int ii;
int count = 0;
proman = roman;
printf("Enter a number in Roman numerals: ");
scanf("%s", roman);
for (; *proman; proman++)
{
*proman = toupper(*proman);
if (*proman != 'M' && *proman != 'D' && *proman != 'C' &&
*proman != 'L' && *proman != 'X' && *proman != 'V' &&
*proman != 'I')
{
printf("Error: entry was not a valid Roman numeral!\n");
printf("Entry must consist of M, D, C, L, X, V, and I only.\n");
return;
}
}
for (proman = roman; *proman; proman++)
{
if (*proman == 'M')
{
count += 1000;
}
else if (*proman == 'D')
{
count += 500;
}
else if (*proman == 'C')
{
if (*(proman+1) == 'M')
{
count += 900;
proman++;
}
else if (*(proman+1) == 'D')
{
count += 400;
proman++;
}
else
{
count += 100;
}
}
else if (*proman == 'L')
{
count += 50;
}
else if (*proman == 'X')
{
if (*(proman+1) == 'C')
{
count += 90;
proman++;
}
else if (*(proman+1) == 'L')
{
count += 40;
proman++;
}
else
{
count += 10;
}
}
else if (*proman == 'V')
{
count += 5;
}
else
{
if (*(proman+1) == 'X')
{
count += 9;
proman++;
}
else if (*(proman+1) == 'V')
{
count += 4;
proman++;
}
else
{
count += 1;
}
}
}
getchar();
printf("\n");
system("sleep 0.5");
printf("Your entry, converted to Arabic numerals, is %d.\n\n", count);
system("sleep 0.5");
}