Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Writting program which will read a coordinate pair for a point in thex-y plane.

Reply
Thread Tools

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

 
 
sam alton
Guest
Posts: n/a
 
      09-15-2010
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.
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      09-15-2010
On 9/14/2010 7:34 PM, sam alton wrote:
> 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-lit...t.html#faq-5.2

 
Reply With Quote
 
 
 
 
Garrett Hartshaw
Guest
Posts: n/a
 
      09-15-2010
On 09/14/2010 10:34 PM, sam alton wrote:
> 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.
 
Reply With Quote
 
sam alton
Guest
Posts: n/a
 
      09-15-2010
On Sep 14, 10:13*pm, Garrett Hartshaw <(E-Mail Removed)> wrote:
> On 09/14/2010 10:34 PM, sam alton wrote:
>
> > 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?
 
Reply With Quote
 
Saeed Amrollahi
Guest
Posts: n/a
 
      09-15-2010
On Sep 15, 5:34*am, sam alton <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
Garrett Hartshaw
Guest
Posts: n/a
 
      09-15-2010
>> 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?


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
 
Reply With Quote
 
Seungbeom Kim
Guest
Posts: n/a
 
      09-16-2010
On 2010-09-15 02:00, Saeed Amrollahi wrote:
> 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.

--
Seungbeom Kim
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
Help in writting a program razanrizk@gmail.com C Programming 0 09-24-2008 12:20 PM
Coordinate of a point in 3 dimensions Piotre Ugrumov C++ 8 01-21-2004 04:09 AM



Advertisments