Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.55 KB | None | 0 0
  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.  
  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. } inventory[MAX_PARTS];
  24.  
  25. int num_parts = 0;   /* number of parts currently stored */
  26.  
  27. int find_part(int number);
  28. int read_line(char str[], int n);
  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 'i': insert();
  52.                 break;
  53.       case 's': search();
  54.                 break;
  55.       case 'u': update();
  56.                 break;
  57.       case 'p': print();
  58.                 break;
  59.       case 'q': return 0;
  60.       default:  printf("Illegal code\n");
  61.     }
  62.     printf("\n");
  63.   }
  64. }
  65.  
  66. /**********************************************************
  67.  * find_part: Looks up a part number in the inventory     *
  68.  *            array. Returns the array index if the part  *
  69.  *            number is found; otherwise, returns -1.     *
  70.  **********************************************************/
  71. int find_part(int number)
  72. {
  73.   int i;
  74.  
  75.   for (i = 0; i < num_parts; i++)
  76.     if (inventory[i].number == number)
  77.       return i;
  78.   return -1;
  79. }
  80.  
  81. /**********************************************************
  82.  * insert: Prompts the user for information about a new   *
  83.  *         part and then inserts the part into the        *
  84.  *         database. Prints an error message and returns  *
  85.  *         prematurely if the part already exists or the  *
  86.  *         database is full.                              *
  87.  **********************************************************/
  88. void insert(void)
  89. {
  90.   int part_number;
  91.   int j = 0;
  92.   char ch;
  93.  
  94.  
  95.   if (num_parts == MAX_PARTS) {
  96.     printf("Database is full; can't add more parts.\n");
  97.     return;
  98.   }
  99.  
  100.   printf("Enter part number: ");
  101.   scanf("%d", &part_number);
  102.   while ( (ch = getchar()) != '\n');
  103.   if (find_part(part_number) >= 0) {
  104.     printf("Part already exists.\n");
  105.     return;
  106.   }
  107.  
  108.   inventory[num_parts].number = part_number;
  109.   printf("Enter part name: ");
  110.   scanf("%s",inventory[num_parts].name);
  111.   printf("Enter quantity on hand: ");
  112.   scanf("%d", &inventory[num_parts].on_hand);
  113.   num_parts++;
  114. }
  115.  
  116. /**********************************************************
  117.  * search: Prompts the user to enter a part number, then  *
  118.  *         looks up the part in the database. If the part *
  119.  *         exists, prints the name and quantity on hand;  *
  120.  *         if not, prints an error message.               *
  121.  **********************************************************/
  122. void search(void)
  123. {
  124.   int i, number;
  125.  
  126.   printf("Enter part number: ");
  127.   scanf("%d", &number);
  128.   i = find_part(number);
  129.   if (i >= 0) {
  130.     printf("Part name: %s\n", inventory[i].name);
  131.     printf("Quantity on hand: %d\n", inventory[i].on_hand);
  132.   } else
  133.     printf("Part not found.\n");
  134. }
  135.  
  136. /**********************************************************
  137.  * update: Prompts the user to enter a part number.       *
  138.  *         Prints an error message if the part doesn't    *
  139.  *         exist; otherwise, prompts the user to enter    *
  140.  *         change in quantity on hand and updates the     *
  141.  *         database.                                      *
  142.  **********************************************************/
  143. void update(void)
  144. {
  145.   int i, number, change;
  146.  
  147.   printf("Enter part number: ");
  148.   scanf("%d", &number);
  149.   i = find_part(number);
  150.   if (i >= 0) {
  151.     printf("Enter change in quantity on hand: ");
  152.     scanf("%d", &change);
  153.     inventory[i].on_hand += change;
  154.   } else
  155.     printf("Part not found.\n");
  156. }
  157.  
  158. /**********************************************************
  159.  * print: Prints a listing of all parts in the database,  *
  160.  *        showing the part number, part name, and         *
  161.  *        quantity on hand. Parts are printed in the      *
  162.  *        order in which they were entered into the       *
  163.  *        database.                                       *
  164.  **********************************************************/
  165. void print(void)
  166. {
  167.   int i;
  168.  
  169.   printf("Part Number   Part Name                  "
  170.          "Quantity on Hand\n");
  171.   for (i = 0; i < num_parts; i++)
  172.     printf("%7d       %-25s%11d\n", inventory[i].number,
  173.            inventory[i].name, inventory[i].on_hand);
  174. }
  175.  
  176. int read_line(char str[], int n)
  177. {
  178.     int ch, i = 0;
  179.     while ((ch = getchar()) != '\n')
  180.         if (i < n)
  181.             str[i++] = ch;
  182.     str[i] = '\0';
  183.     return i;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement