Here is the full code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
typedef enum {
weight,
volume,
piece,
}quantity_type;
typedef struct ingredient Ingredient;
struct ingredient{
char* name; //Name of Ingredient
int quantity; //Quantity of the ingredient
quantity_type Qtype;
Ingredient* left; // left subtree of node
Ingredient* right; // Right subtree of node
};
typedef struct recipe Recipe;
struct recipe {
char* recipename;
Ingredient *list;
Recipe *left;
Recipe *right;
};
/* prototypes for the Binary Search Tree operations */
Ingredient *search_tree(Ingredient *tree, char *search_name);
Ingredient *insert_ingredient_order(Ingredient *tree, char *new_name,int quantity);
Recipe *Recipesearch_tree(Recipe *rtree, char *search_name);
Recipe *Rinsert_ingredient_order(Recipe *rtree, char *name,int quantity,char type);
void print_out_ingredient(Ingredient *tree);
void print_out_recipe(Recipe *rtree);
void free_ingredient(Ingredient *tree);
void free_recipe(Recipe *rtree);
void printmenu(void);
void trim(char *oops);
/* prototypes for the Binary Search Tree operations Ends*/
/*The Main Function*/
int main(int argc,char* argv[]) {
Ingredient *tree = NULL;
Ingredient *location;
Recipe *rtree=NULL;
Recipe *rlocation;
char choice=0;
int quantity;
char name[30];
char buff[30];
char recipename[30];
char type;
int enter = 1;
while(enter ==1){
printmenu();
while((choice = getchar()) != '\n'){
choice=toupper(choice);
getchar();
switch(choice){
case 'R':
printf("Name of Recipe: ");
fgets(recipename,29,stdin);
trim(recipename);
for(;;){
printf("Name of Ingredient: ");
fgets(name,29,stdin);
trim(name);
if(!strcmp(name,"")){
enter=0;
break;
}
printf("Quantity: ");
fgets(buff,29,stdin);
sscanf(buff,"%d",&quantity);
printf("Q_type: ");
fgets(buff,29,stdin);
sscanf(buff,"%c",&type);
rlocation = Recipesearch_tree(rtree, name);
if (!rlocation) {
/* not in tree, so insert the new word */
rtree = Rinsert_ingredient_order(rtree, name,quantity,type);
}
}
break;
case 'I':
for(;;){
printf("Name of Ingredient: ");
fgets(name,29,stdin);
trim(name);
if(!strcmp(name,"")){
enter=0;
break;
}
printf("Quantity: ");
fgets(buff,29,stdin);
sscanf(buff,"%d",&quantity);
printf("Q_type: ");
fgets(buff,29,stdin);
sscanf(buff,"%c",&type);
if (type==('P' || 'p')) {
quantity_type Qtype=piece;
}
else if(type==('W' || 'w')){
quantity_type Qtype=weight;
}
else if(type==('V' || 'v')){
quantity_type Qtype=volume;
}
location = search_tree(tree, name);
if (!location) {
/* not in tree, so insert the new word */
tree = insert_ingredient_order(tree, name,quantity);
}
else {
/*If already in tree, then add the quantity */
location->quantity=location->quantity + quantity;
}
}
break;
case 'C':
print_out_recipe(rtree);
break;
case 'G':
print_out_ingredient(tree);
break;
case 'S':
break;
default:
printf("Invalid option selected\n");
printf("please enter a valid choice\n");
}
printf("\n");
printmenu();
}
}
free_ingredient(tree);
tree=NULL;
free_recipe(rtree);
rtree=NULL;
return 0;
}
/*Search Node*/
Ingredient *search_tree(Ingredient *tree, char *search_name) {
int compare;
if(tree == NULL) {
return NULL;
}
else {
if((compare = strcmp(search_name, tree->name)) == 0) {
return tree; // node found
}
else if (compare < 0) {
return search_tree(tree->left, search_name);
}
else {
return search_tree(tree->right, search_name);
}
}
}
/*Insert Ingredients in Order*/
Ingredient *insert_ingredient_order(Ingredient *tree, char *new_name,int quantity){
if (tree == NULL) {
tree =(Ingredient*)malloc(sizeof(Ingredient));
assert(tree!=NULL);
tree->name =(char *)malloc(strlen(new_name)+1);
strcpy(tree->name, new_name);
tree->quantity =quantity;
tree->left = tree->right = NULL;
}
else if (strcmp(new_name, tree->name) < 0) {
tree->left = insert_ingredient_order(tree->left, new_name,quantity);
}
else {
tree->right = insert_ingredient_order(tree->right, new_name,quantity);
}
return tree;
}
void print_out_ingredient(Ingredient *tree) {
if(tree) {
print_out_ingredient(tree->left);
printf("%4d%-29s\n",tree->quantity,tree->name);
print_out_ingredient(tree->right);
}
}
void free_ingredient(Ingredient *tree){
if(tree) {
free_ingredient(tree->left);
free_ingredient(tree->right);
free(tree->name);
free(tree);
}
}
void printmenu(void){
printf("I R O N C H E F\n");
printf("Add <R>ecipe\n");
printf("Add <I>ngredients\n");
printf("List re<C>ipes\n");
printf("List in<G>redients\n");
printf("<S>uggest a dish\n");
printf("\n");
printf("Selection: ");
}
void trim(char *oops){
while(*oops){
if(*oops=='\n'){
*oops='\0';
}
oops++;
}
}
/* Search Node For Recipe */
Recipe *Recipesearch_tree(Recipe *rtree, char *search_name) {
int compare;
if(rtree == NULL) {
return NULL;
}
else {
if((compare = strcmp(search_name, rtree->recipename)) == 0) {
return rtree; // node found
}
else if (compare < 0) {
return Recipesearch_tree(rtree->left, search_name);
}
else {
return Recipesearch_tree(rtree->right, search_name);
}
}
}Recipe *rinsert_ingredient_order(Recipe *rtree, char *name,int quantity,char type){
Ingredient *tree1=NULL;
if (rtree == NULL) {
rtree =(Recipe*)malloc(sizeof(Recipe));
assert(rtree!=NULL);
rtree->recipename =(char *)malloc(strlen(name)+1);
while(tree1=insert_ingredient_order(tree1,name,quantity,type)=!NULL){
tree1=nsert_ingredient_order(tree1,name,quantity,type);
}
return rtree;
}
/* Insert node in order for Recipe */
/* print out Recipe*/
void print_out_recipe(Recipe *rtree) {
if(rtree) {
print_out_recipe(rtree->left);
//printf("%4d%-29s\n",rtree->quantity,rtree->name);
print_out_recipe(rtree->right);
}
}
void free_recipe(Recipe *rtree){
if(rtree) {
free_recipe(rtree->left);
free_recipe(rtree->right);
free(rtree->recipename);
free(rtree);
}
}