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
|
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
double sarax = rand() % (5-0+1), saray = rand() % (5-0+1);
double calDistance(double, double, double, double);
double evaluate(double, double, double);
int main()
{
srand(time(0));
double x1 = rand() ,
y1 = rand() ,
x2 = rand() ,
y2 = rand() ,
x3 = rand() ,
y3 = rand() ;
double distance1 = 0, distance2 = 0, distance3 = 0;
distance1 = calDistance(sarax, saray, x1, y1) ;
distance2 = calDistance(sarax, saray, x2, y2) ;
distance3 = calDistance(sarax, saray, x3, y3) ;
double close = evaluate(distance1, distance2, distance3);
cout << distance1 << endl;
cout << distance2 << endl;
cout << distance3 << endl;
cout << "the closest supermarket is " << close << "m away" << endl;
return 0;
}
double calDistance(double x1, double y1, double x2, double y2)
{
double x = pow(x2, 2) - pow(x1, 2);
double y = pow(y2, 2) - pow(y1, 2);
double answer = sqrt(x) + sqrt(y);
return answer;
}
double evaluate(double dis1, double dis2, double dis3)
{
double close = dis1;
if (dis2 < dis1)
{
close = dis2;
if(dis3<dis2)
{
close = dis3;
}
}
if(dis3<dis2)
{
close = dis3;
{
if(dis2<dis3)
{
close = dis2;
}
}
}
return close;
}
|