01: #include <iostream>
02: #include <string>
03: #include <vector>
05: using namespace std;
06:
07: class Product {
08: public:
09: Product();
10: void read();
11: bool is_better_than(Product b) const;
12: void print() const;
13: private:
14: string name;
15: double price;
16: int score;
17: };
18:
19: Product::Product()
20: { price = 0;
21: score = 0;
22: }
23:
24: void Product::read()
25: { cout << "Please enter the model name: ";
26: getline(cin, name);
27: cout << "Please enter the price: ";
28: cin >> price;
29: cout << "Please enter the score: ";
30: cin >> score;
31: string remainder; /* read remainder of line */
32: getline(cin, remainder);
33: }
34:
35: bool Product::is_better_than(Product b) const
36: { if (price == 0) return false;
37: if (b.price == 0) return true;
38: return score / price > b.score / b.price;
39: }
40:
41: void Product::print() const
42: { cout << name
43: << " Price: " << price
44: << " Score: " << score << "\n";
45: }
46:
47: int main()
48: { vector<Product> products;
49: Product best_product;
50: int best_index = -1;
51:
52: bool more = true;
53: while (more)
54: { Product next_product;
55: next_product.read();
56: products.push_back(next_product);
57:
58: if (next_product.is_better_than(best_product))
59: { best_index = products.size() - 1;
60: best_product = next_product;
61: }
62:
63: cout << "More data? (y/n) ";
64: string answer;
65: getline(cin, answer);
66: if (answer != "y") more = false;
67: }
68:
69: for (int i = 0; i < products.size(); i++)
70: { if (i == best_index) cout << "best value => ";
71: products[i].print();
72: }
73: return 0;
74: }