Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct
- {
- char* name;
- float price;
- int amount;
- }goods;
- typedef
- struct Vector
- {
- goods* ptr;
- unsigned size;
- unsigned capacity;
- }Vector;
- void initVec(Vector* vec)
- {
- vec->ptr = (goods*)malloc(sizeof(goods) * 5);
- vec->size = 0;
- vec->capacity = 5;
- }
- void addGoods(Vector* vec, char* name, float price, int amount)
- {
- if (vec->size == vec->capacity)
- vec->ptr = (goods*)realloc(vec->ptr, sizeof(goods) * (vec->capacity += 5));
- if (vec->ptr != NULL)
- {
- vec->ptr[vec->size].name = name;
- vec->ptr[vec->size].amount = amount;
- vec->ptr[vec->size].price = price;
- }
- vec->size++;
- }
- void GoodsInfo(goods goods)
- {
- printf("goods\nname: %s\nprice: %0.2f\namount: %d\n", goods.name, goods.price, goods.amount);
- }
- void printDb(Vector* vec)
- {
- for (int i = 0; i < (int)vec->size; ++i)
- {
- GoodsInfo(vec->ptr[i]);
- printf("\n");
- }
- printf("Size: %u, Capacity: %u\n", vec->size, vec->capacity);
- }
- int main()
- {
- Vector shop1;
- initVec(&shop1);
- char** names = (char**)malloc(sizeof(char**) * 1);
- char buffer[255];
- float tprice = 0;
- int tamount = 0;
- int i = 0;
- while (true)
- {
- printf("Enter name of goods number %d\n", i+1);
- gets_s(buffer, 255);
- names[i] = (char*)malloc(sizeof(char*) * strlen(buffer));
- if (buffer[0] == 'S' && buffer[1] == 'T' && buffer[2] == 'O' && buffer[3] == 'P')
- {
- break;
- }
- strcpy(names[i], buffer);
- printf("Enter price\n");
- scanf("%f", &tprice);
- printf("Enter amount\n");
- scanf("%d", &tamount);
- getchar();
- addGoods(&shop1, names[i], tprice, tamount);
- ++i;
- }
- printDb(&shop1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement