#include #include #include using namespace std; struct inputsystem { string line; int pos; }; void init(inputsystem & is) { getline(cin, is.line); is.pos = 0; } char readch(inputsystem & is) { while (true) { if (is.pos >= is.line.length()) { is.pos += 1; return '\n'; } if (is.line[is.pos] != ' ') break; is.pos += 1; } is.pos += 1; return is.line[is.pos - 1]; } void unreadch(inputsystem & is) { is.pos -= 1; } string read(inputsystem & is) { char c = readch(is); if (c == '\n') return "\n"; string symbol = ""; symbol += c; c = readch(is); if (c >= 'a' && c <= 'z') symbol += c; else unreadch(is); return symbol; } struct element { int atno; string symbol; string name; double atwt; }; int readfile(element A[], int max) { ifstream f("/home/www/class/een118/a3010b.txt"); if (f.fail()) { cout << "Can't read the data file\n"; exit(1); } int pos = 1; while (true) { if (pos >= max) { cout << "The array isn't big enough\n"; exit(1); } f >> A[pos].symbol >> A[pos].name >> A[pos].atwt; if (f.fail()) break; A[pos].atno = pos; pos += 1; } f.close(); A[0].atno = 0; A[0].symbol = "Error"; A[0].name = "Error"; A[0].atwt = 0.0; return pos - 1; } void printtable(element A[], int N) { for (int i = 1; i <= N; i += 1) cout << i << ": " << A[i].atno << " " << A[i].symbol << " " << A[i].name << " " << A[i].atwt << "\n"; } element find(string wanted, element A[], int num) { for (int i = 1; i <= num; i += 1) if (A[i].symbol == wanted) return A[i]; return A[0]; } int main() { const int arraysize = 200; element table[arraysize]; int number = 0; number = readfile(table, arraysize); while (true) { cout << "? "; inputsystem I; init(I); while (true) { string sym = read(I); if (sym == "\n") break; element r = find(sym, table, number); if (r.atno == 0) cout << "Element " << sym << " is unknown\n"; else cout << r.atno << " " << r.symbol << " " << r.name << " " << r.atwt << "\n"; } } }