1. /*********************************************************
  2.  * From C PROGRAMMING: A MODERN APPROACH, Second Edition *
  3.  * By K. N. King *
  4.  * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
  5.  * All rights reserved. *
  6.  * This program may be freely distributed for class use, *
  7.  * provided that this copyright notice is retained. *
  8.  *********************************************************/
  9.  
  10. /* inventory.c (Chapter 16, page 391) */
  11. /* Maintains a parts database (array version) */
  12.  
  13. #include <stdio.h>
  14. #include "readline.h"
  15.  
  16. #define NAME_LEN 25
  17. #define MAX_PARTS 100
  18.  
  19. struct part {
  20. int number;
  21. char name[NAME_LEN+1];
  22. int on_hand;
  23. int price;
  24. } inventory[MAX_PARTS];
  25.  
  26. int num_parts = 0; /* number of parts currently stored */
  27.  
  28. int find_part(int number);
  29. void insert(void);
  30. void search(void);
  31. void update(void);
  32. void print(void);
  33.  
  34. /**********************************************************
  35.  * main: Prompts the user to enter an operation code, *
  36.  * then calls a function to perform the requested *
  37.  * action. Repeats until the user enters the *
  38.  * command 'q'. Prints an error message if the user *
  39.  * enters an illegal code. *
  40.  **********************************************************/
  41. int main(void)
  42. {
  43. char code;
  44.  
  45. for (;;) {
  46. printf("Enter operation code: ");
  47. scanf(" %c", &code);
  48. while (getchar() != '\n') /* skips to end of line */
  49. ;
  50. switch (code) {
  51. case 'c': update_price();
  52. break;
  53. case 'i': insert();
  54. break;
  55. case 's': search();
  56. break;
  57. case 'u': update();
  58. break;
  59. case 'p': print();
  60. break;
  61. case 'q': return 0;
  62. default: printf("Illegal code\n");
  63. }
  64. printf("\n");
  65. }
  66. }
  67.  
  68. /**********************************************************
  69.  * find_part: Looks up a part number in the inventory *
  70.  * array. Returns the array index if the part *
  71.  * number is found; otherwise, returns -1. *
  72.  **********************************************************/
  73. int find_part(int number)
  74. {
  75. int i;
  76.  
  77. for (i = 0; i < num_parts; i++)
  78. if (inventory[i].number == number)
  79. return i;
  80. return -1;
  81. }
  82.  
  83. /**********************************************************
  84.  * insert: Prompts the user for information about a new *
  85.  * part and then inserts the part into the *
  86.  * database. Prints an error message and returns *
  87.  * prematurely if the part already exists or the *
  88.  * database is full. *
  89.  **********************************************************/
  90. void insert(void)
  91. {
  92. int part_number;
  93.  
  94. if (num_parts == MAX_PARTS) {
  95. printf("Database is full; can't add more parts.\n");
  96. return;
  97. }
  98.  
  99. printf("Enter part number: ");
  100. scanf("%d", &part_number);
  101. if (find_part(part_number) >= 0) {
  102. printf("Part already exists.\n");
  103. return;
  104. }
  105.  
  106. inventory[num_parts].number = part_number;
  107. printf("Enter part name: ");
  108. read_line(inventory[num_parts].name, NAME_LEN);
  109. printf("Enter quantity on hand: ");
  110. scanf("%d", &inventory[num_parts].on_hand);
  111. printf("Enter price: ");
  112. scanf("%d", &inventory[num_parts].price);
  113. num_parts++;
  114.  
  115.  
  116.  
  117. }
  118.  
  119. /**********************************************************
  120.  * search: Prompts the user to enter a part number, then *
  121.  * looks up the part in the database. If the part *
  122.  * exists, prints the name and quantity on hand; *
  123.  * if not, prints an error message. *
  124.  **********************************************************/
  125. void search(void)
  126. {
  127. int i, number;
  128.  
  129. printf("Enter part number: ");
  130. scanf("%d", &number);
  131. i = find_part(number);
  132. if (i >= 0) {
  133. printf("Part name: %s\n", inventory[i].name);
  134. printf("Quantity on hand: %d\n", inventory[i].on_hand);
  135. printf("Price: %d\n"), inventory[i].price;
  136. } else
  137. printf("Part not found.\n");
  138. }
  139.  
  140. /**********************************************************
  141.  * update: Prompts the user to enter a part number. *
  142.  * Prints an error message if the part doesn't *
  143.  * exist; otherwise, prompts the user to enter *
  144.  * change in quantity on hand and updates the *
  145.  * database. *
  146.  **********************************************************/
  147. void update(void)
  148. {
  149. int i, number, change;
  150.  
  151. printf("Enter part number: ");
  152. scanf("%d", &number);
  153. i = find_part(number);
  154. if (i >= 0) {
  155. printf("Enter change in quantity on hand: ");
  156. scanf("%d", &change);
  157. inventory[i].on_hand += change;
  158. } else
  159. printf("Part not found.\n");
  160. }
  161.  
  162. /**********************************************************
  163.  * print: Prints a listing of all parts in the database, *
  164.  * showing the part number, part name, and *
  165.  * quantity on hand. Parts are printed in the *
  166.  * order in which they were entered into the *
  167.  * database. *
  168.  **********************************************************/
  169. void print(void)
  170. {
  171. int i;
  172.  
  173. printf("Part Number Part Name "
  174. "Quantity on Hand Price\n");
  175. for (i = 0; i < num_parts; i++)
  176. printf("%7d %-25s%11d %d\n", inventory[i].number,
  177. inventory[i].name, inventory[i].on_hand, inventory[i].price);
  178. // printf("%f", inventory[num_parts].price);
  179. }
  180.  
  181. void update_price(void)
  182. {
  183. int i, number, change;
  184.  
  185. printf("Enter part number: ");
  186. scanf("%d", &number);
  187. i = find_part(number);
  188. if (i >= 0) {
  189. printf("Enter new price: ");
  190. scanf("%d", &change);
  191. inventory[i].price = change;
  192. } else
  193. printf("Part not found.\n");
  194. }
  195.