From c1b6ffe70bd281c6c230fd63fabcaac2aff47514 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 7 Apr 2024 23:18:32 -0700 Subject: feat: initial commit --- chapter2/demo1.cxx | 79 +++++++++++++++++++++++++++++++++ chapter2/demo2.cxx | 31 +++++++++++++ chapter2/newpoint.cxx | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++ chapter2/newpoint.h | 82 ++++++++++++++++++++++++++++++++++ chapter2/point.cxx | 33 ++++++++++++++ chapter2/point.h | 49 +++++++++++++++++++++ chapter2/throttle.cxx | 35 +++++++++++++++ chapter2/throttle.h | 58 ++++++++++++++++++++++++ 8 files changed, 487 insertions(+) create mode 100644 chapter2/demo1.cxx create mode 100644 chapter2/demo2.cxx create mode 100644 chapter2/newpoint.cxx create mode 100644 chapter2/newpoint.h create mode 100644 chapter2/point.cxx create mode 100644 chapter2/point.h create mode 100644 chapter2/throttle.cxx create mode 100644 chapter2/throttle.h (limited to 'chapter2') diff --git a/chapter2/demo1.cxx b/chapter2/demo1.cxx new file mode 100644 index 0000000..4c8bcca --- /dev/null +++ b/chapter2/demo1.cxx @@ -0,0 +1,79 @@ +// FILE: demo1.cxx +// This small demonstration shows how the throttle class is used. + +#include // Provides cout and cin +#include // Provides EXIT_SUCCESS +using namespace std; // Allows all Standard Library items to be used +class throttle +{ +public: + // MODIFICATION MEMBER FUNCTIONS + void shut_off( ); + void shift(int amount); + // CONSTANT MEMBER FUNCTIONS + double flow( ) const; + bool is_on( ) const; +private: + int position; +}; + +int main( ) +{ + throttle sample; + int user_input; + // Set the sample throttle to a position indicated by the user. + cout << "I have a throttle with 6 positions." << endl; + cout << "Where would you like to set the throttle? " << endl; + cout << "Please type a number from 0 to 6: "; + cin >> user_input; + sample.shut_off( ); + sample.shift(user_input); + // Shift the throttle down to zero, printing the flow along the way. + while (sample.is_on( )) + { + cout << "The flow is now " << sample.flow( ) << endl; + sample.shift(-1); + } + cout << "The flow is now off" << endl; + return EXIT_SUCCESS; +} + +void throttle::shut_off( ) +// Precondition: None. +// Postcondition: The throttle has been turned off. +{ + position = 0; +} + +double throttle::flow( ) const +// Precondition: shut_off has been called at least once to initialize the +// throttle. +// Postcondition: The value returned is the current flow as a proportion of +// the maximum flow. +{ + return position / 6.0; +} + +void throttle::shift(int amount) +// Precondition: shut_off has been called at least once to initialize the +// throttle. +// Postcondition: The throttle's position has been moved by amount (but not +// below 0 or above 6). +{ + position += amount; + + // Adjust the throttle if it went too high or too low + if (position < 0) + position = 0; + else if (position > 6) + position = 6; +} + +bool throttle::is_on( ) const +// Precondition: shut_off has been called at least once to initialize the +// throttle. +// Postcondition: If the throttle's flow is above 0 then the function +// returns true; otherwise it returns false. +{ + return (flow( ) > 0); +} diff --git a/chapter2/demo2.cxx b/chapter2/demo2.cxx new file mode 100644 index 0000000..89f4bb6 --- /dev/null +++ b/chapter2/demo2.cxx @@ -0,0 +1,31 @@ +// FILE: demo2.cxx +// This small demonstration shows how the revised throttle class is used. +#include // Provides cout and cin +#include // Provides EXIT_SUCCESS +#include "throttle.h" // Provides the throttle class +using namespace std; // Allows all Standard Library items to be used +using main_savitch_2A::throttle; + +const int DEMO_SIZE = 5; // Number of positions in a demonstration throttle + +int main( ) +{ + throttle sample(DEMO_SIZE); // A throttle to use for our demonstration + int user_input; // The position that we set the throttle to + + // Set the sample throttle to a position indicated by the user. + cout << "I have a throttle with " << DEMO_SIZE << " positions." << endl; + cout << "Where would you like to set the throttle?" << endl; + cout << "Please type a number from 0 to " << DEMO_SIZE << ": "; + cin >> user_input; + sample.shift(user_input); + + // Shift the throttle down to zero, printing the flow along the way. + while (sample.is_on( )) + { + cout << "The flow is now " << sample.flow( ) << endl; + sample.shift(-1); + } + cout << "The flow is now off" << endl; + return EXIT_SUCCESS; +} diff --git a/chapter2/newpoint.cxx b/chapter2/newpoint.cxx new file mode 100644 index 0000000..f3f40f4 --- /dev/null +++ b/chapter2/newpoint.cxx @@ -0,0 +1,120 @@ +// FILE: newpoint.cxx +// CLASS IMPLEMENTED: point (See newpoint.h for documentation.) + +#include +#include +#include "newpoint.h" +using namespace std; + +namespace main_savitch_2B +{ + point::point(double initial_x, double initial_y) + { + x = initial_x; // Constructor sets point to a given position + y = initial_y; + } + + void point::shift(double x_amount, double y_amount) + { + x += x_amount; + y += y_amount; + } + + void point::rotate90( ) + { + double new_x; + double new_y; + + new_x = y; // For a 90 degree clockwise rotation the new y is -1 + new_y = -x; // times original x, and the new x is the original y + x = new_x; + y = new_y; + } + + bool operator ==(const point& p1, const point& p2) + { + return + (p1.get_x( ) == p2.get_x( )) + && + (p1.get_y( ) == p2.get_y( )); + } + + bool operator !=(const point& p1, const point& p2) + { + return !(p1 == p2); + } + + point operator +(const point& p1, const point& p2) + { + double x_sum, y_sum; + + // Compute the x and y of the sum + x_sum = (p1.get_x( ) + p2.get_x( )); + y_sum = (p1.get_y( ) + p2.get_y( )); + point sum(x_sum, y_sum); + return sum; + } + + ostream& operator <<(ostream& outs, const point& source) + // Library facilities used: iostream + { + outs << source.get_x( ) << " " << source.get_y( ); + return outs; + } + + istream& operator >>(istream& ins, point& target) + // Library facilities used: iostream + // Friend of: point class + { + ins >> target.x >> target.y; + return ins; + } + + int rotations_needed(point p) + { + int answer; + + answer = 0; + while ((p.get_x( ) < 0) || (p.get_y( ) < 0)) + { + p.rotate90( ); + ++answer; + } + return answer; + } + + void rotate_to_upper_right(point& p) + { + while ((p.get_x( ) < 0) || (p.get_y( ) < 0)) + p.rotate90( ); + } + + double distance(const point& p1, const point& p2) + // Library facilities used: cmath + { + double a, b, c_squared; + + // Calculate differences in x and y coordinates + a = p1.get_x( ) - p2.get_x( ); // Difference in x coordinates + b = p1.get_y( ) - p2.get_y( ); // Difference in y coordinates + + // Pythagorean Theorem to calculate square of distance between points + c_squared = a*a + b*b; + + return sqrt(c_squared); // sqrt calculates square root (from math.h) + } + + point middle(const point& p1, const point& p2) + { + double x_midpoint, y_midpoint; + + // Compute the x and y midpoints + x_midpoint = (p1.get_x( ) + p2.get_x( )) / 2; + y_midpoint = (p1.get_y( ) + p2.get_y( )) / 2; + + // Construct a new point and return it + point midpoint(x_midpoint, y_midpoint); + return midpoint; + } +} + diff --git a/chapter2/newpoint.h b/chapter2/newpoint.h new file mode 100644 index 0000000..159f1fb --- /dev/null +++ b/chapter2/newpoint.h @@ -0,0 +1,82 @@ +// FILE: newpoint.h (revised from point.h in Figure 2.9 on page 58) +// CLASS PROVIDED: point (an ADT for a point on a two-dimensional plane) +// +// CONSTRUCTOR for the point class: +// point(double initial_x = 0.0, double initial_y = 0.0) +// Postcondition: The point has been set to (initial_x, initial_y). +// +// MODIFICATION MEMBER FUNCTIONS for the point class: +// void shift(double x_amount, double y_amount) +// Postcondition: The point has been moved by x_amount along the x axis +// and by y_amount along the y axis. +// +// void rotate90( ) +// Postcondition: The point has been rotated clockwise 90 degrees. +// +// CONSTANT MEMBER FUNCTIONS for the point class: +// double get_x( ) const +// Postcondition: The value returned is the x coordinate of the point. +// +// double get_y( ) const +// Postcondition: The value returned is the y coordinate of the point. +// +// NONMEMBER FUNCTIONS for the point class: +// double distance(const point& p1, const point& p2) +// Postcondition: The value returned is the distance between p1 and p2. +// +// point middle(const point& p1, const point& p2) +// Postcondition: The point returned is halfway between p1 and p2. +// +// point operator +(const point& p1, const point& p2) +// Postcondition: The sum of p1 and p2 is returned. +// +// bool operator ==(const point& p1, const point& p2) +// Postcondition: The return value is true if p1 and p2 are identical. +// +// bool operator !=(const point& p1, const point& p2) +// Postcondition: The return value is true if p1 and p2 are not identical. +// +// ostream& operator <<(ostream& outs, const point& source) +// Postcondition: The x and y coordinates of source have been +// written to outs. The return value is the ostream outs. +// +// istream& operator >>(istream& ins, point& target) +// Postcondition: The x and y coordinates of target have been +// read from ins. The return value is the istream ins. +// +// VALUE SEMANTICS for the point class: +// Assignments and the copy constructor may be used with point objects. + +#ifndef MAIN_SAVITCH_NEWPOINT_H +#define MAIN_SAVITCH_NEWPOINT_H +#include // Provides ostream and istream + +namespace main_savitch_2B +{ + class point + { + public: + // CONSTRUCTOR + point(double initial_x = 0.0, double initial_y = 0.0); + // MODIFICATION MEMBER FUNCTIONS + void shift(double x_amount, double y_amount); + void rotate90( ); + // CONSTANT MEMBER FUNCTIONS + double get_x( ) const { return x; } + double get_y( ) const { return y; } + // FRIEND FUNCTION + friend std::istream& operator >>(std::istream& ins, point& target); + private: + double x, y; // x and y coordinates of this point + }; + + // NONMEMBER FUNCTIONS for the point class + double distance(const point& p1, const point& p2); + point middle(const point& p1, const point& p2); + point operator +(const point& p1, const point& p2); + bool operator ==(const point& p1, const point& p2); + bool operator !=(const point& p1, const point& p2); + std::ostream& operator <<(std::ostream & outs, const point& source); +} + +#endif diff --git a/chapter2/point.cxx b/chapter2/point.cxx new file mode 100644 index 0000000..9b56f9e --- /dev/null +++ b/chapter2/point.cxx @@ -0,0 +1,33 @@ +// FILE: point.cxx +// CLASS IMPLEMENTED: point (See point.h for documentation.) + +#include "point.h" + +namespace main_savitch_2A +{ + + point::point(double initial_x, double initial_y) + { + x = initial_x; // Constructor sets the point to a given position. + y = initial_y; + } + + + void point::shift(double x_amount, double y_amount) + { + x += x_amount; + y += y_amount; + } + + + void point::rotate90( ) + { + double new_x; + double new_y; + + new_x = y; // For a 90 degree clockwise rotation, the new x is the original y, + new_y = -x; // and the new y is -1 times the original x. + x = new_x; + y = new_y; + } +} diff --git a/chapter2/point.h b/chapter2/point.h new file mode 100644 index 0000000..d1da6a0 --- /dev/null +++ b/chapter2/point.h @@ -0,0 +1,49 @@ +// FILE: point.h +// CLASS PROVIDED: point (part of the namespace main_savitch_chapter2) +// +// CONSTRUCTOR for the point class: +// point(double initial_x = 0.0, double initial_y = 0.0) +// Postcondition: The point has been set to (initial_x, initial_y). +// +// MODIFICATION MEMBER FUNCTIONS for the point class: +// void shift(double x_amount, double y_amount) +// Postcondition: The point has been moved by x_amount along the x axis +// and by y_amount along the y axis. +// +// void rotate90( ) +// Postcondition: The point has been rotated clockwise 90 degrees around +// the origin. +// +// CONSTANT MEMBER FUNCTIONS for the point class: +// double get_x( ) const +// Postcondition: The value returned is the x coordinate of the point. +// +// double get_y( ) const +// Postcondition: The value returned is the y coordinate of the point. +// +// VALUE SEMANTICS for the point class: +// Assignments and the copy constructor may be used with point objects. + +#ifndef MAIN_SAVITCH_POINT_H +#define MAIN_SAVITCH_POINT_H + +namespace main_savitch_2A +{ + class point + { + public: + // CONSTRUCTOR + point(double initial_x = 0.0, double initial_y = 0.0); + // MODIFICATION MEMBER FUNCTIONS + void shift(double x_amount, double y_amount); + void rotate90( ); + // CONSTANT MEMBER FUNCTIONS + double get_x( ) const { return x; } + double get_y( ) const { return y; } + private: + double x; // x coordinate of this point + double y; // y coordinate of this point + }; +} + +#endif diff --git a/chapter2/throttle.cxx b/chapter2/throttle.cxx new file mode 100644 index 0000000..18b0b6a --- /dev/null +++ b/chapter2/throttle.cxx @@ -0,0 +1,35 @@ +// FILE: throttle.cxx +// CLASS IMPLEMENTED: throttle (See throttle.h for documentation.) + +#include // Provides assert +#include "throttle.h" // Provides the throttle class definition +using namespace std; // Allows all Standard Library items to be used + +namespace main_savitch_2A +{ + + throttle::throttle( ) + { // A simple on-off throttle + top_position = 1; + position = 0; + } + + throttle::throttle(int size) + // Library facilities used: cassert + { + assert(size > 0); + top_position = size; + position = 0; + } + + void throttle::shift(int amount) + { + position += amount; + + if (position < 0) + position = 0; + else if (position > top_position) + position = top_position; + } + +} diff --git a/chapter2/throttle.h b/chapter2/throttle.h new file mode 100644 index 0000000..8707b37 --- /dev/null +++ b/chapter2/throttle.h @@ -0,0 +1,58 @@ +// FILE: throttle.h +// CLASS PROVIDED: throttle (part of the namespace main_savitch_chapter2) +// +// CONSTRUCTORS for the throttle class: +// throttle( ) +// Postcondition: The throttle has one position above the shut_off position, +// and it is currently shut off. +// +// throttle(int size) +// Precondition: size > 0. +// Postcondition: The throttle has size positions above the shut_off +// position, and it is currently shut off. +// +// MODIFICATION MEMBER FUNCTIONS for the throttle class: +// void shut_off( ) +// Postcondition: The throttle has been turned off. +// +// void shift(int amount) +// Postcondition: The throttle's position has been moved by +// amount (but not below 0 or above the top position). +// +// CONSTANT MEMBER FUNCTIONS for the throttle class: +// double flow( ) const +// Postcondition: The value returned is the current flow as a +// proportion of the maximum flow. +// +// bool is_on( ) const +// Postcondition: If the throttle's flow is above 0 then +// the function returns true; otherwise it returns false. +// +// VALUE SEMANTICS for the throttle class: (See discussion on page 51.) +// Assignments and the copy constructor may be used with throttle objects. + +#ifndef MAIN_SAVITCH_THROTTLE +#define MAIN_SAVITCH_THROTTLE + +namespace main_savitch_2A +{ + class throttle + { + public: + // CONSTRUCTORS + throttle( ); + throttle(int size); + // MODIFICATION MEMBER FUNCTIONS + void shut_off( ) { position = 0; } + void shift(int amount); + // CONSTANT MEMBER FUNCTIONS + double flow( ) const { return position / double(top_position); } + bool is_on( ) const { return (position > 0); } + private: + int top_position; + int position; + }; + +} + +#endif -- cgit v1.2.3