c-first-steps

a C playground
Index Commits Files Refs README
commit 1740cfdb3f8fbfbf2ea9392142aa4925e0ed2769
parent f998270e72e814bd147bc23613441adfda654f54
Author: Martin J. Klöckner <martin.cachari@gmail.com>
Date:   Thu, 26 Nov 2020 01:19:10 -0300

Added ex24.c, ex24_imp.c & ex25_imp.c "Improved versions";

Diffstat:
A95.11/guia03/ex24.c | 27+++++++++++++++++++++++++++
A95.11/guia03/ex24_imp.c | 28++++++++++++++++++++++++++++
A95.11/guia03/ex25_imp.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 108 insertions(+), 0 deletions(-)
diff --git a/95.11/guia03/ex24.c b/95.11/guia03/ex24.c
@@ -0,0 +1,27 @@
+//    Reads a number from stdin and prints it 
+//    in hexadecimal base on stout;
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define MAX_LEN 100
+
+int main ( void ) {
+
+    char buffer[MAX_LEN];
+    int num;
+    char num2[MAX_LEN];
+
+    if(fgets(buffer, MAX_LEN, stdin) == NULL)
+        return 1;
+
+    num = atoi(buffer);
+
+//    Converts num to hexadecimal and stores it
+//    on num2 str;
+    sprintf(num2, "%x\n", num);
+
+//    prints the string with the octal number;
+    printf("%s", num2);
+    return 0;
+}
diff --git a/95.11/guia03/ex24_imp.c b/95.11/guia03/ex24_imp.c
@@ -0,0 +1,28 @@
+//    Reads an non-negative integer from stdin and prints it
+//    on stdout in hexadecimal base; 
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define MAX_LEN 100
+#define ERR_MSG_NEG "Err: the number is negative"
+
+int main ( void ) {
+
+    unsigned int num;
+    char buffer[MAX_LEN];
+
+    if(fgets(buffer, MAX_LEN, stdin) == NULL)
+        return 1;
+
+//    Converts the input str to int, if the number
+//    is negative, puts an error message on stderr;
+    if((num = atoi(buffer)) < 0 ) {
+        fprintf(stderr, ERR_MSG_NEG"\n");
+        return 1;
+    }
+    
+//    prints the integer in hexadecimal base;
+    printf("%X\n", num);
+    return 0;
+}
diff --git a/95.11/guia03/ex25_imp.c b/95.11/guia03/ex25_imp.c
@@ -0,0 +1,53 @@
+//    Reads an integer from stdin and prints it
+//    on stdout in binary base.
+//    only for numbers below 128; 
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define MAX_LEN    8 
+#define ERR_MSG_NEG "Err: invalid number!"
+
+int main ( void ) {
+
+    int i, r, num, aux;
+    char buffer[MAX_LEN];
+//    0000 0000
+    int bin[MAX_LEN] = {0,0,0,0,0,0,0,0};
+
+    if(fgets(buffer, MAX_LEN, stdin) == NULL)
+        return 1;
+
+//    Converts the input str to int, if the number
+//    is negative or greater than 128, puts an 
+//    error message on stderr;
+    if((num = atoi(buffer)) < 0 || num > 128) {
+        fprintf(stderr, ERR_MSG_NEG"\n");
+        return 1;
+    }
+
+//    Converts num to binary base by dividing it
+//    by 2 until its zero, the rest of the division
+//    is stored in every itineration in bin[];
+    for(i = 0; num != 0; i++) {
+        if((r = (num % 2)) == 1) {
+            bin[i] = 1;
+        } else {
+            bin[i] = 0;
+        }
+        num = (num / 2);
+    }
+
+//    This part print bin in reverse order because
+//    the previous algorithm stores the values in 
+//    reverse order;
+    for(i = 7; i >= 0; i--) {
+        if(i == 3)
+            putchar(' ');
+
+        printf("%d", bin[i]);
+    }
+
+    putchar('\n');
+    return 0;
+}