#include <stdio.h>

/* This routine doesn't do anything other than compile */
static void problem2(void) {
  int ten50 ;
  /* int c# ;            -- illegal */
  int _ten50 ;
  /* int 1050 ;          -- illegal */
  /* int int ;           -- illegal */
  int Ten50 ;
  int _ten_fifty_ ;
  /* int (c)2010 ;       -- can't even put the copyright system in the file */
  /* int ten50 ;         -- legal, but you can't declare it twice */
}

#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(107) ;
  PRINTLIT(3e3) ;
  /* PRINTLIT(x15) ;      -- illegal -- x15 is not any constant */
  PRINTLIT('A') ;
  PRINTLIT(007) ;
  PRINTLIT(0x15) ;
}

#define PRINTINT(m) {xxi=21; chr='Q'; xhf=10.5; printf("%-15s is %3d\n", #m, m); }
#define PRINTDBL(m) {xxi=21; chr='Q'; xhf=10.5; printf("%-15s is %6.2f\n", #m, m); }

static void problem4(void) {
    int    xxi = 21 ;
    char   chr = 'Q' ;  /* ASCII code for 'Q' is 121 */
    double xhf = 10.5 ;
    puts("\n\nProblem 4") ;
    PRINTINT(10 + 5 * 4) ;
    PRINTINT(10 * 5 + 4) ;
    PRINTINT(10 + (5 * 4)) ;
    PRINTINT(10 * (5 + 4)) ;
    PRINTINT(5 % 3) ;
    PRINTDBL(1 * 5.0 / 2) ;
    PRINTINT(xxi + 5 / 2) ;
    PRINTDBL(xxi + 5.0 / 2) ;
    PRINTDBL((int) xxi + 2.5) ;
    PRINTINT(1 && 0) ;
    PRINTINT(1 || 0) ;
    PRINTINT(! 1) ;
    PRINTINT(9 / 4 * 3) ;
    PRINTINT(9 * 4 / 3) ;
    PRINTDBL(9 / 4.0 * 3) ;
    PRINTDBL(9 * 4 / 3.0) ;
    PRINTINT(5 > 5) ;
    PRINTINT(5 > 4 + 1) ;
    PRINTINT(5 > 4.0 + 1) ;
    PRINTINT(1 < 2 ? 4 : 6) ;
    PRINTINT(0 < 0 <= 0) ;
    PRINTINT(xxi = 5) ;
    PRINTINT(chr = xhf) ;
    PRINTINT(++chr) ;
    PRINTINT(chr++) ;
}

static void problem5(void) {
    int i ;

    puts("\n\nProblem 5") ;
    for (i=4; i>=2; --i) {
        printf("%d\n", i*i) ;
    }

    puts("");
    i=0 ;
    while (i<4) {
        printf("%d\n", ++i) ;
    }

    int countDown = 4 ;
    puts("") ;
    for (i=0; i<=7; i=i+i) {
        printf("%d\n", i+2) ;
        if (--countDown==0) break ;
    }

    puts("") ;
    for (i=4; i; i = i-2) {
        printf("%d\n", i) ;
    }
}

static void problem6(void) {
    char   c ;
    double d ;
    int    i ;
    char instring[] = "3.1e4 2010 x\n" ;
    puts("\n\nProblem 6") ;
    sscanf(instring, "%lf %d %c", &d, &i, &c) ;
    printf("c = %c, d = %f, i = %d\n", c, d, i) ;
    sscanf(instring, "%c", &c) ;
    printf("c = %c\n", c) ;
    sscanf(instring, "%3lf%c%d", &d, &c, &i) ;
    printf("c = %c, d = %f, i = %d\n", c, d, i) ;
    sscanf(instring, "%lf %c%o", &d, &c, &i) ;
    printf("c = %c, d = %f, i = %d\n", c, d, i) ;
    sscanf(instring, "%d%lf", &i, &d) ;
    printf("d = %f, i = %d\n", d, i) ;
    sscanf(instring, "%lf 20%c", &d, &c) ;
    printf("c = %c, d = %f\n", c, d) ;
}

static void problem7(void) {
    int n = 5 ;
    int j ;
    puts("\n\nProblem 7") ;
    
    printf("help") ;
    for (j = 0; j < n; ++j) {
        printf("%c", '!') ;     // printchar('!') ;
    }
    printf("\n") ;
}

static void problem8(void) {
    int n = 5 ;
    puts("\n\nProblem 8") ;

    if (n%2 == 0 || n<0 || n>25) {
        printf("unacceptable triangle size\n") ;
    }
}

static void problem9(void) {
    double E = 2.718 ;
    int    Q = 81 ;
    printf("%g%5d\n", E, Q) ;
    printf("(%g,%d)\n", E, Q) ;
    printf("%8.4f  %c\n", E, Q) ;
    printf("%9.2e  %2x\n", E, Q) ;
    printf("%3.1f%d\n", E, Q) ;
    printf("%4.0f  %04o\n", E, Q) ;
}

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

    puts("\n\nProblem 10") ;
    for (i = 0 ; i<sizeof(testValues)/ sizeof(testValues[0]); ++i) {
      int n = testValues[i] ;
        printf ("\nn is %d\n", n) ;
        switch(n) {
            case 1:
                printf("solo\n") ;
                break ;
            case 2:
                printf("duet\n") ;
            case 4:
                printf("quartet\n") ;
                break ;
            default:
                printf("choir\n") ;
        }
    }
}

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