Answer:
(a) Here is the declaration of the BookInventory structure:
typedef struct {
char bookID[10];
char author[50];
char title[100];
char publisher[50];
int year;
int quantity;
} BookInventory;
(b) Here is the declaration of the Transactions structure:
typedef struct {
char transactionCode;
int quantity;
} Transactions;
(c) and (d) Here is the definition of a local variable txn of type Transactions, and the code to obtain and process the transaction details:
Transactions txn;
while (1) {
printf("Transaction Code (S=sold, P=purchased, X=exit): ");
scanf(" %c", &txn.transactionCode);
if (txn.transactionCode == 'X') {
printf("Exited. End of Transactions.\\");
break;
}
printf("Transaction Quantity: ");
scanf("%d", &txn.quantity);
switch (txn.transactionCode) {
case 'S':
book.quantity -= txn.quantity;
printf("Sold: %d copies of \"%s\" by %s. There are now %d copies in stock.\\",
txn.quantity, book.title, book.author, book.quantity);
break;
case 'P':
book.quantity += txn.quantity;
printf("Purchased: %d copies of \"%s\" by %s. There are now %d copies in stock.\\",
txn.quantity, book.title, book.author, book.quantity);
break;
default:
printf("Invalid transaction code!\\");
break;
}
}
(e) Here is the complete program that includes the definition of the BookInventory and Transactions structures, the initialization of a book variable, and the transaction processing loop:
#include <stdio.h>
typedef struct {
char bookID[10];
char author[50];
char title[100];
char publisher[50];
int year;
int quantity;
} BookInventory;
typedef struct {
char transactionCode;
int quantity;
} Transactions;
int main() {
BookInventory book = {
"BK-2044",
"JK Rowling",
"Harry Potter and the Prisoner of Azkaban",
"Bloomsbury",
1999,
25
};
Transactions txn;
while (1) {
printf("Transaction Code (S=sold, P=purchased, X=exit): ");
scanf(" %c", &txn.transactionCode);
if (txn.transactionCode == 'X') {
printf("Exited. End of Transactions.\\");
break;
}
printf("Transaction Quantity: ");
scanf("%d", &txn.quantity);
switch (txn.transactionCode) {
case 'S':
book.quantity -= txn.quantity;
printf("Sold: %d copies of \"%s\" by %s. There are now %d copies in stock.\\",
txn.quantity, book.title, book.author, book.quantity);
break;
case 'P':
book.quantity += txn.quantity;
printf("Purchased: %d copies of \"%s\" by %s. There are now %d copies in stock.\\",
txn.quantity, book.title, book.author, book.quantity);
break;
default:
printf("Invalid transaction code!\\");
break;
}
}
return 0;
}