-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroman.c
More file actions
42 lines (30 loc) · 710 Bytes
/
roman.c
File metadata and controls
42 lines (30 loc) · 710 Bytes
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"cs50.h"
int converter (char x)
{
int result;
switch (x) {
case 'M': {result = 1000;break;}
case 'C': {result = 100;break;}
case 'D': {result = 500;break;}
case 'L': {result = 50;break;}
case 'X': {result = 10;break;}
case 'V': {result = 5;break;}
case 'I': {result = 1;break;}
default:
result = 0;
}
return result;
}
int main (void)
{
string R = GetString();
int len = strlen(R);
int i, result = converter(R[len-1]);
for (i = len-1; i > 0; i--)
result += (converter(R[i]) > converter(R[i-1]) ? -1*converter(R[i-1]) : converter(R[i-1]));
printf("%d", result);
return 0;
}