This is an old revision of the document!


아래 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>
*/

#define MAX_WRONG_CNT 6

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 getAlphaCapital( void )
{
/*C
    char c;
    fseek(stdin,0,SEEK_END);
    
    do {
        printf("Please enter a letter: ");
        c = getchar();
    }while( ! isalpha(c) );
    return c;
*/    
}
 
class/os2014f/hw3.1411791493.txt.gz · Last modified: 2025/10/13 12:59 (external edit) · [Old revisions]
Recent changes RSS feed Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki