/* ---------------------------------------------------------------------- ** This program combines the program fragments in sections 14.1 and 14.2. Figure 14.2: A struct type declaration. Figure 14.3: Declarating and initializing a structure. Figure 14.4: Set and use a pointer to a structure. Figure 14.5: Access one member of a structure. Figure 14.6: Structure assignment. Figure 14.7: Returning a structure from a function. Figure 14.8: Call by value with a structure. Figure 14.9: Call by address with a structure. Figure 14.10: An array of structures. Figure 14.11: Comparing two structures. Figure 14.12: Declarations for the lumber program. Figure 14.13: Structure operations. ** ---------------------------------------------------------------------- */ #include "tools.h" typedef struct LUMBER { /* Type for a piece of lumber */ char type [11]; /* Type of wood. */ short int height, width; /* In inches. */ short int length; /* In feet. */ float price; /* Per board, in dollars. */ int quantity; /* Number of items in stock. */ } lumber_t; lumber_t read_lumber( void ); void print_lumber( lumber_t b ); void sell_lumber( int sold, lumber_t* board ); bool boards_equal ( lumber_t board1, lumber_t board2 ); // ------------------------------------------------------------------ void main( void ) { int k; /* Loop counter. */ /* Structured object declarations: Figures 14.3, 14.4, 14.10. */ lumber_t sale; lumber_t plank = { "white oak", 1, 6, 8, 5.80, 158 }; lumber_t* p; lumber_t stock[4] = { { "spruce", 2, 4, 12, 8.20, 27}, { "pine", 2, 3, 10, 5.45, 11}, { "pine", 2, 3, 8, 4.20, 35 }, { "" }, /* Remaining members will be set to 0. */ }; puts( " Demo program for structure operations.\n" ); /* Access parts of a structure; Figures 14.4, 14.5. ------------ */ printf( " Plank price is $%.2f\n", plank.price ); p = & plank; /* Point to a structure. */ printf( " Planks in stock: %i\n", p->quantity ); sell_lumber( 200, p ); /* A pointer argument. */ /* Function calls using structs; Figures 14.2.2. --------------- */ sale = read_lumber(); /* Return a structured result. */ print_lumber( sale ); /* Call by value. */ sell_lumber( 2, &sale ); /* Call by address. */ /* Accessing an array of structures; Section 14.2.3. ----------- */ puts( "\n Reading a new stock item." ); sale = stock[2]; /* Copy a structure. */ stock[3] = read_lumber(); /* Input into an array slot. */ print_lumber( stock[3] ); /* Print one array element. */ sell_lumber( 3, &stock[3] ); /* Call by address. */ printf( "\n Second stock item: $%.2f\n\n", stock[1].price ); /* Test if two structures are equal; Section 14.2.4. ----------- */ for (k = 0; k < 4; ++k) { /* Process every slot of array.*/ if (boards_equal ( sale, stock[k] )) printf( " Sale: " ); else printf( " " ); print_lumber( stock[k] ); } } // ----------------------------------------------------------------- bool boards_equal ( lumber_t board1, lumber_t board2 ) { bool same = true; if (! strequal( board1.type, board2.type )) same = false; else if (board1.height != board2.height) same = false; else if (board1.width != board2.width) same = false; else if (board1.length != board2.length) same = false; else if (board1.price != board2.price) same = false; else if (board1.quantity != board2.quantity) same = false; return same; } // ----------------------------------------------------------------- lumber_t read_lumber( void ) { lumber_t board; puts( " Reading a new stock item." ); printf( " Enter the three dimensions of a board: " ); scanf( "%hi%hi%hi", &board.height, &board.width, &board.length ); printf( " Enter the type of wood: " ); scanf( " %15[^\n]", board.type ); printf( " Enter the price: $" ); scanf( "%g", &board.price ); printf( " Enter the quantity in stock: " ); scanf( "%i", &board.quantity ); return board; } // ----------------------------------------------------------------- void sell_lumber( int sold, lumber_t* board ) { int n = board->quantity; if (n >= sold) { board->quantity -= sold; printf( " OK, sold %i\n", sold ); print_lumber( *board ); } else printf( " Error: cannot sell %i boards (only have \%i).\n", sold, n ); } // ----------------------------------------------------------------- void print_lumber ( lumber_t b ) { printf( " %-11s %hi\" x %hi\" x %hi feet $%.2f stock: %i \n", b.type, b.height, b.width, b.length, b.price, b.quantity ); }