From 33182dd9dd7ceafce41ed7a7f57a21d159b1b4d5 Mon Sep 17 00:00:00 2001 From: Oskar Date: Mon, 23 Sep 2024 21:55:07 +0200 Subject: more --- 7p22.cpp | 3 ++- 7p23.cpp | 11 +++++++++ 7p24.cpp | 11 +++++++++ 7p25.cpp | 17 ++++++++++++++ 7p26.cpp | 12 ++++++++++ 7p27.cpp | 19 +++++++++++++++ 7p28.cpp | 13 +++++++++++ 7p29.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7p30.cpp | 15 ++++++++++++ sales_data.hpp | 29 ++++++++++++----------- screen.hpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 251 insertions(+), 15 deletions(-) create mode 100644 7p23.cpp create mode 100644 7p24.cpp create mode 100644 7p25.cpp create mode 100644 7p26.cpp create mode 100644 7p27.cpp create mode 100644 7p28.cpp create mode 100644 7p29.cpp create mode 100644 7p30.cpp create mode 100644 screen.hpp diff --git a/7p22.cpp b/7p22.cpp index 58d5fbf..7140aed 100644 --- a/7p22.cpp +++ b/7p22.cpp @@ -2,11 +2,12 @@ /* * - * Description + * 7.22 * * */ int main () { + return 0; } diff --git a/7p23.cpp b/7p23.cpp new file mode 100644 index 0000000..a0c75f9 --- /dev/null +++ b/7p23.cpp @@ -0,0 +1,11 @@ + +/* + * + * 7.23 + * + * Done in screen.hpp + */ + +int main () { + return 0; +} diff --git a/7p24.cpp b/7p24.cpp new file mode 100644 index 0000000..896aee1 --- /dev/null +++ b/7p24.cpp @@ -0,0 +1,11 @@ + +/* + * + * 7.24 + * + * Done in screen.hpp + */ + +int main () { + return 0; +} diff --git a/7p25.cpp b/7p25.cpp new file mode 100644 index 0000000..298e673 --- /dev/null +++ b/7p25.cpp @@ -0,0 +1,17 @@ +#include "screen.hpp" +/* + * + * 7.25 + * + * + */ + +int main () { + + // I am not really sure what this means... + Screen s1; + Screen s2(100, 100, '0'); + Screen s3 = s1; + s1 = s2; + return 0; +} diff --git a/7p26.cpp b/7p26.cpp new file mode 100644 index 0000000..58d5fbf --- /dev/null +++ b/7p26.cpp @@ -0,0 +1,12 @@ +#include + +/* + * + * Description + * + * + */ + +int main () { + return 0; +} diff --git a/7p27.cpp b/7p27.cpp new file mode 100644 index 0000000..c5b3822 --- /dev/null +++ b/7p27.cpp @@ -0,0 +1,19 @@ +#include +#include "screen.hpp" + +/* + * + * 7.27 + * + * + */ + +int main () { + + Screen myScreen(5, 5, 'X'); + myScreen.move(4,0).set('#').display(std::cout); + std::cout << "\n"; + myScreen.display(std::cout); + std::cout << "\n"; + return 0; +} diff --git a/7p28.cpp b/7p28.cpp new file mode 100644 index 0000000..fcf0625 --- /dev/null +++ b/7p28.cpp @@ -0,0 +1,13 @@ + +/* + * + * 7.28 + * + * + */ + +int main () { + + // It would return a new screen object + return 0; +} diff --git a/7p29.cpp b/7p29.cpp new file mode 100644 index 0000000..44582bb --- /dev/null +++ b/7p29.cpp @@ -0,0 +1,74 @@ +#include + +/* + * + * 7.29 + * + * + */ + +class Screen { +public: + typedef std::string::size_type Pos; + Screen() = default; + Screen(Pos ht, Pos wd): Height(ht), Width(wd), Contents(ht * wd, ' ') { } + Screen(Pos ht, Pos wd, char c): Height(ht), Width(wd), Contents(ht * wd, c) { } + char get() const { return Contents[Cursor]; } + inline char get(Pos ht, Pos wd) const; + Screen move(Pos r, Pos c); + inline Screen set(char c); + inline Screen set(Pos r, Pos col, char ch); + Screen display(std::ostream &os) { do_display(os); return *this; } + const Screen display(std::ostream &os) const { do_display(os); return *this; } + void printheight(Pos Height); +private: + void do_display(std::ostream &os) const {os << Contents;} + Pos Cursor = 0; + Pos Height = 0; + Pos Width = 0; + std::string Contents; +}; + +inline Screen Screen::set(char c) +{ + Contents[Cursor] = c; // set the new value at the current cursor location + return *this; + // return this object as an lvalue +} + +inline Screen Screen::set(Pos r, Pos col, char ch) +{ + Contents[r*Width + col] = ch; // set specified location to given value + return *this; + // return this object as an lvalue +} + +inline Screen Screen::move(Pos r, Pos c) +{ + Pos row = r * Width; // compute the row location + Cursor = row + c; + // move cursor to the column within that row + return *this; + // return this object as an lvalue +} + +char Screen::get(Pos r, Pos c) const // declared as inline in the class +{ + Pos row = r * Width; + // compute row location + return Contents[row + c]; // return character at the given column +} + +void printheight(Screen::Pos Height) { + std::cout << Height; +} + +int main () { + + Screen myScreen(5, 5, 'X'); + myScreen.move(4,0).set('#').display(std::cout); + std::cout << "\n"; + myScreen.display(std::cout); + std::cout << "\n"; + return 0; +} diff --git a/7p30.cpp b/7p30.cpp new file mode 100644 index 0000000..dcfd64f --- /dev/null +++ b/7p30.cpp @@ -0,0 +1,15 @@ + +/* + * + * 7.30 + * + * + */ + +int main () { + + // Maybe it is clear to some. But to some it might be pretty redundant. + // I think overall it is a matter of preference and such a thing like this + //should be decided on by the code authors. + return 0; +} diff --git a/sales_data.hpp b/sales_data.hpp index 0820cd5..83b43b9 100644 --- a/sales_data.hpp +++ b/sales_data.hpp @@ -1,25 +1,26 @@ #ifndef SALES_DATA_H #define SALES_DATA_H #include +#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): + SalesData(const std::string &s): BookNo(s) { } //constructor, bookNo only + SalesData(const std::string &s, unsigned n, double p): // constructor, both bookno, UnitsSold and Revenue 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; - std::string BookNo; - unsigned int UnitsSold = 0; - double Revenue = 0.0; + SalesData(std::istream &sdcin) { read(sdcin, *this); }; // Constructor, but read from istream + std::string isbn() const { return BookNo; } // member function + SalesData &combine(const SalesData&); // member function, defined elsewhere + inline double avg_price() const; // member function, defined elsewhere + std::string BookNo; // data member + unsigned int UnitsSold = 0; // data member + double Revenue = 0.0; // data member }; -double SalesData::avg_price() const { +inline double SalesData::avg_price() const { if (UnitsSold) return Revenue / UnitsSold; @@ -34,7 +35,7 @@ SalesData& SalesData::combine(const SalesData &rhs) { return *this; } -std::istream &read(std::istream &is, SalesData &item) { +std::istream &read(std::istream &is, SalesData &item) { // Not member function double price = 0; is >> item.BookNo >> item.UnitsSold >> price; @@ -42,14 +43,14 @@ std::istream &read(std::istream &is, SalesData &item) { return is; } -std::ostream &print(std::ostream &os, const SalesData &item) { - +std::ostream &print(std::ostream &os, const SalesData &item) { // Not member function + os << item.isbn() << " " << item.UnitsSold << " " << item.Revenue << " " << item.avg_price(); return os; } -SalesData add(const SalesData sd1, const SalesData sd2) { +SalesData add(const SalesData sd1, const SalesData sd2) { // Not member function SalesData sdsum = sd1; sdsum.combine(sd2); diff --git a/screen.hpp b/screen.hpp new file mode 100644 index 0000000..3afa53a --- /dev/null +++ b/screen.hpp @@ -0,0 +1,62 @@ +#ifndef SCREEN_H +#define SCREEN_H +#include +#include + +class Screen { +public: + typedef std::string::size_type Pos; + Screen() = default; + Screen(Pos ht, Pos wd): Height(ht), Width(wd), Contents(ht * wd, ' ') { } + Screen(Pos ht, Pos wd, char c): Height(ht), Width(wd), Contents(ht * wd, c) { } + char get() const { return Contents[Cursor]; } + inline char get(Pos ht, Pos wd) const; + Screen &move(Pos r, Pos c); + inline Screen &set(char c); + inline Screen &set(Pos r, Pos col, char ch); + Screen &display(std::ostream &os) { do_display(os); return *this; } + const Screen &display(std::ostream &os) const { do_display(os); return *this; } + void printheight(Pos Height); +private: + void do_display(std::ostream &os) const {os << Contents;} + Pos Cursor = 0; + Pos Height = 0; + Pos Width = 0; + std::string Contents; +}; + +inline Screen &Screen::set(char c) +{ + Contents[Cursor] = c; // set the new value at the current cursor location + return *this; + // return this object as an lvalue +} + +inline Screen &Screen::set(Pos r, Pos col, char ch) +{ + Contents[r*Width + col] = ch; // set specified location to given value + return *this; + // return this object as an lvalue +} + +inline Screen &Screen::move(Pos r, Pos c) +{ + Pos row = r * Width; // compute the row location + Cursor = row + c; + // move cursor to the column within that row + return *this; + // return this object as an lvalue +} + +char Screen::get(Pos r, Pos c) const // declared as inline in the class +{ + Pos row = r * Width; + // compute row location + return Contents[row + c]; // return character at the given column +} + +void printheight(Screen::Pos Height) { + std::cout << Height; +} + +#endif -- cgit v1.2.3