SHARE
TWEET

Untitled

a guest Feb 21st, 2020 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     for (int i = 0; i < 5; ++i)
  25.     {
  26.         vec->ptr[i] = (goods*)malloc(sizeof(goods*) * 1);
  27.     }
  28.    
  29.     vec->size = 0;
  30.     vec->capacity = 5;
  31. }
  32.  
  33. void addGoods(Vector* vec, char* name, float price, int amount)
  34. {
  35.     if (vec->size == vec->capacity)
  36.         vec->ptr = (goods**)realloc(vec->ptr, sizeof(goods*) * (vec->capacity += vec->capacity/3));
  37.    
  38.     if (vec->ptr != NULL)
  39.     {
  40.         vec->ptr[vec->size][vec->size].name = malloc(sizeof(char) * (strlen(name) + 1));
  41.         strcpy(vec->ptr[vec->size][vec->size].name, name);
  42.         vec->ptr[vec->size][vec->size].amount = amount;
  43.         vec->ptr[vec->size][vec->size].price = price;
  44.     }
  45.     vec->size++;
  46. }
  47.  
  48. void GoodsInfo(goods* goods)
  49. {
  50.     printf("goods\nname: %s\nprice: %.2f\namount: %d\n", goods->name, goods->price, goods->amount);
  51. }
  52.  
  53.  
  54. void printDb(Vector* vec)
  55. {
  56.     for (int i = 0; i < (int)vec->size; ++i)
  57.     {
  58.         GoodsInfo(vec->ptr[i]);
  59.         printf("\n");
  60.     }
  61.     printf("Size: %u, Capacity: %u\n", vec->size, vec->capacity);
  62. }
  63.  
  64. void order(Vector* vec)
  65. {
  66.     for (int i = 0; i < vec->size-1; ++i)
  67.     {
  68.         for (int j = 0; j < vec->size-1-i;++j)
  69.         if (strcmp(vec->ptr[i][i].name, vec->ptr[i+1][i+1].name) == 1)
  70.         {
  71.             goods* temp = vec->ptr[i];
  72.             vec->ptr[i] = vec->ptr[i + 1];
  73.             vec->ptr[i + 1] = temp;
  74.         }
  75.     }
  76.    
  77. }
  78.  
  79. int main()
  80. {
  81.     Vector shop1;
  82.     initVec(&shop1);
  83.  
  84.     char buffer[255];
  85.     float tprice = 0;
  86.     int tamount = 0;
  87.     int i = 0;
  88.     while (1)
  89.     {
  90.         printf("Enter name of goods number %d\n", i + 1);
  91.         gets_s(buffer, 255);
  92.         if (strcmp(buffer, "STOP") == 0)
  93.         {
  94.             break;
  95.         }
  96.         printf("Enter price\n");
  97.         scanf("%f", &tprice);
  98.         printf("Enter amount\n");
  99.         scanf("%d", &tamount);
  100.         getchar();
  101.  
  102.         addGoods(&shop1, buffer, tprice, tamount);
  103.         ++i;
  104.     }
  105.     printDb(&shop1);
  106.     printf("NEW\n");
  107.     order(&shop1);
  108.     printDb(&shop1);
  109.     getchar();
  110. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top