Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1. /************************************************************************
  2. * Project Name: Array Implementation of Choco List ADT List Version 2 *
  3. * Programmer : Put your Name here *
  4. * Date Completed: June 23, 2016 *
  5. ***********************************************************************/
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #define SIZE 14
  10. /******************************************************************
  11. * Data Structure Definition *
  12. ******************************************************************/
  13. typedef struct {
  14. unsigned int prodID; /* product ID, uniquely identifies an element */
  15. char prodDesc[50]; /* product description*/
  16. float prodPrice; /* product price*/
  17. int prodQty; /* product count or quantity */
  18. }product, products[SIZE]; /* product record */
  19.  
  20. typedef struct cell {
  21. product item; /* array of products */
  22. int prodCtr; /* number of products in the list */
  23. }*chocoList; /* Definition of the ADT List ver 4 */
  24.  
  25. typedef enum {
  26. TRUE, FALSE
  27. }boolean;
  28.  
  29. /******************************************************************
  30. * Function Prototypes *
  31. ******************************************************************/
  32. void initializeList(chocoList *L);
  33. void populateSortedList(chocoList L);
  34. void displayList(chocoList L, char * listName);
  35. void displayProduct(product X);
  36. int insertSorted(chocoList L, product X);
  37. product deleteFirstOccurence(chocoList L, float prodPrice);
  38.  
  39. int main(void)
  40. {
  41. chocoList L; /* sorted List */
  42. product deleted; /* container for the deleted record */
  43.  
  44. /*---------------------------------------------------------------------------------
  45. * Problem #1 :: Initialize the choco list. Display the products in the list after*
  46. * calling populateSortedList(). *
  47. * printf("\n\n\nProblem #1:: "); *
  48. *--------------------------------------------------------------------------------*/
  49. printf("\n\n\nProblem #1: penispenispenis");
  50. initializeList(&L);
  51. populateSortedList(L);
  52. displayList(L,"List");
  53.  
  54.  
  55. /*---------------------------------------------------------------------------------
  56. * Problem #2 :: Delete 1 product from the list by calling deleteFirstOccurence(). *
  57. * Display the returned product record by calling displayProduct(). *
  58. * printf("\n\n\nProblem #2:: "); *
  59. *--------------------------------------------------------------------------------*/
  60.  
  61.  
  62. /*---------------------------------------------------------------------------------
  63. * CHALLENGE :: Redo Problem #s 1 & 2 using either versions 3 or 4 *
  64. * Keep in mind the changes when converting the ADT List Versions *
  65. *--------------------------------------------------------------------------------*/
  66.  
  67. getch();
  68. return 0;
  69. }
  70.  
  71. /****************************************************************************************
  72. * This function initializes the list for its first use. *
  73. ****************************************************************************************/
  74. void initializeList(chocoList *L)
  75. {
  76. *L = (chocoList)malloc(sizeof(struct cell));
  77. }
  78.  
  79. /****************************************************************************************
  80. * This function populates the list by calling insertSorted(). *
  81. ****************************************************************************************/
  82. void populateSortedList(chocoList L)
  83. {
  84. int x, result = 1;
  85.  
  86. product data[] = { {1501, "Hersheys", 100.50, 10},
  87. {1502, "Hersheys Almond", 100.50, 15},
  88. {1503, "Hersheys Krackel", 100.50, 15},
  89. {1701, "Toblerone", 150.75, 20},
  90. {1702, "Toblerone Milk", 150.75, 40},
  91. {1703, "Toblerone Honey", 150.75, 10},
  92. {1550, "Cadbury", 200.00, 30},
  93. {1201, "Kitkat", 97.75, 40},
  94. {1450, "Ferrero", 150.50, 50},
  95. {1601, "Meiji", 75.50, 60},
  96. {1301, "Nestle", 124.50, 70},
  97. {1525, "Lindt", 175.50, 80},
  98. {1545, "Valor", 100.50, 90},
  99. {1455, "Tango", 49.50, 100}
  100. };
  101. for(x = 0; x < 10 && result == 1; x++){
  102. result = insertSorted(L, data[x]);
  103. }
  104. }
  105.  
  106. /****************************************************************************************
  107. * This function display the details of all products in the list. *
  108. ****************************************************************************************/
  109. void displayList(chocoList L, char * listName)
  110. {
  111. int x;
  112. system("CLS"); /* clears the screen before displaying succeeding lines */
  113.  
  114. if(L->prodCtr != 0){
  115. printf("\nElements of the Product List %s:", listName);
  116. printf("\n\n%-10s%-15s%10s%10s","ID","Description","Price","Quantity");
  117. printf("\n%-10s%-15s%10s%10s","--","-----------","-----","--------");
  118.  
  119. for(x = 0 ; x < L->prodCtr; x++){
  120. printf("\n%-10d", L->item[x].prodID);
  121. printf("%-15s", L->item[x].prodDesc);
  122. printf("%10.2f", L->item[x].prodPrice);
  123. printf("%10d", L->item[x].prodQty);
  124. }
  125. }else{
  126. printf("\n\tChoco list is currently empty!\n");
  127. }
  128.  
  129. printf("\n\n Press any key to continue . . . ");
  130. getch();
  131. }
  132.  
  133. /****************************************************************************************
  134. * This function inserts a product X in an alphabetically sorted list according to its *
  135. * product description. It returns a value of 1 for successful insertion. Otherwise, 0. *
  136. * Use memcpy() in shifting the elements downward to provide space for the new product. *
  137. ***************************************************************************************/
  138. int insertSorted(chocoList L, product X)
  139. {
  140. }
  141.  
  142. /****************************************************************************************
  143. * This function deletes the first occurence of a product given the prodPrice.It returns*
  144. * the product record ones it is found. Otherwise, it returns a dummy record containing *
  145. * "XXX" for string values and 0 for integer and float values. Use memcpy() in shifting *
  146. * the elements upward to avoid having empty indices within the array. *
  147. ***************************************************************************************/
  148. product deleteFirstOccurence(chocoList L, float prodPrice)
  149. {
  150. }
  151.  
  152. /****************************************************************************************
  153. * This function display the details of 1 product. *
  154. ***************************************************************************************/
  155. void displayProduct(product X)
  156. {
  157. //system("CLS"); /* clears the screen before displaying succeeding lines */
  158. printf("\n\nElements of Product %d:", X.prodID);
  159. printf("\n\n%-10s%-15s%10s%10s","ID","Description","Price","Quantity");
  160. printf("\n%-10s%-15s%10s%10s","--","-----------","-----","--------");
  161.  
  162. printf("\n%-10d", X.prodID);
  163. printf("%-15s", X.prodDesc);
  164. printf("%10.2f", X.prodPrice);
  165. printf("%10d", X.prodQty);
  166.  
  167. printf("\n\n Press any key to continue . . . ");
  168. getch();
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement