Last active
December 16, 2017 14:34
-
-
Save DragonOsman/55fdfe564195be3661992391c859ef40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Item.h" | |
#include <string> | |
#include <cctype> | |
#include <iostream> | |
#include <algorithm> | |
#include "../../cust_std_lib_facilities.h" | |
Item::Item(const std::string &name, const int iid, const double value) | |
: m_name{ name }, m_iid{ iid }, m_value{ value } | |
{ | |
if (iid < 0) | |
{ | |
throw std::runtime_error{ "Invalid id" }; | |
} | |
if (value <= 0.0) | |
{ | |
throw std::runtime_error{ "Invalid price" }; | |
} | |
} | |
Item::Item(const Item &item) | |
{ | |
if (&item == this) | |
{ | |
std::invalid_argument{ "Line 24, Item::Item(const Item&): Can't do self-assignment" }; | |
} | |
else | |
{ | |
m_name = item.m_name; | |
m_iid = item.m_iid; | |
m_value = item.m_value; | |
} | |
} | |
Item::Item(Item &&item) | |
: m_name{}, m_iid{}, m_value{} | |
{ | |
if (&item != this) | |
{ | |
m_name = item.m_name; | |
m_iid = item.m_iid; | |
m_value = item.m_value; | |
item.m_name = ""; | |
item.m_iid = 0; | |
item.m_value = 0.0; | |
} | |
} | |
Item &Item::operator=(const Item &item) | |
{ | |
if (&item != this) | |
{ | |
m_name = item.m_name; | |
m_iid = item.m_iid; | |
m_value = item.m_value; | |
} | |
return *this; | |
} | |
Item &Item::operator=(const Item &&item) | |
{ | |
if (&item != this) | |
{ | |
m_name = item.m_name; | |
m_iid = item.m_iid; | |
m_value = item.m_value; | |
} | |
return *this; | |
} | |
std::istream &operator>>(std::istream &is, Item &item) | |
{ | |
std::string str; | |
std::getline(is, str, '\n'); | |
if (str.empty()) | |
{ | |
return is; | |
} | |
std::getline(is, str, ','); | |
std::string name = str; | |
item.m_name = name; | |
std::string id_num; | |
std::getline(is, id_num, ','); | |
id_num.erase(std::remove_if(id_num.begin(), id_num.end(), std::isspace)); | |
int iid = std::stoi(id_num); | |
item.m_iid = iid; | |
std::string value_num; | |
std::getline(is, value_num); | |
value_num.erase(std::remove_if(value_num.begin(), value_num.end(), std::isspace)); | |
double value = std::stod(value_num); | |
item.m_value = value; | |
return is; | |
} | |
std::ostream &operator<<(std::ostream &os, const Item &item) | |
{ | |
if (os) | |
{ | |
os << "{ "; | |
os << item.m_name << ", " << item.m_iid << ", " << item.m_value; | |
os << " }"; | |
return os; | |
} | |
os.clear(); | |
return os; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef _WIN32 | |
#pragma once | |
#endif | |
#ifndef ITEM_H | |
#define ITEM_H | |
#include <string> | |
#include <stdexcept> | |
struct Item | |
{ | |
Item(const std::string &name, const int iid, const double value); | |
Item() | |
: m_name{}, m_iid{}, m_value{} {} | |
Item(const Item &item); | |
Item(Item &&item); | |
~Item() {} | |
bool operator==(const Item &item) { return m_name == item.m_name && m_iid == item.m_iid && m_value == item.m_value; } | |
bool operator!=(const Item &item) { return !(*this == item); } | |
bool operator<(const Item &item) { return m_value < item.m_value; } | |
bool operator>(const Item &item) { return m_name > item.m_name; } | |
bool operator<=(const Item &item) { return m_name <= item.m_name; } | |
bool operator>=(const Item &item) { return m_name >= item.m_name; } | |
Item &operator=(const Item &item); | |
Item &operator=(const Item &&item); | |
std::string get_name() const { return m_name; } | |
int get_iid() const { return m_iid; } | |
double get_value() const { return m_value; } | |
void set_name(const std::string &name) { m_name = name; } | |
void set_iid(const int iid) { m_iid = iid; } | |
void set_value(const double value) { m_value = value; } | |
friend std::istream &operator>>(std::istream &is, Item &item); | |
friend std::ostream &operator<<(std::ostream &os, const Item &item); | |
private: | |
std::string m_name; | |
int m_iid; | |
double m_value; | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Osman Zakir | |
// 12 / 12 / 2017 | |
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition | |
// Chapter 21 Drill | |
// Drill Specifications: | |
/** | |
* 1. Define a struct Item { string name; int iid; double value; // more stuff here...};, make | |
* a vector<Item>, vi, and fill it with ten items from a file. | |
* 2. Sort vi by name. | |
* 3. Sort vi by iid . | |
* 4. Sort vi by value; print it in order of decreasing value(i.e., largest value | |
* first). | |
* 5. Insert Item("horse shoe", 99, 12.34) and Item("Canon S400", 9988, 499.95) . | |
* 6. Remove(erase) two Item s identified by name from vi . | |
* 7. Remove(erase) two Item s identified by iid from vi . | |
* 8. Repeat the exercise with a list<Item> rather than a vector<Item>. | |
*/ | |
#include <algorithm> | |
#include <iostream> | |
#include <fstream> | |
#include <vector> | |
#include <string> | |
#include "../../cust_std_lib_facilities.h" | |
#include "Item.h" | |
int main() | |
{ | |
std::vector<Item> vi; | |
std::string filename; | |
std::cin >> filename; | |
std::ifstream ifs{ filename }; | |
try | |
{ | |
if (ifs) | |
{ | |
for (Item item; (ifs >> item).good();) | |
{ | |
vi.push_back(item); | |
} | |
auto iter = vi.begin(); | |
iter = vi.insert(iter, Item{ "horse shoe", 99, 12.34 }); | |
++iter; | |
iter = vi.insert(iter, Item{ "Canon S400", 9988, 499.95 }); | |
std::sort(vi.begin(), vi.end()); | |
std::cout << "sorted and printed in reverse in terms of value:\n"; | |
for (auto r_iter = vi.rbegin(); r_iter != vi.rend(); ++r_iter) | |
{ | |
std::cout << *r_iter << '\n'; | |
} | |
std::cout << "After erasing two items identified by name from vi:\n"; | |
auto search1 = std::find_if(vi.begin(), vi.end(), [](const Item &item) { return item.get_name() == "chair"; }); | |
auto iter1 = vi.erase(search1); | |
auto search2 = std::find_if(vi.begin(), vi.end(), [](const Item &item) { return item.get_name() == "cake"; }); | |
auto iter2 = vi.erase(search2); | |
for (const auto &i : vi) | |
{ | |
std::cout << i << '\n'; | |
} | |
std::cout << "After erasing two items identified by iid from vi:\n"; | |
auto search3 = std::find_if(vi.begin(), vi.end(), [](const Item &item) { return item.get_iid() == 3; }); | |
auto iter3 = vi.erase(search3); | |
auto search4 = std::find_if(vi.begin(), vi.end(), [](const Item &item) { return item.get_iid() == 4; }); | |
auto iter4 = vi.erase(search4); | |
for (const auto &i : vi) | |
{ | |
std::cout << i << '\n'; | |
} | |
} | |
else | |
{ | |
error("Can't open file", filename); | |
} | |
} | |
catch (const std::runtime_error &e) | |
{ | |
std::cerr << "Exception thrown by error() on line 78 in main(): " << e.what() << '\n'; | |
} | |
catch (const std::invalid_argument &ia) | |
{ | |
std::cerr << ia.what() << '\n'; | |
} | |
keep_window_open(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment