#include #include #include #include #include #include #include #include #include #include #include extern unsigned _stklen =54321U; int main(int) { int const MAXELEM=38; //Max number of elements int const FX=25, FX2=35; //X value for first column and second column int const MAXX=4, CURX=3, X=0, Y=1, WIDTH=2;//Array position name definitions //Key press definitions int const LEFT=75, RIGHT=77,ESCP=27,UP=72, DOWN=80,F1=59,F2=60; int const ENTER=13,DEL=83, BACK=8, END=79,HOME=71,TAB=9,RTAB=15; int HIELEM=5,IN=0,j,jj,i,key; //HIELEM takes on the value of the first cell int mode=0,change=0; char imgbuff[MAXELEM+1][30]; //??? char inbuff [MAXELEM+1][15]; //??? int def [MAXELEM+1][5]; //Element structure allocation int valid[MAXELEM+1]; //??? long int minutes,seconds; long double windspeed,winddirection,groundspeed,angle1,angle2,angle3,bearing,airspeed,distance,time; double *values; if((values = (double (*))malloc(((MAXELEM+1)*10))) == NULL) { printf("\nNOT ENOUGH MEMORY FOR Value BUFFER.");goto exit; } values[0]=1; textmode(C80); textbackground(0); clrscr(); textcolor(7); gotoxy(1,1); cprintf("Legs="); textcolor(4); gotoxy(44,1); textbackground(7); cprintf("WINDCALC v1.0e Credits Hit F2 "); gotoxy(44,2); textcolor(0); cprintf("Arrows control cursor. Enter "); gotoxy(44,3); textcolor(0); cprintf("up to 16 legs. Spc toggles "); gotoxy(44,4); textcolor(0); cprintf("mode and converts fields. "); textcolor(7); textbackground(0); //define first cell positions,sizeand its present cursor position. def[0][X] =14; //starting x position def[0][Y] =1; //Starting y position def[0][WIDTH]=2; //Field width def[0][CURX]=def[0][X]; //Current x postion defined as its first position here def[0][MAXX]=def[0][X]+def[0][WIDTH]-1; //Max x position gotoxy(def[0][X]-1,def[0][Y]); //draw bracket around cell textcolor(15); putch('['); textcolor(7); putch('1'); gotoxy(def[0][MAXX]+1,def[0][Y]); textcolor(15);putch(']'); textcolor(7); cprintf(" (Tab/Shf-tab)"); def[1][X] =14; //starting x position def[1][Y] =2; //Starting y position def[1][WIDTH]=6; //Field width def[1][CURX]=def[1][X]; //Current x postion defined as its first position here def[1][MAXX]=def[1][X]+def[1][WIDTH]-1; //Max x position gotoxy(1,2); cprintf("AirSpeed="); gotoxy(def[1][X]-1,def[1][Y]); //draw bracket around cell textcolor(15); putch('['); gotoxy(def[1][MAXX]+1,def[1][Y]); putch(']'); textcolor(7); cprintf(" (cruise)"); def[2][X] =14; //starting x position def[2][Y] =3; //Starting y position def[2][WIDTH]=6; //Field width def[2][CURX]=def[2][X]; //Current x postion defined as its first position here def[2][MAXX]=def[2][X]+def[2][WIDTH]-1; //Max x position gotoxy(1,3); cprintf("WndSpd/Dir="); gotoxy(def[2][X]-1,def[2][Y]); //draw bracket around cell textcolor(15); putch('['); gotoxy(def[2][MAXX]+1,def[2][Y]); putch(']'); putch('/'); def[3][X] =23; //starting x position def[3][Y] =3; //Starting y position def[3][WIDTH]=3; //Field width def[3][CURX]=def[3][X]; //Current x postion defined as its first position here def[3][MAXX]=def[3][X]+def[3][WIDTH]-1; //Max x position gotoxy(def[3][X]-1,def[3][Y]); //draw bracket around cell putch('['); gotoxy(def[3][MAXX]+1,def[3][Y]); putch(']'); textcolor(7); cprintf(" (Wind Out Of)"); def[4][X] =14; //starting x position def[4][Y] =4; //Starting y position def[4][WIDTH]=8; //Field width def[4][CURX]=def[4][X]; //Current x postion defined as its first position here def[4][MAXX]=def[4][X]+def[4][WIDTH]-1; //Max x position gotoxy(1,4); cprintf("Mode="); gotoxy(def[4][X]-1,def[4][Y]); //draw bracket around cell textcolor(15); putch('['); textcolor(7); cprintf("STATUTE"); gotoxy(def[4][MAXX]+1,def[4][Y]); textcolor(15); putch(']'); textcolor(7); cprintf(" (S or N)"); gotoxy(1,5); textcolor(4); textbackground(0); cprintf("------------------------------------------------------------------------------"); gotoxy(1,6); textcolor(15); textbackground(0); cprintf(" BEARING DISTANCE"); //Define each cell as to postion and width ect for future printing textcolor(15); gotoxy(50,6); cprintf("Heading, GS, Time(min,sec)"); textcolor(7); for (i=5;i <= MAXELEM; i=i+2) //Loop to define each input cell. { values[i]=0; //Init data holder for each cell values[i+1]=0; def[i][X] =FX; //starting x position for column 1 of cell i def[i][Y] =5+(i-1)/2; //Starting y position (row) of cell i def[i][WIDTH]=7; //Field width (width of cell i) def[i][CURX] =def[i][X]; //Current x postion (remember last curser pos) def[i][MAXX] =def[i][X]+def[i][WIDTH]-1;//Max x position definition //Column two defs def[i+1][X] =FX2; //starting x position for column 2 def[i+1][Y] =5+(i-1)/2; //Starting y position (row) def[i+1][WIDTH]=7; def[i+1][CURX] =def[i+1][X]; //Current x postion def[i+1][MAXX] =def[i+1][X]+def[i+1][WIDTH]-1; //Max x position } redef: for (i=5;i <= MAXELEM; i=i+2) //Erase brackets from old cells that have { //been undefined gotoxy(def[i][X]-7,def[i][Y]); //Erase Leg markers too cprintf(" "); gotoxy(def[i][X]-1,def[i][Y]); putch(' '); gotoxy(def[i][MAXX]+1,def[i][Y]); putch(' '); gotoxy(def[i+1][X]-1,def[i+1][Y]); putch(' '); gotoxy(def[i+1][MAXX]+1,def[i+1][Y]); putch(' '); } j=1; for (i=5;i <= HIELEM; i=i+2) { gotoxy(def[i][X]-7,def[i][Y]); textcolor(7); cprintf("Leg%0d=",j++); gotoxy(def[i][X]-1,def[i][Y]); textcolor(15); putch('['); gotoxy(def[i][MAXX]+1,def[i][Y]); putch(']'); gotoxy(def[i+1][X]-1,def[i+1][Y]); putch('['); gotoxy(def[i+1][MAXX]+1,def[i+1][Y]); putch(']'); } ret: while(1) { { textbackground(0); textcolor(15); gotoxy(def[IN][CURX],def[IN][Y]); key=getch(); gotoxy(1,8); cprintf ("F1 to Calculate"); gotoxy(1,9); cprintf("F2 for Credits "); gotoxy(1,10); cprintf("ESC to QUIT"); textcolor(7); if((key > 47 && key < 58 || (key == 110) || (key == 78) || (key==32)) && (IN == 4) && (mode==0)) { gotoxy(def[IN][X],def[IN][Y]); textcolor(7); cprintf("NAUTICAL"); mode=1; change=1; } else if((key > 47 && key < 58 || (key == 83) || (key == 115) || (key==32))&& (IN == 4) && (mode==1)) { gotoxy(def[IN][X],def[IN][Y]); textcolor(7); cprintf("STATUTE "); mode=0; change=1; } else if(key == LEFT) { if(def[IN][CURX]-1 >= def[IN][X]) { gotoxy(def[IN][--def[IN][CURX]], def[IN][Y]); } else { gotoxy(def[IN][X],def[IN][Y]); } } else if(key == RIGHT) { if(def[IN][CURX]+1 <= def[IN][MAXX]) { gotoxy(def[IN][++def[IN][CURX]], def[IN][Y]); } else { gotoxy(def[IN][MAXX],def[IN][Y]); } } else if(key == UP || key == RTAB) { if(IN-1 >= 0) { gotoxy(def[--IN][CURX],def[IN][Y]); } else { gotoxy(def[0][CURX],def[0][Y]); } } else if(key == DOWN || key == TAB || key == ENTER) { if(IN+1 <= HIELEM) { gotoxy(def[++IN][CURX],def[IN][Y]); } else { gotoxy(def[HIELEM][CURX],def[HIELEM][Y]); } } else if(key == HOME) { gotoxy((def[0][CURX]=def[0][X]),def[0][Y]); IN=0; } else if(key == END) { gotoxy(def[HIELEM][X],def[HIELEM][Y]); IN=HIELEM; } else if((key == DEL || key == BACK) && (IN != 4)) { puttext(def[IN][X],def[IN][Y],def[IN][MAXX],def[IN][Y]," \a \a \a \a \a \a \a"); //Clear the field gotoxy((def[IN][CURX]=def[IN][X]),def[IN][Y]); values[IN]=0; } else if(key > 47 && key < 58 || (key == 46) || (key==32)) { if(def[IN][CURX] <= def[IN][MAXX]) { gotoxy(def[IN][CURX]++,def[IN][Y]);//Be sure we're at the correct position for writing the next character putch(key); if(def[IN][CURX] > def[IN][MAXX]) { def[IN][CURX]--; //Don't allow write past MAXX } } } else if (key == ESCP) { textbackground(0); textcolor(7); clrscr(); goto exit; } } for(i=0;i<=HIELEM; i++) { gettext(def[i][X],def[i][Y],def[i][MAXX],def[i][Y],&imgbuff[i]); for(j=0; j (MAXELEM/2) - 3) //Check for invalid number of chosen legs { valid[0]=0; //Special case...clear the input field puttext(def[0][X],def[0][Y],def[0][MAXX],def[0][Y]," \a \a \a \a \a \a \a"); //Clear the field gotoxy((def[0][CURX]=def[0][X]),def[0][Y]); } } } change=0; //If new breakpoints number is if(valid[0] == 1 && (4+int(values[0])*2 != HIELEM) && //valid and not the same as before (values[0] >=1 && values[0] <=(MAXELEM/2)-3)) //and between 2 and MAXELEM/2 then OK to change { HIELEM=4+int(values[0])*2; goto redef; } if(key == F2) { textcolor(4); gotoxy(44,1); textbackground(7); cprintf(" WINDCALC v1.0e "); gotoxy(44,2); textcolor(0); cprintf("Freeware Written by Todd Snyder"); gotoxy(44,3); textcolor(0); cprintf("For more info go to "); gotoxy(44,4); textcolor(0); cprintf("www.homefires.us/flight "); textcolor(7); textbackground(0); } if (key == F1) { textcolor(4); gotoxy(44,1); textbackground(7); cprintf("WINDCALC v1.0e Credits Hit F2 "); gotoxy(44,2); textcolor(0); cprintf("Arrows control cursor. Enter "); gotoxy(44,3); textcolor(0); cprintf("up to 16 legs. Spc toggles "); gotoxy(44,4); textcolor(0); cprintf("mode and converts fields. "); textcolor(7); textbackground(0); if(values[1] < .1 || values[1] > 1000) //Airspeed { valid[1]=0; //Special case...clear the input field //puttext(def[1][X],def[1][Y],def[1][MAXX],def[1][Y]," \a \a \a \a \a \a \a"); //Clear the field //gotoxy((def[1][CURX]=def[1][X]),def[1][Y]); //IN=1; } if(values[2] < .1 || values[2] > 1000) //windspeed { valid[2]=0; //Special case...clear the input field //puttext(def[2][X],def[2][Y],def[2][MAXX],def[2][Y]," \a \a \a \a \a \a \a"); //Clear the field //gotoxy((def[2][CURX]=def[2][X]),def[2][Y]); //IN=2; } if(values[3] < 0 || values[3] > 359.999) //wind direction { valid[3]=0; //Special case...clear the input field //puttext(def[3][X],def[3][Y],def[3][MAXX],def[3][Y]," \a \a \a \a \a \a \a"); //Clear the field //gotoxy((def[3][CURX]=def[3][X]),def[3][Y]); //IN=3; } for(i=5;i<=HIELEM; i=i+2) { if(values[i] < 0 || values[i] > 359.999) { valid[i]=0; //Special case...clear the input field //puttext(def[i][X],def[i][Y],def[i][MAXX],def[i][Y]," \a \a \a \a \a \a \a"); //Clear the field //gotoxy((def[i][CURX]=def[i][X]),def[i][Y]); //IN=i; } if(values[i+1] < .1 || values[i+1] > 1000) { valid[i+1]=0; //Special case...clear the input field //puttext(def[i+1][X],def[i+1][Y],def[i+1][MAXX],def[i+1][Y]," \a \a \a \a \a \a \a"); //Clear the field //gotoxy((def[i+1][CURX]=def[i+1][X]),def[i+1][Y]); //IN=i+1; } } for (i=1; i<=HIELEM; i++) //Check for any bad data and move curser to it { if(valid[i] != 1 && i != 4) { IN=i; goto redef; } } airspeed=values[1]; windspeed=values[2]; winddirection=values[3]; winddirection+=180; if(winddirection > 360) {winddirection-=360;} textcolor(15); gotoxy(50,6); cprintf("Heading, GS , Time(min,sec)"); for(i=0;i<=15;i++) //Erase old fields { gotoxy(50,i+8); cprintf(" "); } j=7; for(i=5;i<=HIELEM;i=i+2) { distance=values[i+1]; bearing=values[i]; //Bearing angle1=winddirection-bearing; if(angle1<0) {angle1+=360;} if( (fabsl(angle1-180) < .00001)) { angle2=0; groundspeed=airspeed-windspeed; if(groundspeed <.0001) {groundspeed=.0001;} time=60*(distance/groundspeed); minutes=(time); seconds=(time-minutes)*60; } else if(fabsl(angle1) < .0001) { angle2=0; groundspeed=airspeed+windspeed; if(groundspeed <.0001) {groundspeed=.0001;} time=60*(distance/groundspeed); minutes=(time); seconds=(time-minutes)*60; } else { angle1/=57.2958;//convert to radians angle2=asinl( (windspeed*sinl(angle1))/airspeed); angle3=3.141593-(angle1+angle2); groundspeed=(airspeed*sinl(angle3))/sinl(angle1); if(groundspeed <.0001) {groundspeed=.0001;} angle1*=57.2958; angle2*=57.2958; angle3*=57.2958; } if(angle2 > bearing) {angle2-=360;} //No negative headings..LOL time=60*(distance/groundspeed); minutes=(time); seconds=(time-minutes)*60; textcolor(7); gotoxy(50,j++); if(minutes > 9999) { cprintf("%5.1Lf, %5.1Lf, NEVER ",bearing-angle2,groundspeed); } else{ cprintf("%5.1Lf, %5.1Lf, %4ldm,%2lds",bearing-angle2,groundspeed,minutes,seconds); } } } } exit: free(values); return(1); }