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

  1. /* food.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 "food.h"
  23. #include "util.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <math.h>
  29.  
  30. struct food food_root, food_work, *new_food, meal_root, *new_meal, recipe_root, *new_recipe;
  31.  
  32. char *Nutrient[] =
  33. {
  34. "Calories:","Protein:","Total Fat:","Carbohydrate:","Fiber:",
  35. "Calcium:","Phosphorus:","Iron:","Sodium:","Potassium:",
  36. "Magnesium:","Zinc:","Copper:","Manganese:","Selenium:","Vit A:",
  37. "Vit E:","Thiamin:","Riboflavin:","Niacin:","Panto Acid:",
  38. "Vit B6:","Folate:","Vit B12:","Vit C:","Fat Sat:",
  39. "Fat Mono:","Fat Poly:","Cholesterol:","Linoleic:","Linolenic:"
  40. };
  41.  
  42. float Rdi[] =
  43. {
  44. 2000,55,65,300,25,
  45. 1000,1000,18,2400,4000,
  46. 400,15,2,2,70,5000,
  47. 20,1.5,1.7,20,10,
  48. 2,400,6,60,20,
  49. 25,20,300,10.75,1.5
  50. };
  51.  
  52. float RdiBase[] =
  53. {
  54. 2000,55,65,300,25,
  55. 1000,1000,18,2400,4000,
  56. 400,15,2,2,70,5000,
  57. 20,1.5,1.7,20,10,
  58. 2,400,6,60,20,
  59. 25,20,300,10.75,1.5
  60. };
  61.  
  62. char *RdiUnit[] =
  63. {
  64. "kc","gm","gm","gm","gm",
  65. "mg","mg","mg","mg","mg",
  66. "mg","mg","mg","mg","mcg","IU",
  67. "mg","mg","mg","mg","mg",
  68. "mg","mcg","mcg","mg","gm",
  69. "gm","gm","mg","gm","gm"
  70. };
  71.  
  72. int NutrientCount = 31;
  73.  
  74. int FoodCount = 0;
  75.  
  76. void order_new_food()
  77. {
  78. struct food *food_ptr = &food_root;
  79. while (food_ptr->next != NULL &&
  80.        namestrcmp(new_food->name, food_ptr->next->name) >= 0)
  81.        food_ptr = food_ptr->next;
  82. new_food->next = food_ptr->next;
  83. food_ptr->next = new_food;
  84. }
  85.  
  86. void order_new_recipe()
  87. {
  88. struct food *food_ptr = &recipe_root;
  89. while (food_ptr->next != NULL) food_ptr = food_ptr->next;
  90. new_recipe->next = NULL;
  91. food_ptr->next = new_recipe;
  92. }
  93.  
  94. int namestrcmp(char *name1, char *name2)
  95. {
  96. char namebuf1[61], namebuf2[61];
  97. int count;
  98. for (count = 0 ; count <= 60 ; count++)
  99.  {
  100.  if (name1[count] == ',') namebuf1[count] = '\a';
  101.  else namebuf1[count] = name1[count]; 
  102.  if (name2[count] == ',') namebuf2[count] = '\a';
  103.  else namebuf2[count] = name2[count]; 
  104.  }
  105. return strcmp(namebuf1,namebuf2);
  106. }
  107.  
  108. void order_new_meal()
  109. {
  110. struct food *meal_ptr = &meal_root;
  111. while (meal_ptr->next != NULL &&
  112.        strcmp(new_meal->meal_date, meal_ptr->next->meal_date) < 0)
  113.             meal_ptr = meal_ptr->next;
  114. while (meal_ptr->next != NULL &&
  115.        strcmp(new_meal->meal_date, meal_ptr->next->meal_date) <= 0 &&
  116.        new_meal->meal < meal_ptr->next->meal)
  117.             meal_ptr = meal_ptr->next;
  118. while (meal_ptr->next != NULL &&
  119.        strcmp(new_meal->meal_date, meal_ptr->next->meal_date) <= 0 &&
  120.        new_meal->meal <= meal_ptr->next->meal &&
  121.        strcmp(new_meal->name,meal_ptr->next->name) > 0) 
  122.             meal_ptr = meal_ptr->next;
  123. new_meal->next = meal_ptr->next;
  124. meal_ptr->next = new_meal;
  125. }
  126.  
  127. struct food *food_choice(char *screentitle)
  128. {
  129. struct food *food_ptr;
  130. struct food *ptrlist[1325];
  131. int count = 0, charcount, keysize, lines, junk;
  132. char key[61];
  133. for ( ; ; )
  134.  {
  135.  strcpy(key,key_take());
  136.  if (strcmp(key,"") == 0)
  137.   {
  138.   header(screentitle);
  139.   spacer(0);
  140.   if (strcmp(screentitle,"NUT:  Add Recipes") == 0) printf("\nType beginning of food name to add to recipe (or <enter> to quit):  ");
  141.   else printf("\nType beginning of food name to select (or <enter> to quit):  ");
  142.   get_string(key,60);
  143.   key_clean();
  144.   }
  145.  if (key[0] == '\0') return (struct food *) -1;
  146.  for (charcount = 0 ; charcount < 60 ; charcount++) key[charcount] = toupper(key[charcount]);
  147.  key_put(key);
  148.  header(screentitle);
  149.  food_ptr = &food_root;
  150.  keysize = strlen(key); lines = 0; count = 0;
  151.  while ((food_ptr = food_ptr->next)) if (strncmp(key,food_ptr->name,keysize) == 0) ptrlist[++count] = food_ptr;
  152.  while (count == 0)
  153.   {
  154.   key_clean();
  155.   spacer(-1);
  156.   printf("No food named \"%s\".  Try another (or <enter> to quit):  ",key);
  157.   get_string(key,60);
  158.   if (key[0] == '\0') return (struct food *) 0;
  159.   for (charcount = 0 ; charcount < 60 ; charcount++) key[charcount] = toupper(key[charcount]);
  160.   key_put(key);
  161.   header(screentitle);
  162.   food_ptr = &food_root;
  163.   keysize = strlen(key); lines = 0; count = 0;
  164.   while ((food_ptr = food_ptr->next)) if (strncmp(key,food_ptr->name,keysize) == 0) ptrlist[++count] = food_ptr;
  165.   }
  166.  if (count == 1) return ptrlist[1];
  167.  if (count < 21)
  168.   {
  169.   for (lines = 1 ; lines <= count ; lines++) printf("%2d. %s\n",lines,ptrlist[lines]->name);
  170.   spacer(lines - 2);
  171.   printf("Type number of food category (\"b\" to go back, <enter> to quit):  ");
  172.   get_string(key,60);
  173.   junk = 0;
  174.   if (strcmp(key,"b") == 0 || strcmp(key,"B") == 0) junk = -38;
  175.   if (junk == -38) 
  176.    {
  177.    key_take();
  178.    continue;
  179.    }
  180.   junk = atoi(key); 
  181.   if (junk > 0 && junk <= count) 
  182.    {
  183.    key_put(ptrlist[junk]->name);
  184.    return ptrlist[junk];
  185.    }
  186.   else return (struct food *) 0;   
  187.   }
  188.  if (count > 20) food_subcat(key, ptrlist, count);
  189.  }
  190. }
  191.  
  192. void food_subcat(char *key, struct food **ptrlist, int count)
  193. {
  194. char food_screen[1476];
  195. char *fsptr[121], *thisfsptr, *bufptr, buffer[61], format[11], format100[11], tokenhead[61], junkstring[20];
  196. char *col[10], colformat1[110], colformat2[110];
  197. int i, l, s, subcount = 0, charcount, tokencount, nlength, tokenlength, tokenunique, headlength, linecount, savelinecount = 0, tokenmax = 0, colcount;
  198. for (linecount = 1 ; linecount <= 20 ; linecount++) fsptr[linecount] = food_screen + (61 * linecount);
  199. for (linecount = 1 ; linecount <= count ; linecount++)
  200.  {
  201.  tokencount = 0;
  202.  nlength = strlen(ptrlist[linecount]->name);
  203.  for (charcount = 0 ; charcount <= nlength ; charcount++) if (ptrlist[linecount]->name[charcount] == ',') tokencount++;
  204.  if (tokencount > tokenmax) tokenmax = tokencount;
  205.  }
  206. while (subcount < count && tokenmax > -1)
  207.  {
  208.  tokenmax--;
  209.  subcount = 0;
  210.  for (linecount = 1; subcount < count && linecount <= 20 ; linecount++)
  211.   {
  212.   subcount++;
  213.   nlength = strlen(ptrlist[subcount]->name);
  214.   thisfsptr = fsptr[linecount];
  215.   tokencount = 0;
  216.   for (charcount = 0 ; charcount <= nlength ; charcount++)
  217.    {
  218.    if (ptrlist[subcount]->name[charcount] == ',') tokencount++;
  219.    if (tokencount == tokenmax) 
  220.     {
  221.     thisfsptr[charcount] = '\0';
  222.     break;
  223.     }
  224.    thisfsptr[charcount] = ptrlist[subcount]->name[charcount];
  225.    if (ptrlist[subcount]->name[charcount] == '\0') break;
  226.    }
  227.   if (linecount > 1 && 0 == strcmp(fsptr[linecount],fsptr[linecount-1])) linecount--;
  228.   }
  229.  savelinecount = --linecount;
  230.  }
  231. if (savelinecount > 1)
  232.  {
  233.  for (linecount = 1; linecount <= savelinecount ; linecount++) printf("%2d.  %s\n",linecount,fsptr[linecount]);
  234.  spacer(savelinecount-1);
  235.  printf("Type number of food category (\"b\" to go back, <enter> to quit):  ");
  236.  get_string(junkstring,20);
  237.  if (strcmp(junkstring,"b") == 0 || strcmp(junkstring,"B") == 0) 
  238.   {
  239.   key_take();
  240.   return;
  241.   }
  242.  linecount = atoi(junkstring);
  243.  if (linecount > 0 && linecount <= savelinecount) 
  244.   {
  245.   strcpy(key,fsptr[linecount]);
  246.   for (subcount = 1 ; subcount <= count ; subcount++) if (strcmp(key,ptrlist[subcount]->name) == 0) break;
  247.   if (--subcount == count) strcat(key,",");
  248.   }
  249.  else key[0] = '\0';
  250.  key_put(key);
  251.  }
  252. else
  253.  {
  254.  tokenhead[0] = '\0';
  255.  for (tokenmax = 1 ; ; tokenmax++)
  256.   {
  257.   buffer[0] = '\0';
  258.   tokenunique = 0;
  259.   tokenlength = 0;
  260.   for (subcount = 1 ; subcount <= count ; subcount++)
  261.    {
  262.    bufptr = ptrlist[subcount]->name;
  263.    nlength = 0;
  264.    for (tokencount = 1 ; tokencount <= tokenmax ; tokencount++)
  265.     {
  266.     bufptr += nlength;
  267.     bufptr += strspn(bufptr,",");
  268.     nlength = strcspn(bufptr,",");
  269.     } 
  270.    if (nlength > tokenlength) 
  271.     {
  272.     tokenlength = nlength;
  273.     tokenunique++;
  274.     strncpy(buffer,bufptr,nlength);
  275.     buffer[nlength] = '\0';
  276.     }
  277.    else if (nlength < tokenlength) 
  278.     {
  279.     tokenunique++;
  280.     strncpy(buffer,bufptr,nlength);
  281.     buffer[nlength] = '\0';
  282.     }
  283.    else if (strncmp(buffer,bufptr,nlength) != 0)
  284.     {
  285.     tokenunique++;
  286.     strncpy(buffer,bufptr,nlength);
  287.     buffer[nlength] = '\0';
  288.     }
  289.    else;
  290.    }
  291.   if (tokenunique == 1)
  292.    {
  293.    strcat(buffer,",");
  294.    strcat(tokenhead,buffer);
  295.    }
  296.   if (tokenunique != 1) break;
  297.   }
  298.  if (tokenhead[0] == '\0') savelinecount = 20;
  299.  else savelinecount = 19; 
  300.  headlength = strlen(tokenhead);
  301.  while ( savelinecount * (81 / (tokenlength + 5 ) ) < tokenunique )
  302.   {
  303.   buffer[0] = '\0';
  304.   tokenunique = 0 ;
  305.   tokenlength--;
  306.   for (subcount = 1 ; subcount <= count ; subcount++)
  307.    {
  308.    bufptr = ptrlist[subcount]->name + headlength;
  309.    nlength = strcspn(bufptr,",");
  310.    while (*(bufptr + nlength - 1) == ' ') nlength--;
  311.    if (nlength > tokenlength) nlength = tokenlength;
  312.    if (strncmp(buffer,bufptr,nlength) != 0)
  313.     {
  314.     tokenunique++;
  315.     strncpy(buffer,bufptr,nlength);
  316.     buffer[nlength] = '\0';
  317.     }
  318.    }
  319.   }
  320.  for (linecount = 0 ; linecount <= (tokenunique+2) ; linecount++) fsptr[linecount] = food_screen + ((tokenlength+1) * linecount);
  321.  food_screen[0] = '\0';
  322.  linecount = 0;
  323.  for (subcount = 1 ; subcount <= count ; subcount++)
  324.   { 
  325.   bufptr = ptrlist[subcount]->name + headlength;
  326.   nlength = (strcspn(bufptr,","));
  327.   if (nlength > tokenlength) nlength = tokenlength;
  328.   while (*(bufptr + nlength - 1) == ' ') nlength--;
  329.   strncpy(fsptr[linecount + 1],bufptr,nlength);
  330.   thisfsptr = fsptr[linecount + 1];
  331.   thisfsptr[nlength] = '\0';
  332.   if (strncmp(fsptr[linecount],fsptr[linecount + 1],nlength) != 0) linecount++;
  333.   }
  334.  sprintf(format,"%%2d. %%-%ds",tokenlength);
  335.  sprintf(format100,"%%3d. %%-%ds",tokenlength);
  336.  strcpy(colformat1,format);
  337.  strcpy(colformat2,format);
  338.  colcount = (linecount % savelinecount == 0 ? 0 : 1) + (linecount / savelinecount);
  339.  for (i = 2 ; i <= colcount ; i++)
  340.   {
  341.   if (i != colcount) strcat(colformat1," ");
  342.   strcat(colformat2," ");
  343.   if ((i * savelinecount) < 100 || tokenunique < 100)
  344.    {
  345.    if (i != colcount) strcat(colformat1,format);
  346.    strcat(colformat2,format);
  347.    }
  348.   else 
  349.    {
  350.    if (i != colcount) strcat(colformat1,format100);
  351.    strcat(colformat2,format100);
  352.    }
  353.   }
  354.  strcat(colformat1,"\n");
  355.  strcat(colformat2,"\n");
  356.  col[colcount - 1] = colformat1;
  357.  col[colcount] = colformat2;
  358.  if (headlength > 0 ) printf("%-s\n",tokenhead);
  359.  l = linecount ; s = savelinecount;
  360.  for (i = 1 ; i <= savelinecount ; i++)
  361.   {
  362.   if      (i+(1*s)>l) printf(col[1],i,fsptr[i]);
  363.   else if (i+(2*s)>l) printf(col[2],i,fsptr[i],i+s,fsptr[i+s]);
  364.   else if (i+(3*s)>l) printf(col[3],i,fsptr[i],i+s,fsptr[i+s],i+(2*s),fsptr[i+(2*s)]);
  365.   else if (i+(4*s)>l) printf(col[4],i,fsptr[i],i+s,fsptr[i+s],i+(2*s),fsptr[i+(2*s)],i+(3*s),fsptr[i+(3*s)]);
  366.   else if (i+(5*s)>l) printf(col[5],i,fsptr[i],i+s,fsptr[i+s],i+(2*s),fsptr[i+(2*s)],i+(3*s),fsptr[i+(3*s)],i+(4*s),fsptr[i+(4*s)]);
  367.   else if (i+(6*s)>l) printf(col[6],i,fsptr[i],i+s,fsptr[i+s],i+(2*s),fsptr[i+(2*s)],i+(3*s),fsptr[i+(3*s)],i+(4*s),fsptr[i+(4*s)],i+(5*s),fsptr[i+(5*s)]);
  368.   else if (i+(7*s)>l) printf(col[7],i,fsptr[i],i+s,fsptr[i+s],i+(2*s),fsptr[i+(2*s)],i+(3*s),fsptr[i+(3*s)],i+(4*s),fsptr[i+(4*s)],i+(5*s),fsptr[i+(5*s)],i+(6*s),fsptr[i+(6*s)]);
  369.   else if (i+(8*s)>l) printf(col[8],i,fsptr[i],i+s,fsptr[i+s],i+(2*s),fsptr[i+(2*s)],i+(3*s),fsptr[i+(3*s)],i+(4*s),fsptr[i+(4*s)],i+(5*s),fsptr[i+(5*s)],i+(6*s),fsptr[i+(6*s)],i+(7*s),fsptr[i+(7*s)]);
  370.   else if (i+(9*s)>l) printf(col[9],i,fsptr[i],i+s,fsptr[i+s],i+(2*s),fsptr[i+(2*s)],i+(3*s),fsptr[i+(3*s)],i+(4*s),fsptr[i+(4*s)],i+(5*s),fsptr[i+(5*s)],i+(6*s),fsptr[i+(6*s)],i+(7*s),fsptr[i+(7*s)],i+(8*s),fsptr[i+(8*s)]);
  371.   else;
  372.   }
  373.  spacer(19);
  374.  printf("Type number of food category (\"b\" to go back, <enter> to quit):  ");
  375.  get_string(junkstring,20);
  376.  if (strcmp(junkstring,"b") == 0 || strcmp(junkstring,"B") == 0) 
  377.   {
  378.   key_take();
  379.   return;
  380.   }
  381.  i = atoi(junkstring);
  382.  if (i > 0 && i <= linecount)
  383.   {
  384.   if (headlength > 0)
  385.    {
  386.    strcpy(key,tokenhead);
  387.    strcat(key,fsptr[i]);
  388.    }
  389.   else strcpy(key,fsptr[i]);
  390.   }
  391.  else key[0] = '\0';
  392.  key_put(key);
  393.  }
  394. }
  395.  
  396. int meal_show(char *meal_date, int meal)
  397. {
  398. struct food *meal_ptr = &meal_root;
  399. char namebuf[61];
  400. int count = 0;
  401. printf("Meal Date:  %s                                               Meal Number:  %d\n\n",meal_date,meal);
  402. for ( ; ; )
  403.  {
  404.  if (strcmp(meal_date,meal_ptr->meal_date) == 0 && meal == meal_ptr->meal) 
  405.   {
  406.   count++;
  407.   strncpy(namebuf,meal_ptr->name,60); namebuf[60] = '\0';
  408.   printf("%2d. %-60s     %8.1f oz\n",count,namebuf,meal_ptr->grams/28.35);
  409.   }
  410.  if (meal_ptr->next == NULL) break;
  411.  meal_ptr = meal_ptr->next;
  412.  }
  413. if (count == 0) 
  414.  {
  415.  printf("\n\n\nNo foods have yet been recorded for this meal.\n");
  416.  spacer(6);
  417.  return 0;
  418.  } 
  419. spacer(count + 2);
  420. return 1;
  421. }
  422.  
  423. void food_show(struct food *food_ptr, float *ratio)
  424. {
  425. char servingstring[62];
  426. int count;
  427. if (*ratio > 1 || (*ratio > 0 && *ratio < 1))
  428.  {
  429.  sprintf(servingstring,"%5.1f x %-43s",*ratio,food_ptr->serving);
  430.  servingstring[51] = '\0';
  431.  }
  432. printf("%s\n",food_ptr->name);
  433. if (*ratio == 1) printf("Serving:  %-50s   Grams:  %6.2f\n",food_ptr->serving,food_ptr->grams * *ratio);
  434. else printf("Serving:  %-51s  Grams:  %6.2f\n",servingstring,food_ptr->grams * *ratio);
  435.                  printf("          %-50s  Ounces:  %6.2f\n"," ",*ratio * food_ptr->grams / 28.35);
  436. printf("Percentages of RDI in this serving:\n\n");
  437. for (count = 0; count < NutrientCount; count++) food_work.nutrient[count] = *ratio * 100 * food_ptr->nutrient[count] / Rdi[count]; 
  438. food_display();
  439. spacer(20);
  440. }
  441.  
  442. struct food *food_number(int i)
  443. {
  444. int count;
  445. struct food *food_ptr = &food_root;
  446. for (count = -1; count < i; count++) food_ptr = food_ptr->next;
  447. return food_ptr;
  448. }
  449.  
  450. struct food *meal_find(char *meal_date,int meal)
  451. {
  452. struct food *meal_ptr = &meal_root;
  453. while (meal_ptr->next != NULL)
  454.  {
  455.  meal_ptr = meal_ptr->next;
  456.  if (strcmp(meal_ptr->meal_date,meal_date) == 0 && meal_ptr->meal == meal) return meal_ptr;
  457.  }
  458. return NULL;
  459. }
  460.  
  461. void delete_meal_food(char *meal_date, int meal, int num)
  462. {
  463. struct food *m, *meal_ptr = &meal_root;
  464. int count = 0;
  465. while (meal_ptr->next != NULL)
  466.  {
  467.  if (strcmp(meal_date,meal_ptr->next->meal_date) == 0 && meal == meal_ptr->next->meal) 
  468.   {
  469.   count++;
  470.   if (count == num)
  471.    {
  472.    m = meal_ptr->next;
  473.    meal_ptr->next = meal_ptr->next->next;
  474.    free(m);
  475.    return;
  476.    }
  477.   }
  478.  meal_ptr = meal_ptr->next;
  479.  }
  480. }
  481.  
  482. int meal_count()
  483. {
  484. struct food *meal_ptr = &meal_root;
  485. int count = 0;
  486. char lastdate[7];
  487. int lastmeal;
  488. if (meal_root.next == NULL) return 0;
  489. meal_ptr = meal_root.next;
  490. strcpy(lastdate,meal_ptr->meal_date);
  491. lastmeal = meal_ptr->meal;
  492. count = 1;
  493. while ((meal_ptr = meal_ptr->next))
  494.  {
  495.  if (strcmp(lastdate,meal_ptr->meal_date) != 0 || lastmeal != meal_ptr->meal)
  496.   {
  497.   count++;
  498.   strcpy(lastdate,meal_ptr->meal_date);
  499.   lastmeal = meal_ptr->meal;
  500.   }
  501.  } 
  502. return count;
  503. }
  504.  
  505. int meal_food_count()
  506. {
  507. struct food *meal_ptr = &meal_root;
  508. int count = 0;
  509. if (meal_root.next == NULL) return 0;
  510. meal_ptr = meal_root.next;
  511. count = 1;
  512. while ((meal_ptr = meal_ptr->next)) count++;
  513. return count;
  514. }
  515.  
  516. void clear_work()
  517. {
  518. int count;
  519. for (count = 0; count < NutrientCount; count++) food_work.nutrient[count] = 0;
  520. food_work.grams = 0;
  521. }
  522.  
  523. void food_display()
  524. {
  525. float calsfromfat = 0, calsfromcarb = 0, calsfromprot = 0, correction;
  526. if (food_work.nutrient[0] != 0)
  527.  {
  528.  calsfromfat = (int) (float) (food_work.nutrient[2]*Rdi[2]*8.8235294) / (food_work.nutrient[0]*Rdi[0]/100);
  529.  calsfromcarb = (int) (float) ((food_work.nutrient[3]*Rdi[3]) - (food_work.nutrient[4]*Rdi[4]))* 4.175 / (food_work.nutrient[0]*Rdi[0]/100);
  530.  calsfromprot = (int) (float) (food_work.nutrient[1]*Rdi[1]*4.175) / (food_work.nutrient[0]*Rdi[0]/100);
  531.  calsfromfat = ceil(calsfromfat);
  532.  calsfromprot = ceil(calsfromprot);
  533.  calsfromcarb = ceil(calsfromcarb);
  534.  correction = calsfromfat + calsfromcarb + calsfromprot - 100;
  535.  if (calsfromcarb - correction >= 0) calsfromcarb -= correction;
  536.  else if (calsfromprot - correction >= 0) calsfromprot -= correction;
  537.  else calsfromfat -= correction;
  538.  }
  539. printf("%-13s  %5.0f%%        %-13s  %5.0f%%        %-13s  %5.0f%%\n",Nutrient[0],food_work.nutrient[0],Nutrient[1],food_work.nutrient[1],Nutrient[2],food_work.nutrient[2]);
  540. printf("%-12s                 %-13s  %5.0f%%        %-13s  %5.0f%%\n"," ",Nutrient[3],food_work.nutrient[3],Nutrient[25],food_work.nutrient[25]);
  541. printf("Cals from Fat:   %3.0f%% %6s %-13s  %5.0f%%        %-13s  %5.0f%%\n",calsfromfat," ",Nutrient[4],food_work.nutrient[4],Nutrient[26],food_work.nutrient[26]);
  542. printf("Cals from Carb:  %3.0f%% %6s %-13s  %5.0f%%        %-13s  %5.0f%%\n",calsfromcarb," ",Nutrient[29],food_work.nutrient[29],Nutrient[27],food_work.nutrient[27]);
  543. printf("Cals from Prot:  %3.0f%% %6s %-13s  %5.0f%%        %-13s  %5.0f%%\n",calsfromprot," ",Nutrient[30],food_work.nutrient[30],Nutrient[28],food_work.nutrient[28]);
  544. printf("\n");
  545. printf("%-13s  %5.0f%%        %-13s  %5.0f%%        %-13s  %5.0f%%\n",Nutrient[15],food_work.nutrient[15],Nutrient[20],food_work.nutrient[20],Nutrient[23],food_work.nutrient[23]);
  546. printf("%-13s  %5.0f%%        %-13s  %5.0f%%        %-13s  %5.0f%%\n",Nutrient[17],food_work.nutrient[17],Nutrient[21],food_work.nutrient[21],Nutrient[24],food_work.nutrient[24]);
  547. printf("%-13s  %5.0f%%        %-13s  %5.0f%%        %-13s  %5.0f%%\n",Nutrient[18],food_work.nutrient[18],Nutrient[22],food_work.nutrient[22],Nutrient[16],food_work.nutrient[16]);
  548. printf("%-13s  %5.0f%%\n",Nutrient[19],food_work.nutrient[19]);
  549. printf("\n");
  550. printf("%-13s  %5.0f%%        %-13s  %5.0f%%        %-13s  %5.0f%%\n",Nutrient[5],food_work.nutrient[5],Nutrient[13],food_work.nutrient[13],Nutrient[14],food_work.nutrient[14]);
  551. printf("%-13s  %5.0f%%        %-13s  %5.0f%%        %-13s  %5.0f%%\n",Nutrient[12],food_work.nutrient[12],Nutrient[6],food_work.nutrient[6],Nutrient[8],food_work.nutrient[8]);
  552. printf("%-13s  %5.0f%%        %-13s  %5.0f%%        %-13s  %5.0f%%\n",Nutrient[7],food_work.nutrient[7],Nutrient[9],food_work.nutrient[9],Nutrient[11],food_work.nutrient[11]);
  553. printf("%-13s  %5.0f%%\n",Nutrient[10],food_work.nutrient[10]);
  554. }
  555.  
  556. void delete_recipe_food(int num)
  557. {
  558. struct food *m, *recipe_ptr = &recipe_root;
  559. int count = 0;
  560. while (recipe_ptr->next != NULL)
  561.  {
  562.  count++;
  563.  if (count == num)
  564.   {
  565.   m = recipe_ptr->next;
  566.   recipe_ptr->next = recipe_ptr->next->next;
  567.   free(m);
  568.   return;
  569.   }
  570.  recipe_ptr = recipe_ptr->next;
  571.  }
  572. }
  573.  
  574. void delete_meals(int keep)
  575. {
  576. struct food *meal_ptr = &meal_root, *last_meal_ptr = NULL;
  577. int count = 0;
  578. char meal_date[7], meal = 0;
  579. if (keep < 0) return;
  580. strcpy(meal_date,"");
  581. while (count <= keep && meal_ptr->next != NULL)
  582.  {
  583.  last_meal_ptr = meal_ptr;
  584.  meal_ptr = meal_ptr->next;
  585.  if (strcmp(meal_date,meal_ptr->meal_date) != 0  || meal != meal_ptr->meal) 
  586.   {
  587.   count++;
  588.   strcpy(meal_date,meal_ptr->meal_date);
  589.   meal = meal_ptr->meal;
  590.   }
  591.  }  
  592. if (count <= keep) return;
  593. last_meal_ptr->next = NULL;
  594. while (meal_ptr != NULL)
  595.  {
  596.  last_meal_ptr = meal_ptr;
  597.  meal_ptr = meal_ptr->next;
  598.  free(last_meal_ptr);
  599.  }
  600. }
  601.