000000组合_1

// Exercise 9.12 Solution: Point.h
#ifndef POINT_H
#define POINT_H

class Point 
{
public:
   Point( double = 0.0, double = 0.0 ); // default constructor
   
   // set and get functions
   void setX( double );
   void setY( double );
   double getX(); 
   double getY(); 
private:
   double x; // 0.0 <= x <= 20.0
   double y; // 0.0 <= y <= 20.0
}; // end class Point 

#endif
// Exercise 9.12 Solution: Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H

#include "Point.h" // include definition of class Point

class Rectangle 
{
public:
   // default constructor
   Rectangle( Point = Point( 0.0, 1.0 ), Point = Point( 1.0, 1.0 ),
      Point = Point( 1.0, 0.0 ), Point = Point( 0.0, 0.0 ) );  
   
   // sets x, y, x2, y2 coordinates
   void setCoord( Point, Point, Point, Point );
   double length(); // length
   double width(); // width
   void perimeter(); // perimeter
   void area(); // area
   bool square(); // square
private:
   Point point1; 
   Point point2; 
   Point point3; 
   Point point4; 
}; // end class Rectangle 

#endif

// Exercise 9.12 Solution: Point.cpp
// Member-function definitions for class Point.
#include <stdexcept>
#include "Point.h" // include definition of class Point
using namespace std;

Point::Point( double xCoord, double yCoord )
{
   setX( xCoord ); // invoke function setX
   setY( yCoord ); // invoke function setY
} // end Point constructor

// set x coordinate
void Point::setX( double xCoord )
{
   if ( xCoord >= 0.0 && xCoord <= 20.0 )
      x = xCoord;
   else
      throw invalid_argument( "x must be >= 0.0 and <= 20.0" );
} // end function setX

// set y coordinate
void Point::setY( double yCoord )
{
   if ( yCoord >= 0.0 && yCoord <= 20.0 ) 
      y = yCoord;
   else
      throw invalid_argument( "y must be >= 0.0 and <= 20.0" );
} // end function setY

// return x coordinate
double Point::getX()
{
   return x;
} // end function getX

// return y coordinate
double Point::getY()
{
   return y;
} // end function getY

// Exercise 9.12 Solution: Rectangle.cpp
// Member-function definitions for class Rectangle.
#include <iostream> 
#include <iomanip> 
#include <cmath>
#include <stdexcept>
#include "Rectangle.h" // include definition of class Rectangle
using namespace std;

Rectangle::Rectangle( Point a, Point b, Point c, Point d )
{
   setCoord( a, b, c, d ); // invokes function setCoord
} // end Rectangle constructor

void Rectangle::setCoord( Point p1, Point p2, Point p3, Point p4 )
{//使用成员对象来初始化
   // Arrangement of points
   // p4.........p3
   //  .         .
   //  .         .
   // p1.........p2

   // verify that points form a rectangle
   if ( ( p1.getY() == p2.getY() && p1.getX() == p4.getX()
      && p2.getX() == p3.getX() && p3.getY() == p4.getY() ) ) 
   {
      point1 = p1;
      point2 = p2;
      point3 = p3;
      point4 = p4;
   } // end if
   else
      throw invalid_argument( 
         "Coordinates do not form a rectangle!\n" );
} // end function setCoord

double Rectangle::length()
{
   double side1 = fabs( point4.getY() - point1.getY() ); // get side1
   double side2 = fabs( point2.getX() - point1.getX() ); // get side2
   double length = ( side1 > side2 ? side1 : side2 );
   return length;
} // end function length

double Rectangle::width()
{
   double side1 = fabs( point4.getY() - point1.getY() ); // get side1
   double side2 = fabs( point2.getX() - point1.getX() ); // get side2
   double width = ( side1 < side2 ? side1 : side2 );
   return width;
} // end function width

void Rectangle::perimeter()
{
   cout << fixed << "\nThe perimeter is: " << setprecision( 1 )
        << 2 * ( length() + width() ) << endl; 
} // end function perimeter

void Rectangle::area()
{
   cout << fixed << "The area is: " << setprecision( 1 ) 
      << length() * width() << endl;
} // end function area

bool Rectangle::square() 
{
   return length() == width();
} // end function square
/*1、使用成员对象来初始化*/

// Exercise 9.12 Solution: Ex09_12.cpp
#include <iostream>
#include <stdexcept>
#include "Rectangle.h" // include definition of class Rectangle
using namespace std;

int main()
{
   Point w( 1.0, 1.0 );  
   Point x( 5.0, 1.0 );
   Point y( 5.0, 3.0 );  
   Point z( 1.0, 3.0 );
   Point j( 0.0, 0.0 ); 
   Point k( 1.0, 0.0 );
   Point m( 1.0, 1.0 );  
   Point n( 0.0, 1.0 );

   Rectangle r1( z, y, x, w );
   cout << "Rectangle 1:\n";
   cout << "length = " << r1.length();
   cout << "\nwidth = " << r1.width();
   r1.perimeter();
   r1.area();
   cout << "The rectangle " 
      << ( r1.square() ? "is" : "is not" )
      << " a square.\n";

   Rectangle r2( j, k, m, n );
   cout << "\nRectangle 2:\n";
   cout << "length = " << r2.length();
   cout << "\nwidth = " << r2.width();
   r2.perimeter();
   r2.area();
   cout << "The rectangle " 
      << ( r2.square() ? "is" : "is not" )
      << " a square.\n";

   try
   {
      Rectangle r3( w, x, m, n );
   }
   catch ( invalid_argument &e )
   {
      cout << "\nNot a valid Rectangle" << endl;
   }

   try
   {
      Point v( 99.0, -2.3 );
   }
   catch ( invalid_argument &e )
   {
      cout << "Not a valid Point" << endl;
   }
} // end main

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值