1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#define SMALL 1e-20
#define IsSmall(A) ((A<SMALL) && (A>-SMALL))
#define IsBetween(A,B,C) (((A-B)>-SMALL) && ((A-C)<SMALL))
using namespace std;
int main(void)
{
ifstream compass;
compass.open("compass.txt");
if (!compass)
{
cout << "File not found!\n";
}
ofstream bearing;
bearing.open("bearing.txt");
double Aorig=0, Amod=0, Bear=0;
while (compass >> Aorig)
{
if (IsBetween(Aorig,0,360))
Amod=Aorig;
else if (Aorig<0)
Amod=Aorig-fmod(-Aorig, 360);
else
Amod=fmod(Aorig,360);
//Determines equivalent angle between 0 and 360 degrees
if (IsSmall(Amod-0))
bearing << "N\n";
else if (IsSmall(Amod-90))
bearing << "E\n";
else if (IsSmall(Amod-180))
bearing << "S\n";
else if (IsSmall(Amod-270))
bearing << "W\n";
else if (IsSmall(Amod-360))
bearing << "N\n";
//if angle is equal to 0,90,180,270, or 360, outputs N,S,E,W,or N
if (0<Amod<90)
bearing <<"N"<<Amod<<"E\n";
else if (90<Amod<180)
bearing <<"S"<<180-Amod<<"E\n";
else if(180<Amod<270)
bearing <<"S"<<Amod-180<<"W\n";
else if(270<Amod<360)
bearing <<"N"<<360-Amod<<"W\n";
//Output angle as a bearing starting with N/S then E/W bearing
}
}
|