From 9cabdb383f18e0e1b8dd0d66f19aa62cf498899b Mon Sep 17 00:00:00 2001 From: Joursoir Date: Thu, 4 Feb 2021 13:07:46 +0000 Subject: roman -> arabic --- .gitignore | 2 ++ snc.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ snc.h | 14 +++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 .gitignore create mode 100644 snc.c create mode 100644 snc.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..51fd697 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +snc +*.o diff --git a/snc.c b/snc.c new file mode 100644 index 0000000..723e64f --- /dev/null +++ b/snc.c @@ -0,0 +1,69 @@ +#include + +#include "snc.h" + +int getRomanValue(char symbol) +{ + switch(symbol) { + case 'I': return SNC_ROMAN_I; // have triple notation + case 'V': return SNC_ROMAN_V; + case 'X': return SNC_ROMAN_X; // have triple notation + case 'L': return SNC_ROMAN_L; + case 'C': return SNC_ROMAN_C; // have triple notation + case 'D': return SNC_ROMAN_D; + case 'M': return SNC_ROMAN_M; // have triple notation + } + return 0; +} + +int romanToArabic(char *str) +{ + int answer = 0; + int last_value = 0; + int amount_char = 0; + while(*str) + { + int cur_value = getRomanValue(*str); + if(cur_value == 0) + return -1; + else if(last_value == cur_value || last_value == 0) { + answer += cur_value; + amount_char++; + } + else if(last_value > cur_value) { + answer += cur_value; + amount_char = 1; + } + else if(last_value < cur_value) { + answer += cur_value - 2 * last_value; + if(amount_char > 1) return -1; + amount_char = 1; + } + last_value = cur_value; + + if(amount_char > 3) + return -1; + else if(cur_value == SNC_ROMAN_V || + cur_value == SNC_ROMAN_L || cur_value == SNC_ROMAN_D) { + if(amount_char > 1) return -1; + } + + str++; + } + + return answer; +} + +int main(int argc, char *argv[]) +{ + if(argc < 2) + return printf("Simple numbers converter\n" + "\n" + "Synopsis: snc [number]\n"); + + int answer = romanToArabic(argv[1]); + if(answer == -1) printf("ERROR! %s is not roman number\n", argv[1]); + else printf("answer: %d\n", answer); + + return 0; +} \ No newline at end of file diff --git a/snc.h b/snc.h new file mode 100644 index 0000000..e84ca5c --- /dev/null +++ b/snc.h @@ -0,0 +1,14 @@ +#ifndef SNC_NUMBERS_H +#define SNC_NUMBERS_H + +enum { + SNC_ROMAN_I = 1, + SNC_ROMAN_V = 5, + SNC_ROMAN_X = 10, + SNC_ROMAN_L = 50, + SNC_ROMAN_C = 100, + SNC_ROMAN_D = 500, + SNC_ROMAN_M = 1000 +}; + +#endif /* SNC_NUMBERS_H */ -- cgit v1.2.3-18-g5258