#include <stdio.h> #include <string.h> #include <conio.h> #include <math.h> #include <stdlib.h> const char NUM_CHARSET[] = ".0123456789"; int getnumber(int &iNum, double &dNum) { int ch, i = 0, pos = 0; char num[16]; while ((i<15)&&((ch = getchar())!=10)) { num[i++] = ch; } num[i] = '\0'; if ((pos = strspn(num, NUM_CHARSET)) != strlen(num)) { return -1; } else { if (strchr(num, '.') != 0) { dNum = atof(num); return 0; } else { iNum = atoi(num); return 1; } } } int main(int argc, char* argv[]) { int i, j, result, iNum, n = 0, *max_i, *max_j, nmax = -1; double *x, *y, dNum, max = 0.0f, distance = 0.0f; printf("Please enter interger number of points (1-100000): "); if ((result = getnumber(iNum, dNum)) == -1) { printf("Wrong number of points: "); return 1; } if (result) n = iNum; else n = (int)dNum; if ((n == 0) || (n > 100000)){ printf("Wrong number of points: "); return 1; } printf("The number of points is %u\n", n); x = new double[n]; y = new double[n]; max_i = new int[n]; max_j = new int[n]; printf("Please enter points coordinates down follow (like 40.04 or 40): \n"); for (i=0; i<n; i++) { do { printf("X%d = ", i); } while ((result = getnumber(iNum, dNum)) == -1); if (result) x[i] = (double)iNum; else x[i] = dNum; printf("X%d = %.3f\n", i, x[i]); do { printf("Y%d = ", i); } while ((result = getnumber(iNum, dNum)) == -1); if (result) y[i] = (double)iNum; else y[i] = dNum; printf("Y%d = %.3f\n\n", i, y[i]); } for (i=0; i<n; i++) { for (j=(i+1); j<n; j++) { distance = sqrt((x[j] - x[i])*(x[j] - x[i]) + (y[j] - y[i])*(y[j] - y[i])); if (max == distance) { nmax++; max_i[nmax] = i; max_j[nmax] = j; } else if (max < distance) { max = distance; nmax = 0; max_i[nmax] = i; max_j[nmax] = j; } } } if (nmax >= 0) { printf("Maximum distance is %f\n", max); printf("Points coordinates are :\n"); for (i=0; i < (nmax+1); i++) printf("{%.3f, %.3f} - {%.3f, %.3f}\n", x[max_i[i]], y[max_i[i]], x[max_j[i]], y[max_j[i]]); } getchar(); getchar(); delete [] x; delete [] y; delete [] max_i; delete [] max_j; return 0; } |