Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Exams / Exam2 / prob3.cpp
diff options
context:
space:
mode:
Commit3d7b742eb8e9012e21a0516ad21fc7aefd043bf7 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-01-11 10:09:44 +0200
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-01-11 10:09:44 +0200
Filesda790d25fcfa3251a8f57ee531c6a4fddd64804e /Year 1/Programming introduction/Exams/Exam2/prob3.cpp
uni-projects-3d7b742eb8e9012e21a0516ad21fc7aefd043bf7.tar
uni-projects-3d7b742eb8e9012e21a0516ad21fc7aefd043bf7.tar.gz
uni-projects-3d7b742eb8e9012e21a0516ad21fc7aefd043bf7.zip
Parenta6fd917db5d633aecfe0307b016108e78dd2f4f7 (diff)
Added my work from todays up exam
1 files changed, 41 insertions, 0 deletions
-rw-r--r--Year 1/Programming introduction/Exams/Exam2/prob3.cpp41
diff --git a/Year 1/Programming introduction/Exams/Exam2/prob3.cpp b/Year 1/Programming introduction/Exams/Exam2/prob3.cpp
new file mode 100644
index 0000000..8f1a85a
--- /dev/null
+++ b/Year 1/Programming introduction/Exams/Exam2/prob3.cpp
@@ -0,0 +1,41 @@
+#include <iostream>
+
+int getLetterValue(char letter, int n, char *letters, int *values) {
+ for (int i = 0; i < n; i++) {
+ if (letters[i] == letter)
+ return values[i];
+ }
+ return 0;
+}
+
+int calculate(char *str, int n, char *letters, int *values) {
+ if (str[1] == '\0')
+ return getLetterValue(str[0], n, letters, values);
+
+ int sum = 0, prod = getLetterValue(str[0], n, letters, values);
+
+ for (int i = 1; str[i] != '\0'; i++) {
+ if (str[i] == '+') {
+ sum += prod;
+ prod = 1;
+ }
+ else if (str[i] != '.')
+ prod *= getLetterValue(str[i], n, letters, values);
+ }
+
+ sum += prod;
+
+ return sum;
+}
+
+int main() {
+ char str[] = "a.b+b.c+d+a.c";
+ char letters[] = { 'a', 'b', 'c', 'd' };
+ int values[] = { 2 , 5 , 3 , 42 };
+ std::cout << calculate(str, 4, letters, values) << std::endl;;
+
+ char str1[] = "h.e.l.l.o+w.o.r.l.d";
+ char letters1[] = { 'd', 'e', 'h', 'l', 'o', 'r' };
+ int values1[] = { 3 , 2 , 3 , 1 , 5 , 10 };
+ std::cout << calculate(str1, 6, letters1, values1) << std::endl;
+}