#include #include #include #include #include "base28.h" using namespace std; struct binaryplace { char name[22]; char state[2]; int unique, population; short int intersection; float area, latitude, longitude, distance; }; void trim(string & s) { int start = 0, stop = s.length() - 1; while (start <= stop && s[start] == ' ') start += 1; while (stop >= start && s[stop] == ' ') stop -= 1; if (start > stop) s = ""; else s = s.substr(start, stop - start + 1); } void find(istream & in, int endpos, string wantedname) { binaryplace bp; set_string_length(wantedname, 35); big w(wantedname); int begin = 0, end = endpos / sizeof(bp) - 1; while (true) { if (begin > end) { cout << "not found\n"; break; } int mid = (begin + end) / 2; in.seekg(mid * sizeof(bp), ios::beg); in.read((char *) & bp, sizeof(bp)); big b((char *)bp.name, 22); int order = b.compare(w); cout << setw(6) << mid << ": seeing '" << b.to_string() << "', wanted '" << wantedname << "'\n"; if (order == 0) { while (mid > 0) { mid -= 1; in.seekg(mid * sizeof(bp), ios::beg); in.read((char *) & bp, sizeof(bp)); big b((char *)bp.name, 22); int order = b.compare(w); if (order != 0) { mid += 1; break; } } while (true) { in.seekg(mid * sizeof(bp), ios::beg); in.read((char *) & bp, sizeof(bp)); big b((char *)bp.name, 22); int order = b.compare(w); if (order == 0) { string s = b.to_string(); trim(s); cout << bp.state[0] << bp.state[1] << " " << s << "\n"; mid += 1; } else break; } break; } else if (order < 0) begin = mid + 1; else end = mid - 1; } } void find(istream & in, int endpos, string wantedname, string wantedstate) { binaryplace bp; set_string_length(wantedname, 35); big w(wantedname); int begin = 0, end = endpos / sizeof(bp) - 1; while (true) { if (begin > end) { cout << "not found\n"; break; } int mid = (begin + end) / 2; in.seekg(mid * sizeof(bp), ios::beg); in.read((char *) & bp, sizeof(bp)); big b((char *)bp.name, 22); string thisstate; thisstate += bp.state[0]; thisstate += bp.state[1]; int order = b.compare(w); cout << setw(6) << mid << ": seeing '" << thisstate << "' '" << b.to_string() << "', wanted '" << wantedstate << "' '" << wantedname << "'\n"; if (order == 0 && thisstate == wantedstate) { string s = b.to_string(); trim(s); cout << "found it\n"; cout << s << ", " << thisstate << ", population = " << bp.population << ", area = " << bp.area << "\n"; cout << " latitude = " << bp.latitude << ", longitude = " << bp.longitude << ", intersection = " << bp.intersection << ", distance = " << bp.distance << "\n\n"; break; } if (order == 0) { if (thisstate < wantedstate) begin = mid + 1; else end = mid - 1; } else if (order < 0) begin = mid + 1; else end = mid - 1; } } int main() { ifstream in("binaryplaces.dat", ios::in | ios::binary); if (in.fail()) { cerr << "cant open\n"; exit(1); } in.seekg(0, ios::end); int endpos = in.tellg(); while (true) { string wantedname, wantedstate, line; cout << "> "; getline(cin, line); if (cin.fail()) { cout << "\n"; break; } trim(line); cout << "begin '" << line[0] << "'\n"; if (line[0] == 'N') { wantedname = line.substr(1); trim(wantedname); find(in, endpos, wantedname); } else if (line[0] == 'S') { wantedname = line.substr(1); trim(wantedname); int end = wantedname.length() - 1; while (end >= 0 && wantedname[end] != ' ') end -= 1; if (end < 0) { cout << "bad\n"; continue; } wantedstate = wantedname.substr(end + 1); wantedname = wantedname.substr(0, end); trim(wantedname); find(in, endpos, wantedname, wantedstate); } else cout << "bad\b"; } in.close(); }