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);
- for (int i = 0; i < 5; ++i)
- {
- vec->ptr[i] = (goods*)malloc(sizeof(goods*) * 1);
- }
- 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 += vec->capacity/3));
- if (vec->ptr != NULL)
- {
- vec->ptr[vec->size][vec->size].name = malloc(sizeof(char) * (strlen(name) + 1));
- strcpy(vec->ptr[vec->size][vec->size].name, name);
- vec->ptr[vec->size][vec->size].amount = amount;
- vec->ptr[vec->size][vec->size].price = price;
- }
- vec->size++;
- }
- void GoodsInfo(goods* goods)
- {
- printf("goods\nname: %s\nprice: %.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);
- }
- void order(Vector* vec)
- {
- for (int i = 0; i < vec->size-1; ++i)
- {
- for (int j = 0; j < vec->size-1-i;++j)
- if (strcmp(vec->ptr[i][i].name, vec->ptr[i+1][i+1].name) == 1)
- {
- goods* temp = vec->ptr[i];
- vec->ptr[i] = vec->ptr[i + 1];
- vec->ptr[i + 1] = temp;
- }
- }
- }
- int main()
- {
- Vector shop1;
- initVec(&shop1);
- char buffer[255];
- float tprice = 0;
- int tamount = 0;
- int i = 0;
- while (1)
- {
- printf("Enter name of goods number %d\n", i + 1);
- gets_s(buffer, 255);
- if (strcmp(buffer, "STOP") == 0)
- {
- break;
- }
- printf("Enter price\n");
- scanf("%f", &tprice);
- printf("Enter amount\n");
- scanf("%d", &tamount);
- getchar();
- addGoods(&shop1, buffer, tprice, tamount);
- ++i;
- }
- printDb(&shop1);
- printf("NEW\n");
- order(&shop1);
- printDb(&shop1);
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement