vt-cs-projects / c02_parsing_coor / c02.c
c02.c
Raw
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

#define MAX_MINUTES 60
#define MAX_SECONDS 3600
#define DASHES 100
#define MAX_LENGTH 10

int main(int args, char *files[])
{
    // Reads in input file and create output file
    FILE *readF = fopen(files[1], "r");
    FILE *outF = fopen(files[2], "w");

    // Checks for input file
    if (readF == NULL)
    {
        printf("Error:  unable to create %s\n", files[1]);
        return 1;
    }

    // Char arrays to hold various portions of lines in text files
    char str1[MAX_LENGTH], str2[MAX_LENGTH], str3[MAX_LENGTH], str4[MAX_LENGTH], str5[MAX_LENGTH], str6[MAX_LENGTH];
    char str7[MAX_LENGTH], str8[MAX_LENGTH], str9[MAX_LENGTH], str10[MAX_LENGTH], str11[MAX_LENGTH], str12[MAX_LENGTH];
    char str13[MAX_LENGTH], str14[MAX_LENGTH], str15[MAX_LENGTH], str16[MAX_LENGTH];

    // Floats to hold decimal degrees
    float ddlong1, ddlang1, ddlong2, ddlang2;

    // Prints tiles
    int i;
    fprintf(outF, "\t\t\tFirst Coordinate\t\t\t\t\t\tSecond Coordinate\t\t\t\t\tDist Sec\t\t\tDMS\n");
    for (i = 0; i < DASHES; i++)
    {
        fprintf(outF, "-");
    }
    fprintf(outF, "\n");

    // Scans desired values from input document from each line
    while (fscanf(readF, "(%3s%2s%2s%s%2s%2s%2s%s (%3s%2s%2s%s%2s%2s%2s%s\n", str1, str2, str3, str4, str5, str6, str7, str8, str9, str10, str11, str12, str13, str14, str15, str16) > 0)
    {
        // Transforms the needed chars from input flie into floating points for computation
        ddlong1 = (atof(str1) + (atof(str2) / MAX_MINUTES) + (atof(str3) / MAX_SECONDS));
        ddlang1 = (atof(str5) + (atof(str6) / MAX_MINUTES) + (atof(str7) / MAX_SECONDS));
        ddlong2 = (atof(str9) + (atof(str10) / MAX_MINUTES) + (atof(str11) / MAX_SECONDS));
        ddlang2 = (atof(str13) + (atof(str14) / MAX_MINUTES) + (atof(str15) / MAX_SECONDS));

        // Checks if East or South
        if (str4[0] == 'E')
        {
            ddlong1 = ddlong1 * (-1.0f);
        }
        if (str12[0] == 'E')
        {
            ddlong2 = ddlong2 * (-1.0f);
        }
        if (str8[0] == 'S')
        {
            ddlang1 = ddlang1 * (-1.0f);
        }
        if (str16[0] == 'S')
        {
            ddlang2 = ddlang2 * (-1.0f);
        }

        // Taxi Cab Metric Function
        double taxi = fabs((double)(ddlong1 - ddlong2)) + fabs((double)(ddlang1 - ddlang2));

        // Transforms output of taxi function to DMS
        int d = (int)taxi;
        int m = (int)(taxi * MAX_MINUTES);
        float sec = round((float)((taxi * MAX_MINUTES) - m) * MAX_MINUTES);

        // Distance Seconds Calculation
        int ds = (m * MAX_MINUTES) + sec;

        // Prints output to output file
        fprintf(outF, "(%sd %sm %ss %s %sd %sm %ss %s  (%sd %sm %ss %s %sd %sm %ss %s\t\t\t%4d\t\t%dd %02dm %02ds\n",
                str1, str2, str3, str4, str5, str6, str7, str8, str9, str10, str11, str12, str13, str14, str15, str16, ds, d, m, (int)sec);
    }

    fclose(readF);
    fclose(outF);

    return 0;
}