commit b0f8afaf7a629e1b8072dd6d38deffa452bfa61b
parent 9feb7702581b6a7edbec3fa0dd1e4765ffc21359
Author: Martin J. Klockner <martin.cachari@gmail.com>
Date: Wed, 18 Nov 2020 00:01:37 -0300
Added ej8.c, ej8_modular.c, ej9_modular.c; modular because in the main program the
code is separated into functions.
Diffstat:
3 files changed, 129 insertions(+), 0 deletions(-)
diff --git a/95.11/guia03/ej8.c b/95.11/guia03/ej8.c
@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include <string.h>
+
+size_t str_len(const char *str) {
+ size_t i;
+ for(i = 0; str[i] != '\0'; i++) {}
+
+ return i;
+}
+
+int str_cmp(const char *str1, const char *str2);
+
+int main(void) {
+
+ char s1[] = "ABC";
+ char s2[] = "ABC";
+
+ size_t s1_len, s2_len;
+ int i, j, cmp;
+
+ s1_len = str_len(s1);
+ s2_len = str_len(s2);
+
+ j = (s1_len > s2_len) ? s1_len : s2_len;
+
+ for(i = 0, cmp = 0; i < j; i++) {
+ if(s1[i] < s2[i]) {
+ cmp -= (s2[i] - s1[i]);
+ } else
+ if (s1[i] > s2[i]) {
+ cmp += (s1[i] - s2[i]);
+ }
+ }
+ printf("%d\n", cmp);
+ return 0;
+}
diff --git a/95.11/guia03/ej8_modular.c b/95.11/guia03/ej8_modular.c
@@ -0,0 +1,46 @@
+#include <stdio.h>
+
+size_t str_len(const char *str) {
+ size_t i;
+ for(i = 0; str[i] != '\0'; i++) {}
+
+ return i;
+}
+
+int str_cmp(const char *str1, const char *str2) {
+ if(str1 == NULL || str2 == NULL)
+ return 1;
+
+ char aux1, aux2;
+ int i, j, cmp, str1_len, str2_len;
+ str1_len = str_len(str1);
+ str2_len = str_len(str2);
+
+ j = (str1_len > str2_len) ? str1_len : str2_len;
+
+ for(i = 0, cmp = 0; i < j; i++) {
+ aux1 = str1[i];
+ aux2 = str2[i];
+ if(aux1 < aux2) {
+ cmp -= (aux2 - aux1);
+ } else
+ if (aux1 > aux2) {
+ cmp += (aux1 - aux2);
+ }
+ }
+
+ return cmp;
+}
+
+
+int main(void) {
+
+ char s1[] = "AB ";
+ char s2[] = "ABC";
+
+ int i;
+ i = str_cmp(s1, s2);
+
+ printf("%d\n", i);
+ return 0;
+}
diff --git a/95.11/guia03/ej9_modular.c b/95.11/guia03/ej9_modular.c
@@ -0,0 +1,47 @@
+#include <stdio.h>
+
+size_t str_len(const char *str) {
+ size_t i;
+ for(i = 0; str[i] != '\0'; i++) {}
+
+ return i;
+}
+
+int str_cmp(const char *str1, const char *str2, size_t n) {
+ if(str1 == NULL || str2 == NULL)
+ return 1;
+
+ char aux1, aux2;
+ int i, j, cmp, str1_len, str2_len;
+ str1_len = str_len(str1);
+ str2_len = str_len(str2);
+
+ if(n > str1_len || n > str2_len)
+ return 1;
+
+ for(i = 0, cmp = 0; i <= n; i++) {
+ aux1 = str1[i];
+ aux2 = str2[i];
+ if(aux1 < aux2) {
+ cmp -= (aux2 - aux1);
+ } else
+ if (aux1 > aux2) {
+ cmp += (aux1 - aux2);
+ }
+ }
+
+ return cmp;
+}
+
+
+int main(void) {
+
+ char s1[] = "AB ";
+ char s2[] = "ABC";
+
+ int i;
+ i = str_cmp(s1, s2, 4);
+
+ printf("%d\n", i);
+ return 0;
+}