int main(int argc, const char * argv[]) {
    
    int a = 1, b = 3, c = 5, d = 7,
        e = 2, f = 4, g = 6, h = 8;
    void *ptr1 = &a, *ptr2 = &b, *ptr3 = &c, *ptr4 = &d,
         *ptr5 = &e, *ptr6 = &f, *ptr7 = &g, *ptr8 = &h;
    
    struct Ordered_container* container1 = OC_create_container( (OC_comp_fp_t)compare_int_ptr);
    
    
    assert(container1);
    OC_insert(container1, ptr3);
    print_containter(container1); // 5
    OC_insert(container1, ptr2);
    print_containter(container1); // 3 5
    OC_insert(container1, ptr6);   // 4
    print_containter(container1);  // 3 4 5
    OC_insert(container1, ptr4);
    print_containter(container1); // 3 4 5 7
    OC_insert(container1, ptr1);
    print_containter(container1); // 1 3 4 5 7
    OC_insert(container1, ptr8);
    print_containter(container1); // 1 3 4 5 7 8
    
    printf("%d\n",OC_get_size(container1)); // 6
    printf("%d\n",OC_empty(container1));    // 0
    OC_clear(container1);
    printf("%d\n",OC_get_size(container1));
    
    
    OC_insert(container1, ptr3);
    print_containter(container1); // 5
    OC_insert(container1, ptr2);
    print_containter(container1); // 3 5
    OC_insert(container1, ptr4);
    print_containter(container1); // 3 5 7
    OC_insert(container1, ptr1);
    print_containter(container1); // 1 3 5 7
    OC_insert(container1, ptr8);
    print_containter(container1); // 1 3 5 7 8
    
    
    //OC_destroy_container(container1);
    
    void* found_item_ptr1 = OC_find_item(container1, ptr4);
    
    if (!found_item_ptr1) {
        printf("found_item_ptr not found");
    } else {
        printf("found_item_ptr");
        printf(" and it's %d\n", *(int*)OC_get_data_ptr(found_item_ptr1));
    }
    
    OC_delete_item(container1, found_item_ptr1);
    print_containter(container1); // 1 3 5 8

    void* found_item_ptr2 = OC_find_item(container1, ptr1);
    OC_delete_item(container1, found_item_ptr2);
    void* found_item_ptr3 = OC_find_item(container1, ptr8);
    OC_delete_item(container1, found_item_ptr3);
    
    print_containter(container1); // 3 5
    
    OC_insert(container1, ptr6);   // 4
    print_containter(container1);  // 3 4 5
    OC_insert(container1, ptr5);   // 2
    print_containter(container1);  // 2 3 4 5
    printf("%d\n",OC_get_size(container1)); // 4

    OC_apply(container1, (OC_apply_fp_t)print_int); // 2 3 4 5
    printf("\n");
    

    

    
    
    return 0;
    
    
}
Ejemplo n.º 2
0
int main( void )
{
    int i;
    
    /* holds both command characters */
    char command[ 2 ];
    
    /* set up the three libraries */
    struct Ordered_container* lib_title = OC_create_container( comp_Record_by_title );
    struct Ordered_container* lib_ID    = OC_create_container( comp_Record_by_ID );
    struct Ordered_container* catalog   = OC_create_container( comp_Collection_by_name );
    
    for ( ; ; )
    {
        printf( "\nEnter command: " );
        
        /* load the command chars */
        for ( i = 0 ; i < 2; ++i)
        {
            command[ i ] = get_command_char();
        }
        
        switch ( command[ 0 ] )
        {
            case 'f' :/* find (records only)*/
                switch ( command[ 1 ] )
                {
                    case 'r' :
                        find_record_print( lib_title );
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
                break;
            case 'p' : /* print */
                switch ( command[ 1 ] )
                {
                    case 'r' :
                        print_record( lib_ID );
                        break;
                    case 'L':
                        print_containter( lib_title, "Library", "records", (void (*)(void*))print_Record );
                        break;
                    case 'C':
                        /* print_catalog( catalog ); */
                        print_containter( catalog, "Catalog", "collections", (void (*)(void*))print_Collection );
                        break;
                    case 'a': /* allocation */
                        print_allocation( lib_title, lib_ID, catalog );
                        break;
                    case 'c':
                        print_collection_main( catalog );
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
                break;
            case 'm': /* modify (rating only) */
                switch ( command[ 1 ] )
                {
                    case 'r':
                        modify_rating( lib_ID );
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
                break;
            case 'a' : /* add */
                switch ( command[ 1 ] )
                {
                    case 'r' :
                        add_record( lib_title, lib_ID );
                        break;
                    case 'c':
                        add_coll( catalog );
                        break;
                    case 'm':
                        add_member( lib_ID , catalog );
                        break;
                    case 'a': /* allocation */
                        /* throw error */
                        print_error_clear( "Unrecognized command!\n");
                        break;
                    default:
                        break;
                }
                break;
            case 'd': /* delete */
                switch ( command[ 1 ] )
                {
                    case 'r' :
                        delete_record( lib_title, lib_ID, catalog );
                        break;
                    case 'c':
                        delete_collection( catalog );
                        break;
                    case 'm':
                         remove_member( lib_ID , catalog );
                        break;
                    case 'a': /* allocation */
                        /* throw error */
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
                break;
            case 'c': /* clear */
                switch ( command[ 1 ] )
                {
                    case 'L':
                        clear_library( lib_title, lib_ID, catalog, "All records deleted\n" );
                        break;
                    case 'C':
                        clear_container( catalog, ( void(*)(void*) )destroy_Collection, "All collections deleted\n" );
                        break;
                    case 'A':
                        clear_all(lib_title, lib_ID, catalog, "All data deleted\n" );
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
                break;
            case 's': /* save */
                switch ( command[ 1 ] ) {
                    case 'A':
                        save_all_to_file( lib_title, catalog );
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
                break;
            case 'r': /* restore */
                switch ( command[ 1 ] ) {
                    case 'A':
                        load_from_file( lib_title, lib_ID, catalog );
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
                break;
            case 'q':
                switch ( command[ 1 ] )
                {
                    case 'q':
                        /* clean up memory */
                        quit( lib_title, lib_ID, catalog );
                        break;
                    default:
                        print_error_clear( "Unrecognized command!\n");
                        break;
                }
            default:
                /* throw error for bad input */
                print_error_clear( "Unrecognized command!\n");
                break;
        }
    }
	return 0;
}