aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-160/duncan-c-white/C/.build2
-rw-r--r--challenge-160/duncan-c-white/C/Makefile13
-rw-r--r--challenge-160/duncan-c-white/C/README1
-rw-r--r--challenge-160/duncan-c-white/C/ch-1.c91
-rw-r--r--challenge-160/duncan-c-white/README3
5 files changed, 110 insertions, 0 deletions
diff --git a/challenge-160/duncan-c-white/C/.build b/challenge-160/duncan-c-white/C/.build
new file mode 100644
index 0000000000..7a8295430e
--- /dev/null
+++ b/challenge-160/duncan-c-white/C/.build
@@ -0,0 +1,2 @@
+BUILD = ch-1
+#CFLAGS = -g
diff --git a/challenge-160/duncan-c-white/C/Makefile b/challenge-160/duncan-c-white/C/Makefile
new file mode 100644
index 0000000000..61dae2a678
--- /dev/null
+++ b/challenge-160/duncan-c-white/C/Makefile
@@ -0,0 +1,13 @@
+BUILD = ch-1
+CC = gcc
+CFLAGS = #-g
+
+all: $(BUILD)
+
+clean:
+ /bin/rm -f $(BUILD) *.o core a.out
+
+ch-1: ch-1.o
+ $(CC) $(CFLAGS) ch-1.o -o ch-1
+
+ch-1.o: ch-1.c
diff --git a/challenge-160/duncan-c-white/C/README b/challenge-160/duncan-c-white/C/README
new file mode 100644
index 0000000000..8fcb95df91
--- /dev/null
+++ b/challenge-160/duncan-c-white/C/README
@@ -0,0 +1 @@
+Thought I'd also have a go at translating ch-1.pl into C..
diff --git a/challenge-160/duncan-c-white/C/ch-1.c b/challenge-160/duncan-c-white/C/ch-1.c
new file mode 100644
index 0000000000..5a46654f95
--- /dev/null
+++ b/challenge-160/duncan-c-white/C/ch-1.c
@@ -0,0 +1,91 @@
+/*
+ * TASK #1 - Four Is Magic - done in C.
+ * MY NOTES: ok. Pretty easy.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+
+char *n_to_word[] = {
+ "zero",
+ "one",
+ "two",
+ "three",
+ "four",
+ "five",
+ "six",
+ "seven",
+ "eight",
+ "nine"
+};
+
+
+bool debug = false;
+
+
+/*
+ * int n = process_args( argc, argv );
+ * Process cmd line options (setting debug if --debug/-d option given)
+ * and the optional value n (default 3), return n.
+ * My goodness, it's painful to write this sort of code..
+ */
+int process_args( int argc, char **argv )
+{
+ int pos = 1;
+ if( argc > 1 &&
+ (strcmp( argv[1], "--debug" )==0 || strcmp( argv[1], "-d")==0 ) )
+ {
+ debug = true;
+ pos++;
+ }
+ if( argc < pos )
+ {
+ fprintf( stderr,
+ "Usage: four-is-magic [--debug] [N] (default 3, < 10)\n" );
+ exit(1);
+ }
+ int n = argc > pos ? atoi( argv[pos] ) : 3;
+ if( n==0 ) n = 3;
+ if( n < 0 || n > 9 )
+ {
+ fprintf( stderr, "four-is-magic: n ($n) must be 0..9\n" );
+ exit(1);
+ }
+ return n;
+}
+
+
+int main( int argc, char **argv )
+{
+ int n = process_args( argc, argv );
+
+ char *word = n_to_word[n];
+
+ char answer[512];
+ char *p = answer;
+
+ while( strcmp( word, "four") != 0 )
+ {
+ int len = strlen(word);
+ char *lenword = n_to_word[len];
+ strcpy( p, word );
+ strcat( p, " is " );
+ strcat( p, lenword );
+ strcat( p, ", " );
+ p += strlen(p);
+ if( debug )
+ {
+ printf( "debug: word=%s, len=%d, lenword=%s, "
+ "added '%s is %s'\n",
+ word, len, lenword, word, lenword );
+ }
+ word = lenword;
+ }
+
+ strcpy( p, "four is magic." );
+
+ puts( answer );
+ return 0;
+}
diff --git a/challenge-160/duncan-c-white/README b/challenge-160/duncan-c-white/README
index c84ca6bc89..b73af0151f 100644
--- a/challenge-160/duncan-c-white/README
+++ b/challenge-160/duncan-c-white/README
@@ -24,6 +24,9 @@ Example 3:
MY NOTES: ok. Pretty easy.
+GUEST LANGUAGE: As a bonus, I'd also have a go at translating ch-1.pl into C,
+look in the C directory.
+
TASK #2 - Equilibrium Index