home *** CD-ROM | disk | FTP | other *** search
- /* set_test.c -- for testing set library
-
- by A. S. Cherdak, 12/26/88
- written for Turbo-C, Version 2.0
-
- DISCLAIMER - DISCLAIMER - DISCLAIMER - DISCLAIMER - DISCLAIMER
- Use at your own risk, The author is NOT responsible for ANY
- damages resulting from the use, proper or improper, of this
- software.
-
-
- This program is a do-little-to-nothing code for demonstrating applications
- of the functions in the set library. Throughout this code, there are usually
- several applications of the same set function. This was done to provide a
- bit of variety and to show that there are more than one way to use these
- functions. The functions included in the set library are those needed to
- implement as much of the standard Pascal set functionality as possible at
- this time.
-
- */
-
- #include <stdio.h>
- #include "sets.h"
- defset(responses,CHARACTER,256,0);
- typedef enum {apples,oranges,grapes,pears,peaches} fruit;
-
- /*************************************************************************/
- void example1()
- /*************************************************************************/
- {
- /* Example - 1: Adapted From Standard Pascal User Reference Manual --
- Subtracts characters from a set and prints the remaining characters.
- */
- char c;
- defset(alphabet,CHARACTER,256,0); /* define two sets in automatic storage */
- defset(temp,CHARACTER,256,0);
-
- /* Load one set with upper case alphabet */
- if(set_of(&alphabet,'A'__'Z',_E) != SUCCESS)
- {
- printf("Failure creating set = \"alphabet\"!");
- exit(1);
- }
-
- /* clear temporary set */
- set_clear(&temp);
-
- c = '\0';
- printf("\n\n\n********************* example1 **************************");
- printf("\nADD AND DELETE CHARACTERS FROM A SET OF 'A'__'Z'. DISPLAY DELETED");
- printf("\nAND ADDED CHARACTERS AND THE REMAINING CHARACTERS IN THE SET.");
- printf("\nUses set_difference(), set_of(), set_print(), defset(),");
- printf("\nset_clear(), and add_member().\n");
- printf("\nEnter characters to be deleted from\n");
- set_print(&alphabet);
- printf("\n");
-
- do {
- /* PRINT DELETED CHARACTERS */
- printf("%c",c);
-
- /* get character to be deleted from alphabet */
- c = getch();
-
- /* add character to temp set */
- if(add_member(&temp,c) == FAILURE)
- {
- printf("\nFailure creating set \"temp\"!");
- exit(1);
- }
- }
- while(c != 0x0d);
- printf(" DELETED\n");
- if(set_difference(&temp,&alphabet,&temp) != SUCCESS)
- {
- printf("\nFailure in set_difference!");
- exit(1);
- }
- set_print(&temp);
-
- } /* end example1 */
-
- /*************************************************************************/
- void example2()
- /*************************************************************************/
- {
- defset(seta,CHARACTER,256,0);
- defset(setb,CHARACTER,256,0);
- defset(setc,CHARACTER,256,0);
-
- printf("\n\n\n********************* example2 **************************");
- printf("\nCREATE TWO OVERLAPPING SETS. TAKE THE INTERSECTION OF THEM");
- printf("\nDUMP THE SETS AND PRINT THE CONTENTS OF THE INTERSECTION.");
- printf("\nUses set_of(), dump_set(), set_print(), set_intersect(),");
- printf("\nand kill_set().");
-
- if(set_of(&seta,'A'__'Z','a'__'z',_E) == FAILURE)
- {
- printf("\nFailure populating seta!");
- exit(1);
- }
- if(set_of(&setb,'Z'__'b',_E) == FAILURE)
- {
- printf("\nFailure populating setb!");
- exit(1);
- }
-
- printf("\nSets:");
- dump_set(&seta);
- set_print(&seta);
- dump_set(&setb);
- set_print(&setb);
- printf("\nIntersection set:");
- if(set_intersect(&setc,&seta,&setb) == FAILURE)
- {
- printf("\nFailure intersecting sets!");
- exit(1);
- }
- dump_set(&setc);
- set_print(&setc);
-
- } /* end example2 */
-
-
- /*************************************************************************/
- void example3()
- /*************************************************************************/
- {
- defset(seta,CHARACTER,256,0);
- defset(setb,CHARACTER,256,0);
- defset(setc,CHARACTER,256,0);
-
- printf("\n\n\n********************* example3 **************************");
- printf("\nCREATE TWO NON-OVERLAPPING SETS. TAKE THE UNION OF THEM");
- printf("\nDUMP THE SETS AND PRINT THE CONTENTS OF THE UNION.");
- printf("\nUses set_of(), dump_set(), print_set(), and set_union().");
-
- if(set_of(&seta,'A'__'Z',_E) == FAILURE)
- {
- printf("\nFailure populating seta!");
- exit(1);
- }
- if(set_of(&setb,'a'__'z',_E) == FAILURE)
- {
- printf("\nFailure populating setb!");
- exit(1);
- }
-
- printf("\nSets:");
- dump_set(&seta);
- set_print(&seta);
- dump_set(&setb);
- set_print(&setb);
- printf("\nUnion set:");
- if(set_union(&setc,&seta,&setb) == FAILURE)
- {
- printf("\nFailure in set_union!");
- exit(1);
- }
-
- dump_set(&setc);
- set_print(&setc);
-
- } /* end example3 */
-
-
- /*************************************************************************/
- void example4()
- /*************************************************************************/
- {
- defset(A,CHARACTER,256,0);
- defset(B,CHARACTER,256,0);
- defset(C,ENUMERATED,5,0);
- defset(D,ENUMERATED,5,0);
-
- printf("\n\n\n********************* example4 **************************");
- printf("\nTEST FOR SET EQUALITY, SET INEQUALITY, AND SET INCLUSION.");
- printf("\nCREATE FOUR SETS OF THE SAME TYPE SUCH THAT A = B AND C < D.");
- printf("\nAPPLY THE TESTS AND DISPLAY THE SETS AND THE RESULTS.");
- printf("\nUses set_equality(), set_inequality(), set_included_in(),");
- printf("\nset_includes().");
- printf("\n\n");
-
- /* set up the sets */
-
- if(set_of(&A,'A'__'Z',_E) == FAILURE)
- {
- printf("\nFailure populating set A!");
- exit(1);
- }
- if(set_of(&B,'A'__'Z',_E) == FAILURE)
- {
- printf("\nFailure populating set B!");
- exit(1);
- }
- if(set_of(&C,apples __ peaches,_E) == FAILURE)
- {
- printf("\nFailure populating set C!");
- exit(1);
- }
- if(set_of(&D,oranges __ pears,_E) == FAILURE)
- {
- printf("\nFailure populating set D!");
- exit(1);
- }
-
- printf("\nSet A "); /* test for equality 1 */
- set_print(&A);
- if(set_equality(&A,&B))
- printf(" equals ");
- else
- printf(" doesn't equal ");
- printf("Set B ");
- set_print(&B);
-
-
- printf("\nSet A "); /* test for equality 2 */
- set_print(&A);
- if(set_equality(&A,&C))
- printf(" equals ");
- else
- printf(" doesn't equal ");
- printf("Set C ");
- set_print(&C);
-
-
- printf("\nSet C "); /* test for inequality 1 */
- set_print(&C);
- if(set_inequality(&C,&D))
- printf(" doesn't equal ");
- else
- printf(" equals ");
- printf("Set D ");
- set_print(&D);
-
-
- printf("\nSet A "); /* test for inequality 2 */
- set_print(&A);
- if(set_inequality(&A,&B))
- printf(" doesn't equal ");
- else
- printf(" equals ");
- printf("Set B ");
- set_print(&B);
-
- printf("\nSet A "); /* test for inclusion 1 */
- set_print(&A);
- if(set_included_in(&A,&C))
- printf(" is included in ");
- else
- printf(" is not included in ");
- printf("Set C ");
- set_print(&C);
-
- printf("\nSet D "); /* test for inclusion 2 */
- set_print(&D);
- if(set_included_in(&D,&C))
- printf(" is included in ");
- else
- printf(" is not included in ");
- printf("Set C ");
- set_print(&C);
-
- printf("\nSet C "); /* test for includes 1 */
- set_print(&C);
- if(set_includes(&C,&D))
- printf(" includes ");
- else
- printf(" does not include ");
- printf("Set D ");
- set_print(&D);
-
- printf("\nSet D "); /* test for includes 2 */
- set_print(&D);
- if(set_includes(&D,&C))
- printf(" includes ");
- else
- printf(" does not include ");
- printf("Set C ");
- set_print(&C);
-
- } /* end example4 */
-
-
-
- /*************************************************************************/
- void next(int i)
- /*************************************************************************/
- /* gets user response */
- {
- int yesno;
-
- do {
- printf("\nNext Example (Number %d) or Quit (N or Q): ",i);
- while(!kbhit());
- yesno = getch();
- }
- while(! in_set(&responses,yesno));
-
- if(toupper(yesno) == 'Q')
- exit(0);
-
- } /* end next */
-
-
- /*************************************************************************/
- main()
- /*************************************************************************/
- {
- if(set_of(&responses,'N','n','Q','q',_E) == FAILURE)
- {
- printf("\nFailure in setting up set of responses!");
- exit(1);
- }
-
- next(1);
- example1();
- next(2);
- example2();
- next(3);
- example3();
- next(4);
- example4();
- }
-