小型飞机导航软件

· · 个人记录

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <conio.h>
#include <windows.h>
#include <queue>
using namespace std;

#define BANNER_COLOR FOREGROUND_BLUE | FOREGROUND_INTENSITY
#define NORMAL_COLOR FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
#define METHOD_COLOR (NORMAL_COLOR) | FOREGROUND_INTENSITY
#define QUERY_COLOR FOREGROUND_BLUE | FOREGROUND_INTENSITY
#define ALPHA_COLOR FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY
#define ERROR_COLOR FOREGROUND_RED | FOREGROUND_INTENSITY
#define MAN_POINT_COLOR FOREGROUND_GREEN
#define VNAV_COLOR FOREGROUND_GREEN | FOREGROUND_INTENSITY
#define ACTIVE_COLOR FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY

HANDLE handle;

inline void chgcolor(DWORD color) {
    handle = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(handle, color);
}

double mpi, deg180, deg90;
#define pi mpi
const double earth_radius = 60.0*1.852;
// Config:
int transon = 0;
double measure = 1.0;   // For nm 1.852.
    int nmode = 0;             // For nm 1.

void waitesc() {
    do {} while (getch() != 27);    // esc
}

inline double absof(double x) {
    if (x>0.0) return x;
    else return -x;
}


double deg2rad(double deg) {
    return deg/180.0*pi;
}

double rad2deg(double rad) {
    return rad/pi*180.0;
}

// If cause MLE then shrink.
#define WPTSIZE 2048
#define LIBSIZE 300003

int wcount = 0;
int saved_count = 0;


struct waypoint {
    char wname[6];
    double longit, latit; // jingdu, weidu (N-E positive, S-W negative)
} wpts[WPTSIZE], saved[LIBSIZE];

#define SAVEDS "SAVES.WPT"

inline double distance_to(waypoint a, waypoint b, int transform = 0) {
    double re = absof(b.longit - a.longit);
    if (a.latit == b.latit && (a.latit == 0.0 || a.latit == -0.0)) {
        return re*earth_radius;
    }
    if (re == 0.0 || re == -0.0) {
        return absof(a.latit-b.latit)*earth_radius;
    }

    //if (re > 180.0) re = 360.0 - re;
    if (transform) re = 360.0 - re;

    double longa = deg2rad(a.longit), longb = deg2rad(b.longit);
    double lata = deg2rad(a.latit), latb = deg2rad(b.latit);

    //double symb = 1.0;
    if (lata * latb < 0.0) latb = -absof(latb);
    else latb = absof(latb);
    lata = absof(lata);
    //latb = absof(latb);   // Should not be influenced

    double coss = sin(lata)*sin(latb)+cos(lata)*cos(latb)*cos(deg2rad(re));

    // For debug only.
    //printf("cosS = %lf, Delta = %lf", coss, re);
    // end.

    //if (coss > 0.0) {
    //  return
    //} else {
    //
    //}
    return earth_radius*rad2deg(acos(coss))/measure;

    //return earth_radius*acos(cos(deg90-longa)*cos(deg90-longb)+sin(deg90-longa)*sin(deg90-longb)*cos(latb-lata));
}


// We can't directly use memory, so we use disk instead:
#define DIJ_DISK "PLANNER\\DIJ.TMP"
#define DIJ_SWAP "PLANNER\\DIJSW.TMP"
double GIVE_UP_DIS = 15000.0;

// Every single point shouldn't be longer than this:
double CONT_REQ_DIS = 200.0;    // Will be input
// This is for Boeing 747. You Toupluv can hardly reach this
// In order: dis[i], plan_count[i], plan[i][...]

char extended[LIBSIZE];
double current_diss[LIBSIZE] = {0.0};   // Read current distances only

//short origin_route[LIBSIZE], origin_len;
int current_from[LIBSIZE], current_acc[LIBSIZE];

void copy_stream(FILE *fo, FILE *fw) {
    int i, j, leng, td;
    fscanf(fo, "%d", &leng);
    fprintf(fw, "%d ", leng);
    for (j = 0; j < leng; j++) {
        fscanf(fo, "%d", &td);
        fprintf(fw, "%d ", td);
    }
    fprintf(fw, "\n");
}

#define PRDEBUG 1
// SHOULDN'T BE 0 NOW

// Will prepare for a route from point A from point B, At O(n^2) -- means 10^10, 150+ sec.
void prepare_route_inner(int from, int to) {
    system("cls");
    chgcolor(METHOD_COLOR);
    printf("Planning a route from %s to %s, please wait ... 00%%", saved[from].wname, saved[to].wname);
    memset(extended, 0, sizeof(extended));
    int current_ex = from, i, j;
    for (i = 0; i < saved_count; i++) {
        current_diss[i] = GIVE_UP_DIS;
    }
    current_diss[from] = 0.0;
    current_from[from] = -1;
    current_acc[from] = 1;
    //queue<int> q;
    for (i = 0; i < saved_count; i++) {
        //current_ex = q.front();
        extended[current_ex] = 1;
        // Now try extending j
//      printf("\b\b\b%02d%%", i * 100 / saved_count);
//      printf("\rPlanning a route from %s to %s, please wait ... %02d%% (%6d/%6d)", saved[from].wname, saved[to].wname, i * 100 / saved_count, i, saved_count);
        for (j = 0; j < saved_count; j++) {
            if (extended[j]) continue;
            double dt = distance_to(saved[current_ex], saved[j], transon);
            if (dt > CONT_REQ_DIS) continue;    // Can't be used to expand. This is useful because this has been the original distance
            double curd = current_diss[current_ex] + dt;
            if (curd < GIVE_UP_DIS && curd < current_diss[j]) { // Was 'greater' originally...?
                current_diss[j] = curd;
                current_from[j] = current_ex;
                current_acc[j] = current_acc[current_ex] + 1;
            }
        }
        double minn = GIVE_UP_DIS;
        int sel = -1;
        for (j = 0; j < saved_count; j++) {
            if ((!extended[j]) && current_diss[j] < minn) {
                minn = current_diss[j];
                sel = j;
            }
        }
        printf("\rPlanning a route from %s to %s, please wait ... %02d%% (%5.2lf/%5.2lf)", saved[from].wname, saved[to].wname, int(minn * 100 / GIVE_UP_DIS), minn, GIVE_UP_DIS);
        if (sel < 0) {
            if (current_diss[to] >= GIVE_UP_DIS) {
                chgcolor(ERROR_COLOR);
                printf("\nSystem can't find a route!\n");
                waitesc();
                chgcolor(NORMAL_COLOR);
                return;
            } else {
                break;  // Successfully completed.
            }
        }
        current_ex = sel;
    }
    // Now everything is ready. Just read it in.
//#if PRDEBUG
//  printf("\nExporting route... Count = %d\n", current_acc[to]);
//#endif
    wcount = current_acc[to];
    strcpy(wpts[wcount - 1].wname, saved[to].wname);
    wpts[wcount - 1].latit = saved[to].latit;
    wpts[wcount - 1].longit = saved[to].longit;
    int cur = to, at = wcount - 1;
    while (current_from[cur] >= 0) {
        at--;
        cur = current_from[cur];
        strcpy(wpts[at].wname, saved[cur].wname);
        wpts[at].latit = saved[cur].latit;
        wpts[at].longit = saved[cur].longit;
//#if PRDEBUG
//      printf("At %d for cur %d (%s)\n", at, cur, wpts[at].wname);
//#endif
    }
    // Finished!
#if PRDEBUG
    if (at != 0) {
        printf("Warning: corrupt at %d\n", at);
        system("pause");
        printf("\n");
    }
#endif
}

void prepare_route(int from, int to) {
#if !PRDEBUG
    try {
#endif
        prepare_route_inner(from, to);
#if !PRDEBUG
    } catch (...) {
        chgcolor(ERROR_COLOR);
        printf("ERROR! Cannot run dijkstra planner.");
        waitesc();
    }
#endif
}


inline void lat_display(waypoint a) {
    printf("%9.3lf %c",absof(a.latit),(a.latit > 0 ? 'N' : (a.latit < 0 ? 'S': ' ')));
}

inline void lon_display(waypoint a) {
    printf("%9.3lf %c",absof(a.longit),(a.longit > 0 ? 'E' : (a.longit < 0 ? 'W' : ' ')));
}

// Advanced navigation (Drive pass...)

// Require Ma, Mb given !
inline double alpha_to(waypoint a, waypoint b, double Ma, double Mb) {
    double longa = deg2rad(a.longit), longb = deg2rad(b.longit);
    double lata = deg2rad(a.latit), latb = deg2rad(b.latit);
    return Ma - Mb + (longa - longb)*sin((lata+latb)/2.0);
}

void degree_trans_out(double deg) {
    // dec to 60 mode.
    double fd = floor(deg);
    double smallp = (deg - fd) * 60;
    printf(" (%d deg %.2lf min)", int(fd), smallp);
}

// Range: [0, 360). Avoiding 360 ? DIS IS UNUSED
double angle_to(waypoint a, waypoint b, double dis_suggestion, int transform = 0) {
    //if (dis < 0) dis = distance_to(a, b);
    double longs = b.longit - a.longit;//a.longit - b.longit;
    if (a.latit == b.latit && (a.latit == 0.0 || a.latit == -0.0)) {
        if (b.longit > a.longit) return 90.0;
        else return 270.0;
    }
    if (longs == 0.0 || longs == -0.0) {
        if (b.latit > a.latit) return 0.0;
        else return 180.0;
    }
    double longsym = 1.0;
    if (longs < 0.0) longsym = -1.0;
    double re = absof(longs);
    if (transform) re = 360.0 - re;
    double deltal = deg2rad(re);//deg2rad(b.latit-a.latit);

    double longa = deg2rad(a.longit), longb = deg2rad(b.longit);
    double lata = deg2rad(a.latit), latb = deg2rad(b.latit);

    //double symb = 1.0;
    double latasym = 1.0;
    if (lata * latb <= 0.0) latb = -absof(latb);
    else latb = absof(latb);
    if (lata <= 0.0) latasym = -1.0;
    lata = absof(lata);

    //double coss = symb*sin(lata)*sin(latb)+cos(lata)*cos(latb)*cos(deg2rad(re)))
    double coss = sin(lata)*sin(latb)+cos(lata)*cos(latb)*cos(deg2rad(re));
    double dis = acos(coss);

    //UNUSED:
    ////double asresult = asin(cos(latb)*sin(deltal)/sin(dis));

    //double coresult = atan(sin(deltal)/(cos(deg2rad(a.longit))*tan(deg2rad(b.longit))-sin(deg2rad(a.longit)*cos(deltal))));
    //double asresult = asin(cos(deg2rad(b.latit))*sin(deltal)/sin(dis));
    double sind = sin(deltal)/(cos(lata)*tan(latb)-sin(lata)*cos(deltal));
    double coresult = atan(sind);
    //asresult = rad2deg(asresult);
    coresult = rad2deg(coresult);
    // I can make sure of tan, but I can't sure about sin (as)
    // This is simply for debug, or sometimes for a better control:
    //printf("lamb = %lf (D=%lf), lat1 = %lf, lat2 = %lf, co = %lf, as = %lf\n", deltal, absof(a.longit - b.longit), lata, latb, coresult, 0.0);
    // end.

    //double res = coresult;

    if (coresult < 0) {
        // 90 - 270?
        coresult += 180.0;
    }

    double basics = 0.0, res;

     // The first name (whether N or S) depends on starter N/S
    if (latasym < 0) {
        if (longsym < 0) {
            // SW
            basics = 180.0 + coresult;
        } else {
            // SE
            basics = 180.0 - coresult;
        }
    } else {
        if (longsym < 0) {
            // NW
            basics = 0.0 - coresult;
        } else {
            // NE
            basics = 0.0 + coresult;
        }
    }
    res = basics;
    if (res >= 360.0) res -= 360.0;
    else if (res < 0.0) res += 360.0;

    return res;

    /*
    if (asresult > 0) {
        // TOWARDS EAST
        if (coresult > 0) {
            // Northeast
            return coresult;
        } else {
            // Southeast
            return 180.0+coresult;
        }
    } else if (asresult == 0) {
        // STRAIGHT
        if (coresult > 0) {
            return 0.0; // ?
        } else if (coresult == 0) {
            // BOTH 90 / 270 USE THIS
            if (a.longit>b.longit) return 270.0;
            else return 90.0;
        } else {
            return 180.0;       // ?
        }
    } else {
        // TOWARDS WEST
        if (coresult > 0) {
            return coresult+180.0;
        } else {
            return coresult+360.0;
        }
    }
    */
}

const int pagecount = 15;

int progress = 0;   // The point AFTER (INCLUDE ITSELF) will be shown

double gotalpha = 0.0;
int hasalpha = 0;

inline int getpc() {
    int actwcount = wcount - progress;
    return actwcount / pagecount + (actwcount % pagecount ? 1 : 0);
}

void clear_line() {
    //putchar('\r');
    int i;
    for (i = 0; i < 75; i++) putchar('\b');
    for (i = 0; i < 75; i++) putchar(' ');
    for (i = 0; i < 75; i++) putchar('\b');
    putchar('\r');
}

char crt[8], rpath[64], tmpinput[8];
int desires[64], desire_count = 0;

int select_desire() {
    printf("Confirm waypoint: -----");
    int sel = 0, finish = 0;
    char ch;
    while (1) {
        clear_line();
        if (sel >= desire_count) {
            //printf("\b\b\b\b\b%5s", "<MAN>");
            chgcolor(MAN_POINT_COLOR);
            printf("<Manually select>");
        }
        else {
            chgcolor(METHOD_COLOR);
            printf("%5s ", saved[desires[sel]].wname);
            lon_display(saved[desires[sel]]);
            putchar(' ');
            lat_display(saved[desires[sel]]);
        }
        ch = getch();
        switch (ch) {
            case 13:
                finish = 1;
                break;
            case 27:
                return -1;
                break;  // Gave up
            case 'z': case 'Z':
                if (sel > 0) sel--;
                break;
            case 'x': case 'X':
                if (sel < desire_count) sel++;
                break;

        }
        if (finish) break;
    }
    clear_line();
    return sel;
}


// -1 -> Current point. (Borders means the end)
// Returning -1 = Gave up
// Will from SAVED instead of WPTS if from_array_mode = 1
int select_point(int default_sel = -1, int allow_borders = 0, int from_array_mode = 0) {
    if (from_array_mode == 1) {
        scanf("%s", tmpinput);
        desire_count = 0;
        for (int i = 0; i < saved_count; i++) {
            if (strcmp(tmpinput, saved[i].wname) == 0) {
                desires[desire_count++] = i;
            }
        }
        int sel1 = select_desire();
        if (sel1 >= desire_count) {
            printf("Input %s LONG and LAT in order: ");
            ///// TODO: ADD THE SAME FOR SEL2 AND ADD INPUT
            strcpy(saved[saved_count].wname, tmpinput);
            scanf("%lf%lf", &saved[saved_count].longit, &saved[saved_count].latit);
            sel1 = saved_count;
            saved_count++;
        } else if (sel1 >= 0) {
            sel1 = desires[sel1];
        }
        return sel1;
    }
    waypoint* wptarget = wpts;
    int* wpmax = &wcount;
    int prog = progress; //?
    if (from_array_mode) {
        wptarget = saved;
        prog = 0;
        wpmax = &saved_count;
    }
    if (default_sel < 0) default_sel = prog;
    clear_line();
    char op;
    int current_sel = default_sel;
    chgcolor(NORMAL_COLOR);
    printf("Select waypoint: ----- [----]");
    while (1) {
        chgcolor(METHOD_COLOR);
        if (current_sel >= (*wpmax)) printf("\b\b\b\b\b\b\b\b\b\b\b\b%5s [----]", "<End>");
        else printf("\b\b\b\b\b\b\b\b\b\b\b\b%5s [%4d]", wptarget[current_sel].wname, current_sel);
        op = getch();
        switch (op) {
            case 'z': case 'Z': // Left/Right arrow
                if (current_sel > prog) current_sel--;
                break;
            case 'x': case 'X':
                if (allow_borders) {
                    if (current_sel < (*wpmax)) current_sel++;
                } else if (current_sel + 1 < (*wpmax)) current_sel++;
                break;
            case 27:
                return -1;  // Gave up
                break;
            case 13:
                return current_sel;
                break;
        }
    }
}

// SHOULD EMPLACE FIRST
// 1 - success, 0 - failed
int put_point(int where, int override = 0) {
    desire_count = 0;
    chgcolor(NORMAL_COLOR);
    printf("Input waypoint name, LON and LAT in order: ");
    // This can only be inside.
    scanf("%s", tmpinput);
    for (int i = 0; i < saved_count; i++) {
        if (strcmp(tmpinput, saved[i].wname) == 0) {
            desires[desire_count++] = i;
        }
    }
    // Confirm
    int sel = select_desire();
    if (sel < 0) return 0;
    if (sel >= desire_count) {
        // Manually input ...
        chgcolor(NORMAL_COLOR);
        printf("Input %s LONG and LAT in order: ", tmpinput);
        scanf("%lf%lf", &wpts[where].longit, &wpts[where].latit);
        // TODO: TO SAVE THE POINT.
        if (saved_count >= LIBSIZE) {
            chgcolor(ERROR_COLOR);
            printf("WARNING: Saved points overloaded");
            waitesc();
        } else {
            strcpy(saved[saved_count].wname, tmpinput);
            saved[saved_count].longit = wpts[where].longit;
            saved[saved_count].latit = wpts[where].latit;
            saved_count++;
        }
    } else if (override) {
        chgcolor(ERROR_COLOR);
        printf("Override %s ? (Y/N)", tmpinput);
        switch (getch()) {
            case 'Y': case 'y':
                clear_line();
                chgcolor(METHOD_COLOR);
                printf("Input %s LONG and LAT in order: ", tmpinput);
                scanf("%lf%lf", &wpts[where].longit, &wpts[where].latit);
                saved[desires[sel]].longit = wpts[where].longit;
                saved[desires[sel]].latit = wpts[where].latit;
                break;
            default:
                wpts[where].longit = saved[desires[sel]].longit;
                wpts[where].latit = saved[desires[sel]].latit;
                break;
        }

    } else {
        wpts[where].longit = saved[desires[sel]].longit;
        wpts[where].latit = saved[desires[sel]].latit;
    }
    strcpy(wpts[where].wname, tmpinput);
    return 1;
}

void sync_saved() {
    chgcolor(NORMAL_COLOR);
    printf("Saving waypoint library ...");
    FILE *fs = fopen(SAVEDS, "w");
    for (int i = 0; i < saved_count; i++) {
        fprintf(fs, "%s %lf %lf\n", saved[i].wname, saved[i].longit, saved[i].latit);
    }
    fclose(fs);
}

int savespage = 0;
double request_dis = -1.00;

int tdat_id = -1;
double tdat_after = 0.0;

void points_print(int order, waypoint wp) {
    printf("%04d %5s ",order,wp.wname);
    lon_display(wp);
    putchar(' ');
    lat_display(wp);
    if (order == tdat_id) printf(" <T/D> after %.2lf", tdat_after);
    printf("\n");
}

void saves_module() {
    // This is an external page
    while (1) {
        system("cls");
        int savespage_total = saved_count / pagecount + 1;
        chgcolor(BANNER_COLOR);
        printf("SeaNVU For Topuluv 154");
        chgcolor(METHOD_COLOR);
        printf(" | Z/X Scrolls | [Q]uit this\n======= Saved Waypoints =======\n%4d Point saved  Page %2d / %2d\n\n", saved_count, savespage + 1, savespage_total);
        chgcolor(NORMAL_COLOR);
        for (int i = savespage * pagecount; i < saved_count && i < savespage * pagecount + pagecount; i++) {
            points_print(i, saved[i]);
        }
        printf("\n===========================================\n");
        chgcolor(METHOD_COLOR);
        printf("[A]dd [M]odify [D]elete");
        char ch = getch();
        int sel;
        switch (ch) {
            case 'Z': case 'z':
                if (savespage > 0) savespage--;
                break;
            case 'X': case 'x':
                if (savespage + 1 < savespage_total) savespage++;
                break;
            case 'A': case 'a':
                if (saved_count >= LIBSIZE) {
                    chgcolor(ERROR_COLOR);
                    printf("Cannot contain too much points!");
                    waitesc();
                    break;
                }
                printf("Input waypoint, LONG and LAT in order: ");
                scanf("%s%lf%lf", &saved[saved_count].wname, &saved[saved_count].longit, &saved[saved_count].latit);
                saved_count++;
                break;
            case 'M': case 'm':
                //printf("Select waypoint: -----");
                if (saved_count <= 0) {
                    chgcolor(ERROR_COLOR);
                    printf("No point to modify!");
                    waitesc();
                    break;
                }
                printf("Input point to modify: ");
                sel = select_point(0,0,1);
                if (sel >= 0) {
                    printf("Input waypoint, LONG and LAT in order: ");
                    scanf("%s%lf%lf", &saved[sel].wname, &saved[sel].longit, &saved[sel].latit);
                }
                break;
            case 'D': case 'd':
                if (saved_count <= 0) {
                    chgcolor(ERROR_COLOR);
                    printf("No point to delete!");
                    waitesc();
                    break;
                }
                printf("Input point to delete: ");
                sel = select_point(0,0,1);
                if (sel >= 0) {
                    for (int i = sel; i < saved_count; i++) {
                        strcpy(saved[i].wname, saved[i+1].wname);
                        saved[i].longit = saved[i+1].longit;
                        saved[i].latit = saved[i+1].latit;
                    }
                    saved_count--;
                }
                break;
            case 'Q': case 'q':
                return;
                break;
        }
    }
}

void refresh_td() {
    double total_dis = 0.0;
    if (request_dis < 0.0) return;
    for (int i = wcount - 2; i >= 0; i--) {
        double cur_dis = distance_to(wpts[i], wpts[i+1], transon);
        total_dis += cur_dis;
        if (total_dis >= request_dis) {
            tdat_id = i;
            tdat_after = total_dis - request_dis;   // Something gotten more
            return;
        }
    }
    tdat_id = -1;   // No available data
}

int main(int argc, char* argv[]) {
    mpi = 3*acos(0.5);
    deg180 = mpi;
    deg90 = mpi/2;

    // Preload saved waypoints

    printf("Preloading waypoints ...");
    FILE *fs = fopen(SAVEDS,"r");
    if (fs != NULL) {
        while (!feof(fs)) {
            fscanf(fs, "%s%lf%lf", &saved[saved_count].wname, &saved[saved_count].longit, &saved[saved_count].latit);
            saved_count++;
        }
        saved_count--;
        fclose(fs);
    }

    system("cls");
    chgcolor(NORMAL_COLOR);
    // Load
    int pageid = 0;
    do {
        system("cls");
        chgcolor(BANNER_COLOR);
        printf("SeaNVU For Tu154 ");
        chgcolor(METHOD_COLOR);
        printf("| Z/X Scroll | R:Plan | B:Saves | E:");
        if (tdat_id >= 0) {
            if (progress > tdat_id) {
                chgcolor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
            } else if (progress == tdat_id) {
                chgcolor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
            } else {
                chgcolor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
            }
        }
        else chgcolor(FOREGROUND_RED | FOREGROUND_INTENSITY);
        printf("VNAV");
        chgcolor(METHOD_COLOR);
        printf(" | [U]nit: %s\n",nmode ? "NM" : "KM");
        chgcolor(NORMAL_COLOR);
        printf("=========================================\n");
        chgcolor(ALPHA_COLOR);
        printf("%3d Waypoints | %3d Go past | Page %2d / %2d\n\n",wcount,progress,pageid+1,getpc() > 0 ? getpc() : 1);
        chgcolor(NORMAL_COLOR);
        int pstart = pageid * pagecount + progress;
        int i;
        for (i = pstart; i < wcount && i < pstart+pagecount; i++) {
            if (i == progress) chgcolor(ACTIVE_COLOR);
            else if (i == tdat_id) chgcolor(VNAV_COLOR);
            else chgcolor(NORMAL_COLOR);
            points_print(i, wpts[i]);
        }
        chgcolor(NORMAL_COLOR);
        printf("========================================");
        chgcolor(ALPHA_COLOR);
        if (hasalpha) printf(" <Alpha: %.2lf>", gotalpha);
        chgcolor(METHOD_COLOR);
        printf("\n[F]orecast [N]av [P]ush P[o]p [A]dd [I]ns [D]el [M]od [S]ave [L]oad/[V] Alp[h]a");
        char ch = getch();
        clear_line();
        double mag1, mag2, dis, total_dis, de, dise, calt, cspd, reqt;
        int sel1, sel2;
        FILE *f;
        switch (ch) {
            case 'E': case 'e':
                printf("Input current speed: ");
                scanf("%lf",&cspd);
                if (cspd < 0) {
                    request_dis = -1.0;
                    break;
                }
                printf("Input current altitude: ");
                scanf("%lf",&calt);
                if (calt < 0) {
                    request_dis = -1.0;
                    break;
                }
                printf("Input current V/S (Per minute): ");
                scanf("%lf",&dise);
                if (dise <= 0) {
                    request_dis = -1.0;
                    break;
                }
                // s = vt, t = s/v.
                reqt = calt / dise / 60.0;// Unit: hour (60 divided)
                request_dis = reqt * cspd;
                refresh_td();
                break;
            case 'R': case 'r':
                printf("Input acceptable distance: ");
                scanf("%lf", &CONT_REQ_DIS);
                clear_line();
                printf("Input departure: ");
//              sel1 = select_point(0,0,1);
                // This can only be inside.
                // Confirm
                sel1 = select_point(0,0,1);
                if (sel1 < 0) break;
                printf("\nInput arrival: ");
                sel2 = select_point(0,0,1);
                if (sel2 < 0) break;
//              printf("Select arrival from %s : \n", saved[sel1].wname);
//              sel2 = select_point(0,0,1);
                dis = distance_to(saved[sel1], saved[sel2], transon);
                printf("Input ranged distance (Direct flying = %.2lf): ", dis);
                scanf("%lf", &GIVE_UP_DIS);
                prepare_route(sel1, sel2);
                refresh_td();
                break;
            case 'B': case 'b':
                saves_module();
                break;
            case 'F': case 'f':
                // Show forecast from the top point to the second top
                if (wcount - progress < 2) {
                    chgcolor(ERROR_COLOR);
                    printf("No next point!");
                } else {
                    chgcolor(ALPHA_COLOR);
                    double dis = distance_to(wpts[progress], wpts[progress+1], transon);
                    double degs = angle_to(wpts[progress], wpts[progress+1], dis);
                    printf("Distance = %.2lf, Angle = %.1lf", dis, degs);
                    degree_trans_out(degs);
                }
                waitesc();
                break;
            case 'P': case 'p':
                if (progress > 0) {
                    progress--;
                } else {
                    chgcolor(ERROR_COLOR);
                    printf("No point to push!");
                    waitesc();
                }
                break;
            case 'O': case 'o':
                if (progress >= wcount) {
                    chgcolor(ERROR_COLOR);
                    printf("No point to pop!");
                    waitesc();
                } else {
                    // Indicate where to pop to
                    int ptarget = select_point(progress + 1, 1);
                    if (ptarget >= 0) progress = ptarget;
                }
                break;
            case 'A': case 'a':
                if (wcount >= WPTSIZE) {
                    chgcolor(ERROR_COLOR);
                    printf("Too many waypoints!");
                    waitesc();
                } else {
                    //printf("Input waypoint name, LON and LAT in order: ");
                    //scanf("%s%lf%lf",&wpts[wcount].wname,&wpts[wcount].longit,&wpts[wcount].latit);
                    if (put_point(wcount)) {
                        wcount++;
                        refresh_td();
                    }
                }
                break;
            case 'I': case 'i': case 'M': case 'm':
                if (wcount >= WPTSIZE) {
                    chgcolor(ERROR_COLOR);
                    printf("Too many waypoints!");
                    waitesc();
                } else {
                    int rel = 0;
                    if (ch == 'I' || ch == 'i') rel = 1;
                    int ptarget = select_point(-1, rel);
                    if (ptarget >= 0) {
                        // Emplace
                        if (ch == 'I' || ch == 'i') {
                            for (i = wcount; i > ptarget; i--) {
                                strcpy(wpts[i].wname,wpts[i-1].wname);
                                wpts[i].longit=wpts[i-1].longit;
                                wpts[i].latit=wpts[i-1].latit;
                            }
                        }
                        clear_line();
                        int res = put_point(ptarget, (ch == 'M' || ch == 'm'));
                        //printf("Input waypoint name, LON and LAT in order: ");
                        //scanf("%s%lf%lf",&wpts[ptarget].wname,&wpts[ptarget].longit,&wpts[ptarget].latit);
                        if (res) {
                            if (ch == 'I' || ch == 'i') wcount++;
                            refresh_td();
                        }
                    }
                }
                break;
            case 'D': case 'd':
                if (progress >= wcount) {
                    chgcolor(ERROR_COLOR);
                    printf("No point to delete!");
                    waitesc();
                } else {
                    int sel = select_point();
                    if (sel < 0) break;
                    for (i = sel; i < wcount; i++) {
                        strcpy(wpts[i].wname,wpts[i+1].wname);
                        wpts[i].longit=wpts[i+1].longit;
                        wpts[i].latit=wpts[i+1].latit;
                    }
                    wcount--;
                    refresh_td();
                }
                break;
            case 'S': case 's':
                printf("Input company route name: ");
                char crt[8];
                scanf("%s",crt);
                char rpath[64];
                sprintf(rpath, "COROUTE\\%s", crt);
                f = fopen(rpath, "w");
                fprintf(f,"%d\n",wcount);
                for (i = 0; i < wcount; i++) {
                    fprintf(f,"%s %lf %lf\n",wpts[i].wname,wpts[i].longit,wpts[i].latit);
                }
                fclose(f);
                break;
            case 'V': case 'v':
                system("dir /w COROUTE\\");
                // Passthrough on purpose !
            case 'L': case 'l':
                printf("Input company route name: ");
                //char crt[8];
                scanf("%s",crt);
                //char rpath[64];
                sprintf(rpath, "COROUTE\\%s", crt);
                f = fopen(rpath, "r");
                if (f == NULL) {
                    clear_line();
                    chgcolor(ERROR_COLOR);
                    printf("ERROR: CAN'T READ!");
                    waitesc();
                } else {
                    progress = 0;
                    fscanf(f, "%d", &wcount);
                    for (i = 0; i < wcount; i++) {
                        fscanf(f, "%s%lf%lf",wpts[i].wname,&wpts[i].longit,&wpts[i].latit);
                    }
                    fclose(f);
                    refresh_td();
                }
                break;
            case 'H': case 'h':
                clear_line();
                printf("Input Mag of the first point: ");
                scanf("%lf",&mag1);
                clear_line();
                printf("Input Mag of the final point: ");
                scanf("%lf",&mag2);
                clear_line();
                gotalpha = alpha_to(wpts[0], wpts[wcount - 1], mag1, mag2);
                hasalpha = 1;
                chgcolor(ALPHA_COLOR);
                printf("Alpha = %.3lf\n", gotalpha);
                waitesc();
                break;
            case 'Q': case 'q':
                sync_saved();
                return 0;
            case 'Z': case 'z':
                if (pageid > 0) pageid--;
                break;
            case 'X': case 'x':
                if (pageid + 1 < getpc()) pageid++;
                break;
            case 'U': case 'u':
                nmode = !nmode;
                if (nmode) measure = 1.852;
                else measure = 1.0;
                break;
            case 'T': case 't':
                transon = !transon;
                break;
            case 'N': case 'n':
                //int transform = 0;
                //normal_load:
                printf("Point 1: \n");
                sel1 = select_point();
                if (sel1 + 1 >= wcount || sel1 < 0) break;
                printf("\nPoint 2 from %s: \n", wpts[sel1].wname);
                sel2 = select_point(sel1 + 1);
                if (sel2 < 0) break;
                dis = distance_to(wpts[sel1], wpts[sel2], transon);
                total_dis = 0.0;
                for (i = sel1; i < sel2; i++) {
                    total_dis += distance_to(wpts[i], wpts[i+1], transon);
                }
                clear_line();
                de = angle_to(wpts[sel1], wpts[sel2], dis, transon);
                chgcolor(ALPHA_COLOR);
                printf("Route from %s to %s:\n", wpts[sel1].wname, wpts[sel2].wname);
                printf("Direct Dis = %.2lf, Total Dis = %.2lf, Angle = %.1lf", dis, total_dis, de);
                degree_trans_out(de);
                waitesc();
                break;
            default:
                break;

        }
    } while (1);
    return 0;
}

SAVES.WPT 内容如下:

ABC 7.285103 9.037803
ABC 7.285103 9.037803
ABI 43.154819 11.549211
ABI 43.154819 11.549211
ABU 13.169508 32.462833
ABU 13.169508 32.462833
ABV 16.533833 -28.570653
ABV 16.533833 -28.570653
ACC -0.155364 5.634069
ACC -0.155364 5.634069
AD -3.916978 5.282817
AD -3.916978 5.282817