diff options
-rw-r--r-- | 7p22.cpp | 3 | ||||
-rw-r--r-- | 7p23.cpp | 11 | ||||
-rw-r--r-- | 7p24.cpp | 11 | ||||
-rw-r--r-- | 7p25.cpp | 17 | ||||
-rw-r--r-- | 7p26.cpp | 12 | ||||
-rw-r--r-- | 7p27.cpp | 19 | ||||
-rw-r--r-- | 7p28.cpp | 13 | ||||
-rw-r--r-- | 7p29.cpp | 74 | ||||
-rw-r--r-- | 7p30.cpp | 15 | ||||
-rw-r--r-- | sales_data.hpp | 29 | ||||
-rw-r--r-- | screen.hpp | 62 |
11 files changed, 251 insertions, 15 deletions
@@ -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 <iostream> + +/* + * + * 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 <iostream> +#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 <iostream> + +/* + * + * 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 <string> +#include <iostream> 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 <string> +#include <iostream> + +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 |