rendered paste body#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
using namespace std;
struct POINT {
double x;
double y;
};
struct POINT g_coord(void);
struct POINT m_point(struct POINT a, struct POINT b);
double d_apart(struct POINT a, struct POINT b);
void p_results(struct POINT a, struct POINT b);
void p_point(struct POINT z);
void p_welcome(void);
void p_end(void);
int main(void) {
POINT p_one, p_two;
char garbage[21];
bool repeat = true;
p_welcome();
while(repeat) {
cout << "Where is your first point? ";
p_one = g_coord();
cout << "Where is your second point? ";
p_two = g_coord();
cout << endl << "Thank you!! Calculating... Done." << endl << endl;
p_results(p_one, p_two);
cout << "Would you like to keep going? (Y/N) ";
cin.getline(garbage, 20);
if((garbage[0] == 'n') || (garbage[0] == 'N')) {
repeat = false;
}
}
p_end();
system("pause");
return 0;
}
struct POINT g_coord(void) {
POINT z;
char coord[256], *end;
bool digit = false;
cin.getline(coord, 256);
if(coord[0] == '(') {
z.x = strtod(&(coord[1]), &end);
}
else {
z.x = strtod(coord, &end);
}
int length = (int)strlen(coord);
for(int i = 0; i < length; i++) {
if((*(end + i) == '-') || isdigit((*(end + i)))) {
z.y = strtod(end + i, &end);
i = length + 1;
}
}
return z;
}
double d_apart(struct POINT a, struct POINT b) {
return sqrt(pow((b.x - a.x), 2) + pow((b.y - a.y), 2));
}
struct POINT m_point(struct POINT a, struct POINT b) {
POINT z;
z.x = ((a.x + b.x)/2);
z.y = ((a.y + b.y)/2);
return z;
}
void p_point(struct POINT a) {
cout << "(" << a.x << ", " << a.y << ")";
}
void p_welcome(void) {
cout << "Welcome to the 2D Point Program!!!" << endl << endl;
}
void p_end(void) {
cout << endl << "Thank you for using the 2PP!!" << endl;
}
void p_results(struct POINT a, struct POINT b) {
POINT p_one = a;
POINT p_two = b;
p_point(p_one); cout << " is " << d_apart(p_one, p_two) << " units away from " << endl;
p_point(p_two); cout << "." << endl;
cout << "The midpoint of the line segment from "; p_point(p_one); cout << " to " << endl;
p_point(p_two); cout << " is "; p_point(m_point(p_one, p_two)); cout << "." << endl << endl;
}