/* Program looking at the use of union and struct and typedef and enum and bitfields and char * arrays and #define R.Metcalfe 23/9/2013 RJM Programming http://www.rjmprogramming.com.au */ #include #include #define NUM_CARDS 52 #define NUM_SUITS 4 #define NUM_FACES 13 /* enum for personal snapshot information */ typedef enum characteristic { i_happy = 0, i_hot, i_sweaty, i_anxious, i_speakinging, i_listening, i_travelling, i_sitting, i_standing, i_lying, i_reading, i_eating, i_drinking, i_cooking, i_waiting, i_itchy} adjective; typedef enum thesuits { clubs = 1, spades, diamonds, hearts } asuit; typedef enum thefacevalue { ace = 1, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king } aface; /* struct for deck of cards */ typedef struct onecard { enum thesuits cardsuit; enum thefacevalue cardvalue; } deck [ NUM_CARDS ]; char *csuits[ NUM_SUITS + 1 ] = { "", "Clubs", "Spades", "Diamonds", "Hearts" }; char *cfaces[ NUM_FACES + 1 ] = { "", "Ace of ", "2 of ", "3 of ", "4 of ", "5 of ", "6 of ", "7 of ","8 of ", "9 of ", "10 of ", "Jack of ", "Queen of ", "King of " }; /* struct for imaginary function storage */ typedef struct imaginary_number { double real_number; double imaginary_number; } ourimaginary_value; /* struct for personal snapshot information */ typedef struct snapshot { unsigned happy : 1; unsigned hot : 1; unsigned sweaty : 1; unsigned anxious : 1; unsigned speaking : 1; unsigned listening : 1; unsigned travelling : 1; unsigned sitting : 1; unsigned standing : 1; unsigned lying : 1; unsigned reading : 1; unsigned eating : 1; unsigned drinking : 1; unsigned cooking : 1; unsigned waiting : 1; unsigned itchy : 1; } snapshot_value; /* our all purpose union */ union uvalue { snapshot snapshot_value; int num_fingers_guess; double celsius_temperature; imaginary_number ourimaginary_value; onecard deck [ NUM_CARDS ]; }; int main() { char ans = 'N'; double num; /* enum characteristic of interest */ int j_cooking = characteristic(i_cooking); /* our all purpose union instance */ union uvalue ourunion; /* Sizeof union analysis */ printf("Size of union uvalue ourunion is %ld\n", sizeof(union uvalue)); printf("Size of struct snapshot snapshot_value is %ld\n", sizeof(ourunion.snapshot_value)); printf("Size of int num_fingers_guess is %ld\n", sizeof(ourunion.num_fingers_guess)); printf("Size of double celsius_temperature is %ld\n", sizeof(ourunion.celsius_temperature)); printf("Size of struct imaginary_number ourimaginary_value is %ld\n", sizeof(ourunion.ourimaginary_value)); printf("Size of struct onecard deck [ NUM_CARDS ] is %ld\n\n", sizeof(ourunion.deck)); printf("Memory pointer &ourunion is %ld\n", (unsigned long)&ourunion); /* Characteristics (snapshot) union usage */ printf("\n\nAre you itchy? Y/N? "); scanf(" %c", &ans); if (ans == 'Y' || ans == 'y') ourunion.snapshot_value.itchy = true; printf("Memory pointer &ourunion.snapshot_value is %ld\n", (unsigned long)&ourunion.snapshot_value); printf("Memory pointer &ourunion.snapshot_value.cooking is %ld\n", (unsigned long)&ourunion.snapshot_value + j_cooking); printf("Value of ourunion.snapshot.itchy is %d\n", (int)ourunion.snapshot_value.itchy); printf("Value of ourunion.num_fingers_guess is %d\n", ourunion.num_fingers_guess); /* How many fingers union usage */ printf("\n\nHow many fingers (no thumbs please) am I holding up? "); scanf(" %c", &ans); if (ans >= '0' && ans <= '9') ourunion.num_fingers_guess = (ans - '0'); printf("Value of ourunion.num_fingers_guess is %d\n", ourunion.num_fingers_guess); printf("Value of ourunion.snapshot.cooking is %d\n", (int)ourunion.snapshot_value.cooking); printf("Memory pointer &ourunion.num_fingers_guess is %ld\n", (unsigned long)&ourunion.num_fingers_guess); printf("Value of &ourunion.celsius_temperature is %lf\n", ourunion.celsius_temperature); /* Celcius temperature union usage */ printf("\n\nWhat is the temperature in Celcius degrees? "); scanf(" %lf", &ourunion.celsius_temperature); printf("Value of ourunion.num_fingers_guess is %d\n", ourunion.num_fingers_guess); printf("Value of &ourunion.celsius_temperature is %lf\n", ourunion.celsius_temperature); printf("Memory pointer &ourunion.celsius_temperature is %ld\n", (unsigned long)&ourunion.celsius_temperature); printf("Value of ourunion.ourimaginary_value.real_number is %lf\n", ourunion.ourimaginary_value.real_number); printf("Value of ourunion.ourimaginary_value.imaginary_number is %lf\n", ourunion.ourimaginary_value.imaginary_number); /* Imaginary number union usage */ printf("\n\nWhat is a number of which you want to know the square root (it can be negative)? "); scanf(" %lf", &num); if (num >= 0.0) { ourunion.ourimaginary_value.real_number = pow(num, 0.5); ourunion.ourimaginary_value.imaginary_number = 0.0; } else { ourunion.ourimaginary_value.imaginary_number = pow(-num, 0.5); ourunion.ourimaginary_value.real_number = 0.0; } printf("Value of ourunion.ourimaginary_value.real_number is %lf\n", ourunion.ourimaginary_value.real_number); printf("Value of ourunion.ourimaginary_value.imaginary_number is %lf\n", ourunion.ourimaginary_value.imaginary_number); printf("Memory pointer &ourunion.ourimaginary_value is %ld\n", (unsigned long)&ourunion.ourimaginary_value); printf("Memory pointer &ourunion.deck is %ld\n", (unsigned long)&ourunion.deck); printf("Memory pointer &ourunion.deck[10] is %ld\n", (unsigned long)&ourunion.deck[10]); /* Playing card union usage */ num = -1.0; printf("\n\nWhat is your hidden playing card face value out of A/K/Q/J/10/9/8/7/6/5/4/3/2 ? "); scanf(" %c", &ans); ourunion.deck[0].cardvalue = ten; while (num < 0.0) { num = 1.0; if (ans == '1') ourunion.deck[0].cardvalue = ten; else if (ans == '2') ourunion.deck[0].cardvalue = two; else if (ans == '3') ourunion.deck[0].cardvalue = three; else if (ans == '4') ourunion.deck[0].cardvalue = four; else if (ans == '5') ourunion.deck[0].cardvalue = five; else if (ans == '6') ourunion.deck[0].cardvalue = six; else if (ans == '7') ourunion.deck[0].cardvalue = seven; else if (ans == '8') ourunion.deck[0].cardvalue = eight; else if (ans == '9') ourunion.deck[0].cardvalue = nine; else if (ans == 'J' || ans == 'j') ourunion.deck[0].cardvalue = jack; else if (ans == 'Q' || ans == 'q') ourunion.deck[0].cardvalue = queen; else if (ans == 'K' || ans == 'k') ourunion.deck[0].cardvalue = king; else if (ans == 'A' || ans == 'a') ourunion.deck[0].cardvalue = ace; else { num = -1.0; printf("\nWhat is your hidden playing card face value out of A/K/Q/J/10/9/8/7/6/5/4/3/2 ? "); scanf(" %c", &ans); } } num = -1.0; printf("What is your hidden playing card suit out of C/S/D/H ? "); scanf(" %c", &ans); while (num < 0.0) { num = 1.0; if (ans == 'C' || ans == 'c') ourunion.deck[0].cardsuit = clubs; else if (ans == 'S' || ans == 's') ourunion.deck[0].cardsuit = spades; else if (ans == 'D' || ans == 'd') ourunion.deck[0].cardsuit = diamonds; else if (ans == 'H' || ans == 'h') ourunion.deck[0].cardsuit = hearts; else { num = -1.0; printf("\nWhat is your hidden playing card suit out of C/S/D/H ? "); scanf(" %c", &ans); } } printf("Value of ourunion.deck[0] is %s%s\n", cfaces[(int)ourunion.deck[0].cardvalue], csuits[(int)ourunion.deck[0].cardsuit]); printf("Value of ourunion.deck[0].cardvalue is %d\n", (int)ourunion.deck[0].cardvalue); printf("Value of ourunion.deck[0].cardsuit is %d\n", (int)ourunion.deck[0].cardsuit); }