/* Create a Meeting object. This is the only function that allocates memory for a Meeting and the contained data. */ struct Meeting* create_Meeting(int time, const char* topic) { struct Meeting* newMeeting = malloc(sizeof(struct Meeting)); newMeeting->time = time; newMeeting->topic = strAllocCpy(topic); newMeeting->participants = OC_create_container(comparePeople); return newMeeting; }
int main(void) { char* topic1 = "Hello"; int time1 = 10; int room1 = 1001; struct Meeting* Meeting1 = create_Meeting(time1, topic1); struct Room* Room1 = create_Room(room1); printf("%d\n", add_Room_Meeting(Room1, Meeting1)); // 0 printf("%d\n", add_Room_Meeting(Room1, Meeting1)); // -1 print_Meeting(find_Room_Meeting(Room1, time1)); printf("%d\n", remove_Room_Meeting(Room1, Meeting1)); // 0 printf("%d\n", remove_Room_Meeting(Room1, Meeting1)); // -1 printf("%d\n", add_Room_Meeting(Room1, Meeting1)); // 0 clear_Room(Room1); print_Room(Room1); const char *topicA = "Whisky", *topicB = "Tango"; const char *firstname1 = "Bo", *lastname1 = "Jackson", *phoneno1 = "123"; const char *firstname2 = "Mike", *lastname2 = "David", *phoneno2 = "456"; struct Person* person1 = create_Person(firstname1, lastname1, phoneno1); struct Person* person2 = create_Person(firstname2, lastname2, phoneno2); struct Meeting *new_Meeting1 = create_Meeting(10, topicA); struct Meeting *new_Meeting2 = create_Meeting(9, topicB); struct Ordered_container* people = OC_create_container((OC_comp_fp_t) compare_Person_lastname); OC_insert(people, person1); OC_insert(people, person2); struct Room* room2 = create_Room(1000); add_Meeting_participant(new_Meeting1, person1); add_Meeting_participant(new_Meeting1, person2); add_Room_Meeting(room2, new_Meeting1); add_Room_Meeting(room2, new_Meeting2); printf("\n...saveload/file...\n"); FILE *fp = fopen("Room_savefile.txt", "w"); save_Room(room2, fp); fclose(fp); fp = fopen("Room_savefile.txt", "r"); struct Room *new_Room = load_Room(fp, people); print_Room(new_Room); fclose(fp); }
int main() { while (1) { char cmd1; char cmd2; struct Ordered_container* people; people = OC_create_container((OC_comp_fp_t)comparePeople); printf("\nEnter command: "); scanf(" %c", &cmd1); scanf("%c", &cmd2); switch (cmd1) { case 'p': break; case 'a': switch (cmd2) { case 'i': { addIndividual(people); } break; default: printErrUnrecCmd(); break; } break; case 'r': break; case 'd': break; case 's': break; case 'l': break; case 'q': switch (cmd2) { case 'q' : return 0; break; default: printErrUnrecCmd(); break; } break; default: printErrUnrecCmd(); break; } } return 0; }
int main(void) { //OC_comp_fp_t f_ptr; struct Ordered_container* container_ptr = OC_create_container(&thing_comp_f); OC_insert(container_ptr, make_thing(5)); OC_insert(container_ptr, make_thing(3)); OC_insert(container_ptr, make_thing(4)); OC_insert(container_ptr, make_thing(1)); OC_insert(container_ptr, make_thing(2)); OC_insert(container_ptr, make_thing(99)); OC_insert(container_ptr, make_thing(-2)); for (int i = 0; i < 7; ++i) { printf("Element %d: %d\n", i, *(int*)(*(container_ptr->array + i))); } printf("size = %d\n", OC_get_size(container_ptr)); printf("Empty = %d\n", OC_empty(container_ptr)); int search_num = 5; int* data_ptr = OC_find_item(container_ptr, &search_num); printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr)); search_num = 99; printf("Apply_if_arg returned: %d\n", OC_apply_if_arg(container_ptr, (OC_apply_if_arg_fp_t)apply_if_arg_tester, &search_num)); data_ptr = OC_find_item_arg(container_ptr, &search_num, alt_thing_comp_f); printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr)); printf("Things: %d\n", g_num_things); OC_apply(container_ptr, &delete_thing); OC_clear(container_ptr); printf("Things: %d\n", g_num_things); printf("size = %d\n", OC_get_size(container_ptr)); printf("Empty = %d\n", OC_empty(container_ptr)); OC_destroy_container(container_ptr); void* test = malloc(8); printf("Old way %p New Way %p\n", *(void**)test, test); return 0; }
int main(void) { //OC_comp_fp_t f_ptr; struct Ordered_container* container_ptr = OC_create_container(&thing_comp_f); OC_insert(container_ptr, make_thing(5)); OC_insert(container_ptr, make_thing(3)); OC_insert(container_ptr, make_thing(4)); OC_insert(container_ptr, make_thing(1)); OC_insert(container_ptr, make_thing(2)); OC_insert(container_ptr, make_thing(99)); OC_insert(container_ptr, make_thing(-2)); printf("size = %d\n", OC_get_size(container_ptr)); printf("Empty = %d\n", OC_empty(container_ptr)); Print_container(container_ptr, sizeof(int)); int search_num = 5; int* data_ptr = OC_find_item(container_ptr, &search_num); printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr)); search_num = 99; printf("Apply_if_arg returned: %d\n", OC_apply_if_arg(container_ptr, (OC_apply_if_arg_fp_t)apply_if_arg_tester, &search_num)); data_ptr = OC_find_item_arg(container_ptr, &search_num, alt_thing_comp_f); printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr)); printf("Things: %d\n", g_num_things); OC_apply(container_ptr, &delete_thing); OC_clear(container_ptr); printf("Things: %d\n", g_num_things); printf("size = %d\n", OC_get_size(container_ptr)); printf("Empty = %d\n", OC_empty(container_ptr)); Print_container(container_ptr, sizeof(int)); OC_destroy_container(container_ptr); return 0; }
int main(void) { /* test with some Point objects - the container items hold their addresses, not the actual Point structs! */ /* "brace initialization" is convenient for structure types */ struct Point s1 = {1, 1}; struct Point s2 = {1, 2}; struct Point s3 = {2, 1}; struct Point s4 = {2, 2}; struct Point probe1 = {1, 2}; struct Point probe2 = {99, 99}; struct Ordered_container * container; container = OC_create_container(compare_Points); printf("size is %d\n", OC_get_size(container)); OC_apply(container, print_as_Point); /* fill the container with s1, s2, s3, s4 but insert in this order: s3, s1, s4, s2, and see if the points get put in the right order. */ printf("\ninserting (%d, %d)\n", s3.x, s3.y); OC_insert(container, &s3); printf("size is %d\n", OC_get_size(container)); OC_apply(container,print_as_Point); printf("\ninserting (%d, %d)\n", s1.x, s1.y); OC_insert(container, &s1); printf("size is %d\n",OC_get_size(container)); OC_apply(container, print_as_Point); printf("\ninserting (%d, %d)\n", s4.x, s4.y); OC_insert(container, &s4); printf("size is %d\n", OC_get_size(container)); OC_apply(container, print_as_Point); printf("\ninserting (%d, %d)\n", s2.x, s2.y); OC_insert(container, &s2); printf("size is %d\n", OC_get_size(container)); OC_apply(container, print_as_Point); /* use find_item and delete_item */ find_and_remove(container, probe1); printf("size is %d\n", OC_get_size(container)); OC_apply(container, print_as_Point); find_and_remove(container, probe2); printf("size is %d\n", OC_get_size(container)); OC_apply(container, print_as_Point); /* demo another apply function - note function pointer cast is not needed for this one */ { int odd_or_even_value = 42; OC_apply_arg(container, demo_func, (void *)&odd_or_even_value); odd_or_even_value = 3; OC_apply_arg(container, demo_func, (void *)&odd_or_even_value); } OC_clear(container); printf("size is %d\n", OC_get_size(container)); OC_apply(container, print_as_Point); OC_destroy_container(container); /* using the pointer "container" is undefined at this point */ printf("Done\n"); return 0; }
int main( void ) { int i; int cur_size_container = 0; char* test_string[] = { "t5", "t6", "t3", "t9", "t7", "t1", "t2" }; int size_test = 7; char* s1 = "s1"; /*char* s2 = "s2"; char* s3 = "s3"; char* s4 = "s4"; char* s5 = "s5"; char* s6 = "s6"; char* probe1 = "s3"; char* probe2 = "s99"; char* probe3 = "t2"; char* probe4 = "t11";*/ void* node; struct Ordered_container * container; printf("Starting test\nCreating\n" ); /* create the container */ container = OC_create_container((int (*)(const void*, const void*))compare_string); for ( i = 0 ; i < size_test ; ++i ) { printf("Inserting %s\n", test_string[ i ] ); OC_insert( container, test_string[ i ] ); cur_size_container = OC_get_size( container ); printf("\tsize is %d\n", cur_size_container ); /* make sure it has the correct size */ assert( ( i + 1 ) == cur_size_container ); if ( /*( i % 3 ) == 1 */ 1 ) { OC_apply( container, (void (*)(void*))print_as_string) ; } } assert( OC_apply_if( container, (int (*)(void*))right_form ) == 0 ); printf( "Inserting s1\n " ); OC_insert( container, s1 ); OC_apply( container, (void (*)(void*))print_as_string) ; assert( OC_apply_if( container, (int (*)(void*))right_form ) == ('s' - 't' ) ); node = OC_find_item( container, s1 ); OC_apply( container, (void (*)(void*))print_as_string) ; /* check the correct pointer is returned */ printf( "\nAfter adding s1\n" ); assert( OC_get_data_ptr( node ) == s1 ); /* check to make sure delete is working */ OC_delete_item( container, node ); assert( OC_get_size( container ) == size_test ); /* check clear function */ OC_clear( container ); assert( OC_empty( container ) != 0 ); /* rebuild array to run more tests */ for ( i = 0 ; i < size_test ; ++i ) { OC_insert( container, test_string[ i ] ); cur_size_container = OC_get_size( container ); /* make sure it has the correct size */ assert( ( i + 1 ) == cur_size_container ); } /* removing one element at a time */ for( i = 0 ; i < size_test ; ++i ) { printf( "%d: removing %s\n", i , test_string[i] ); OC_apply( container, (void (*)(void*))print_as_string) ; find_and_remove( container, test_string[i] ); assert( ( size_test - i - 1 ) == OC_get_size( container ) ); } /* clean up memory */ /* run with valgrind to ensure no mem leaks */ OC_destroy_container( container ); return 0; }
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; }
int main() { struct Ordered_container* people; struct Ordered_container* rooms; people = OC_create_container((OC_comp_fp_t)comparePeople); rooms = OC_create_container((OC_comp_fp_t)compareRooms); while (1) { char cmd1; char cmd2; printf("\nEnter command: "); scanf(" %c", &cmd1); scanf("%c", &cmd2); switch (cmd1) { case 'p': switch (cmd2) { case 'i': printIndividual(people); break; case 'r': printRoom(rooms); break; case 'm': printMeeting(rooms); break; case 's': printSchedule(rooms); break; case 'g': printGroup(people); break; default: printErrUnrecCmd(); break; } break; case 'a': switch (cmd2) { case 'i': addIndividual(people); break; case 'r': addRoom(rooms); break; case 'm': addMeeting(rooms); break; case 'p': addParticipant(rooms, people); break; default: printErrUnrecCmd(); break; } break; case 'r': switch (cmd2) { case 'm': rescheduleMeeting(rooms); break; default: printErrUnrecCmd(); break; } break; case 'd': switch (cmd2) { case 'i': deleteIndividual(people, rooms); break; case 'r': deleteRoom(rooms); break; case 'm': deleteMeeting(rooms); break; case 'p': deleteParticipant(rooms, people); break; case 's': deleteSchedule(rooms); break; case 'g': deleteGroup(rooms, people); break; case 'a': deleteAll(rooms, people); break; default: printErrUnrecCmd(); break; } break; case 's': break; case 'l': break; case 'q': switch (cmd2) { case 'q' : printf("All meetings deleted\n"); printf("All rooms deleted\n"); printf("All persons deleted\n"); printf("Done\n"); return 0; break; default: printErrUnrecCmd(); break; } break; default: printErrUnrecCmd(); break; } } return 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; }