Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. typedef struct
  6. {
  7. char* name;
  8. float price;
  9. int amount;
  10.  
  11. }goods;
  12.  
  13. typedef
  14. struct Vector
  15. {
  16. goods* ptr;
  17. unsigned size;
  18. unsigned capacity;
  19. }Vector;
  20.  
  21. void initVec(Vector* vec)
  22. {
  23. vec->ptr = (goods*)malloc(sizeof(goods) * 5);
  24. vec->size = 0;
  25. vec->capacity = 5;
  26. }
  27.  
  28. void addGoods(Vector* vec, char* name, float price, int amount)
  29. {
  30. if (vec->size == vec->capacity)
  31. vec->ptr = (goods*)realloc(vec->ptr, sizeof(goods) * (vec->capacity += 5));
  32. if (vec->ptr != NULL)
  33. {
  34. vec->ptr[vec->size].name = name;
  35. vec->ptr[vec->size].amount = amount;
  36. vec->ptr[vec->size].price = price;
  37. }
  38. vec->size++;
  39. }
  40.  
  41. void GoodsInfo(goods goods)
  42. {
  43. printf("goods\nname: %s\nprice: %0.2f\namount: %d\n", goods.name, goods.price, goods.amount);
  44. }
  45.  
  46.  
  47. void printDb(Vector* vec)
  48. {
  49. for (int i = 0; i < (int)vec->size; ++i)
  50. {
  51. GoodsInfo(vec->ptr[i]);
  52. printf("\n");
  53. }
  54. printf("Size: %u, Capacity: %u\n", vec->size, vec->capacity);
  55. }
  56.  
  57. int main()
  58. {
  59.  
  60. Vector shop1;
  61. initVec(&shop1);
  62. char** names = (char**)malloc(sizeof(char**) * 1);
  63.  
  64. char buffer[255];
  65. float tprice = 0;
  66. int tamount = 0;
  67. int i = 0;
  68. while (true)
  69. {
  70. printf("Enter name of goods number %d\n", i+1);
  71. gets_s(buffer, 255);
  72. names[i] = (char*)malloc(sizeof(char*) * strlen(buffer));
  73. if (buffer[0] == 'S' && buffer[1] == 'T' && buffer[2] == 'O' && buffer[3] == 'P')
  74. {
  75. break;
  76. }
  77. strcpy(names[i], buffer);
  78. printf("Enter price\n");
  79. scanf("%f", &tprice);
  80. printf("Enter amount\n");
  81. scanf("%d", &tamount);
  82. getchar();
  83.  
  84. addGoods(&shop1, names[i], tprice, tamount);
  85. ++i;
  86. }
  87. printDb(&shop1);
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement