feat(src/kr):Save state of source files
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Mon, 10 Jul 2023 10:41:25 +0000 (10:41 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Mon, 10 Jul 2023 10:41:25 +0000 (10:41 +0000)
src/kr_exercises/ch1/s1.6/e1-14-nv..char_freq_nv.c [new file with mode: 0644]
src/kr_exercises/ch1/s1.9/array_test.c [new file with mode: 0644]
src/kr_exercises/ch1/s1.9/compile.sh [new file with mode: 0755]
src/kr_exercises/ch1/s1.9/count.c [new file with mode: 0644]
src/kr_exercises/ch1/s1.9/e1-17..print_gt80.c [new file with mode: 0644]
src/kr_exercises/ch1/s1.9/print_steps.c [new file with mode: 0644]

diff --git a/src/kr_exercises/ch1/s1.6/e1-14-nv..char_freq_nv.c b/src/kr_exercises/ch1/s1.6/e1-14-nv..char_freq_nv.c
new file mode 100644 (file)
index 0000000..a80ddfa
--- /dev/null
@@ -0,0 +1,48 @@
+#include <stdio.h>
+
+/* A program to print the frequency of different UTF-8 characters
+   taking into account the presence of diacritical marks.*/
+
+#define INT_CHAR_MIN   32
+#define INT_CHAR_MAX  126
+#define INT_CHAR_LEN  INT_CHAR_MAX - INT_CHAR_MIN + 1
+#define INT_NEWLINE    10
+
+
+int main() {
+  int c, counts_idx, flg_intchar_err;
+  int counts[INT_CHAR_LEN];
+  for(int i = 0; i < INT_CHAR_LEN; ++i)
+    counts[i] = 0;
+
+  
+  /* Read char from input; Ignore newlines. */
+  flg_intchar_err = 0;
+  while( (c = getchar()) != EOF ) {
+    if( c >= INT_CHAR_MIN && c <= INT_CHAR_MAX ) {
+      /* Increment corresponding element in counts[] array */
+      counts_idx = c - INT_CHAR_MIN;
+      ++counts[counts_idx];
+    } else if ( c != INT_NEWLINE )
+      flg_intchar_err = 1;
+  };
+
+  /* Print contents of counts[i] */
+  printf("counts[i]:(ASCII code):( symbol ): count : histogram bar\n");
+  for(int i = 0; i < INT_CHAR_LEN; ++i) {
+    printf("counts[%03d]:(%03d):( %c ):%03d:",
+           i, i + INT_CHAR_MIN, i + INT_CHAR_MIN, counts[i]);
+    for(int j = 0; j < counts[i]; ++j)
+      printf("#");
+    printf("\n");
+  };
+    
+
+  /* Report input errors */
+  if( flg_intchar_err == 1 )
+    printf("ERROR:Unrecognized char int provided.");
+  return 0;
+};
+
+/* Author: Steven Baltakatei Sandoval
+   License: GPLv3+ */
diff --git a/src/kr_exercises/ch1/s1.9/array_test.c b/src/kr_exercises/ch1/s1.9/array_test.c
new file mode 100644 (file)
index 0000000..2408519
--- /dev/null
@@ -0,0 +1,45 @@
+#include <stdio.h>
+
+#define MAXLINE 1000
+
+int mygetline(char line[], int maxline); /* 'my-' to avoid name collision */
+
+int main() {
+  int len;
+  char line[MAXLINE];
+
+  while( (len=mygetline(line, MAXLINE)) > 0 ) {
+    line[0]=64; // '@'
+    printf("%s\n", line);
+  };
+  
+  return 0;
+};
+
+/* mygetline: read a line into s, \0-term, return length v2 */
+int mygetline(char s[], int lim) {
+  int c, i, k, j;
+
+  /* Read chars into s[] until lim reached */
+  for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i )
+    s[i] = c;
+  j = i; /* Store char count for counting beyond lim */
+
+  /* \0 terminate array s[] */
+  if (c=='\n') {
+    s[i] = c;
+    ++i;
+  };
+  s[i] = '\0';
+  
+  /* Count up remaining chars beyond lim until EOF or '\n' reached */
+  if (j>=lim-1) {
+    while ( (c=getchar()) != EOF && c != '\n')
+      ++j;
+  };
+  if (c=='\n')
+    ++j;
+
+  /* Return char count (including chars beyond array limit)*/
+  return j;
+};
diff --git a/src/kr_exercises/ch1/s1.9/compile.sh b/src/kr_exercises/ch1/s1.9/compile.sh
new file mode 100755 (executable)
index 0000000..33e3ef5
--- /dev/null
@@ -0,0 +1,28 @@
+#!/usr/bin/env bash
+# Desc: Check sytnax and compiles file using gcc
+# Usage: compile.sh mysource.c
+
+yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
+die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
+try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
+main() {
+    # Check arg
+    if [[ $# -ne 1 ]]; then die "FATAL:Incorrect argument count:$#"; fi;
+    if [[ ! -f $1 ]]; then die "FATAL:Not a file:$1"; else f="$1"; fi;
+
+    # Set var names
+    f="$(readlink -f $f)"; fe="${f%.c}";
+    yell "DEBUG:f :$f";
+    yell "DEBUG:fe:$fe";
+
+    # Check syntax
+    if ! try gcc -fsyntax-only "$f"; then die "FATAL:Syntax Error."; fi;
+
+    # Compile
+    time { try gcc -o "$fe" "$f"; };
+
+    return 0;
+}; # main program
+
+main "$@";
+
diff --git a/src/kr_exercises/ch1/s1.9/count.c b/src/kr_exercises/ch1/s1.9/count.c
new file mode 100644 (file)
index 0000000..472b26b
--- /dev/null
@@ -0,0 +1,27 @@
+#include <stdio.h>
+#define MAXLINE 1000
+
+/* Exercise 1-17. Write a program to print all input lines that are
+   longer than 80 characters. */
+
+int get_line(char line[], int maxline);
+
+int main() {
+  printf("DEBUG:Begin main().\n");
+  char line[MAXLINE];
+
+  
+  get_line(MAX_LENGTH);
+  return 0;
+};
+
+int get_line(int max) {
+  int c;
+  printf("DEBUG:Begin get_line().\n");
+  printf("DEBUG:max:%d.\n", max);
+  while(int i = 0; i < max-1 && (c = getchar()) != EOF && c != '\n'; ++i) {
+
+  }
+  
+  return 0;
+}
diff --git a/src/kr_exercises/ch1/s1.9/e1-17..print_gt80.c b/src/kr_exercises/ch1/s1.9/e1-17..print_gt80.c
new file mode 100644 (file)
index 0000000..1f04d59
--- /dev/null
@@ -0,0 +1,71 @@
+#include <stdio.h>
+
+/* Exercise 1-17. Write a program to print all input lines that are
+   longer than 80 characters. */
+
+/* Strat: Read characters until 1) a newline character is encountered,
+   2) EOF is encountered, or 3) MAXLINE characters are read from the
+   most recent line.
+
+   If a newline character is encountered, check if line's character
+   count is greater than 80. If so, print the line.
+
+   If an EOF is encountered, check if the line's character count is
+   greater than 80. If so, print the line.
+
+   If the line has more than MAXLINE characters, then print only
+   MAXLINE characters.
+
+*/
+
+#define MINLINE 80  /* minimum line length required to be output */
+#define MAXLINE 90 /* maximum input line size */
+
+int mygetline(char line[], int maxline); /* 'my-' to avoid name collision */
+
+int main() {
+  int len;             /* working line length */
+  char line[MAXLINE];  /* working line*/
+
+  /* Read lines and line lengths */
+  while ((len = mygetline(line, MAXLINE)) > 0) {
+
+    printf("DEBUG:%d\n",len);
+    /* Check if line length > MINLINE */
+    if ( len >= MINLINE ) {
+      printf("OUTPUT:%s\n", line);
+    };
+    printf("DEBUG:end loop =============================\n");
+  };
+  printf("DEBUG:End of main()==========================\n");
+  return 0;
+};
+
+/* mygetline: read a line into s, \0-term, return length v2 */
+int mygetline(char s[], int lim) {
+  
+  int c, i, k, j;
+
+  /* Read chars into s[] until lim reached */
+  for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i )
+    s[i] = c;
+  j = i; /* Store char count for counting beyond lim */
+
+  /* \0 terminate array s[] */
+  if (c=='\n') {
+    s[i] = c;
+    ++i;
+  };
+  s[i] = '\0';
+  
+  /* Count up remaining chars beyond lim until EOF or '\n' reached */
+  if (j>=lim-1) {
+    while ( (c=getchar()) != EOF && c != '\n')
+      ++j;
+  };
+  if (c=='\n')
+    ++j;
+
+  /* Return char count (including chars beyond array limit)*/
+  return j;
+};
diff --git a/src/kr_exercises/ch1/s1.9/print_steps.c b/src/kr_exercises/ch1/s1.9/print_steps.c
new file mode 100644 (file)
index 0000000..1ac149e
--- /dev/null
@@ -0,0 +1,16 @@
+#include <stdio.h>
+
+/* Prints a staircase made of characters. */
+
+#define HEIGHT 140
+
+int main() {
+  for(int i=1; i<=HEIGHT; i++) {
+    for(int j=i; j>0; j--) {
+      printf("%d",i % 10);
+      if (j==1)
+        printf("\n");
+    };
+  };
+  return 0;
+};