#include "burguer.h"
int main (void)
{
tMenu m;
tOrders o;
tBurguer b;
tIngredient i;
int pos;
init (&m, &o);
b.id = 0;
strcpy(b.name, "special");
b.ingredients=NULL;
b.numIng=0;
b.price=10.5;
strcpy(i.name, "lettuce");
i.quantity = 100;
add_ingredient (&b, i);
strcpy(i.name, "onion");
i.quantity = 100;
add_ingredient (&b, i);
strcpy(i.name, "bacon");
i.quantity = 100;
add_ingredient (&b, i);
printf("\n number of ingredients = %d \n", b.numIng);
for (pos=0; pos<b.numIng; pos++)
printf("\n i=%d name=%s quantity=%d\n",
pos, b.ingredients[pos].name, b.ingredients[pos].quantity);
del_ingredient (&b, "lettuce", 50);
del_ingredient (&b, "onion", 100);
del_ingredient (&b, "bacon", 200);
del_ingredient (&b, "mushroom", 20);
printf("\n number of ingredients = %d \n", b.numIng);
for (pos=0; pos<b.numIng; pos++)
printf("\n i=%d name=%s quantity=%d\n",
pos, b.ingredients[pos].name, b.ingredients[pos].quantity);
return 0;
}
#include "burguer.h"
void init(tMenu *m, tOrders *o)
{
m = NULL;
printf("[ OK ] Menu Initialized!\n");
o->orders = (tOrder *)malloc(sizeof(tOrder));
if (o->orders == NULL)
printf("[ERROR] Unable to allocate memory for Orders\n");
printf("[ OK ] Orders Initialized!\n");
}
void add_ingredient(tBurguer *b, tIngredient i)
{
if (b->numIng == 0) // If no ingredient yet...
{
b->ingredients = (tIngredient *)malloc(sizeof(tIngredient)); // Allocate memory for one ingredient
if (b->ingredients == NULL) // Sanity check
{
printf("[ERROR]: Memory error\n");
}
else
{
printf("[ OK ] Ingredients Initialized!\n");
b->ingredients[0] = i; // Initialize the list with the first ingredient "i"
b->numIng++; // Increment the ingredient count by one
return; // ...and exit the function.
}
}
else // If there is/are ingredient(s) already...
{
for (int pos = 0; pos < b->numIng; pos++) // Cycle through all ingredients looking for a match.
{
printf("Pos: %i b->numIng: %i\n", pos, b->numIng);
if (!strcmp(b->ingredients[pos].name, i.name)) // If the ingredient is already in the list
{
b->ingredients[pos].quantity += i.quantity; // ... then add "i.quantity" grams.
printf("[ OK ] Ingredient \"%s\" added +%igrams\n", i.name, i.quantity);
return; // ... ends the loop and exits the function.
}
else if (pos == (b->numIng - 1)) // If I'm the last item in the list and no match then realloc() one and add ingredient.
{
b->ingredients = (tIngredient *)realloc(b->ingredients, (b->numIng + 1) * sizeof(tIngredient));
if (b->ingredients == NULL)
{
printf("[ERROR] Unable to reallocate more memory for ingredients\n");
return;
}
b->ingredients[pos + 1] = i; // Adds ingredient to the new position
b->numIng++; // ...and adds one to the ingredient count.
return; // ...and exits the function.
}
}
}
}
bool del_ingredient(tBurguer *b, char *name, int quantity)
{
if (!b->numIng) // If there's no ingredients then fail
{
printf("[ERROR] Burguer id:%i doesn't have ingredients!\n", b->id);
return false;
}
else
{
for (int pos = 0; pos < b->numIng; pos++) // Loop through the ingredient list and...
{
printf("Ingrediente %s pos %i\n", b->ingredients[pos].name, pos);
if (!strcmp(b->ingredients[pos].name, name) && (quantity < b->ingredients[pos].quantity)) // If name matches AND quantity ins't bigger than stored
{
b->ingredients[pos].quantity -= quantity; // Substract quantity and return true.
return true;
}
else if (!strcmp(b->ingredients[pos].name, name) && (quantity >= b->ingredients[pos].quantity)) // If name matches AND quantity is bigger than stored
{
int resize_index = 0; // Initializes Aux array index
tIngredient *aux = (tIngredient *)malloc(b->numIng * sizeof(tIngredient)); // Create an auxiliar dynamic array the size of the actual one.
if (aux == NULL)
{
printf("[ERROR] Unable to allocate AUX memory!\n");
return false;
}
memcpy(aux, b->ingredients, b->numIng * sizeof(tIngredient)); // Copy b->ingredients memory into the aux list
for (int pos = 0; pos < b->numIng; pos++)
{
printf("Aux[%i].name = %s - resize_index=%i\n", pos, aux[pos].name, resize_index);
if (strcmp(aux[pos].name, name)) // If name doesn't equal to aux[pos].name then add back to the list
{
b->ingredients[resize_index] = aux[pos];
printf("b->ingredients[%i].name= %s\n", resize_index, b->ingredients->name);
resize_index++;
}
}
b->ingredients = (tIngredient *)realloc(b->ingredients, sizeof(resize_index * sizeof(tIngredient))); // This will free up the last element
if (b->ingredients == NULL)
{
printf("[ERROR] Unable to reallocate memory for ingredient\n");
free(aux);
return false;
}
b->numIng -= 1; // Update the current number of ingredients
free(aux); // Free the auxiliar array
aux = NULL; // and null the aux pointer
return false;
}
}
printf("[ERROR] No ingredient with name %s exists\n", name); // If no ingredient is found then return false.
return false;
}
return false;
}
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAX_BURGER_TYPE 100
#define MAX_CHAR 20
typedef enum
{
false,
true
} bool;
typedef char string[MAX_CHAR + 1];
typedef struct
{
string name;
int quantity;
} tIngredient;
typedef struct
{
int id;
string name;
int numIng;
float price;
tIngredient *ingredients;
} tBurguer;
typedef struct
{
int id;
int burguer_id;
int quantity;
} tOrder;
typedef struct
{
tOrder *orders;
} tOrders;
typedef struct
{
tBurguer *burguers[MAX_BURGER_TYPE];
} tMenu;
void init(tMenu *m, tOrders *o);
void add_ingredient(tBurguer *b, tIngredient i);
bool del_ingredient(tBurguer *b, char *name, int quantity);