这个是要求
这个是名叫unknowData文件的数据
-0.14139,-0.04982,-0.9887
-0.78343,0.03778,-0.62033
-0.10727,-0.45019,-0.88647
0.27052,0.89044,-0.36596
0.02577,-0.94375,-0.32964
0.04292,0.77025,-0.63629
0.34953,-0.21364,-0.91224
0.33303,-0.22032,-0.91682
-0.02345,-0.0008,0.99972
-0.93624,0.05372,-0.34722
0.98704,-0.07778,0.14034
-0.17921,0.06468,0.98168
-0.97292,-0.0459,-0.22655
-0.94747,0.11368,-0.29897
-0.00969,0.14051,0.99003
0.03503,-0.9617,0.27185
0.0281,-0.99724,-0.06873
0.04918,0.99218,0.11471
0.99109,-0.09806,0.09011
0.07826,0.99649,0.02972
-0.06071,0.03074,-0.99768
0.99468,-0.09528,-0.03905
-0.92184,0.06845,-0.38149
在最后的文件输出要像类似于这种
这是头文件
#pragma once
#include <vector>
#include <string>
using namespace std;
class CLASSIFIER
{
public:
double x, y, z;
string orientation;
public:
CLASSIFIER(double x = 0.0, double y = 0.0, double z = 0.0, string orientation = "") {
this->x = x;
this->y = y;
this->z = z;
this->orientation = orientation;
}
double getX() {
return x;
}
double getY() {
return y;
}
double getz() {
return y;
}
string getorientation() {
return orientation;
}
};
void printDummyClassifier();
void printKNNClassifier();
double euclideanDistance(const CLASSIFIER& c1, const CLASSIFIER& c2);
string nearestNeighbor(const CLASSIFIER& classifier, const vector<CLASSIFIER>& trainingData);
这是cpp文件
#include "CLASSIFIER.h"
#include <iostream>
#include <limits>
#include <cmath>
using namespace std;
void printDummyClassifier() {
cout << "DummyClassifier is not implemented yet" << endl;
}
void printKNNClassifier() {
cout << "printKNNClassifier is not implemented yet" << endl;
}
double euclideanDistance(const CLASSIFIER& c1, const CLASSIFIER& c2) {
double dx = c1.x - c2.x;
double dy = c1.y - c2.y;
double dz = c1.z - c2.z;
return sqrt(dx * dx + dy * dy + dz * dz);
}
string nearestNeighbor(const CLASSIFIER& classifier, const vector<CLASSIFIER>& trainingData) {
string orientation;
double minDistance = numeric_limits<double>::max();
for (const CLASSIFIER& c : trainingData) {
double distance = euclideanDistance(classifier, c);
if (distance < minDistance) {
minDistance = distance;
orientation = c.orientation;
}
}
return orientation;
这是main 文件
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <limits>
#include <cmath>
#include "CLASSIFIER.h"
using namespace std;
int main() {
vector<CLASSIFIER> trainingData;
ifstream file;
string filename;
cout << "Choose an option: " << endl;
cout << "1. Print dummy classifier" << endl;
cout << "2. Determine phone orientation (NearestNeighbor)" << endl;
cout << "3. Print k-NN classifier" << endl;
cout << "4. Read data from file(nearestNeighbor)" << endl;
int choice;
cin >> choice;
ofstream outfile("result.txt");
if (!outfile.is_open()) {
cerr << "Error: Could not open file result.txt" << endl;
return 1;
}
if (choice == 1) {
printDummyClassifier();
}
else if (choice == 2) {
cout << "Enter data (x, y, z): ";
double x, y, z;
cin >> x >> y >> z;
CLASSIFIER classifier = { x, y, z };
string orientation = nearestNeighbor(classifier, trainingData);
cout << "The corresponding phone orientation is: " << orientation << endl;
int label_as_a_number;
if (z < 0)
{
label_as_a_number = 1;
cout << "face up" << endl;
}
else if (z > 0)
{
label_as_a_number = 2;
cout << "face down" << endl;
}
else if (y < 0)
{
label_as_a_number = 3;
cout << "face portrail" << endl;
}
else if (y > 0)
{
label_as_a_number = 4;
cout << "portrail upside down" << endl;
}
else if (x < 0)
{
label_as_a_number = 5;
cout << "landscope left" << endl;
}
else if (x > 0)
{
label_as_a_number = 6;
cout << "landscope right" << endl;
}
outfile << x << "," << y << "," << z << "," << orientation << "," << label_as_a_number << endl;
}
else if (choice == 3) {
printKNNClassifier();
}
else if (choice == 4) {
cout << "Enter data file name: ";
cin >> filename;
file.open(filename.c_str());
if (!file.is_open()) {
cerr << "Error: Could not open file " << filename << endl;
return 1;
}
double x, y, z;
string orientation;
while (file >> x >> y >> z >> orientation) {
CLASSIFIER classifier = { x, y, z, orientation };
trainingData.push_back(classifier);
}
file.close();
}
else {
cout << "Invalid choice" << endl;
}
outfile.close();
return 0;
}
现在的问题是,有叫result的文件但文件里面的内容是选项2用户输入的内容,不是名叫unknowData文件判断后的数据