1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;
// Global area for defining functions and C++ classes
class OrderedPair{
public:
OrderedPair(OrderedPair&, OrderedPair&);
OrderedPair(double,double);
void setA(double, double);
void setB(double, double);
double getA();
double getB();
double distance(const OrderedPair&, const OrderedPair&);
double slope(const OrderedPair&, const OrderedPair&);
private:
double x, y, x1, y1;
};
OrderedPair::OrderedPair(double x, double y)
{}
OrderedPair::OrderedPair(OrderedPair&A, OrderedPair&B)
{x==A.x; y==A.y; x1==B.x; y1=B.y;}
void OrderedPair::setA(double a, double b){x==a; y=b;}
void OrderedPair::setB(double c, double d){x1==c; y1==d;}
double OrderedPair::getA(){return x; return y;}
double OrderedPair::getB(){return x1; return y1;}
double OrderedPair::distance(const OrderedPair&A, const OrderedPair&B)
{return sqrt (pow(B.x-A.x, 2)+pow(B.y-A.y, 2));}
double OrderedPair::slope(const OrderedPair&A, const OrderedPair&B)
{return ((B.y-A.y)/(B.x-A.x));}
int main () {
double a, b, c, d;
OrderedPair A(a, b);
OrderedPair B(c, d);
cout << "Enter the first set of ordered pair." << endl;
cin >> a >> b;
cout << "Enter the second set of ordered pair." << endl;
cin >> c >> d;
if (!cin.eof()&&cin.good()){
if (c-a==0){
A.setA(a, b);
B.setB(c, d);
cout << A.getA() << endl;
cout << "The distance between the two points is " << A.distance(A, B) << "." << endl;
cout << "The slope cannot be calculated because of division by 0." << endl;
}
else{
A.setA(a, b);
B.setB(c, d);
cout << "The distance between the two points is " << A.distance(A, B) << "." << endl;
cout << "The slope of the line that joins the two points is " << A.slope(A, B) << "." << endl;
}
}
else
cout << "The data entered is invalid" << endl;
return EXIT_SUCCESS;
}
|