Home Segments Index Top Previous Next

514: Mainline

Here is the revised program, with an addition to the printf statement that displays the recorded trade description:

#include   
#include  
/* Define the trade apparatus */ 
struct trade {double price; int number; char *description;}; 
/* Define trade array and input buffer array*/ 
struct trade *trade_pointers[100]; 
char input_buffer[100]; 
/* Define price-computing function */ 
double trade_price (struct trade *x) { 
  return x -> price * x -> number; 
} 
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 %f %i %f\n", 
            trade_pointers[counter] -> description, 
            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 --- 
F-BLD 3.300000 300 990.000000 
T-GG 2.500000 400 1000.000000 
AX-FLY 9.900000 100 990.000000 
AB-CH11 10.100000 100 1010.000000 
C-IBM 5.000000 200 1000.000000