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

/* This routine doesn't do anything other than compile */
static void problem2(void) {
  int _sparkle ;
  int Ferd ;
  int ford ;
  int h8 ;
  /* int Ar#e ;          -- illegal */
  int for6 ;
  /* int 8t ;            -- illegal */
  int hashTable ;
  /* int for ;           -- illegal */
}

#define PRINTLIT(m) printf("%-10s prints as integer %d\n", #m, m) ;
/* If it isn't an integer literal, it will print as junk */
void problem3(void) {
  PRINTLIT('2') ;
  /* PRINTLIT(0209) ;       -- 9 is not octal digit */
  PRINTLIT(2.09e2) ;
  PRINTLIT("309") ;
  /* PRINTLIT(00209) ;      -- 9 is not octal digit */
  PRINTLIT(209.0) ;
}

#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 problem4(void) {
    int    vii =    7 ;
    double vhf =  2.5 ;
    puts("\n\nProblem 4") ;
    PRINTDBL(2.5 * 2 / 5) ;
    PRINTDBL(2.5 * (2 / 5)) ;
    PRINTDBL((2.5 * 2) / 5) ;
    PRINTINT(3 % 5) ;
    PRINTINT(5 % 3) ;
    PRINTDBL(3 + 4 - 0.0) ;
    PRINTDBL(3 + 4 * 0.0) ;
    PRINTINT(3 * 4 / 5) ;
    PRINTINT(3 >= 5) ;
    PRINTINT(3 != 5) ;
    PRINTINT(3 < 4 < 5) ;
    PRINTINT(5 > 4 > 3) ;
    PRINTINT(0 < 1 == 3) ;
    PRINTINT(0 == 1 > 3) ;
    PRINTINT(3 < 4 && 5) ;
    PRINTINT(3 > 4 && 5) ;
    PRINTINT(! 3 < 4) ;
    PRINTINT(3 + 4 > 5 ) ;
    PRINTINT(vii++) ;
    PRINTINT(++vii) ;
    /* PRINTINT(1 && vii++ : vii ? 0) ;      -- illegal */
    /* PRINTINT(1 || vii++ : vii ? 0) ;      -- illegal */
    /* PRINTDBL(vhf =* 2) ;                  -- illegal */
    PRINTDBL(2.0 * (int) vhf) ;
    PRINTDBL(2.0 * (double) vhf) ;
}

static void problem5(void) {
    int i ;

    puts("\n\nProblem 5") ;

    puts("\n  loop 1") ;

    for (i=13; i>=5; i=i/2) {
      printf("%d\n", i) ;
    }

    puts("\n  loop 2") ;

    i = -3 ;
    while (i+1) {
      printf("%d\n", ++i) ;
    }

    puts("\n  loop 3") ;

    for (i=0; i<=8; i += 4) {
      printf("%d\n", i+10) ;
    }

    puts("\n  loop 4") ;

    for (i=3; i<0; --i) {
      printf("%d\n", i) ;
    }

    puts("");
}


#define TSTFMT(instring, format) \
  { \
    int i, j; \
    if (sscanf((instring), (format), &i, &j) != 2 || i != 209 || j != 2011) \
      printf("Failure on %s\n", (format)) ; \
  }

static void problem6(void) {
    int    i, j ;
    puts("\n\nProblem 6") ;
    
    TSTFMT("    209  2011\n",       "%d%d"              ) ;
    TSTFMT("  {  209  2011   }\n",  " {%d%d }"          ) ;
    TSTFMT(" 209  zap  2011\n",     "%d zap%d"          ) ;
    TSTFMT(" ( 209 , 2011 )\n",     " (%d ,%d )"        ) ;
    TSTFMT(" i= 209 ; j= 2011 ;\n", " i =%d ; j = %d ;" ) ;
    TSTFMT("  209% 2011%\n",        "%d%%%d%%"          ) ;
}

static void problem7(void) {
    int i ;
    int testValues[] = {0, 1, 2} ;

    puts("\n\nProblem 7") ;
    for (i = 0 ; i<sizeof(testValues)/ sizeof(testValues[0]); ++i) {
      int n = testValues[i] ;
        printf ("\nn is %d\n", n) ;
        switch(n) {
            case 0:
                printf("none\n") ;
                break ;
            case 1:
                printf("one\n") ;
            default:
                printf("many\n") ;
        }
    }
}

static void problem8(void) {
    int V[13] = { 1, 3, 5, 7, 9, 11, 0, 2, 4, 6, 8, 10, 12 } ;
    puts("\n\nProblem 7") ;
    
    {
        int bigcount ;
        int i; 

	bigcount = 0 ;
	for(i=0; i<13; ++i) {
            if (V[i] > 5) {
                ++bigcount ;
	    }
	}

	if (bigcount != 7) {
	    puts("FAILURE on problem 8\n") ;
	}
    }
}

static void problem9(void) {
    /* This is so bad... */
    puts("type the line") ;
    puts("  2, 3, 5, 8;") ;

    {
        int nextNumber ;
	int count ;
	double sum ;

	/* Read the first number */
	scanf("%d", &nextNumber) ;
	sum = nextNumber ;
	count = 1 ;

	/* Keep reading until something other than a comma */
	while(scanf(" ,%d", &nextNumber) == 1) {
	    sum = sum + nextNumber ;
	    count++ ;
	}
	
	printf("The average is %.3lf\n", sum / count) ;
    }
}


void exam1(void) {
    puts("Exam 1\n") ;
    problem3() ;
    problem4() ;
    problem5() ;
    problem6() ;
    problem7() ;
    problem8() ;
    problem9() ;
}
