![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
In summary, the following is the analyze_trades
program, modified to use enumeration constants with character-code
assignments.
#include#include /* Define trade structure, industry enumeration, and arrays */ struct trade {double price; int number; char *description;}; enum {food = 'F', trucking = 'T', computers = 'C', metals = 'M', health = 'H', airline = 'A'}; struct trade *trade_pointers[100]; char input_buffer[100]; /* Define auxiliary functions */ double trade_price (struct trade *x) { return x -> price * x -> number; } char* industry_name (struct trade *t) { switch (t -> description[0]) { case food: return "Food"; break; case trucking: return "Trucking"; break; // More cases default: return "Unknown"; break; } } char* company_name (struct trade *t) { char* cptr = t -> description; for (; cptr[0] != '-'; ++cptr) ; return ++cptr; } main ( ) { /* Declare various variables */ int limit, counter, number; double price; /* Read trade information */ for (limit = 0; 3 == scanf ("%s%lf%i", input_buffer, &price, &number); ++limit) { trade_pointers[limit] = (struct trade*) malloc (sizeof (struct trade)); trade_pointers[limit] -> price = price; trade_pointers[limit] -> number = number; trade_pointers[limit] -> description = (char*) malloc (strlen (input_buffer) + 1); strcpy (trade_pointers[limit] -> description, input_buffer); } /* Display report */ for (counter = 0; counter < limit; ++counter) printf ("%s %s %f %i %f\n", industry_name(trade_pointers[counter]), company_name(trade_pointers[counter]), trade_pointers[counter] -> price, trade_pointers[counter] -> number, trade_price(trade_pointers[counter])); } --- Data --- F-BLD 3.3 300 T-GG 2.5 400 AX-FLY 9.9 100 AB-CH11 10.1 100 C-IBM 5.0 200 --- Result --- Food BLD 3.300000 300 990.000000 Trucking GG 2.500000 400 1000.000000 Airline FLY 9.900000 100 990.000000 Airline CH11 10.100000 100 1010.000000 Computers IBM 5.000000 200 1000.000000