diff --git a/exercises/ex05/find_track_oo.c b/exercises/ex05/find_track_oo.c index d039a6d3..3cf219c3 100644 --- a/exercises/ex05/find_track_oo.c +++ b/exercises/ex05/find_track_oo.c @@ -20,7 +20,9 @@ char tracks[][80] = { }; -typedef regex_t Regex; +typedef struct { + regex_t inner_struct[1]; +} Regex; /* Returns a new Regex that matches the given pattern. @@ -30,27 +32,50 @@ typedef regex_t Regex; * returns: new Regex */ Regex *make_regex(char *pattern, int flags) { - // FILL THIS IN! - return NULL; + regex_t regex; + int ret; + + ret = regcomp(®ex, pattern, REG_EXTENDED | REG_NOSUB); + if (ret) { + fprintf(stderr, "Could not compile regex\n"); + exit(1); + } + + Regex *r = malloc(sizeof(Regex)); + r->inner_struct[0] = regex; + + return r; } /* Checks whether a regex matches a string. * -* regex: Regex pointer +* regex: Regex * s: string * returns: 1 if there's a match, 0 otherwise */ int regex_match(Regex *regex, char *s) { - // FILL THIS IN! - return 0; + int ret; + char msgbuf[100]; + + ret = regexec(&(regex->inner_struct[0]), s, 0, NULL, 0); + if (!ret) { + // printf("Track %i: '%s'\n", i, tracks[i]); + return 1; + } else if (ret == REG_NOMATCH) { + return 0; + } else { + regerror(ret, &(regex->inner_struct[0]), msgbuf, sizeof(msgbuf)); + fprintf(stderr, "Regex match failed: %s\n", msgbuf); + exit(1); + } } /* Frees a Regex. * -* regex: Regex pointer +* regex: Regex */ void regex_free(Regex *regex) { - // FILL THIS IN! + regfree(&(regex->inner_struct[0])); } diff --git a/exercises/ex05/rand.c b/exercises/ex05/rand.c index 2235cd40..1ae04180 100644 --- a/exercises/ex05/rand.c +++ b/exercises/ex05/rand.c @@ -5,6 +5,8 @@ License: MIT License https://opensource.org/licenses/MIT */ #include +#include "rand.h" +#include // generate a random float using the algorithm described // at http://allendowney.com/research/rand @@ -78,7 +80,37 @@ float my_random_float2() // compute a random double using my algorithm double my_random_double() { - // TODO: fill this in + int mant; + int exp = 2047; + int mask = 1; + uint64_t x; + + union { + double d; + int i; + } b; + + // generate random bits until we see the first set bit + while (1) { + x = rand(); + if (x == 0) { + exp -= 63; + } else { + break; + } + } + + // find the location of the first set bit and compute the exponent + while (x & mask) { + mask <<= 1; + exp--; + } + + // use the remaining bit as the mantissa + mant = x >> 12; + b.i = (exp << 52) | mant; + + return b.d; } // return a constant (this is a dummy function for time trials) @@ -114,7 +146,7 @@ float random_float() // generate a random double using the standard algorithm -float random_double() +double random_double() { int x; double f; diff --git a/exercises/ex05/time_rand.c b/exercises/ex05/time_rand.c index edbb1b3f..6a7deb9a 100644 --- a/exercises/ex05/time_rand.c +++ b/exercises/ex05/time_rand.c @@ -32,7 +32,6 @@ double get_seconds() { } /* Compute the total time used by a function. - iters: number of times to call the function func: function to call */ @@ -52,8 +51,24 @@ double time_func(int iters, float(*func)()) return t1 - t0; } +double time_func_double(int iters, double(*func)()) +{ + int i; + double d; + double t0, t1; + + srandom(time(NULL)); + + t0 = get_seconds(); + for (i=0; i