/* dietpad.c -- command functions for dietaid.c */ /* copywrited June 1986 by Terry W. Gintz */ #define MAXITEMS 202 /* total items in ingredient list (ingredients.c) */ struct itemvalues ingredient[31]; /* up to 30 ingredients per recipe */ char ingred[28]=" Ingredient "; char carbo[13]="Carbohydrate"; char pro[10]=" Protein "; char fats[8]=" Fat "; char cal[9]="Calories"; char amount[9]=" Amount"; int number=0; /* number of ingredients in recipe */ int adding; /* set flag to add ingredients until "NONE" entered for name */ int valid; /* set flag on valid name (anything but "NONE" or "NULL") */ int totalcal; /* total calories in recipe */ short int saved=1; /* set flag until new recipe is saved */ double totalcar,totalfat,totalpro; /* totals for recipe */ void addtorecipe(),clearlist(),cutoff(),divide(),showdata(); void saverecipe(),loadrecipe(); char getanswer(); short int matched; /* set flag when ingredient data is known */ extern FILE *console; char *p,q,buffer[31]; short int waittable; newrecipe() { register short int i,n; if (NOT saved) saveoption(); number=0; saved=1; clearlist(); for (i=0;i<30;i++){ /* add up to 30 ingredients per recipe */ valid=0; adding=1; matched=0; while ((NOT matched)||(NOT valid)){ getname(i); /* get name of ingredient */ if (NOT adding) /* no more ingredients to add */ break; for (n=0;n0){ showdata(); saved=0; /* protect data from being overwritten until save option is used or offered */ } } showtable() { register short int i,n,a,b; b=0; a=0; n=1; waittable=0; fprintf(console,"These are the ingredients I know values for: \n"); for (i=0;i to Continue with Listing"); if (waittable) fprintf(console,", 'Q' to quit"); rewind(console); fscanf(console,"%c",&q); rewind(console); } void addtorecipe() { register short int i,n,start; if (number==0){ fprintf(console,"Use 'N' to enter a new recipe\n"); return; } if (number==30){ fprintf(console,"Aren't 30 ingredients enough for one recipe?\n"); fprintf(console,"One has to draw the line somewhere. . .\n\n"); return; } start=number; /* start adding from ingredient # */ for (i=number;i<30;i++){ /* add up to 30 ingredients per recipe */ valid=0; adding=1; matched=0; while ((NOT matched)||(NOT valid)){ getname(i); /* get name of ingredient */ if (NOT adding) /* no more ingredients to add */ break; matched=0; /* name not valid until checked against data list or ingredient data entered manually */ for (n=0;nstart){ showdata(); saved=0; /* protect data from being overwritten until save option is used or offered */ } } void cutoff() { register short inc,n; int calo,i; double carb,fa,prot; char answer; if (number==0){ fprintf(console,"There's no ingredient to delete!\n"); return; } i=0; fprintf(console,"Delete which ingredient (1-%d)?",number); rewind(console); fscanf(console,"%d",&i); rewind(console); if ((i>number)||(i<=0)){ fprintf(console,"No such ingredient!\n"); return; } --i; if (ingredient[i].quantity!=1.0){ calo=(int)(ingredient[i].quantity*ingredient[i].calories); prot=ingredient[i].quantity*ingredient[i].protein; carb=ingredient[i].quantity*ingredient[i].carbohydrate; fa=ingredient[i].quantity*ingredient[i].fat; } else{ calo=ingredient[i].calories; prot=ingredient[i].protein; carb=ingredient[i].carbohydrate; fa=ingredient[i].fat; } fprintf(console,"\n%2d %2.2lf ",i+1,ingredient[i].quantity); fprintf(console,"%c ",ingredient[i].measurement); fprintf(console,"%-28s",ingredient[i].name); fprintf(console," %6d ",calo); fprintf(console,"%5.1lf ",prot); fprintf(console,"%5.1lf ",carb); fprintf(console,"%5.1lf\n\n",fa); fprintf(console,"Is this the ingredient you want to delete?"); rewind(console); answer=getanswer(); rewind(console); if (toupper(answer)=='Y'){ --number; for (n=i;n0) named=1; } fp=fopen(filename,"w"); if (fp==NULL){ fprintf(console,"\nCan't open file\n"); return; } fprintf(fp,"%s\t%d\n","RECIPE",number); for (i=0;i<31;i++){ temp[0]=ingredient[i].measurement; temp[1]='\0'; fprintf(fp,"%lf\t%s",ingredient[i].quantity,temp); fprintf(fp,"\t%s\t%d",ingredient[i].name,ingredient[i].calories); fprintf(fp,"\t%lf\t",ingredient[i].protein); fprintf(fp,"%lf\t%lf\n",ingredient[i].carbohydrate,ingredient[i].fat); } fclose(fp); saved=1; }