#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static void problem1(void) {
  char bunchChars[1000] ;
  puts("\n\nProblem 1") ;
  strncpy(bunchChars, "J Dean Brock", 8) ;
  puts(bunchChars) ;
}

#define PRINTINT(m) {vii= 7; vhf=2.5; printf("%-20s is %3d\n", #m, m); }
#define PRINTDBL(m) {vii= 7; vhf=2.5; printf("%-20s is %6.2f\n", #m, m); }

static void problem2(void) {
  int    vii =    7 ;
  double vhf =  2.5 ;
  puts("\n\nProblem 2") ;
  PRINTINT(1 && vii++ ? vii : 0) ;
  PRINTINT(1 || vii++ ? vii : 0) ;
  PRINTDBL(vhf *= 2) ;
  PRINTINT(vii == vhf ) ;
  PRINTINT(vii = vhf) ;
  PRINTINT(vii % 10) ;
  PRINTINT(vii / 10) ;
  PRINTINT(7 || !5) ;
  PRINTINT(7 && !5) ;
  PRINTINT(vii == 6 || 8) ;
}

static void problem3(void) {
  int  n ;
  int *p, *q ;
  int  v[3] ;

  puts("\n\nProblem 3") ;

  for (n = 0; n<3; ++n) {
    v[n] = 100 + 10*n ;
  }
  printf("%d %d %d %d\n", n, v[0], v[1], v[2]) ;

  p = &v[0] ;
  v[1] = (*p)++ ;
  v[2] = *(p++) ;
  n = *p ;
  printf("%d %d %d %d\n", n, v[0], v[1], v[2]) ;

  p = &v[1] ;
  q = p ;
  p[1] = *p ;
  *q = *p + 7 ;
  printf("%d %d %d %d\n", n, v[0], v[1], v[2]) ;

  p = &v[0] ;
  q = &v[2] ;
  v[1] = q - p ;
  n  = *q - *p ;
  printf("%d %d %d %d\n", n, v[0], v[1], v[2]) ;

  p = &v[0] ;
  ++*++p ;
  printf("%d %d %d %d\n", n, v[0], v[1], v[2]) ;
}

#define NUMROWS  7
#define NUMCOLS 15
static const char probGrid[NUMROWS][NUMCOLS] = {
  /*
  "+++++++++++++++",
  "+ \/ece  \\/  +", 
  "+ /\     //\ \/",
  "+   \ \/     /\",
  "+ \/  /\/ c89 +",
  "+ /    /\     +"
  */
  "+++++++++++++++",
  "+ \\/ece  \\\\/  +", 
  "+ /\\     //\\ \\/",
  "+   \\ \\/     /\\",
  "+ \\/  /\\/ c89 +",
  "+ /    /\\     +"
} ;

void problem4(void) {
  int r ;
  int c ;
  int numX = 0 ;
  for(r=0; r<NUMROWS-1; ++r) {
    for(c=0; c<NUMCOLS-1; ++c) {
      if (probGrid[r][c] == '\\' 
	  && probGrid[r][c+1] == '/'
	  && probGrid[r+1][c] == '/'
	  && probGrid[r+1][c+1] == '\\' ) {
	++numX ;
      }
    }
  }
  printf("There were %d X's\n", numX) ;
}

void problem5(void) {
  char x ;
  char * const y = &x ;    /* &x to avoid uniitialized variable message */
  char const * z = &x ;
  /* y  = &x ;     error: assignment of read-only variable y */
  z  = &x ;
  *y = 'A' ;
  /* *z = 'B' ;    error: assignment of read-only location *z    */
  /* z  = 'C' ;    warning: assignment makes pointer from integer */
}


int problem6impl(const char s[100]) {
  int yorn = fopen(s, "r") != NULL ;
  return yorn ;
}

void problem6(void) {
  char a[100] = "ece209-f11-e2-sol.c" ;
  char b[100] = "NoSuchFile" ;
  puts("\n\nProblem 6") ;
  printf("  Testing problem 6 on \"%s\" --> %d\n", a, problem6impl(a)) ;
  printf("  Testing problem 6 on \"%s\" --> %d\n", b, problem6impl(b)) ;
}

int problem7impl(const char s[100]) {
  int yorn = isdigit(s[0]) ;
  return yorn ;
}

void problem7(void) {
  char a[100] = "No digits" ;
  char b[100] = "1 digit" ;
  puts("\n\nProblem 7") ;
  printf("  Testing problem 7 on \"%s\" --> %d\n", a, problem7impl(a)) ;
  printf("  Testing problem 7 on \"%s\" --> %d\n", b, problem7impl(b)) ;
}

int problem8impl(const char s[100]) {
  int yorn ;
  char *abcFirst = strstr(s, "abc") ;
  if (abcFirst == NULL) {
    yorn = 0 ;
  } else {
    yorn = strstr(&abcFirst[3], "abc") == NULL ;
  }
  return yorn ;
}

void problem8(void) {
  char a[100] = "not in here" ;
  char b[100] = "exactly one abc here" ;
  char c[100] = "abcabc" ;
  char d[100] = "*abcdef" ;
  puts("\n\nProblem 8") ;
  printf("  \"%s\" --> %d\n", a, problem8impl(a)) ;
  printf("  \"%s\" --> %d\n", b, problem8impl(b)) ;
  printf("  \"%s\" --> %d\n", c, problem8impl(c)) ;
  printf("  \"%s\" --> %d\n", d, problem8impl(d)) ;
}

int problem9impl(const char s[100]) {
  int yorn ;
  const char *strPtr ;
  yorn = 0 ;
  strPtr = &s[0] ;
  while (!yorn && strPtr != NULL) {
    strPtr = strstr(strPtr, "abc") ;
    if (strPtr != NULL) {
      strPtr = strPtr + 3 ;
      yorn = isdigit(*strPtr) ;
    }
  }
  return yorn ;
}

void problem9(void) {
  char a[100] = "xabc5" ;
  char b[100] = "!abc!5abc5" ;
  char c[100] = "abcde" ;
  char d[100] = "abcx abc2 abc3" ;
  puts("\n\nProblem 9") ;
  printf("  \"%s\" --> %d\n", a, problem9impl(a)) ;
  printf("  \"%s\" --> %d\n", b, problem9impl(b)) ;
  printf("  \"%s\" --> %d\n", c, problem9impl(c)) ;
  printf("  \"%s\" --> %d\n", d, problem9impl(d)) ;
}

void exam1(void) {
  puts("Exam 2\n") ;
  problem1() ;
  problem2() ;
  problem3() ;
  problem5() ;
  problem6() ;
  problem7() ;
  problem8() ;
  problem9() ;
}

int main(int argc, char *argv[]) {
  exam1() ;
  return EXIT_SUCCESS ;
}
