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

  1. /*
  2.     Nut nutrition software 
  3.     Copyright (C) 1998 Jim Jozwiak.
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #include "anameal.h"
  21. #include "food.h"
  22. #include "util.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <math.h>
  27. #include <time.h>
  28.  
  29. void analyze_meals()
  30. {
  31. struct food *food_ptr, *meal_ptr, *food_ptr_abacus[FoodCount];
  32. struct food *food_recommended[18];
  33. int meals, count, mealcount, meal = 0, max, deficnuts;
  34. int prodefic, abacuscount, spacecount, frcount, high_grams;
  35. char meal_date[7], inbuf[128], *servptr, servbuf[61];
  36. float deficpct[NutrientCount], food_abacus[FoodCount], serving_ratio_abacus[FoodCount];
  37. int deficnut[NutrientCount];
  38. float thispct, totaldefic, serving_ratio;
  39. mealcount = meal_count();
  40. if (mealcount == 0)
  41.  {
  42.  header("NUT:  Analyze Meals");
  43.  spacer(0);
  44.  printf("\nThere are no meals in database.  Press <enter> to continue..."); 
  45.  count = get_int();
  46.  return;
  47.  }
  48. max = mealcount;
  49. while (max > 0)
  50.  {
  51.  meal_ptr = &meal_root;
  52.  strcpy(meal_date,"");
  53.  if (max > mealcount) max = mealcount;
  54.  meals = 0;
  55.  header("NUT:  Analyze Meals");
  56.  if (max != 1) printf("Here are RDI average percentages for your previous %d meals:\n\n",max);
  57.  else printf("Here are RDI average percentages for your previous 1 meal:\n\n");
  58.  clear_work();
  59.  while (meal_ptr->next != NULL && meals <= max)
  60.   {
  61.   meal_ptr = meal_ptr->next;
  62.   if (strcmp(meal_date,meal_ptr->meal_date) != 0 || meal != meal_ptr->meal)
  63.    {
  64.    strcpy(meal_date,meal_ptr->meal_date);
  65.    meal = meal_ptr->meal;
  66.    meals++;
  67.    }
  68.   if (meals > max) break;
  69.   for (count = 0; count < NutrientCount; count++) food_work.nutrient[count] = food_work.nutrient[count] + ((300 * meal_ptr->nutrient[count]) / (Rdi[count] * max)); 
  70.   }
  71.  food_display();
  72.  spacer(17);
  73.  printf("Type number of meals to analyze or \"s\" for food suggestions.\n");
  74.  printf("Just press <enter> to quit:  ");
  75.  get_string(inbuf,127);
  76.  if (strncmp(inbuf,"s",1) == 0)
  77.   {
  78.   header("NUT:  Food Suggestions");
  79.   deficnuts = -1;
  80.   totaldefic = 0;
  81.   for (count = 0; count < NutrientCount; count++) 
  82.    {
  83.    if (count == 0 || count == 1 || count == 2 || count == 3 || count == 8 || count == 25 || 
  84.       count == 26 || count == 27 || count == 28 || 
  85.       100 - food_work.nutrient[count] <= 0) deficpct[count] = 0;
  86.    else 
  87.     {
  88.     deficnuts++;
  89.     deficnut[deficnuts] = count;
  90.     deficpct[deficnuts] = (((float) max) / 3) * (100 - food_work.nutrient[count]);
  91.     totaldefic += deficpct[deficnuts];
  92.     }
  93.    }
  94.   if (deficnuts > 0) deficsort(deficpct, deficnut, deficnuts);
  95.   if (deficnuts == -1 || deficpct[0] == 0)
  96.    {
  97.    printf("You have achieved the RDI for all nutrients.");
  98.    spacer(0);
  99.    printf("\nPress <enter> to continue..."); 
  100.    count = get_int();
  101.    return;        
  102.    }
  103.   printf("Here are those nutrients with additional daily percentages in the RDI:\n\n");
  104.   spacecount = 0;
  105.   for (count = 0; count <= deficnuts; count++) 
  106.    {
  107.    if (totaldefic >= 1) printf("%-31s%-13s %3.0f%%\n"," ",Nutrient[deficnut[count]],deficpct[count]);
  108.    else printf("%-31s%-13s %f%%\n"," ",Nutrient[deficnut[count]],deficpct[count]);
  109.    if (deficnuts > count && count == 16) printf("%-31s%-23s\n"," ","plus additional nutrients...");
  110.    if (deficnuts > count && count == 16) break;
  111.    }
  112.   spacer(deficnuts + 3);
  113.   printf("\nPress <enter> to continue..."); 
  114.   count = get_int();
  115.   header("NUT:  Food Suggestions");
  116.   printf("Here are randomly selected foods that provide the additional nutrients noted:\n\n");
  117.   frcount = -1;
  118.   while (totaldefic > 0)
  119.    {
  120.    food_ptr = &food_root;
  121.    abacuscount = -1;
  122.    while (food_ptr->next != NULL)
  123.     {
  124.     food_ptr = food_ptr->next;
  125.     food_ptr_abacus[++abacuscount] = food_ptr;
  126.     prodefic = 0;
  127.     serving_ratio_abacus[abacuscount] = 10;
  128.     high_grams = 114;
  129.     for (count = 0; count <= deficnuts; count++)
  130.      {
  131.      thispct = 100 * food_ptr->nutrient[deficnut[count]] / Rdi[deficnut[count]]; 
  132.      serving_ratio = deficpct[count] / thispct;
  133.      if (serving_ratio < serving_ratio_abacus[abacuscount] && serving_ratio * food_ptr->grams > 14) serving_ratio_abacus[abacuscount] = serving_ratio;
  134.      }
  135.     servptr = strcpy(servbuf,food_ptr->serving);
  136.     servptr = strtok(servbuf," ");
  137.     while (servptr != NULL)
  138.      {
  139.      if (strcmp("fl",servptr) == 0) high_grams = 228;
  140.      servptr = strtok(NULL," ");
  141.      }
  142.     servptr = strcpy(servbuf,food_ptr->name);
  143.     servptr = strtok(servbuf,",");
  144.     while (servptr != NULL)
  145.      {
  146.      if (strcmp("DEHYD",servptr) == 0 || strcmp("DRY",servptr) == 0) high_grams = 114;
  147.      servptr = strtok(NULL,",");
  148.      }
  149.     servptr = strcpy(servbuf,food_ptr->name);
  150.     servptr = strtok(servbuf,",");
  151.     while (servptr != NULL)
  152.      {
  153.      if (strcmp("FROZEN DSSRT",servptr) == 0) high_grams = 114;
  154.      servptr = strtok(NULL,",");
  155.      }
  156.     servptr = strcpy(servbuf,food_ptr->name);
  157.     servptr = strtok(servbuf,",");
  158.     while (servptr != NULL)
  159.      {
  160.      if (strcmp("CEREALS RTE",servptr) == 0) serving_ratio_abacus[abacuscount] = 0;
  161.      if (strcmp("CEREALS",servptr) == 0)
  162.       {
  163.       servptr = strtok(NULL,",");
  164.       if (strcmp("RTE",servptr) == 0) serving_ratio_abacus[abacuscount] = 0;
  165.       }
  166.      servptr = strtok(NULL,",");
  167.      }
  168.     servptr = strcpy(servbuf,food_ptr->name);
  169.     servptr = strtok(servbuf,",");
  170.     while (servptr != NULL)
  171.      {
  172.      if (strcmp("INFANT FORMULA",servptr) == 0 || strcmp("CHILD FORMULA",servptr) == 0 || strcmp("BABYFOOD",servptr) == 0) serving_ratio_abacus[abacuscount] = 0;
  173.      servptr = strtok(NULL,",");
  174.      }   
  175.     if (serving_ratio_abacus[abacuscount] > 1 && high_grams == 114) serving_ratio_abacus[abacuscount] = 1;
  176.     if ((serving_ratio_abacus[abacuscount] * food_ptr->grams) > high_grams) serving_ratio_abacus[abacuscount] = high_grams / food_ptr->grams;
  177.     for (count = 0; count <= deficnuts; count++)
  178.      {
  179.      thispct = serving_ratio_abacus[abacuscount] * 100 * food_ptr->nutrient[deficnut[count]] / Rdi[deficnut[count]]; 
  180.      if (thispct > deficpct[count]) thispct = deficpct[count];
  181.      prodefic += thispct;
  182.      }
  183.     if (serving_ratio_abacus[abacuscount] == 0) prodefic = 0;
  184.     food_abacus[abacuscount] = prodefic;
  185.     for (count = 0 ; count <= frcount; count++) if (food_ptr_abacus[abacuscount] == food_recommended[count]) food_abacus[abacuscount] = -1;
  186.     }
  187.    abacuscount = random_max_var_array(food_abacus, abacuscount);
  188.    food_ptr = food_ptr_abacus[abacuscount];            
  189.    food_recommended[++frcount] = food_ptr;
  190.    printf("%4.0f gm or %4.1f oz %-60s\n",serving_ratio_abacus[abacuscount] * food_ptr->grams,serving_ratio_abacus[abacuscount] * food_ptr->grams / 28.35,food_ptr->name);
  191.    spacecount++;
  192.    totaldefic = 0;
  193.    for (count = 0; count <= deficnuts; count++) 
  194.     {
  195.     thispct = serving_ratio_abacus[abacuscount] * 100 * food_ptr->nutrient[deficnut[count]] / Rdi[deficnut[count]]; 
  196.     if (thispct > deficpct[count]) thispct = deficpct[count];
  197.     deficpct[count] -= thispct;
  198.     totaldefic += deficpct[count];
  199.     }
  200.    }
  201.   spacer(++spacecount);
  202.   printf("Press <enter> to continue...");
  203.   count = get_int();
  204.   }
  205.  else max = atoi(inbuf);
  206.  }
  207. }
  208.  
  209. void deficsort(float *deficpct, int *deficnut, int deficnuts)
  210. {
  211. int count, subcount, tempint;
  212. float tempfloat;
  213. for (count = 0 ; count < deficnuts ; count++)
  214.  {
  215.  for (subcount = count + 1 ; subcount <= deficnuts ; subcount++)
  216.   {
  217.   if (deficpct[subcount] > deficpct[count])
  218.    {
  219.    tempfloat = deficpct[count];
  220.    deficpct[count] = deficpct[subcount];
  221.    deficpct[subcount] = tempfloat;
  222.    tempint = deficnut[count];
  223.    deficnut[count] = deficnut[subcount];
  224.    deficnut[subcount] = tempint;
  225.    }
  226.   }
  227.  }
  228. }
  229.  
  230. int random_max_var_array(float abacus[], int last)
  231. {
  232. int count, i = 0, max_count = 0, rand_count;
  233. unsigned seed;
  234. time_t t;
  235. for ( count = 1 ; count < last ; count++ )
  236.  {
  237.  if (abacus[count] > abacus[i])
  238.   {
  239.   i = count;
  240.   max_count = 1;
  241.   }
  242.  else if (abacus[count] == abacus[i]) max_count++;
  243.  }
  244. time(&t);
  245. seed = (unsigned) t;
  246. srand(seed);
  247. rand_count = rand() % max_count;
  248. for (count = 0 ; count < last ; count++)
  249.  {
  250.  if (abacus[count] == abacus[i]) rand_count--;
  251.  if (rand_count < 0) return count;
  252.  }
  253. return i;
  254. }
  255.