Thread: C/C++ examples

  1. #16
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    OK, I guess I'll add my own AutoPtr class onto this thread.
    http://tech.groups.yahoo.com/group/boost/files/AutoPtr/

  2. #17
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    My version of animal guessing game in C:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    struct tree_t
    {
            char* item;
            struct tree_t* left;
            struct tree_t* right;
    };        
    
    void getstr(char* s, int n)
    {
            char* p;
            fgets(s, n, stdin);
            for (p=s; *p; ++p)
            {
                    if (*p == '\n')
                    {
                            *p = '\0';
                            break;
                    }
            }
    }
    
    int yes()
    {
            char buff[16];
            fgets(buff, 16, stdin);
            return (tolower(buff[0]) == 'y');
    }
    
    int main()
    {
    
            struct tree_t* tree;
            char* guess;
            char* animal;
            char* ques;
    
            tree = malloc(sizeof(struct tree_t));
            tree->item = "bird";
            tree->left = tree->right = 0;
    
            while (1)
            {
                    printf("Are you thinking of an animal? ");
                    if (! yes()) break;
            
                    while(tree->left)
                    {
                            printf("&#37;s? ", tree->item);
                            if (yes())
                                    tree = tree->right;
                            else
                                    tree = tree->left;
                    }
            
                    guess = tree->item;
                    printf("Is it %s? ", guess);
                    if (yes())
                    {
                            printf("I rule!\n");
                            continue;
                    }
            
                    printf("What is the animal's name? ");
                    animal = malloc(64);
                    getstr(animal, 64);
                    printf("What question would distinguish a %s from a %s? ", animal, guess);
                    ques = malloc(256);
                    getstr(ques, 256);
                            
                    tree->item = ques;
                    printf("If the animal were %s the answer would be? ", animal);
                    tree->left = malloc(sizeof(struct tree_t));
                    tree->right = malloc(sizeof(struct tree_t));
                    if (yes())
                    {
                            tree->left->item = guess;
                            tree->right->item = animal;
                    }
                    else
                    {
                            tree->left->item = animal;
                            tree->right->item = guess;
                    }
                    tree->left->left = tree->left->right = 0;
                    tree->right->left = tree->right->right = 0;
            }
            return 0;
    }
    Last edited by msp; 09-05-2007 at 02:41 AM. Reason: Corrected some minor code errors

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > #include <malloc.h>
    Use stdlib.h for prototyping malloc

    > return ((buff[0] | 32) == 'y');
    toupper() or tolower() would be a lot easier to understand, and not limited to systems using ASCII.

    Your main() has no return statement.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    Thanks Salem for pointing this out!
    I have made the modifications.

  5. #20
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    Print the binary tree on console in a visual way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows - how to get started... examples don't help
    By nonoob in forum Windows Programming
    Replies: 6
    Last Post: 09-26-2008, 05:45 AM
  2. Thread Pool libraries or examples
    By Mastadex in forum Windows Programming
    Replies: 6
    Last Post: 08-24-2008, 08:58 PM
  3. Serial/Activation Key Validation Examples
    By Welder in forum C Programming
    Replies: 0
    Last Post: 11-04-2007, 10:34 PM
  4. Examples of C++ variables
    By CrazYjoe in forum C++ Programming
    Replies: 9
    Last Post: 01-20-2007, 01:33 PM
  5. Treeview examples.
    By Sebastiani in forum Windows Programming
    Replies: 0
    Last Post: 09-21-2003, 12:16 PM