summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-02-04 13:07:46 +0000
committerJoursoir <chat@joursoir.net>2021-02-04 13:07:46 +0000
commit9cabdb383f18e0e1b8dd0d66f19aa62cf498899b (patch)
treeab4a8438907f5a4081fcc4c09a45e523f7eb8e2e
downloadsnc-9cabdb383f18e0e1b8dd0d66f19aa62cf498899b.tar.gz
snc-9cabdb383f18e0e1b8dd0d66f19aa62cf498899b.tar.bz2
snc-9cabdb383f18e0e1b8dd0d66f19aa62cf498899b.zip
roman -> arabic
-rw-r--r--.gitignore2
-rw-r--r--snc.c69
-rw-r--r--snc.h14
3 files changed, 85 insertions, 0 deletions
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 <stdio.h>
+
+#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 */