명해 C 언어 중급편 제3장 답안

69177 단어
연습 3-1
/*     (  :    /    )*/

#include 
#include 
#include 

int human;        /*       */
int comp;        /*        */
int win_no;        /*      */
int lose_no;    /*      */
int draw_no;    /*      */

char* hd[] = { "  ", "  ", " " };        /*    */

/*---      ---*/
void initialize(void)
{
    win_no = 0;        /*      */
    lose_no = 0;        /*      */
    draw_no = 0;        /*      */

    srand(time(NULL));    /*         */

    printf("      !!
"); } /*--- ( / )---*/ void jyanken(void) { int i; comp = rand() % 3; /* (0~2)*/ do { printf("
\a ···
"); for (i = 0; i < 3; i++) printf(" (%d)%s", i, hd[i]); printf(":"); scanf("%d", &human); /* */ } while (human < 0 || human > 2); } /*--- / / ---*/ void count_no(int result) { switch (result) { case 0: draw_no++; puts(""); break; /* */ case 1: lose_no++; puts(""); break; /* */ case 2: win_no++; puts(""); break; /* */ } } /*--- ---*/ int confirm_retry(void) { int x; printf(" ···(0) (1) :"); scanf("%d", &x); return x; } int main(void) { int judge; /* */ int retry; /**/ initialize(); /* */ do { jyanken(); /* */ /* */ printf(" %s, %s。
", hd[comp], hd[human]); judge = (human - comp + 3) % 3; /* */ count_no(judge); /* / / */ retry = confirm_retry(); /* */ } while (retry == 1); printf("%d %d %d 。
", win_no, lose_no, draw_no); return 0; }

연습 3-2
/*     (  :   3   )*/

#include 
#include 
#include 

int human;        /*       */
int comp;        /*        */
int win_no;        /*      */
int lose_no;    /*      */
int draw_no;    /*      */

char* hd[] = { "  ", "  ", " " };        /*    */

/*---      ---*/
void initialize(void)
{
    win_no = 0;        /*      */
    lose_no = 0;        /*      */
    draw_no = 0;        /*      */

    srand(time(NULL));    /*         */

    printf("      !!
"); } /*--- ( / ) ---*/ void jyanken(void) { int i; comp = rand() % 3; /* (0~2)*/ do { printf("
\a ···
"); for (i = 0; i < 3; i++) printf(" (%d)%s", i, hd[i]); printf(":"); scanf("%d", &human); /* */ } while (human < 0 || human > 2); } /*--- / / ---*/ void count_no(int result) { switch (result) { case 0: draw_no++; break; /* */ case 1: lose_no++; break; /* */ case 2: win_no++; break; /* */ } } /*--- ---*/ void disp_result(int result) { switch (result) { case 0: puts(""); break; /* */ case 1: puts(""); break; /* */ case 2: puts(""); break; /* */ } } int main(void) { int judge; /* */ int cnt; printf(" ?"); scanf("%d", &cnt); initialize(); /* */ do { jyanken(); /* */ /* */ printf(" %s, %s。
", hd[comp], hd[human]); judge = (human - comp + 3) % 3; /* */ count_no(judge); /* / / */ disp_result(judge); /* */ } while (win_no < cnt && lose_no < cnt); printf(win_no == cnt?"
□ 。
" : "
■ 。
"); printf("%d %d %d 。
", win_no, lose_no, draw_no); return 0; }

연습
/*     (  :    /    )*/

#include 
#include 
#include 

int human;        /*       */
int comp;        /*        */
int win_no;        /*      */
int lose_no;    /*      */
int draw_no;    /*      */
char* hd[] = { "  ", "  ", " " };        /*    */

/*---      ---*/
void initialize(void)
{
    win_no = 0;        /*      */
    lose_no = 0;        /*      */
    draw_no = 0;        /*      */

    srand(time(NULL));    /*         */

    printf("      !!
"); } /*--- ( / )---*/ void jyanken(void) { int i; comp = rand() % 3; /* (0~2)*/ do { comp = rand() % 3; } while (comp == 1); do { printf("
\a ···
"); for (i = 0; i < 3; i++) printf(" (%d)%s", i, hd[i]); printf(":"); scanf("%d", &human); /* */ } while (human < 0 || human > 2); } /*--- / / ---*/ void count_no(int result) { switch (result) { case 0: draw_no++; break; /* */ case 1: lose_no++; break; /* */ case 2: win_no++; break; /* */ } } /*--- ---*/ void disp_result(int result) { switch (result) { case 0: puts(""); break; /* */ case 1: puts(""); break; /* */ case 2: puts(""); break; /* */ } } /*--- ---*/ int confirm_retry(void) { int x; printf(" ···(0) (1) :"); scanf("%d", &x); return x; } int main(void) { int judge; /* */ int retry; /**/ initialize(); /* */ do { jyanken(); /* */ /* */ printf(" %s, %s。
", hd[comp], hd[human]); judge = (human - comp + 3) % 3; /* */ count_no(judge); /* / / */ disp_result(judge); /* */ retry = confirm_retry(); /* */ } while (retry == 1); printf("%d %d %d 。
", win_no, lose_no, draw_no); return 0; }

 
 
 
연습 3-4
/*     (  :    /    )*/

#include 
#include 
#include 

int human;        /*       */
int comp;        /*        */
int win_no;        /*      */
int lose_no;    /*      */
int draw_no;    /*      */
char* hd[] = { "  ", "  ", " " };        /*    */

/*---      ---*/
void initialize(void)
{
    win_no = 0;        /*      */
    lose_no = 0;        /*      */
    draw_no = 0;        /*      */

    srand(time(NULL));    /*         */

    printf("      !!
"); } /*--- ( / )---*/ void jyanken(void) { int i; static int cnt = 0; if (cnt == 0) { comp = 0; cnt++; } else { comp = rand() % 3; } /* (0~2)*/ do { printf("
\a ···
"); for (i = 0; i < 3; i++) printf(" (%d)%s", i, hd[i]); printf(":"); scanf("%d", &human); /* */ } while (human < 0 || human > 2); } /*--- / / ---*/ void count_no(int result) { switch (result) { case 0: draw_no++; break; /* */ case 1: lose_no++; break; /* */ case 2: win_no++; break; /* */ } } /*--- ---*/ void disp_result(int result) { switch (result) { case 0: puts(""); break; /* */ case 1: puts(""); break; /* */ case 2: puts(""); break; /* */ } } /*--- ---*/ int confirm_retry(void) { int x; printf(" ···(0) (1) :"); scanf("%d", &x); return x; } int main(void) { int judge; /* */ int retry; /**/ initialize(); /* */ do { jyanken(); /* */ /* */ printf(" %s, %s。
", hd[comp], hd[human]); judge = (human - comp + 3) % 3; /* */ count_no(judge); /* / / */ disp_result(judge); /* */ retry = confirm_retry(); /* */ } while (retry == 1); printf("%d %d %d 。
", win_no, lose_no, draw_no); return 0; }

연습 3-5
/*     (  :    /    )*/

#include 
#include 
#include 

int human;        /*       */
int comp;        /*        */
int win_no;        /*      */
int lose_no;    /*      */
int draw_no;    /*      */
char* hd[] = { "  ", "  ", " " };        /*    */

/*---      ---*/
void initialize(void)
{
    win_no = 0;        /*      */
    lose_no = 0;        /*      */
    draw_no = 0;        /*      */

    srand(time(NULL));    /*         */

    printf("      !!
"); } /*--- ( / )---*/ void jyanken(void) { static int cnt=0; int i; comp = rand() % 3; /* (0~2)*/ cnt++; do { printf("
\a ···
"); for (i = 0; i < 3; i++) printf(" (%d)%s", i, hd[i]); printf(":"); scanf("%d", &human); /* */ } while (human < 0 || human > 2); if (cnt % 5 == 0) { comp = (human + 2) % 3; } } /*--- / / ---*/ void count_no(int result) { switch (result) { case 0: draw_no++; break; /* */ case 1: lose_no++; break; /* */ case 2: win_no++; break; /* */ } } /*--- ---*/ void disp_result(int result) { switch (result) { case 0: puts(""); break; /* */ case 1: puts(""); break; /* */ case 2: puts(""); break; /* */ } } /*--- ---*/ int confirm_retry(void) { int x; printf(" ···(0) (1) :"); scanf("%d", &x); return x; } int main(void) { int judge; /* */ int retry; /**/ initialize(); /* */ do { jyanken(); /* */ /* */ printf(" %s, %s。
", hd[comp], hd[human]); judge = (human - comp + 3) % 3; /* */ count_no(judge); /* / / */ disp_result(judge); /* */ retry = confirm_retry(); /* */ } while (retry == 1); printf("%d %d %d 。
", win_no, lose_no, draw_no); return 0; }

연습 3-6
/*     (  :   3   )*/

#include 
#include 
#include 

int human;        /*       */
int comp;        /*        */
int win_no;        /*      */
int lose_no;    /*      */
int draw_no;    /*      */
int v[128];
int x[128];
int m[128];
int cnt = 0;
int i;

char* hd[] = { "  ", "  ", " " };        /*    */
char* result[] = { "  ", "   ", "   " };        /*    */

/*---      ---*/
void initialize(void)
{
    win_no = 0;        /*      */
    lose_no = 0;        /*      */
    draw_no = 0;        /*      */

    srand(time(NULL));    /*         */

    printf("      !!
"); } /*--- ( / ) ---*/ void jyanken(void) { int i; comp = rand() % 3; /* (0~2)*/ v[cnt] = comp; do { printf("
\a ···
"); for (i = 0; i < 3; i++) printf(" (%d)%s", i, hd[i]); printf(":"); scanf("%d", &human); x[cnt] = human; } while (human < 0 || human > 2); cnt++; } /*--- / / ---*/ void count_no(int result) { switch (result) { case 0: draw_no++; break; /* */ case 1: lose_no++; break; /* */ case 2: win_no++; break; /* */ } } /*--- ---*/ void disp_result(int result) { switch (result) { case 0: puts(""); break; /* */ case 1: puts(""); break; /* */ case 2: puts(""); break; /* */ } } int main(void) { int judge; /* */ i = 0; initialize(); /* */ do { jyanken(); /* */ /* */ printf(" %s, %s。
", hd[comp], hd[human]); judge = (human - comp + 3) % 3; /* */ m[i] = judge; count_no(judge); /* / / */ disp_result(judge); /* */ i++; } while (win_no < 3 && lose_no < 3); printf(win_no == 3 ? "
□ 。
" : "
■ 。
"); printf("%d %d %d 。
", win_no, lose_no, draw_no); for (i = 0; i ) { printf(" %d , %s, %s, %s
",i+1,hd[x[i]],hd[v[i]],result[m[i]]); } return 0; }

좋은 웹페이지 즐겨찾기