Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //#include <math.h>
- typedef struct item_t // typedef struct of item_t
- {
- char* name;
- float price;
- int quantity;
- } item_t;
- int numberOfItemsInWarehouse(item_t** items)
- {
- int cnt=0;;
- for(int it = 0; it<5; it++)
- {
- if (items[it] != NULL)
- {
- cnt++;
- }
- }
- return cnt;
- }
- void addItem(item_t** items, char* name, float price, int quantity)
- {
- //printf("%s // %f // %d\n", name, price, quantity);
- for (int it=0; it<5; it++)
- {
- if (items[it] == NULL)
- {
- //char* _name = (char*)malloc(sizeof(name));
- //_name = name;
- items[it] = malloc(sizeof(item_t));
- items[it]->price = price;
- items[it]->quantity = quantity;
- items[it]->name = malloc(sizeof(name));
- strcpy(((items[it])->name),name);
- //items[it] = item;
- //printf("add:%s / %f / %d\n",items[it]->name, items[it]->price, items[it]->quantity);
- return;
- }
- }
- printf("Warehouse is full!\n");
- }
- void printInventory(item_t** items)
- {
- //printf("printing\n");
- for(int it = 0; it<5; it++)
- {
- if(items[it] != NULL)
- {
- //printf("not null\n");
- printf("%s / %.2f / %d\n",items[it]->name, items[it]->price, items[it]->quantity);
- }
- }
- }
- void removeItem(item_t** items, char* name)
- {
- printf("name =%s\n",name);
- printf("items[it]->name =%s\n",items[0]->name);
- printf("items[it]->name =%s\n",items[1]->name);
- for( int it = 0; it < 5; it++)
- {
- if ((items[it] != NULL) && (strcmp( items[it]->name, name) == 0));
- {
- printf("%s / %.2f / %d\n",items[it]->name, items[it]->price, items[it]->quantity);
- free(items[it]->name);
- free(items[it]);
- items[it]->name = NULL;
- items[it] = NULL;
- /*for(int it2 = 4; it2 > it; it2--)
- {
- if(items[it2] != NULL)
- {
- items[it] = items[it2];
- items[it2] = NULL;
- return;
- }
- }*/
- //while( (it < 4) && (items[it+1] != NULL))
- //{
- // items[it] = items[it+1];
- // it++;
- //}
- //items[it] = NULL;
- return;
- }
- }
- }
- int main(void)
- {
- item_t* wareHouse[5]; // create pointer array for warehouse
- for(int it =0; it<5; it++) // initialise all pointers at NULL
- {
- wareHouse[it] = NULL;
- }
- char command;
- char name[100];
- float price = 0;
- int quantity = 0;
- while(1)
- {
- printf("Command (a/r/p/i/s/q): ");
- scanf(" %c", &command);
- if (command == 'a')
- {
- printf("Add item.\n");
- printf("name? ");
- //scanf(" %s", name);
- char c;
- while((c=getchar() != '\n'));
- scanf("%[^\n]%*c",name);
- printf("price? ");
- scanf(" %f", &price);
- printf("quantity? ");
- scanf(" %d", &quantity);
- addItem(wareHouse, name, price, quantity);
- }
- else if (command == 'p')
- {
- printf("Inventory:\n");// print inventory
- printInventory(wareHouse);
- }
- else if (command == 'q') // quit program
- {
- return 0;
- }
- else if (command == 'r') // remove item
- {
- printf("remove item: ");
- scanf(" %s", name);
- removeItem(wareHouse,name);
- }
- else if (command == 'i') // sell item
- {
- }
- else if (command == 's') // sort item on price
- {
- }
- }
- return -1; // error
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement