From a10a6e35975660b89b465faf5baacd6df9c43a34 Mon Sep 17 00:00:00 2001 From: Oskar Date: Wed, 18 Sep 2024 16:12:27 +0200 Subject: more, 7.15 not started yet --- 7p10.cpp | 15 +++++++++++++++ 7p11.cpp | 22 ++++++++++++++++++++++ 7p12.cpp | 11 +++++++++++ 7p13.cpp | 34 ++++++++++++++++++++++++++++++++++ 7p14.cpp | 11 +++++++++++ 7p15.cpp | 12 ++++++++++++ 7p3.cpp | 2 +- 7p7.cpp | 31 +++++++++++++++++++++++++++++++ 7p8.cpp | 14 ++++++++++++++ 7p9.cpp | 19 +++++++++++++++++++ person.hpp | 14 ++++++++++++++ sales_data.hpp | 33 ++++++++++++++++++++++++++++++--- 12 files changed, 214 insertions(+), 4 deletions(-) create mode 100644 7p10.cpp create mode 100644 7p11.cpp create mode 100644 7p12.cpp create mode 100644 7p13.cpp create mode 100644 7p14.cpp create mode 100644 7p15.cpp create mode 100644 7p7.cpp create mode 100644 7p8.cpp create mode 100644 7p9.cpp 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 +#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 +#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 + +/* + * + * Description + * + * + */ + +int main () { + return 0; +} diff --git a/7p3.cpp b/7p3.cpp index eee5db3..81129f6 100644 --- a/7p3.cpp +++ b/7p3.cpp @@ -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 () { diff --git a/7p7.cpp b/7p7.cpp new file mode 100644 index 0000000..8ea2b02 --- /dev/null +++ b/7p7.cpp @@ -0,0 +1,31 @@ +#include +#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; +} diff --git a/7p8.cpp b/7p8.cpp new file mode 100644 index 0000000..9b7c34d --- /dev/null +++ b/7p8.cpp @@ -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; +} diff --git a/7p9.cpp b/7p9.cpp new file mode 100644 index 0000000..9f5d283 --- /dev/null +++ b/7p9.cpp @@ -0,0 +1,19 @@ +#include +#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; +} diff --git a/person.hpp b/person.hpp index 66ec7f0..9d1e7ea 100644 --- a/person.hpp +++ b/person.hpp @@ -1,6 +1,7 @@ #ifndef PERSON_H #define PERSON_H #include +#include 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 +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 -- cgit v1.2.3