#include #include #include using namespace std; struct input_object { string line; int pos; int len; void readline() { getline(cin, line); pos = 0; len = line.length(); } string nextsymbol() { char c1, c2; c1 = line[pos]; pos += 1; string s = ""; s += c1; if (pos >= len) return s; c2 = line[pos]; if (c2 >= 'a' && c2 <= 'z') { pos += 1; return s + c2; } return s; } bool anyleft() { return pos < len; } }; string trim(string s) { int left = 0, len = s.length(); while (left < len && s[left] == ' ') left += 1; if (left == len) return ""; int right = len-1; while (right >= 0 && s[right] == ' ') right -= 1; return s.substr(left, right-left+1); } int scan_file(ifstream & fin, int positions[]) { int count = 0; while (true) { int pos = fin.tellg(); string line; getline(fin, line); if (fin.fail()) break; count += 1; positions[count] = pos; } cout << count << " lines read\n"; fin.clear(); return count; } struct element { string symbol, name; int atno; double atwt; }; element find_element(string sym, int starts[], int N, ifstream & fin) { for (int atno = 1; atno <= N; atno += 1) { fin.seekg(starts[atno]); string line; getline(fin, line); string symbol, name, atwt; symbol = trim(line.substr(0, 2)); if (symbol == sym) { name = trim(line.substr(2, 13)); atwt = trim(line.substr(15)); element res; res.symbol = sym; res.name = name; res.atno = atno; res.atwt = atof(atwt.c_str()); return res; } } } void main() { int starts[110]; ifstream fin; fin.open("data.txt"); if (fin.fail()) { cout << "Couldn't open file\n"; exit(1); } int count = scan_file(fin, starts); input_object io; io.readline(); while (io.anyleft()) { string sy = io.nextsymbol(); cout << "symbol '" << sy << "'\n"; element e = find_element(sy, starts, count, fin); cout << e.atno << ": '" << e.symbol << "', '" << e.name << "', '" << e.atwt << "'\n"; } fin.close(); }