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

static void problem1(void) {
  char bunchChars[1000] ;

  /* solution for Problem 1 */
  strncpy(bunchChars, "Yourname", 5) ;

  puts("\nProblem 1") ;
  printf("%.5s\n", bunchChars) ;
}

#define PRINTINT(m) {viii=8; half = 0.5 ; printf("%-20s is %3d\n", #m, m); }
#define PRINTDBL(m) {viii=8; half = 0.5 ; printf("%-20s is %6.2f\n", #m, m); }

static void problem2(void) {
  int viii =   8 ;
  double half = 0.5 ;
  puts("\nProblem 2") ;
  PRINTDBL(8 * 0.5) ;
  PRINTDBL(half * 0) ;
  PRINTINT(8/4*2) ;
  PRINTINT(5 < 6 <= 6) ;
  PRINTINT(2*(1/2)) ;
  PRINTINT(1 && 0 == 0) ;
  PRINTDBL(half = 5) ;
  PRINTINT(viii && 4+4 == 8) ;
  PRINTINT(! viii == 0) ;
  PRINTINT(viii += 2) ;
}

static void problem4(void) {
  int f(int   m, int  n, int *o) ;
  int g(int  *p, int *q) ;
  int h(int **r, int  s) ;
  int a ;
  int *b ;
  int c[15] ;
  return ;
  /* f(c[3], 0.0, &b) ;        warning on 3rd parameter */
  /* f(a, a) ;                 error   */
  /* g(&b, &b) ;               warning on 1st and 2nd parameters */
  g(c, c) ;
  g(&a, &a) ;
  g(c+5, c+10) ;
  /* h(&b, b) ;                warning on 2nd parameter */
  /* h(b, &b) ;                warning on 1st and 2nd parameters */
  /* h(&c[3], c[3]) ;          warning on 1st parameter */
}

static void problem5(void) {
  int a, b ;
  int *p, *q ;
  int v[2] ;
  puts("\nProblem 5") ;
  a = 10 ;
  b = 20 ;
  for (a = 0; a<2; ++a) {
    v[a] = 30 + a*10 ;
  }
  printf("%d %d %d %d\n", a, b, v[0], v[1]) ;
  
  p = &a ;
  *p = b + 5 ;
  v[0] = *p + 7 ;
  printf("%d %d %d %d\n", a, b, v[0], v[1]) ;
  
  p = &a ;
  q = &b ;
  *p = *q ;
  *q = *p ;
  printf("%d %d %d %d\n", a, b, v[0], v[1]) ;

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

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

static int dice[5]    = { 4, 2, 2, 5, 2};
static int histo[6]   = { 0, 3, 0, 1, 1, 0};
static int i, j, m, s ;

void problem6(void) {

  /* solution for Problem 6 */
  m = dice[0] ;
  for(i=1; i<5; ++i) {
    if (dice[i] > m) {
      m = dice[i] ;
    }
  }

  puts("\nProblem 6") ;
  printf("  largest is %d\n", m) ;
}

/* solution for Problem 7 */
static int sum(const int *V, int n) {
  int i ;
  int s = 0 ;
  for(i=0; i<n; ++i) {
    s = s + V[i] ;
  }
  return s ;
}

static void problem7(void) {
  s = sum(dice, 5) ;

  puts("\nProblem 7") ;
  printf("  sum is %d\n", s) ;
}

static

/* solution for Problem 8 */
int problem8_9(int x, int *V, int n) ;

/* solution for Problem 9 */
static int problem8_9(int x, int *V, int n) {
  int t, i ;
  t = 0 ;
  for (i= 0; i<n; ++i) {
    if (V[i] == x) {
      ++t ;
    }
  }
  return t ;
}

static void problem10(void) {

  /* solution for Problem 10 */
  int t = problem8_9(2, dice, 6) ;

  puts("\nProblem 10") ;
  printf("  number times 2 in dice is %d\n", t) ;
}

static int problem11_func(char c) {
  int yorn = 0 ;

  /* solution for Problem 11 */
  yorn = isdigit(c) || islower(c) ;

  return yorn ;
}

static void problem11(void) {
  char testcase[3] = {'0', 'x', 'U'} ;
  int i ;
  puts("\nProblem 11") ;
  for (i=0; i<3; ++i) {
    printf("  with %c, yorn is %d\n",
	   testcase[i], problem11_func(testcase[i])) ;
  }
}

static int problem12_func(const char *s) {
  int yorn = 0 ;

  /* solution for Problem 12 */
  yorn = strchr(s, '@') != NULL ;

  return yorn ;
}

static void problem12(void) {
  char *testcase[3] = {"no at", "@ here", "who @"} ;
  int i ;
  puts("\nProblem 12") ;
  for (i=0; i<3; ++i) {
    printf("  with \"%s\", yorn is %d\n",
	   testcase[i], problem12_func(testcase[i])) ;
  }
}

static int problem13_func(const char *s) {
  int yorn = 0 ;

  /* solution for Problem 13 */
  int p ;
  p = strlen(s) ;
  yorn = p>0 && s[p-1] == '!' ;

  return yorn ;
}

static void problem13(void) {
  char *testcase[3] = {"", "no b", "yes !"} ;
  int i ;
  puts("\nProblem 13") ;
  for (i=0; i<3; ++i) {
    printf("  with \"%s\", yorn is %d\n",
	   testcase[i], problem13_func(testcase[i])) ;
  }
}

static int problem14_func(const char *s) {
  int yorn = 0 ;

  /* solution for Problem 14 */
  char *p ;
  p = strchr(s, '@') ;
  yorn = p!=NULL && strncmp(p+1, "cs.unca.edu", 11)==0 ;

  return yorn ;
}

static void problem14(void) {
  char *testcase[3] = {" no at here",
		       " one @xxx another @cs.unca.edu",
		       "brock@cs.unca.edu"
  } ;
  int i ;
  puts("\nProblem 14") ;
  for (i=0; i<3; ++i) {
    printf("  with \"%s\", yorn is %d\n",
	   testcase[i], problem14_func(testcase[i])) ;
  }
}

static int problem15_func(void) {
  int yorn = 0 ;

  /* solution for Problem 15 */
  FILE *f ;
  f = fopen("read.me", "r") ;
  yorn = f!=NULL ;

  if (f != NULL) {
    fclose(f) ;
  }
  return yorn ;
}

static void problem15(void) {
  char *testcase[2] = {"read.me exists",
		       "no read.me"
  } ;
  int testyorn[2] = { 0, 0} ;
  int i ;
  FILE *f ;

  puts("\nProblem 15") ;
  f = fopen("read.me", "w") ;
  fclose(f) ;
  testyorn[0] = problem15_func() ;
  remove("read.me") ;
  testyorn[1] = problem15_func() ;
  
  for (i=0; i<2; ++i) {
    printf("  when %s, yorn is %d\n",
	   testcase[i], testyorn[i]) ;
  }
}

static int problem16_func(void) {
  int yorn = 0 ;

  /* solution for Problem 16 */
  FILE *f ;
  f = fopen("read.me", "r") ;
  yorn = f!=NULL && fgetc(f)=='?' && fgetc(f)==EOF ;

  if (f != NULL) {
    fclose(f) ;
  }
  return yorn ;
}

static void problem16(void) {
  char *testcase[5] = {"no read.me",
		       "read.me is empty",
		       "read.me is ?",
		       "read.me is !",
		       "read.me is !!"
  } ;
  int testyorn[5] = { 0, 0, 0, 0, 0} ;
  int i ;
  FILE *f ;

  puts("\nProblem 16") ;
  remove("read.me") ;
  testyorn[0] = problem16_func() ;

  f = fopen("read.me", "w") ;
  fclose(f) ;
  testyorn[1] = problem16_func() ;

  f = fopen("read.me", "w") ;
  fputs("?", f) ;
  fclose(f) ;
  testyorn[2] = problem16_func() ;

  f = fopen("read.me", "w") ;
  fputs("!", f) ;
  fclose(f) ;
  testyorn[3] = problem16_func() ;

  f = fopen("read.me", "w") ;
  fputs("!!", f) ;
  fclose(f) ;
  testyorn[3] = problem16_func() ;
  
  for (i=0; i<5; ++i) {
    printf("  when %s, yorn is %d\n",
	   testcase[i], testyorn[i]) ;
  }
}

void exam2(void) {
  puts("Exam 2\n") ;
  problem1() ;
  problem2() ;
  problem4() ;
  problem5() ;
  problem6() ;
  problem7() ;
  problem10() ;
  problem11() ;
  problem12() ;
  problem13() ;
  problem14() ;
  problem15() ;
  problem16() ;
}
