-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
200 lines (164 loc) · 3.3 KB
/
main.cpp
File metadata and controls
200 lines (164 loc) · 3.3 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Author David Robison
// Date 4/20/2013
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
float Ax[100], Ay[100];
int Acount = 0;
float Bx[100], By[100];
int Bcount = 0;
float Ix[100], Iy[100];
int Icount = 0;
int correctClass[100];
int correct, incorrect, aCorrect, bCorrect;
ifstream infile;
ofstream outfile;
float calculateDistance(float x, float y, float x1, float y1);
void knn( float x, float y, int correctClass, int k);
void resetCounters();
void printResults();
int main()
{
float x, y;
correct, incorrect = 0;
string classA = "1.txt";
string classB = "2.txt";
string input = "input.txt";
string output = "output.txt";
// Read in Class A
infile.open(classA.c_str());
if (!infile)
{
cout << "Unable to open class A"<< endl;
}
while(!infile.eof())
{
infile >> Ax[Acount] >> Ay[Acount];
Acount++;
}
infile.close();
// Read in Class B
infile.open(classB.c_str());
if (!infile)
{
cout << "Unable to open class B"<< endl;
}
while(!infile.eof())
{
infile >> Bx[Bcount] >> By[Bcount];
Bcount++;
}
infile.close();
// Open output
outfile.open(output.c_str());
// Open input
infile.open(input.c_str());
if (!infile)
{
cout << "Unable to open output"<< endl;
}
while(!infile.eof())
{
infile >> Ix[Icount] >> Iy[Icount] >> correctClass[Icount];
Icount++;
}
// K = 1
resetCounters();
for( int i=0; i<Icount; i++)
{
knn(Ix[i], Iy[i], correctClass[i], 1);
}
outfile << "k = 1" << endl;
printResults();
// K = 3
resetCounters();
for( int i=0; i<Icount; i++)
{
knn(Ix[i], Iy[i], correctClass[i], 3);
}
outfile << "k = 3" << endl;
printResults();
// K = 5
resetCounters();
for( int i=0; i<Icount; i++)
{
knn(Ix[i], Iy[i], correctClass[i], 5);
}
outfile << "k = 5" << endl;
printResults();
// K = 7
resetCounters();
for( int i=0; i<Icount; i++)
{
knn(Ix[i], Iy[i], correctClass[i], 7);
}
outfile << "k = 7" << endl;
printResults();
infile.close();
outfile.close();
}
float calculateDistance(float x, float y, float x1, float y1)
{
float part1 = (x - x1) * (x - x1);
float part2 = (y - y1) * (y - y1);
float answer = sqrt(part1+part2);
return answer;
}
void knn( float x, float y, int correctClass, int k)
{
int classifiedAs=1;
float smallA[100];
float smallB[100];
fill_n(smallA, 100, 100);
fill_n(smallB, 100, 100);
for( int i=0; i<Acount; i++)
{
smallA[i] = calculateDistance(x, y, Ax[i], Ay[i]);
}
for( int i=0; i<Bcount; i++)
{
smallB[i] = calculateDistance(x, y, Bx[i], By[i]);
}
sort(begin(smallA), end(smallA));
sort(begin(smallB), end(smallB));
int currentA = 0;
int currentB = 0;
while (currentA+currentB < k )
{
if( smallA[currentA] < smallB[currentB] )
currentA++;
else
currentB++;
}
if( currentA > currentB)
classifiedAs = 1;
else
classifiedAs = 2;
if(correctClass == classifiedAs)
{
correct++;
if( correctClass == 1 )
aCorrect++;
else
bCorrect++;
}
else
incorrect++;
}
void resetCounters()
{
correct = 0;
incorrect = 0;
aCorrect = 0;
bCorrect = 0;
}
void printResults()
{
outfile << "Total Correct - " << correct << endl;
outfile << "Total Incorrect - " << incorrect << endl;
outfile << "Class 1 Correct - " << aCorrect << endl;
outfile << "Class 2 Correct - " << bCorrect << endl;
outfile << endl;
}