Box the compass: Difference between revisions

Content added Content deleted
(Added Prolog)
(Added C++)
Line 21: Line 21:
index = ( i mod 32) + 1</pre>
index = ( i mod 32) + 1</pre>


=={{header|C++}}==
Using the Boost libraries
{{libheader|Boost}}
<lang C++>#include <map>;
#include <string>;
#include <vector>
#include <boost/assign/list_inserter.hpp>
#include <boost/assign/std/vector.hpp>
#include <boost/format.hpp>
#include <boost/foreach.hpp>
#include <iostream>;
#include <math.h>

using std::string;
using namespace boost::assign;
using boost::format;

int get_Index(float angle)
{
return static_cast<int>(floor(angle / 11.25 +0.5 )) % 32 + 1;
}

string get_Abbr_From_Index(int i)
{
static bool first_time(true);
static std::map<int, string> points;
if (first_time){
first_time = false;
insert (points)
( 1, "N") ( 2, "NbE") ( 3, "NNE") ( 4, "NEbN")
( 5, "NE") ( 6, "NEbE") ( 7, "ENE") ( 8, "EbN")
( 9, "E") (10, "EbS") (11, "ESE") (12, "SEbE")
(13, "SE") (14, "SEbS") (15, "SSE") (16, "SbE")
(17, "S") (18, "SbW") (19, "SSW") (20, "SWbS")
(21, "SW") (22, "SWbW") (23, "WSW") (24, "WbS")
(25, "W") (26, "WbN") (27, "WNW") (28, "NWbW")
(29, "NW") (30, "NWbN") (31, "NNW") (32, "NbW");
}
return points[i];
}

string Build_Name_From_Abbreviation(string a)
{
string retval;

for (int i = 0; i < a.size(); ++i){
if ((1 == i) && (a[i] != 'b') && (a.size() == 3)) retval += "-";
switch (a[i]){
case 'N' : retval += "north"; break;
case 'S' : retval += "south"; break;
case 'E' : retval += "east"; break;
case 'W' : retval += "west"; break;
case 'b' : retval += " by "; break;
}
}
retval[0] = toupper(retval[0]);
return retval;
}

int main()
{
std::vector<float> headings;
headings += 0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5, 84.37, 84.38, 101.25,
118.12, 118.13, 135.0, 151.87, 151.88, 168.75, 185.62, 185.63, 202.5,
219.37, 219.38, 236.25, 253.12, 253.13, 270.0, 286.87, 286.88, 303.75,
320.62, 320.63, 337.5, 354.37, 354.38;
int i;
format f("%1$4d %2$-25s %3%");

BOOST_FOREACH(float a, headings)
{
i = get_Index(a);
std::cout << f % i % Build_Name_From_Abbreviation(get_Abbr_From_Index(i)) % a << std::endl;
}

return 0;
}</lang>
Output:
<pre>
1 North 0
2 North by east 16.87
3 North-northeast 16.88
4 Northeast by north 33.75
5 Northeast 50.62
6 Northeast by east 50.63
7 East-northeast 67.5
8 East by north 84.37
9 East 84.38
10 East by south 101.25
11 East-southeast 118.12
12 Southeast by east 118.13
13 Southeast 135
14 Southeast by south 151.87
15 South-southeast 151.88
16 South by east 168.75
17 South 185.62
18 South by west 185.63
19 South-southwest 202.5
20 Southwest by south 219.37
21 Southwest 219.38
22 Southwest by west 236.25
23 West-southwest 253.12
24 West by south 253.13
25 West 270
26 West by north 286.87
27 West-northwest 286.88
28 Northwest by west 303.75
29 Northwest 320.62
30 Northwest by north 320.63
31 North-northwest 337.5
32 North by west 354.37
1 North 354.38
</pre>
=={{header|Clojure}}==
=={{header|Clojure}}==
{{trans|Tcl}}
{{trans|Tcl}}