home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: OtherApp / OtherApp.zip / nut-os2.zip / src / viewrdi.c < prev    next >
Text File  |  1998-09-11  |  5KB  |  127 lines

  1. /* viewrdi.c */
  2.  
  3. /*
  4.     Nut nutrition software 
  5.     Copyright (C) 1998 Jim Jozwiak.
  6.  
  7.     Source code OS/2 adaptation by Michele Della Guardia
  8.     E-mail : mikedg@ghostbbs.ml.org
  9.     Team OS/2 Italy
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License as published by
  13.     the Free Software Foundation; either version 2 of the License, or
  14.     (at your option) any later version.
  15.  
  16.     This program is distributed in the hope that it will be useful,
  17.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.     GNU General Public License for more details.
  20.  
  21.     You should have received a copy of the GNU General Public License
  22.     along with this program; if not, write to the Free Software
  23.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25.  
  26. #include "viewrdi.h"
  27. #include "food.h"
  28. #include "util.h"
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. void view_rdis(int opt)
  33. {
  34. int count, num;
  35. int intdiv = NutrientCount / 2 + (NutrientCount % 2 == 0 ? 0 : 1);
  36. for ( ; ; )
  37.  {
  38.  if (opt == 2) header("NUT:  View RDI and Rank Foods by Nutrients per Serving");
  39.  if (opt == 1) header("NUT:  View RDI and Rank Foods by Nutrients per 100 Grams");
  40.  if (opt == 0) header("NUT:  View RDI and Rank Foods by Nutrients per 100 Calories");
  41.  for ( count = 0 ; count <= NutrientCount / 2; count++)
  42.   {
  43.   printf("     %2d. %-13s  %7.1f %-3s      ",count+1,Nutrient[count],Rdi[count],RdiUnit[count]);
  44.   if (count+intdiv < NutrientCount) printf("%2d. %-13s  %7.1f %-3s    \n",count+1+intdiv,Nutrient[count+intdiv],Rdi[count+intdiv],RdiUnit[count+intdiv]);
  45.   else printf("\n");
  46.   }
  47.  if (opt == 2) printf("\n\n\n Serving sizes are the arbitrary serving sizes in the USDA Nutrient Database.\n"); 
  48.  if (opt == 1) printf("\n\n\n The rankings per 100 grams are the most compact good sources of the nutrient.\n"); 
  49.  if (opt == 0) printf("\n\n\n  The rankings per 100 calories are the bulkiest good sources of the nutrient.\n"); 
  50.  spacer(count + 4);
  51.  printf("\nType number of Nutrient to see food rankings (or <enter> to quit):  ");
  52.  num = get_int();
  53.  if (num < 1 || num > NutrientCount) return;
  54.  rank_foods(num - 1,opt);
  55.  }
  56. }
  57.  
  58. void rank_foods(int num, int opt)
  59. {
  60. int count, max, junk, displayed = 0;
  61. float abacus[FoodCount];
  62. struct food *food_ptr = &food_root;
  63. char head[80] = "NUT:  Foods Ranked by Percentage of ";
  64. char thisname[63];
  65. float theseounces;
  66. strncat(head,Nutrient[num],strlen(Nutrient[num])-1);
  67. if (opt == 0) strcat(head," RDI per 100 Calories");
  68. if (opt == 1) strcat(head," RDI per 100 Grams (3.5 oz)");
  69. if (opt == 2) strcat(head," RDI per Serving");
  70. if (num == 0 && opt == 0)
  71.  {
  72.  header(head); 
  73.  printf("These rankings are percentages of the RDI per 100 calories, to identify the\n");
  74.  printf("foods which provide a high daily percentage of a nutrient with a low daily\n");
  75.  printf("percentage of calories.  Thus, calories are not ranked.\n");
  76.  spacer(3);
  77.  printf("\nPress <enter> to continue...  ");
  78.  junk = get_int();
  79.  return;
  80.  } 
  81. if (opt == 0) for (count = 0 ; count < FoodCount ; count++) 
  82.  {
  83.  food_ptr = food_ptr->next;
  84.  if (food_ptr->nutrient[0] != 0) abacus[count] = (10000 * food_ptr->nutrient[num]) / (food_ptr->nutrient[0] * Rdi[num]);
  85.  else abacus[count] = 0;
  86.  }
  87. if (opt == 1) for (count = 0 ; count < FoodCount ; count++) 
  88.  {
  89.  food_ptr = food_ptr->next;
  90.  abacus[count] = (10000 * food_ptr->nutrient[num]) / (food_ptr->grams * Rdi[num]);
  91.  }
  92. if (opt == 2) for (count = 0 ; count < FoodCount ; count++) 
  93.  {
  94.  food_ptr = food_ptr->next;
  95.  abacus[count] = 100 * food_ptr->nutrient[num] / Rdi[num];
  96.  }
  97. junk = 'm';
  98. while (displayed <= (FoodCount - 20) && (junk == 'm' || junk == 'M'))
  99.  {
  100.  header(head); 
  101.  for (count = 0; count < 20; count++)
  102.   {
  103.   max = max_array(abacus);
  104.   food_ptr = food_number(max);
  105.   strncpy(thisname,food_ptr->name,60); thisname[60] = '\0';
  106.   if (opt == 0)
  107.    {
  108.    theseounces = 100 * food_ptr->grams / food_ptr->nutrient[0] / 28.35 ; 
  109.    printf("%-60s    %6.1f oz %5.0f%%\r",thisname,theseounces,abacus[max]);
  110.    }
  111.   if (opt == 1) printf("%-60s              %5.0f%%\r",thisname,abacus[max]);
  112.   if (opt == 2) printf("%-60s    %6.1f oz %5.0f%%\r",thisname,food_ptr->grams / 28.35,abacus[max]);
  113.   abacus[max] = 0;
  114.   }
  115.  displayed += 20;
  116.  printf("\nPress \"m\" for more (or just <enter> to quit)...  ");
  117.  junk = get_char();
  118.  }
  119. }
  120.  
  121. int max_array(float abacus[])
  122. {
  123. int count, i = 1;
  124. for ( count = 2 ; count <= FoodCount ; count++ ) if (abacus[count] > abacus[i]) i = count;
  125. return i;
  126. }
  127.