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
|
#include <iostream>
using namespace std;
struct HighwayTable {
// If the length matches, and the character at position is value...
unsigned length;
unsigned position;
char value;
// .. then
bool isPrimary; // primary or auxilliary?
bool north_south; // runs north/south or east/west?
unsigned auxPos, auxLen; // if aux, then pos and len of substring for main highway
};
int main(int argc, char **argv) {
HighwayTable table[] = {
// len pos value primary? N/S auxPos auxLen
{1, 0, '5', true, true, 0, 0 },
{3, 1, '0', false, true, 2, 1 },
{2, 1, '5', true, true, 0, 0 },
{2, 1, '0', true, false, 0, 0 },
{3, 2, '5', false, true, 1, 2 },
{3, 2, '0', false, false, 1, 2 }
};
string highway;
while (cin >> highway) {
bool found = false;
for (HighwayTable &t : table) {
if (highway.length() == t.length && highway[t.position] == t.value) {
cout << "I-" << highway
<< " is " << (t.isPrimary ? "primary" : "auxiliary")
<< ", ";
if (!t.isPrimary) {
cout << "serving I-" << highway.substr(t.auxPos, t.auxLen)
<< ", ";
}
cout << "going " << (t.north_south ? "north/south" : "east/west") << ".\n";
found = true;
break;
}
}
if (!found) {
cout << highway << " is not a valid interstate highway number.\n";
}
}
return 0;
}
|