#include #include #include 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; } ~list() { while (all != NULL) { link * next = all->next; delete all; all = next; } } void destroy_all() { while (all != NULL) { link * next = all->next; delete all->thing; delete all; all = next; } } 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; rev = ptr; ptr = next; } all = rev; } void apply(void f(item *)) { link * ptr = all; while (ptr != NULL) { f(ptr->thing); ptr = ptr->next; } } int int_accumulate(int operation(item *, int), int init) { int value = init; link * ptr = all; while (ptr != NULL) { value = operation(ptr->thing, value); ptr = ptr->next; } return value; } link * link_accumulate(link * operation(item *, link *), link * init) { link * value = init; link * ptr = all; while (ptr != NULL) { value = operation(ptr->thing, value); ptr = ptr->next; } return value; } template T accumulate(T operation(item *, T), T init) { T value = init; link * ptr = all; while (ptr != NULL) { value = operation(ptr->thing, value); ptr = ptr->next; } return value; } }; void upprice(item * x) { x->price = (int)(x->price * 1.10 + 0.5); } int addprice(item * x, int already) { return already + x->price; } link * connect(item * x, link * already) { return new link(x, already); } 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 (cin.fail()) { cout << "\n"; break; } 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 if (cmd == "add") { int t = all.int_accumulate(addprice, 0); cout << "the total price is $" << t / 100 << "." << t / 10 % 10 << t % 10 << "\n"; } else if (cmd == "connect") { link * ptr = all.link_accumulate(connect, NULL); while (ptr != NULL) { ptr->thing->print(); ptr = ptr->next; } } else if (cmd == "both") { int t = all.accumulate(addprice, 0); cout << "the total price is $" << t / 100 << "." << t / 10 % 10 << t % 10 << "\n"; link * ptr = all.accumulate(connect, NULL); while (ptr != NULL) { ptr->thing->print(); ptr = ptr->next; } } else cout << "error\n"; } }