From d8d8f1c987f118843d44ea27ac516f8fe866ded4 Mon Sep 17 00:00:00 2001 From: Oskar Date: Tue, 17 Sep 2024 15:02:56 +0200 Subject: more --- 7p1.cpp | 33 +++++++++++++++++++++++++++++++++ const-pointer-tests.cpp | 24 ++++++++++++++++++++++++ sales_data.hpp | 6 ++++++ 3 files changed, 63 insertions(+) create mode 100644 7p1.cpp create mode 100644 const-pointer-tests.cpp diff --git a/7p1.cpp b/7p1.cpp new file mode 100644 index 0000000..4183d0c --- /dev/null +++ b/7p1.cpp @@ -0,0 +1,33 @@ +#include +#include "sales_data.hpp" + +/* + * + * 7.1 + * + * + * Not really sure if we're supposed to add the data of the + * other members but i assume not because now i literally + * just changed 1.24 to fit the sales_data format + */ + +int main () { + + SalesData Item; + SalesData CurItem; + if(std::cin >> CurItem.BookNo >> CurItem.UnitsSold >> CurItem.Revenue) { + int Count = 1; + while(std::cin >> Item.BookNo >> Item.UnitsSold >> Item.Revenue) { + if(Item.BookNo == CurItem.BookNo) { + Count++; + } else { + std::cout << "ISBN: " << CurItem.BookNo << " has " << Count << " transactions" << std::endl; + CurItem = Item; + Count = 1; + } + } + std::cout << "ISBN: " << CurItem.BookNo << " has " << Count << " transactions" << std::endl; + } + + return 0; +} diff --git a/const-pointer-tests.cpp b/const-pointer-tests.cpp new file mode 100644 index 0000000..dceda16 --- /dev/null +++ b/const-pointer-tests.cpp @@ -0,0 +1,24 @@ +#include + +/* + * + * Trying to make sense of const + * + * + */ + +int main () { + int a = 0; + int b = 0; + int c = 0; + int const* pointer1 = &a; // Cannot change underlying object, can point to other object (Please for the love of god dont use this version its so confusing, use pointer3 version instead because its the same thing and much more clear + int *const pointer2 = &b; // Can change underlying object, can not point to new object + const int* pointer3 = &c; // Cannot change underlying object, can point to other object + std::cout << pointer1 << "\n" + << pointer2 << "\n" + << pointer3 << std::endl; + + const int *const pointer4 = &a; // Cannot change underlying object, cannot point to another object + std::cout << pointer4 << std::endl; + return 0; +} diff --git a/sales_data.hpp b/sales_data.hpp index cce4469..31929af 100644 --- a/sales_data.hpp +++ b/sales_data.hpp @@ -2,9 +2,15 @@ #define SALES_DATA_H #include struct SalesData { + std::string isbn() const { return BookNo; } + SalesData &combine(const SalesData); + double avg_price() const; std::string BookNo; unsigned int UnitsSold = 0; double Revenue = 0.0; }; +SalesData add(const SalesData&, const SalesData&); +std::ostream &print(std::ostream&, const SalesData&); +std::istream &read(std::istream&, SalesData&); #endif -- cgit v1.2.3