home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: OtherApp / OtherApp.zip / nut-os2.zip / src / addreci.c < prev    next >
C/C++ Source or Header  |  1998-09-10  |  7KB  |  252 lines

  1. /* addreci.c */
  2.  
  3. /*
  4.     Nut nutrition software 
  5.     Copyright (C) 1998 Jim Jozwiak.
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "addreci.h"
  23. #include "food.h"
  24. #include "util.h"
  25. #include "db.h"
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30.  
  31. void add_recipes()
  32. {
  33. struct food *food_ptr = &food_root;
  34. char key[61];
  35. int c, delfood, junk, likeit = 'n';
  36. float ratio, one = 1, servings = 0;
  37. key[0] = '\0';
  38. key_clean();
  39. while (likeit == 'N' || likeit == 'n')
  40.  {
  41.  header("NUT:  Add Recipes");
  42.  spacer(-1);
  43.  printf("Type name of new recipe (or <enter> to quit):  ");
  44.  get_string(food_work.name,60);
  45.  if (strcmp(food_work.name,"") == 0) return;
  46.  junk = strlen(food_work.name);
  47.  for (c = 0 ; c < junk ; c++) food_work.name[c] = toupper(food_work.name[c]);
  48.  for (c = junk ; c >= 0 ; c--)
  49.   {
  50.   if (food_work.name[c] == ' ') food_work.name[c] = '\0';
  51.   else break;
  52.   }
  53.  while (food_ptr->next != NULL)
  54.   {
  55.   food_ptr = food_ptr->next;
  56.   if (food_work.name[0] == food_ptr->name[0]) if (strcmp(food_work.name,food_ptr->name) == 0)
  57.   {
  58.    get_string(food_work.name,60);
  59.    for (c = 0 ; c < 60 ; c++) food_work.name[c] = toupper(food_work.name[c]);
  60.    food_ptr = &food_root;
  61.    }
  62.   }
  63.  header("NUT:  Add Recipes");
  64.  spacer(0);
  65.  printf("Recipe name is \"%-s.\"\n",food_work.name);
  66.  printf("Is this correct?  (y/n):  ");
  67.  likeit = get_char();
  68.  }
  69. if (strcmp(food_work.name,"") == 0) return;
  70. for ( ; ; )
  71.  {
  72.  for ( ; ; )
  73.   {
  74.   if (recipe_root.next != NULL)
  75.    {
  76.    header("NUT:  Add Recipes");
  77.    if (! recipe_show())
  78.     {
  79.     printf("\nPress <enter> to continue...");
  80.     junk = get_int();
  81.     }
  82.    else
  83.     {
  84.     printf("\nEnter number to delete or name of next food (or <enter> to quit):  ");
  85.     get_string(key,60);
  86.     if (strcmp(key,"") == 0)
  87.      {
  88.      header("NUT:  Add Recipes");
  89.      recipe_show();
  90.      printf("\nDo you want to save recipe in the food database?  (y/n):  ");
  91.      junk = get_char();
  92.      if (junk == 'n' || junk == 'N')
  93.       {
  94.       while (recipe_root.next != NULL) delete_recipe_food(1);
  95.       return;
  96.       }
  97.      header("NUT:  Add Recipes");
  98.      recipe_show();
  99.      printf("\nDo you want to add more foods to recipe?  (y/n):  ");
  100.      junk = get_char();
  101.      if (junk == 'y' || junk == 'Y') continue;
  102.      clear_work();
  103.      food_ptr = &recipe_root;
  104.      while (food_ptr->next != NULL)
  105.       {
  106.       food_ptr = food_ptr->next;
  107.       for (c = 0; c < NutrientCount; c++) food_work.nutrient[c] += 100 * food_ptr->nutrient[c] / Rdi[c]; 
  108.       food_work.grams += food_ptr->grams;
  109.       }
  110.      while (recipe_root.next != NULL) delete_recipe_food(1);
  111.      while (servings <= 0)
  112.       {
  113.       header("NUT:  Add Recipes");
  114.       new_recipe_show();
  115.       printf("\nHow many servings does this recipe make?  ");
  116.       get_string(key,60);
  117.       servings = atof(key);
  118.       if (servings <= 0) continue;
  119.       header("NUT:  Add Recipes");
  120.       new_recipe_show();
  121.       printf("\nIs \"%-s\" the correct number of servings for this recipe?  (y/n)  ",key);
  122.       junk = get_char();
  123.       if (junk == 'N' || junk == 'n') servings = 0; 
  124.       }
  125.      for (c = 0; c < NutrientCount; c++) food_work.nutrient[c] /= servings; 
  126.      food_work.grams /= servings;
  127.      new_recipe_show();
  128.      printf("\nPlease enter description of serving size:  ");
  129.      get_string(food_work.serving,50); 
  130.      new_recipe_show();
  131.      printf("\nWould you like to change weight of serving?  (y/n):  ");
  132.      junk = get_char();
  133.      if (junk == 'y' || junk == 'Y')
  134.       {
  135.       junk = 'n';
  136.       while (junk == 'n' || junk == 'N')
  137.        {
  138.        new_recipe_show();
  139.        printf("\nType new weight of serving {xxxg for grams, xxxo for oz}:  ");
  140.        get_recipe_qty(&food_work.grams);
  141.        new_recipe_show();
  142.        printf("\nIs weight now correct?  (y/n):  ");
  143.        junk = get_char();
  144.        }
  145.       }
  146.      if ((new_food = malloc(sizeof(struct food))) == NULL)
  147.       {
  148.       printf("We are out of memory.  Bummer.\n");
  149.       abort();
  150.       } 
  151.      for (c = 0; c < NutrientCount; c++) food_work.nutrient[c] *= Rdi[c] / 100; 
  152.      memcpy(new_food,&food_work,sizeof(struct food));
  153.      order_new_food();
  154.      FoodCount++;
  155.      write_food_db();
  156.      write_nut_raw_food_file();
  157.      for (c = 0; c < NutrientCount; c++) food_work.nutrient[c] *= 100 / Rdi[c]; 
  158.      new_recipe_show();
  159.      printf("\nRecipe saved in food database.  Press <enter> to continue...  ");
  160.      junk = get_int();
  161.      food_work.name[0] = '\0';
  162.      food_work.serving[0] = '\0';
  163.      return;
  164.      }
  165.     }
  166.    }
  167.   delfood = atoi(key);
  168.   if (strcmp(key,"") == 0 || delfood == 0) break;
  169.   delete_recipe_food(delfood);
  170.   strcpy(key,"");
  171.   }
  172.  ratio = -38;
  173.  while (ratio < 0)
  174.   {
  175.   key_put(key);
  176.   food_ptr = food_choice("NUT:  Add Recipes");
  177.   if (food_ptr == (struct food *) -1) return;
  178.   if (food_ptr == (struct food *)  0) key_clean();
  179.   if (food_ptr == (struct food *)  0) continue;
  180.   header("NUT:  Add Recipes");
  181.   food_show(food_ptr, &one);
  182.   printf("\nType # of servings {xxxg for grams, xxxo for oz, \"b\" for back}:  ");
  183.   get_qty(&ratio, &(food_ptr->grams));
  184.   if (ratio < 0)
  185.    {
  186.    key_take();
  187.    strcpy(key,key_take());
  188.    }
  189.   }
  190.  if (ratio == 0)
  191.   {
  192.   key_clean();
  193.   strcpy(key,"");
  194.   }
  195.  if (ratio > 0)
  196.   {
  197.   key_clean();
  198.   strcpy(key,"");
  199.   if ((new_recipe = malloc(sizeof(struct food))) == NULL)
  200.    {
  201.    printf("We are out of memory.  Bummer.\n");
  202.    abort();
  203.    } 
  204.   memcpy(new_recipe,food_ptr,sizeof(struct food));
  205.   new_recipe->grams *= ratio;
  206.   for (c = 0; c < NutrientCount; c++) new_recipe->nutrient[c] *= ratio; 
  207.   order_new_recipe();
  208.   }
  209.  }
  210. }
  211.  
  212. void new_recipe_show()
  213. {
  214. header("NUT:  Add Recipes");
  215. printf("%s\n",food_work.name);
  216. printf("Serving:  %-50s     Grams:  %6.2f\n",food_work.serving,food_work.grams);
  217. printf("          %-50s    Ounces:  %6.2f\n"," ",food_work.grams / 28.35);
  218. printf("Percentages of RDI in this serving:\n\n");
  219. food_display();
  220. spacer(20);
  221. }
  222.  
  223. void get_recipe_qty(float *result)
  224. {
  225. char buff[128];
  226. gets(buff);
  227. if (strchr(buff,'g') != NULL) *result = (float) atof(buff);
  228. else if (strchr(buff,'o') != NULL) *result = (float) atof(buff) * 28.35;
  229. else *result = ((float) 0);
  230. }
  231.  
  232. int recipe_show()
  233. {
  234. struct food *recipe_ptr = &recipe_root;
  235. int count = 0;
  236. printf("Recipe Name:  %-s\n\n",food_work.name);
  237. while (recipe_ptr->next != NULL)
  238.  {
  239.  recipe_ptr = recipe_ptr->next;
  240.  count++;
  241.  printf("%2d. %-60s     %8.1f oz\n",count,recipe_ptr->name,recipe_ptr->grams/28.35);
  242.  }
  243. if (count == 0) 
  244.  {
  245.  printf("\n\n\nNo foods have yet been recorded for this recipe.\n");
  246.  spacer(6);
  247.  return 0;
  248.  } 
  249. spacer(count + 2);
  250. return 1;
  251. }
  252.