아래 C코드를 C++코드로 바꾸어라. 특히 /*C….*/로 컴멘트 처리된 부분은 반드시 같은 기능을 하는 C++코드로 변환하여라. 컴파일이 가능하고 실행까지 가능해야 함.

실행예:

There are 4 words stored.
The 1 th word is apple.
The 2 th word is pear.
The 3 th word is peach.
The 4 th word is banana.
The randomly chosen word is peach.
Please enter a letter: t
You entered a letter t.
Wrong count = 0
<draw_hangman(0) 결과>
Wrong count = 1
<draw_hangman(1) 결과> 
...
(생략)
Wrong count = 6
<draw_hangman(6) 결과>

코드

/*C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
*/

void draw_hangman( int n );
char getAlpha( void );

int main( void )
{
    
    //****************************************************//
    // Static Word List //
    // ***************************************************//

/*C
    char *wordList[] = { "apple", "pear", "peach", "banana" };
    int wordCount = sizeof(wordList) / sizeof(wordList[0]);
*/
        
    //**************************************//
    // Display Word List //
    // *************************************//
    
/*C
    printf("There are %d words stored.\n", wordCount );
    
    for(int i=0; i<wordCount; i++) {
        printf("The %d th word is %s.\n", i+1, wordList[i]);
    }
    
*/
    
    //**************************************//
    // Pick a word at random //
    // *************************************//
    
/*C
    // get current time
    time_t t;
    time(&t);
    
    // set random seed
    srand( t );
    
    char *theAnswer = wordList[ rand() % wordCount ];
    printf( "The randomly chosen word is %s.\n", theAnswer );
*/
            
/*C
    printf( "Please enter a letter: ");
    char r = getAlpha();
    
    printf("You entered a letter %c.\n", r );
*/
    for( int i=0; i < 7; i++ ) {
       printf( "Wrong count = %d\n", i );
       draw_hangman(i);
    }
    
    system("pause");
    
    return 0;
}

void draw_hangman( int n )
{
/*C
    printf("     __   \n");
    printf("    |  |  \n");
    if( n == 0 ) {
        printf("    |     \n");
        printf("    |     \n");
        printf("    |     \n");
    }
    else if( n == 1 ) {
        // Draw a hangman
        printf("    |  O  \n");
        printf("    |     \n");
        printf("    |     \n");
    }
    else if( n == 2 ) {
        // Draw a hangman
        printf("    |  O  \n");
        printf("    |  @  \n");
        printf("    |     \n");
    }
    else if( n == 3 ) {
        // Draw a hangman
        printf("    |  O  \n");
        printf("    | /@  \n");
        printf("    |     \n");
    }
    else if( n == 4 ) {
        // Draw a hangman
        printf("    |  O  \n");
        printf("    | /@) \n");
        printf("    |     \n");
    }
    else if( n == 5 ) {
        // Draw a hangman
        printf("    |  O  \n");
        printf("    | /@) \n");
        printf("    | /   \n");
    }
    else if( n == 6 ) {
        // Draw a hangman
        printf("    |  O  \n");
        printf("    | /@) \n");
        printf("    | / ) \n");
    }
    printf("    |     \n");
    printf("  __|__   \n");
*/
}

char getAlpha( void )
{
/*C
    char c;
    fseek(stdin,0,SEEK_END);
    
    do {
        printf("Please enter a letter: ");
        c = getchar();
    }while( ! isalpha(c) );
    return c;
*/    
}

채점:

총점 10점. 컴파일 안되면 -7. C++로 수정이 안된 부분이 있으면 부분 감점. draw_hangman()에서 string 변수를 사용해서 코드를 짧게 구현하면 가산점 최대 5점. 이 가산점은 숙제점수 총점에 가산됨. 숙제가 만점이면 전체 총점에 반영 됨.

 
class/os2014f/hw3.txt · Last modified: 2017/06/17 09:36 (external edit) · [Old revisions]
Recent changes RSS feed Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki