Writting program which will read a coordinate pair for a point in thex-y plane.

S

sam alton

Write a program which will read a coordinate pair for a point in the x-
y plane. It should output whether the point is at the origin (0, 0),
on the X axis [such as (6, 0)], on the Y axis [such as (0, -2)], or in
one of four quadrants: for example: (3, 1) is in quadrant 1, (-2, 2)
is in quadrant 2, (-5, -10) is in quadrant 3, and (5, -1) is in
quadrant 4.
 
R

red floyd

Write a program which will read a coordinate pair for a point in the x-
y plane. It should output whether the point is at the origin (0, 0),
on the X axis [such as (6, 0)], on the Y axis [such as (0, -2)], or in
one of four quadrants: for example: (3, 1) is in quadrant 1, (-2, 2)
is in quadrant 2, (-5, -10) is in quadrant 3, and (5, -1) is in
quadrant 4.

Your program may be found at the following address:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2
 
G

Garrett Hartshaw

Write a program which will read a coordinate pair for a point in the x-
y plane. It should output whether the point is at the origin (0, 0),
on the X axis [such as (6, 0)], on the Y axis [such as (0, -2)], or in
one of four quadrants: for example: (3, 1) is in quadrant 1, (-2, 2)
is in quadrant 2, (-5, -10) is in quadrant 3, and (5, -1) is in
quadrant 4.

// Point.cpp
//
// To use: Compile with the point's coordinates defined as XPOS and YPOS
// and run the resulting file
// e.g. To test the origin (0, 0) run the following command
// (assuming you are using gcc):
// g++ Point.cpp -DXPOS=0 -DYPOS=0 && ./a.out
//
#include <iostream>
#include <string>

#include <boost/mpl/int.hpp>
#include <boost/mpl/and.hpp>
#include <boost/mpl/comparison.hpp>
#include <boost/mpl/eval_if.hpp>
namespace mpl = boost::mpl;

typedef mpl::int_<XPOS> xpos;
typedef mpl::int_<YPOS> ypos;
typedef mpl::int_<0> zero;

typedef mpl::less<xpos, zero> x_less_0;
typedef mpl::equal_to<xpos, zero> x_equal_0;
typedef mpl::greater<xpos, zero> x_greater_0;

typedef mpl::less<ypos, zero> y_less_0;
typedef mpl::equal_to<ypos, zero> y_equal_0;
typedef mpl::greater<ypos, zero> y_greater_0;

enum { ORIGIN, X_AXIS, Y_AXIS, QUAD_1, QUAD_2, QUAD_3, QUAD_4 };
template <int msg> struct message { typedef message<msg> type; static
std::string value; };
template<> std::string message<ORIGIN>::value = "At the origin";
template<> std::string message<X_AXIS>::value = "On the x-axis";
template<> std::string message<Y_AXIS>::value = "On the y-axis";
template<> std::string message<QUAD_1>::value = "In quadrant 1";
template<> std::string message<QUAD_2>::value = "In quadrant 2";
template<> std::string message<QUAD_3>::value = "In quadrant 3";
template<> std::string message<QUAD_4>::value = "In quadrant 4";

typedef mpl::and_<x_equal_0, y_equal_0> at_origin;
typedef mpl::eval_if<x_less_0, message<QUAD_2>, message<QUAD_1> > q12_msg;
typedef mpl::eval_if<x_less_0, message<QUAD_3>, message<QUAD_4> > q34_msg;
typedef mpl::eval_if<y_greater_0, q12_msg, q34_msg> q_msg;
typedef mpl::eval_if<x_equal_0, message<Y_AXIS>, q_msg> ya_msg;
typedef mpl::eval_if<y_equal_0, message<X_AXIS>, ya_msg> aq_msg;
typedef mpl::eval_if<at_origin, message<ORIGIN>, aq_msg> msg;

int main() { std::cout << msg::type::value << std::endl; }

-Garrett

P.S. this is not meant to be an actual solution, I just wanted to
practice template metaprogramming.
 
S

sam alton

Write a program which will read a coordinate pair for a point in the x-
y plane. It should output whether the point is at the origin (0, 0),
on the X axis [such as (6, 0)], on the Y axis [such as (0, -2)], or in
one of four quadrants: for example: (3, 1) is in quadrant 1, (-2, 2)
is in quadrant 2, (-5, -10) is in quadrant 3, and (5, -1) is in
quadrant 4.

// Point.cpp
//
// To use: Compile with the point's coordinates defined as XPOS and YPOS
//         and run the resulting file
// e.g. To test the origin (0, 0) run the following command
//       (assuming you are using gcc):
// g++ Point.cpp -DXPOS=0 -DYPOS=0 && ./a.out
//
#include <iostream>
#include <string>

#include <boost/mpl/int.hpp>
#include <boost/mpl/and.hpp>
#include <boost/mpl/comparison.hpp>
#include <boost/mpl/eval_if.hpp>
namespace mpl = boost::mpl;

typedef mpl::int_<XPOS> xpos;
typedef mpl::int_<YPOS> ypos;
typedef mpl::int_<0>    zero;

typedef mpl::less<xpos, zero>     x_less_0;
typedef mpl::equal_to<xpos, zero> x_equal_0;
typedef mpl::greater<xpos, zero>  x_greater_0;

typedef mpl::less<ypos, zero>     y_less_0;
typedef mpl::equal_to<ypos, zero> y_equal_0;
typedef mpl::greater<ypos, zero>  y_greater_0;

enum { ORIGIN, X_AXIS, Y_AXIS, QUAD_1, QUAD_2, QUAD_3, QUAD_4 };
template <int msg> struct message { typedef message<msg> type; static
std::string value; };
template<> std::string message<ORIGIN>::value = "At the origin";
template<> std::string message<X_AXIS>::value = "On the x-axis";
template<> std::string message<Y_AXIS>::value = "On the y-axis";
template<> std::string message<QUAD_1>::value = "In quadrant 1";
template<> std::string message<QUAD_2>::value = "In quadrant 2";
template<> std::string message<QUAD_3>::value = "In quadrant 3";
template<> std::string message<QUAD_4>::value = "In quadrant 4";

typedef mpl::and_<x_equal_0, y_equal_0>                       at_origin;
typedef mpl::eval_if<x_less_0, message<QUAD_2>, message<QUAD_1> > q12_msg;
typedef mpl::eval_if<x_less_0, message<QUAD_3>, message<QUAD_4> > q34_msg;
typedef mpl::eval_if<y_greater_0, q12_msg, q34_msg>               q_msg;
typedef mpl::eval_if<x_equal_0, message<Y_AXIS>, q_msg>           ya_msg;
typedef mpl::eval_if<y_equal_0, message<X_AXIS>, ya_msg>          aq_msg;
typedef mpl::eval_if<at_origin, message<ORIGIN>, aq_msg>          msg;

int main() { std::cout << msg::type::value << std::endl; }

  -Garrett

P.S. this is not meant to be an actual solution, I just wanted to
practice template metaprogramming.

Thank you so much actually i know alittle bit of template
metaprogramming, and this working fine except some few error but i
corrected. Can you try writing in C++ is you can?
 
S

Saeed Amrollahi

Write a program which will read a coordinate pair for a point in the x-
y plane. It should output whether the point is at the origin (0, 0),
on the X axis [such as (6, 0)], on the Y axis [such as (0, -2)], or in
one of four quadrants: for example: (3, 1) is in quadrant 1, (-2, 2)
is in quadrant 2, (-5, -10) is in quadrant 3, and (5, -1) is in
quadrant 4.

Hi Sam
Your question seems to be a homework/assignment.
You have to make an effort to C++ programming at first place.
BTW, I'll show you some clues to your problem:
1. You can design/define a Point class like this:
class Point { // 2D point
int x, y;
public:
// set of constructors
Point() : x(0), y(0) {}
Point(int x_, int y_) : x(x_), y(y_) {}
// ...
// Examining object states
int X() const { return x; }
int Y() const { return y; }
// Where is point?
enum EWhere { CENTER, X_AXIS, Y_AXIS, QUADRANT_1, ... };
EWhere Where() const
{
// A lot of if-else
if (x == 0 && y == 0) return CENTER;
else if (y == 0) return X_AXIS;
// ...
}
};
2. You can define 2 (friend) I/O operators:
istream& operator>>(const istream&, Point&);
ostream& operator<<(ostream&, const Point&);
3. If the type of coordinates of points may be vary (sometimes int,
sometimes double, ...), you can define a template class:
template<class T>
class Point {
// like before
};

4. You can do a lot of good things to extend the problem statement.

Regards,
-- Saeed Amrollahi
 
G

Garrett Hartshaw

P.S. this is not meant to be an actual solution, I just wanted to
Thank you so much actually i know alittle bit of template
metaprogramming, and this working fine except some few error but i
corrected. Can you try writing in C++ is you can?

Since this does seem to be a homework question, I will not translate it
for you, however, as you said you know some metaprogramming, it should
not be that hard to translate it to runtime executable code.
-Garrett
 
S

Seungbeom Kim

1. You can design/define a Point class like this:
class Point { // 2D point
int x, y;
public:
// set of constructors
Point() : x(0), y(0) {}
Point(int x_, int y_) : x(x_), y(y_) {}
// ...
// Examining object states
int X() const { return x; }
int Y() const { return y; }
// Where is point?
enum EWhere { CENTER, X_AXIS, Y_AXIS, QUADRANT_1, ... };
EWhere Where() const
{
// A lot of if-else
if (x == 0 && y == 0) return CENTER;
else if (y == 0) return X_AXIS;
// ...
}
};

I wonder if you really need a class with private data members and
public accessor functions, when the need for information hiding and
invariant maintenance is not clear. Wouldn't

struct point { int x, y; };

suffice? My rule has been to write classes with private data members
only for those that need information hiding and invariant maintenance.

(I might even be tempted to go further and write:

typedef std::array<int, 2> point;

which would be better with a "strong typedef" that created a new type.)

Then Where can be a simple free function that takes a const point&
argument, and you don't have to make operator<< and operator>> friends.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top