Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <iomanip>
- #include <cstdlib>
- #include <sstream>
- using namespace std;
- class CSales {
- public:
- CSales () {}
- CSales (string i, int p, int c) {
- this->m_isbn = i;
- this->m_price = p;
- this->m_count = c;
- this->m_revenue = p*c;
- }
- friend ostream & operator << ( ostream &os, const CSales &x ); //const;
- friend istream & operator >> ( istream &is, const CSales &x );
- string m_isbn;
- int m_price;
- int m_count;
- int m_revenue;
- };
- ostream & operator << ( ostream &os, const CSales &x ) {
- os << "isbn: " << x.m_isbn << endl;
- os << "price: " << x.m_price << endl;
- os << "sold: " << x.m_count << "x." << endl;
- os << "____________" << endl;
- return os;
- }
- istream & operator >> ( istream &is, const CSales &x ) {
- if ( is >> x.m_isbn >> x.m_price >> x.m_count ) {
- x.m_revenue = x.m_count*x.m_price;
- }
- return is;
- }
- bool cmp_isbn(const CSales *a, const CSales *b) {
- return (a->m_isbn < b->m_isbn);
- }
- int main() {
- vector<CSales*> dbs1;
- CSales *candidate = new CSales();
- cout << "start entering transactions (isbn, price, quantity):" << endl;
- while ( cin >> *candidate ) {
- //sorted by isbn
- auto iter = lower_bound(dbs1.begin(), dbs1.end(), candidate, cmp_isbn);
- //found book in database
- if ( iter != dbs1.end() && (*iter)->m_isbn == candidate->m_isbn ) {
- (*iter)->m_count += candidate->m_count;
- (*iter)->m_revenue += candidate->m_count * candidate->m_price;
- } else {
- //add book to database
- dbs1.insert(iter, candidate);
- }
- }
- for (auto it : dbs1) {
- cout << it;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement