#include <iostream> #include <fstream> #include <string> using namespace std; struct item { string descr; int price; item(string d, int p) { descr = d; price = p; } void print() { cout << descr << ", $" << price / 100 << "." << price / 10 % 10 << price % 10 << "\n"; } }; struct link { item * thing; link * next; link(item * t, link * n = NULL) { thing = t; next = n; } }; class list { protected: link * all; public: list() { all = NULL; } void add(item * it) { all = new link(it, all); } void print() { link * ptr = all; while (ptr != NULL) { ptr->thing->print(); ptr = ptr->next; } } item * find(string s) { link * ptr = all; while (ptr != NULL) { if (ptr->thing->descr == s) return ptr->thing; ptr = ptr->next; } return NULL; } item * nth(int n) { link * ptr = all; for (int i = 0; ptr != NULL && i < n; i += 1, ptr = ptr->next) { } if (ptr == NULL) return NULL; return ptr->thing; } void reverse() { link * ptr = all; link * rev = NULL; while (ptr != NULL) { link * next = ptr->next; ptr->next = rev; void apply(void f(item *)) { link * ptr = all; while (ptr != NULL) { f(ptr->thing); ptr = ptr->next; } } }; void upprice(item * x) { x->price = (int)(x->price * 1.10 + 0.5); } int main() { list all; ifstream in("data"); if (in.fail()) { cerr << "Can't open data\n"; exit(1); } while (true) { string des; in >> des; if (des == "*") break; int prc; in >> prc; all.add(new item(des, prc)); } in.close(); while (true) { string cmd; cout << "> "; cin >> cmd; if (cmd == "print") all.print(); else if (cmd == "up") all.apply(upprice); else if (cmd == "find") { string s; cin >> s; item * i = all.find(s); if (i == NULL) cout << "not found\n"; else i->print(); } else if (cmd == "nth") { int n; cin >> n; item * i = all.nth(n); if (i == NULL) cout << "not found\n"; else i->print(); } else if (cmd == "rev") all.reverse(); else cout << "error\n"; } }