-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsales_man.cpp
More file actions
110 lines (94 loc) · 2.03 KB
/
sales_man.cpp
File metadata and controls
110 lines (94 loc) · 2.03 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<iostream>
#include<string.h>
#include<cmath>
#define SIZE 1000
using namespace std;
struct Point {
float x;
float y;
};
class SalesMan {
private:
char name[31];
Point pth_followed[SIZE];
int total_points;
int compare_points(Point p1, Point p2, int choice=0){
if(choice == 0){
return p1.x > p2.x;
}
return p1.y > p2.y;
}
double distance(Point p1, Point p2){
double delta_x = p1.x - p2.x;
double delta_y = p1.y - p2.y;
return sqrt(delta_x * delta_x + delta_y * delta_y);
}
public:
SalesMan(char* n, Point point_arr[], int size){
strcpy(name, n);
total_points = size;
for(int i = 0; i < total_points; i++){
pth_followed[i] = point_arr[i];
}
}
void sort_using_dimension(int choice=0){
Point temp;
for(int i = 1; i < total_points; i++){
for(int j = 0; j < total_points-i; j++){
if(compare_points(pth_followed[j], pth_followed[j+1], choice)){
temp = pth_followed[j];
pth_followed[j] = pth_followed[j+1];
pth_followed[j+1] = temp;
}
}
}
}
void sort_using_distance_from_point(Point p){
Point temp;
for(int i = 1; i < total_points; i++){
for(int j = 0; j < total_points - i; j++){
if(distance(pth_followed[j], p) >
distance(pth_followed[j+1], p)){
temp = pth_followed[j];
pth_followed[j] = pth_followed[j+1];
pth_followed[j+1] = temp;
}
}
}
}
void display_path(){
cout<<"Path Followed is: "<<endl;
for(int i = 0; i < total_points; i++){
cout<<"->";
cout<<"("<<pth_followed[i].x<<", "<<pth_followed[i].y<<")";
}
}
};
int main(){
char name[31];
cout<<"Name: ";
cin.getline(name, sizeof(name));
Point p1[5];
for(int i = 0; i < 5; i++){
cout<<"Input Point "<<i+1<<endl;
cout<<"x: ";
cin>>p1[i].x;
cout<<"y: ";
cin>>p1[i].y;
cout<<endl;
}
SalesMan s(name, p1, 5);
s.display_path();
s.sort_using_dimension();
cout<<endl;
s.display_path();
s.sort_using_dimension(1);
cout<<endl;
s.display_path();
Point p;
p.x = 2;
p.y = 5;
s.sort_using_distance_from_point(p);
cout<<endl;
s.display_path();
}