ThreeDPoint.h
/*
* Copyright 2011 Steven Gribble
*
* This file is the solution to an exercise problem posed during
* one of the UW CSE 333 lectures (333exercises).
*
* 333exercises is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 333exercises is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 333exercises. If not, see <http://www.gnu.org/licenses/>.
*/
// Lecture 10, Extra Exercise #1
#ifndef THREEDPOINT_H_
#define THREEDPOINT_H_
#include <iostream>
using std::ostream;
// A "ThreeDPoint" is a class representing a three-dimensional point.
// Internally, a ThreeDPoint is represented with doubles for the x, y,
// and z axis values.
class ThreeDPoint {
public:
// Constructors; we use initialization lists to initialize.
ThreeDPoint() : x_(0), y_(0), z_(0) { }
ThreeDPoint(const double x, const double y, const double z)
: x_(x), y_(y), z_(z) { }
// Copy constructor.
ThreeDPoint(const ThreeDPoint &pt);
// Accessors (inlined).
double get_x() const { return x_; }
double get_y() const { return y_; }
double get_z() const { return z_; }
// Inner product.
double InnerProduct(const ThreeDPoint &pt) const;
// Add, subtract operators.
ThreeDPoint operator+(const ThreeDPoint &pt) const;
ThreeDPoint operator-(const ThreeDPoint &pt) const;
// Add/subtract and assign operators.
ThreeDPoint &operator+=(const ThreeDPoint &pt);
ThreeDPoint &operator-=(const ThreeDPoint &pt);
// Assignment operator.
ThreeDPoint &operator=(const ThreeDPoint &pt);
// Override "<<" for std::ostream.
friend ostream &operator<<(ostream &out, const ThreeDPoint &pt);
private:
double x_, y_, z_;
};
#endif // THREEDPOINT_H_
ThreeDPoint.cc
/*
* Copyright 2011 Steven Gribble
*
* This file is the solution to an exercise problem posed during
* one of the UW CSE 333 lectures (333exercises).
*
* 333exercises is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 333exercises is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 333exercises. If not, see <http://www.gnu.org/licenses/>.
*/
// Lecture 10, Extra Exercise #1
#include "ThreeDPoint.h"
using std::ostream;
// Copy constructor.
ThreeDPoint::ThreeDPoint(const ThreeDPoint &pt) {
x_ = pt.x_;
y_ = pt.y_;
z_ = pt.z_;
}
// Inner product.
double ThreeDPoint::InnerProduct(const ThreeDPoint &pt) const {
double retval = 0.0;
retval += (x_ * pt.x_);
retval += (y_ * pt.y_);
retval += (z_ * pt.z_);
return retval;
}
// Arithmetic operators.
ThreeDPoint ThreeDPoint::operator+(const ThreeDPoint &pt) const {
ThreeDPoint retval(*this); // Uses our copy constructor!
retval.x_ += pt.x_;
retval.y_ += pt.y_;
retval.z_ += pt.z_;
return retval;
}
ThreeDPoint ThreeDPoint::operator-(const ThreeDPoint &pt) const {
ThreeDPoint retval(*this); // Uses our copy constructor!
retval.x_ -= pt.x_;
retval.y_ -= pt.y_;
retval.z_ -= pt.z_;
return retval;
}
// Arithmetic + assign operators.
ThreeDPoint &ThreeDPoint::operator+=(const ThreeDPoint &pt) {
x_ += pt.x_;
y_ += pt.y_;
z_ += pt.z_;
return *this;
}
ThreeDPoint &ThreeDPoint::operator-=(const ThreeDPoint &pt) {
x_ -= pt.x_;
y_ -= pt.y_;
z_ -= pt.z_;
return *this;
}
// Assignment operator.
ThreeDPoint &ThreeDPoint::operator=(const ThreeDPoint &pt) {
if (this != &pt) {
x_ = pt.x_;
y_ = pt.y_;
z_ = pt.z_;
}
return *this;
}
// Override "<<" for std::ostream. Note that this is not
// a member function of ThreeDPoint, but a global non-member function.
// The "friend" is there so we can directly access pt's x_, y_, z_
// privates.
ostream &operator<<(ostream &out, const ThreeDPoint &pt) {
out << "(" << pt.x_ << "," << pt.y_ << "," << pt.z_ << ")";
return out;
}
useThreeDPoint.cc
/*
* Copyright 2011 Steven Gribble
*
* This file is the solution to an exercise problem posed during
* one of the UW CSE 333 lectures (333exercises).
*
* 333exercises is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 333exercises is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 333exercises. If not, see <http://www.gnu.org/licenses/>.
*/
// Lecture 10, Extra Exercise #1
//
// Write a C++ program that:
// - has a class representing a 3-dimensional point
// - has methods to:
// - return the inner product of two points
// - handles “<<“, “+”, “-”, “+=”, “-=”, “=”, and copy constructors
// - uses “const” in all the right places
#include <cstdlib>
#include "ThreeDPoint.h"
int main(int argc, char **argv) {
ThreeDPoint a, b(1.0, 2.0, 3.0), c(b), d = c, e;
b += a;
c += b;
d -= c;
e = d + (a + b + c - d);
a = c - (b + b);
a = a;
cout << a << " " << b << " " << c << " " << d << " " << e << endl;
cout << b.InnerProduct(e) << endl;
return EXIT_SUCCESS;
}
ThreeDBox.h
/*
* Copyright 2011 Steven Gribble
*
* This file is the solution to an exercise problem posed during
* one of the UW CSE 333 lectures (333exercises).
*
* 333exercises is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 333exercises is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 333exercises. If not, see <http://www.gnu.org/licenses/>.
*/
// Lecture 10, Extra Exercise #2
#ifndef THREEDBOX_H_
#define THREEDBOX_H_
#include <iostream>
#include "ThreeDPoint.h"
using std::ostream;
// A "ThreeDBox" is a class representing a three-dimensional box
// aligned with the axis with right angles. Internally, we
// represent a ThreeDBox with two ThreeDPoint objects: a and b,
// where a.x_ <= b.x_, a.y_ <= b.y_, a.z_ <= b.z_.
class ThreeDBox {
public:
// Constructors; use initialization lists to initialize.
ThreeDBox() : a_(), b_() {}
ThreeDBox(const ThreeDPoint &a, const ThreeDPoint &b)
: a_(a), b_(b) { }
// Copy constructor.
ThreeDBox(const ThreeDBox &bx);
// Accessors (inlined).
ThreeDPoint get_a() const { return a_; }
ThreeDPoint get_b() const { return b_; }
// Volume.
double Volume() const;
// Tests if self is inside bx.
bool IsInside(const ThreeDBox &bx) const;
// Assignment operator.
ThreeDBox &operator=(const ThreeDBox &pt);
// Override "<<" for std::ostream.
friend ostream &operator<<(ostream &out, const ThreeDBox &pt);
private:
ThreeDPoint a_, b_;
};
#endif // THREEDBOX_H_
ThreeDBox.cc
/*
* Copyright 2011 Steven Gribble
*
* This file is the solution to an exercise problem posed during
* one of the UW CSE 333 lectures (333exercises).
*
* 333exercises is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 333exercises is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 333exercises. If not, see <http://www.gnu.org/licenses/>.
*/
// Lecture 10, Extra Exercise #2
#include <iostream>
#include "ThreeDBox.h"
using std::ostream;
// Copy constructor.
ThreeDBox::ThreeDBox(const ThreeDBox &bx) {
a_ = bx.a_;
b_ = bx.b_;
}
// Volume.
double ThreeDBox::Volume() const {
double vol = 0.0;
vol = b_.get_x() - a_.get_x();
vol *= (b_.get_y() - a_.get_y());
vol *= (b_.get_z() - a_.get_z());
return vol;
}
// Test inside.
bool ThreeDBox::IsInside(const ThreeDBox &bx) const {
if ((bx.a_.get_x() > a_.get_x()) ||
(bx.b_.get_x() < b_.get_x()))
return false;
if ((bx.a_.get_y() > a_.get_y()) ||
(bx.b_.get_y() < b_.get_y()))
return false;
if ((bx.a_.get_z() > a_.get_z()) ||
(bx.b_.get_z() < b_.get_z()))
return false;
return true;
}
// Assignment operator.
ThreeDBox &ThreeDBox::operator=(const ThreeDBox &pt) {
if (this != &pt) {
a_ = pt.a_;
b_ = pt.b_;
}
return *this;
}
// Override "<<" for std::ostream.
ostream &operator<<(ostream &out, const ThreeDBox &pt) {
out << "[ " << pt.a_ << " -- " << pt.b_ << " ]";
return out;
}
useThreeDBox.cc
/*
* Copyright 2011 Steven Gribble
*
* This file is the solution to an exercise problem posed during
* one of the UW CSE 333 lectures (333exercises).
*
* 333exercises is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 333exercises is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 333exercises. If not, see <http://www.gnu.org/licenses/>.
*/
// Lecture 10, Extra Exercise #2
//
// Write a C++ program that:
//
// - has a class representing a 3-dimensional box
//
// - uses your exercise 1 class representing 3d points to store
// the coordinates of the vertices that define it
//
// - assume the box has right-angles only and its faces are
// parallel to the axes, so you need two vertices to define it
//
// - has methods to:
//
// - test if one box is inside another box
//
// - return the volume of a box
//
// - handles '<<', '=' and a copy constructor
//
// - uses 'const' in all the right places
#include <cstdlib>
#include "ThreeDPoint.h"
#include "ThreeDBox.h"
int main(int argc, char **argv) {
ThreeDPoint a(1, 1, 1), b(1, 1, 2), c(2, 2, 2), d(2, 4, 6);
ThreeDBox empty(a, a), flat(a, b), small(a, c), big(a, d);
ThreeDBox test = empty;
cout << empty << endl;
cout << flat << endl;
cout << small << endl;
cout << big << endl;
cout << empty.Volume() << " " << flat.Volume() << " ";
cout << small.Volume() << " " << big.Volume() << endl;
cout << test << endl;
test = big;
cout << test << endl;
cout << big.IsInside(small) << " ";
cout << small.IsInside(big) << " ";
cout << empty.IsInside(empty) << endl;
return EXIT_SUCCESS;
}
ThreeDPoint.h
/*
* Copyright 2011 Steven Gribble
*
* This file is the solution to an exercise problem posed during
* one of the UW CSE 333 lectures (333exercises).
*
* 333exercises is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 333exercises is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 333exercises. If not, see <http://www.gnu.org/licenses/>.
*/
// Lecture 10, Extra Exercise #3
#ifndef _THREEDPOINT_H_
#define _THREEDPOINT_H_
#include <iostream>
using namespace std;
// A "ThreeDPoint" is a class representing a three-dimensional point.
// Internally, a ThreeDPoint is represented with doubles for the x, y,
// and z axis values.
class ThreeDPoint {
public:
// Constructors; we use initialization lists to initialize.
ThreeDPoint() : x_(0), y_(0), z_(0) { }
ThreeDPoint(const double x, const double y, const double z)
: x_(x), y_(y), z_(z) { }
// Copy value of py to this ThreeDPoint
ThreeDPoint &CopyFrom(const ThreeDPoint &py);
// Accessors (inlined).
double get_x() const { return x_; }
double get_y() const { return y_; }
double get_z() const { return z_; }
// Inner product.
double InnerProduct(const ThreeDPoint &pt) const;
// Add, subtract operators.
ThreeDPoint operator+(const ThreeDPoint &pt) const;
ThreeDPoint operator-(const ThreeDPoint &pt) const;
// Add/subtract and assign operators.
ThreeDPoint &operator+=(const ThreeDPoint &pt);
ThreeDPoint &operator-=(const ThreeDPoint &pt);
// Override "<<" for std::ostream.
friend ostream &operator<<(ostream &out, const ThreeDPoint &pt);
private:
// Disable the copy constructor and assignment operator.
ThreeDPoint(const ThreeDPoint &pt);
ThreeDPoint &operator=(const ThreeDPoint &pt);
double x_, y_, z_;
};
#endif // _THREEDPOINT_H_
ThreeDPoint.cc
/*
* Copyright 2011 Steven Gribble
*
* This file is the solution to an exercise problem posed during
* one of the UW CSE 333 lectures (333exercises).
*
* 333exercises is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 333exercises is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 333exercises. If not, see <http://www.gnu.org/licenses/>.
*/
// Lecture 10, Extra Exercise #3
#include "ThreeDPoint.h"
using namespace std;
// CopyFrom.
ThreeDPoint &ThreeDPoint::CopyFrom(const ThreeDPoint &py) {
x_ = py.x_;
y_ = py.y_;
z_ = py.z_;
return *this;
}
// Inner product.
double ThreeDPoint::InnerProduct(const ThreeDPoint &pt) const {
double retval = 0.0;
retval += (x_ * pt.x_);
retval += (y_ * pt.y_);
retval += (z_ * pt.z_);
return retval;
}
// Arithmetic operators.
ThreeDPoint ThreeDPoint::operator+(const ThreeDPoint &pt) const {
ThreeDPoint retval;
retval.x_ = x_ + pt.x_;
retval.y_ = y_ + pt.y_;
retval.z_ = z_ + pt.z_;
return retval;
}
ThreeDPoint ThreeDPoint::operator-(const ThreeDPoint &pt) const {
ThreeDPoint retval;
retval.x_ = x_ - pt.x_;
retval.y_ = y_ - pt.y_;
retval.z_ = z_ - pt.z_;
return retval;
}
// Arithmetic + assign operators.
ThreeDPoint &ThreeDPoint::operator+=(const ThreeDPoint &pt) {
x_ += pt.x_;
y_ += pt.y_;
z_ += pt.z_;
return *this;
}
ThreeDPoint &ThreeDPoint::operator-=(const ThreeDPoint &pt) {
x_ -= pt.x_;
y_ -= pt.y_;
z_ -= pt.z_;
return *this;
}
// Override "<<" for std::ostream. Note that this is not a member
// function of ThreeDPoint, but a global non-member function. The
// "friend" is there so we can directly access pt's x_, y_, z_
// private variables.
ostream &operator<<(ostream &out, const ThreeDPoint &pt) {
out << "(" << pt.x_ << "," << pt.y_ << "," << pt.z_ << ")";
return out;
}
useThreeDPoint
/*
* Copyright 2011 Steven Gribble
*
* This file is the solution to an exercise problem posed during
* one of the UW CSE 333 lectures (333exercises).
*
* 333exercises is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 333exercises is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 333exercises. If not, see <http://www.gnu.org/licenses/>.
*/
// Lecture 10, Extra Exercise #3
//
// Modify your 3D Point class from Lecture 10, Extra Exercise #1
// - disable the copy constructor and assignment operator
// - attempt to use copy & assign in code, and see what error
// the compiler generates
// - write a CopyFrom() member function and try using it instead
#include <cstdlib>
#include "ThreeDPoint.h"
int main(int argc, char **argv) {
ThreeDPoint a(5,6,7), b(1.0, 2.0, 3.0);
// This should produce a compiler error, since we disabled the
// copy constructor.
// ThreeDPoint c(b);
// So should this, since we disabled the assignment operator.
// ThreeDPoint d;
// d = a;
// But, this should work.
ThreeDPoint e;
e.CopyFrom(a);
cout << a << " " << b << " " << e << endl;
cout << b.InnerProduct(e) << endl;
return EXIT_SUCCESS;
}
GetWord.h
/*
* Copyright 2011 Steven Gribble
*
* This file is the solution to an exercise problem posed during
* one of the UW CSE 333 lectures (333exercises).
*
* 333exercises is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 333exercises is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 333exercises. If not, see <http://www.gnu.org/licenses/>.
*/
// Lecture 10, Extra Exercise #4
#ifndef _GETWORD_H_
#define _GETWORD_H_
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// A "GetWord" is a class that can be used to open a file and
// read successive white-space-separated, ASCII words from
// the file.
class GetWord {
public:
// Constructor. Throws an exception on error.
GetWord(string filename) : filename_(filename) {
// We'll temporarily elevate the exceptions bitmask on
// f_ so that an exception is thrown if we encounter an
// error while opening filename.
f_.exceptions(ifstream::failbit);
// Try to open filename_.
f_.open(filename_.c_str(), ifstream::in);
// Clear the exceptions bitmask, so no more exceptions
// are thrown.
f_.exceptions(ifstream::goodbit);
}
// Destructor.
~GetWord() {
if (f_.is_open())
f_.close();
}
// Get the next word from the file. Returns a non-empty
// string if there is a next word, or an empty string
// if we've hit EOF.
string GetNextWord();
private:
// Disable the copy constructor and assignment operator.
GetWord(const GetWord &gw);
GetWord &operator=(const GetWord &gw);
string filename_;
ifstream f_;
};
#endif // _GETWORD_H_
GetWord.cc
/*
* Copyright 2011 Steven Gribble
*
* This file is the solution to an exercise problem posed during
* one of the UW CSE 333 lectures (333exercises).
*
* 333exercises is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 333exercises is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 333exercises. If not, see <http://www.gnu.org/licenses/>.
*/
// Lecture 10, Extra Exercise #4
#include <sstream>
#include <cctype>
#include "GetWord.h"
using namespace std;
string GetWord::GetNextWord() {
stringstream ss;
string retstr = "";
// Zoom ahead to the start of the next word, or return
// empty string if we hit EOF first.
while (f_.good()) {
char c;
c = f_.peek();
if (!f_.good())
return retstr;
if (isalnum(c))
break;
f_.get(c);
}
// Keep chomping until we hit the end of the word or EOF.
while (f_.good()) {
char c;
f_.get(c);
if (f_.good()) {
if (isalnum(c))
ss << c;
else
break;
}
}
// Done with the word; return it.
return ss.str();
}
useWord.cc
/*
* Copyright 2011 Steven Gribble
*
* This file is the solution to an exercise problem posed during
* one of the UW CSE 333 lectures (333exercises).
*
* 333exercises is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 333exercises is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 333exercises. If not, see <http://www.gnu.org/licenses/>.
*/
// Lecture 10, Extra Exercise #4
//
// Write a C++ class that:
// - is given the name of a file as a constructor argument
// - has a “GetNextWord( )” method that returns the next whitespace or
// newline-separate word from the file as a copy of a “string”
// object, or an empty string once you hit EOF.
// - has a destructor that cleans up anything that needs cleaning up
#include <unistd.h>
#include <cstdlib>
#include <iostream>
#include "GetWord.h"
void Usage(char *name) {
std::cout << "Usage: " << name << " filename" << std::endl;
exit(EXIT_FAILURE);
}
int main(int argc, char **argv) {
if (argc != 2) {
Usage(argv[0]);
}
GetWord gw(argv[1]);
while (1) {
string next = gw.GetNextWord();
if (next == "")
break;
std::cout << next << std::endl;
}
return EXIT_SUCCESS;
}