diff options
-rw-r--r-- | 7p10.cpp | 15 | ||||
-rw-r--r-- | 7p11.cpp | 22 | ||||
-rw-r--r-- | 7p12.cpp | 11 | ||||
-rw-r--r-- | 7p13.cpp | 34 | ||||
-rw-r--r-- | 7p14.cpp | 11 | ||||
-rw-r--r-- | 7p15.cpp | 12 | ||||
-rw-r--r-- | 7p3.cpp | 2 | ||||
-rw-r--r-- | 7p7.cpp | 31 | ||||
-rw-r--r-- | 7p8.cpp | 14 | ||||
-rw-r--r-- | 7p9.cpp | 19 | ||||
-rw-r--r-- | person.hpp | 14 | ||||
-rw-r--r-- | sales_data.hpp | 33 |
12 files changed, 214 insertions, 4 deletions
diff --git a/7p10.cpp b/7p10.cpp new file mode 100644 index 0000000..da25d3c --- /dev/null +++ b/7p10.cpp @@ -0,0 +1,15 @@ + +/* + * + * 7.10 + * + * + */ + +int main () { + + // if (read(read(cin, data1), data2)) + // It first runs read(cin , data1), if it's successful then it returns cin + // And then read(cin , data2) runs and we check if thats successful + return 0; +} diff --git a/7p11.cpp b/7p11.cpp new file mode 100644 index 0000000..6e45361 --- /dev/null +++ b/7p11.cpp @@ -0,0 +1,22 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 7.11 + * + * + */ + +int main () { + + SalesData sd1 = {"0-201-78345-X"}; + SalesData sd2 = {"0-201-78345-X", 1, 1}; + SalesData sd3("0-201-78000-X"); + SalesData sd4 = {"0-201-78000-X", 21, 22}; + print(std::cout, sd1) << std::endl; + print(std::cout, sd2) << std::endl; + print(std::cout, sd3) << std::endl; + print(std::cout, sd4) << std::endl; + return 0; +} diff --git a/7p12.cpp b/7p12.cpp new file mode 100644 index 0000000..b96b63e --- /dev/null +++ b/7p12.cpp @@ -0,0 +1,11 @@ + +/* + * + * 7.12 + * + * Done in sales_data.hpp + */ + +int main () { + return 0; +} diff --git a/7p13.cpp b/7p13.cpp new file mode 100644 index 0000000..bfacb6a --- /dev/null +++ b/7p13.cpp @@ -0,0 +1,34 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 7.13 + * + * + */ + +int main () { + + SalesData total(std::cin); + if (std::cin) { + SalesData trans(std::cin); + while(std::cin) { + if (total.isbn() == trans.isbn()) + total.combine(trans); + else { + print(std::cout, total) << std::endl; + total = trans; + } + + read(std::cin, trans); + } + + print(std::cout, total) << std::endl; + } else { + std::cerr << "Error" << std::endl; + return -1; + } + + return 0; +} diff --git a/7p14.cpp b/7p14.cpp new file mode 100644 index 0000000..b9c1177 --- /dev/null +++ b/7p14.cpp @@ -0,0 +1,11 @@ + +/* + * + * 7.14 + * + * No idea what this exercise wants me to do... + */ + +int main () { + return 0; +} diff --git a/7p15.cpp b/7p15.cpp new file mode 100644 index 0000000..58d5fbf --- /dev/null +++ b/7p15.cpp @@ -0,0 +1,12 @@ +#include <iostream> + +/* + * + * Description + * + * + */ + +int main () { + return 0; +} @@ -9,7 +9,7 @@ * what is supposed to be correct. It may be because i misunderstood * The exercise back in the 1.24 exercise. * - * I have modified this program now to be more like i think it should be. + * I have modified this program now to be more like i think it should be... */ int main () { @@ -0,0 +1,31 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 7.7 + * + * + */ + +int main () { + + SalesData CurItem; + if(read(std::cin, CurItem)) { + SalesData Item; + while(read(std::cin, Item)) { + if(Item.isbn() == CurItem.isbn()) { + CurItem.combine(Item); + } else { + print(std::cout, CurItem); + std::cout << std::endl; + CurItem = Item; + } + } + + print(std::cout, CurItem); + std::cout << std::endl; + } + + return 0; +} @@ -0,0 +1,14 @@ + +/* + * + * 7.8 + * + * + */ + +int main () { + + // Read has no const ref SalesData because we are writing to the Revenue member with new data + // Print has const ref SalesData because we are only reading from it + return 0; +} @@ -0,0 +1,19 @@ +#include <iostream> +#include "person.hpp" + +/* + * + * 7.9 + * + * Done in person.hpp + * + */ + +int main () { + + Person person1; + read(std::cin, person1); + print(std::cout, person1); + std::cout << std::endl; + return 0; +} @@ -1,6 +1,7 @@ #ifndef PERSON_H #define PERSON_H #include <string> +#include <vector> struct Person { std::string Name; @@ -9,4 +10,17 @@ struct Person { std::string GetName() const { return Name; } }; +std::istream &read(std::istream &pcin, Person &p1) { + + std::getline(pcin, p1.Name); + std::getline(pcin, p1.Address); + return pcin; +} + +std::ostream &print(std::ostream &pcout, const Person &p1) { + + pcout << p1.GetName() << "\n" << p1.GetAddress(); + return pcout; +} + #endif diff --git a/sales_data.hpp b/sales_data.hpp index 7cb3022..0820cd5 100644 --- a/sales_data.hpp +++ b/sales_data.hpp @@ -1,7 +1,16 @@ #ifndef SALES_DATA_H #define SALES_DATA_H #include <string> +struct SalesData; +SalesData add(const SalesData&, const SalesData&); +std::ostream &print(std::ostream&, const SalesData&); +std::istream &read(std::istream&, SalesData&); struct SalesData { + SalesData() = default; + SalesData(const std::string &s): BookNo(s) { } + SalesData(const std::string &s, unsigned n, double p): + BookNo(s), UnitsSold(n), Revenue(p*n) { } + SalesData(std::istream &sdcin) { read(sdcin, *this); }; std::string isbn() const { return BookNo; } SalesData &combine(const SalesData&); double avg_price() const; @@ -25,8 +34,26 @@ SalesData& SalesData::combine(const SalesData &rhs) { return *this; } -SalesData add(const SalesData&, const SalesData&); -std::ostream &print(std::ostream&, const SalesData&); -std::istream &read(std::istream&, SalesData&); +std::istream &read(std::istream &is, SalesData &item) { + + double price = 0; + is >> item.BookNo >> item.UnitsSold >> price; + item.Revenue = price * item.UnitsSold; + return is; +} + +std::ostream &print(std::ostream &os, const SalesData &item) { + + os << item.isbn() << " " << item.UnitsSold << " " + << item.Revenue << " " << item.avg_price(); + return os; +} + +SalesData add(const SalesData sd1, const SalesData sd2) { + + SalesData sdsum = sd1; + sdsum.combine(sd2); + return sdsum; +} #endif |