Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. //#include <math.h>
  5.  
  6. typedef struct item_t // typedef struct of item_t
  7. {
  8. char* name;
  9. float price;
  10. int quantity;
  11. } item_t;
  12.  
  13. int numberOfItemsInWarehouse(item_t** items)
  14. {
  15. int cnt=0;;
  16. for(int it = 0; it<5; it++)
  17. {
  18. if (items[it] != NULL)
  19. {
  20. cnt++;
  21. }
  22. }
  23. return cnt;
  24. }
  25.  
  26. void addItem(item_t** items, char* name, float price, int quantity)
  27. {
  28. //printf("%s // %f // %d\n", name, price, quantity);
  29. for (int it=0; it<5; it++)
  30. {
  31. if (items[it] == NULL)
  32. {
  33. //char* _name = (char*)malloc(sizeof(name));
  34. //_name = name;
  35. items[it] = malloc(sizeof(item_t));
  36. items[it]->price = price;
  37. items[it]->quantity = quantity;
  38. items[it]->name = malloc(sizeof(name));
  39. strcpy(((items[it])->name),name);
  40. //items[it] = item;
  41.  
  42. //printf("add:%s / %f / %d\n",items[it]->name, items[it]->price, items[it]->quantity);
  43.  
  44. return;
  45. }
  46. }
  47. printf("Warehouse is full!\n");
  48. }
  49.  
  50. void printInventory(item_t** items)
  51. {
  52. //printf("printing\n");
  53. for(int it = 0; it<5; it++)
  54. {
  55. if(items[it] != NULL)
  56. {
  57. //printf("not null\n");
  58. printf("%s / %.2f / %d\n",items[it]->name, items[it]->price, items[it]->quantity);
  59. }
  60. }
  61. }
  62.  
  63. void removeItem(item_t** items, char* name)
  64. {
  65. printf("name =%s\n",name);
  66. printf("items[it]->name =%s\n",items[0]->name);
  67. printf("items[it]->name =%s\n",items[1]->name);
  68.  
  69. for( int it = 0; it < 5; it++)
  70. {
  71. if ((items[it] != NULL) && (strcmp( items[it]->name, name) == 0));
  72. {
  73. printf("%s / %.2f / %d\n",items[it]->name, items[it]->price, items[it]->quantity);
  74. free(items[it]->name);
  75. free(items[it]);
  76. items[it]->name = NULL;
  77. items[it] = NULL;
  78. /*for(int it2 = 4; it2 > it; it2--)
  79. {
  80. if(items[it2] != NULL)
  81. {
  82. items[it] = items[it2];
  83. items[it2] = NULL;
  84. return;
  85. }
  86. }*/
  87. //while( (it < 4) && (items[it+1] != NULL))
  88. //{
  89. // items[it] = items[it+1];
  90. // it++;
  91. //}
  92. //items[it] = NULL;
  93.  
  94. return;
  95. }
  96. }
  97. }
  98.  
  99. int main(void)
  100. {
  101. item_t* wareHouse[5]; // create pointer array for warehouse
  102.  
  103. for(int it =0; it<5; it++) // initialise all pointers at NULL
  104. {
  105. wareHouse[it] = NULL;
  106. }
  107.  
  108. char command;
  109. char name[100];
  110. float price = 0;
  111. int quantity = 0;
  112.  
  113. while(1)
  114. {
  115. printf("Command (a/r/p/i/s/q): ");
  116. scanf(" %c", &command);
  117. if (command == 'a')
  118. {
  119. printf("Add item.\n");
  120. printf("name? ");
  121. //scanf(" %s", name);
  122. char c;
  123. while((c=getchar() != '\n'));
  124. scanf("%[^\n]%*c",name);
  125.  
  126. printf("price? ");
  127. scanf(" %f", &price);
  128. printf("quantity? ");
  129. scanf(" %d", &quantity);
  130.  
  131. addItem(wareHouse, name, price, quantity);
  132. }
  133. else if (command == 'p')
  134. {
  135. printf("Inventory:\n");// print inventory
  136. printInventory(wareHouse);
  137. }
  138. else if (command == 'q') // quit program
  139. {
  140. return 0;
  141. }
  142. else if (command == 'r') // remove item
  143. {
  144. printf("remove item: ");
  145. scanf(" %s", name);
  146. removeItem(wareHouse,name);
  147. }
  148. else if (command == 'i') // sell item
  149. {
  150.  
  151. }
  152. else if (command == 's') // sort item on price
  153. {
  154.  
  155. }
  156. }
  157.  
  158. return -1; // error
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement