c++读取文件数据再转换成新的文件输出有偿

这个是要求

img

这个是名叫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
在最后的文件输出要像类似于这种

img

这是头文件
#pragma once
#include 
#include 
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& trainingData);
这是cpp文件
#include "CLASSIFIER.h"
#include 
#include 
#include 
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& 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 
#include 
#include 
#include 
#include 
#include 
#include "CLASSIFIER.h"
using namespace std;


int main() {
    vector 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文件判断后的数据

修改后的代码如下:


#include "CLASSIFIER.h"
#include <limits>
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <limits>
#include <cmath>
#include "CLASSIFIER.h"
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;
}
 
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;
            }

            if (label_as_a_number == 1) {
                outfile << x << "," << y << "," << z << "," << label_as_a_number << "," << "face up" << endl;
            }
            if (label_as_a_number == 2) {
                outfile << x << "," << y << "," << z << "," << label_as_a_number << "," << "face down" << endl;
            }
            if (label_as_a_number == 3) {
                outfile << x << "," << y << "," << z << "," << label_as_a_number << "," << "face portrail" << endl;
            }
            if (label_as_a_number == 4) {
                outfile << x << "," << y << "," << z << "," << label_as_a_number << "," << "portrail upside down" << endl;
            }
            if (label_as_a_number == 5) {
                outfile << x << "," << y << "," << z << "," << label_as_a_number << "," << "landscope left" << endl;
            }
            if (label_as_a_number == 6) {
                outfile << x << "," << y << "," << z << "," << label_as_a_number << "," << "landscope right" << 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 ) {
                CLASSIFIER classifier = { x, y, z };
                trainingData.push_back(classifier);
            }
            file.close();
 
            // 以下是添加的代码
            for (const CLASSIFIER& c : trainingData) {
                double x = c.x;
                double y = c.y;
                double z = c.z;
                string orientation = nearestNeighbor(c, trainingData);

                int label_as_a_number;
                if (z < 0)
                {
                    label_as_a_number = 1;
                }
                else if (z > 0)
                {
                    label_as_a_number = 2;
                }
                else if (y < 0)
                {
                    label_as_a_number = 3;
                }
                else if (y > 0)
                {
                    label_as_a_number = 4;
                }
                else if (x < 0)
                {
                    label_as_a_number = 5;
                }
                else if (x > 0)
                {
                    label_as_a_number = 6;
                }
                if (label_as_a_number == 1) {
                    outfile << x << "," << y << "," << z << "," << label_as_a_number << "," << "face up" << endl;
                }
                if(label_as_a_number == 2) {
                    outfile << x << "," << y << "," << z << "," << label_as_a_number << "," << "face down" << endl;
                }
                if (label_as_a_number == 3) {
                    outfile << x << "," << y << "," << z << "," << label_as_a_number << "," << "face portrail" << endl;
                }
                if (label_as_a_number == 4) {
                    outfile << x << "," << y << "," << z << "," << label_as_a_number << "," << "portrail upside down" << endl;
                }
                if (label_as_a_number == 5) {
                    outfile << x << "," << y << "," << z << "," << label_as_a_number << "," << "landscope left" << endl;
                }
                if (label_as_a_number == 6) {
                    outfile << x << "," << y << "," << z << "," << label_as_a_number << "," << "landscope right" << endl;
                }
            }
            outfile.close();
        }

        else {
            cout << "Invalid choice" << endl;
        }

        outfile.close();
        
        return 0;
    }

运行结果如下:

img

以下内容引用CHATGPT:

首先,需要读取名为unknowData的文件中的数据,并将其转换为CLASSIFIER类型的向量。可以使用类似以下代码的方式实现:

ifstream infile("unknowData.txt");
if (!infile.is_open()) {
    cerr << "Error: Could not open file unknowData.txt" << endl;
    return 1;
}

double x, y, z;
vector<CLASSIFIER> trainingData;
while (infile >> x >> y >> z) {
    CLASSIFIER classifier = {x, y, z};
    string orientation = nearestNeighbor(classifier, trainingData);
    int label_as_a_number;
    if (z < 0) {
        label_as_a_number = 1;
        orientation = "face up";
    } else if (z > 0) {
        label_as_a_number = 2;
        orientation = "face down";
    } else if (y < 0) {
        label_as_a_number = 3;
        orientation = "face portrail";
    } else if (y > 0) {
        label_as_a_number = 4;
        orientation = "portrail upside down";
    } else if (x < 0) {
        label_as_a_number = 5;
        orientation = "landscope left";
    } else if (x > 0) {
        label_as_a_number = 6;
        orientation = "landscope right";
    }
    trainingData.push_back({x, y, z, orientation});
    outfile << x << "," << y << "," << z << "," << orientation << "," << label_as_a_number << endl;
}
infile.close();

其中,先打开名为unknowData.txt的文件,然后读取其中的数据,将其转换为CLASSIFIER类型的向量。在转换的过程中,需要通过nearestNeighbor函数计算出每个CLASSIFIER数据点的方向。另外,需要根据z、y、x值的大小判断出数据点的方向,并用对应的字符串表示。

最后,将转换后的数据输出到result.txt文件中。完整的代码如下:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <limits>
#include <cmath>
#include "CLASSIFIER.h"
using namespace std;

int main() {
    ofstream outfile("result.txt");
    if (!outfile.is_open()) {
        cerr << "Error: Could not open file result.txt" << endl;
        return 1;
    }

    vector<CLASSIFIER> trainingData;

    ifstream infile("unknowData.txt");
    if (!infile.is_open()) {
        cerr << "Error: Could not open file unknowData.txt" << endl;
        return 1;
    }

    double x, y, z;
    while (infile >> x >> y >> z) {
        CLASSIFIER classifier = {x, y, z};
        string orientation = nearestNeighbor(classifier, trainingData);
        int label_as_a_number;
        if (z < 0) {
            label_as_a_number = 1;
            orientation = "face up";
        } else if (z > 0) {
            label_as_a_number = 2;
            orientation = "face down";
        } else if (y < 0) {
            label_as_a_number = 3;
            orientation = "face portrail";
        } else if (y > 0) {
            label_as_a_number = 4;
            orientation = "portrail upside down";
        } else if (x < 0) {
            label_as_a_number = 5;
            orientation = "landscope left";
        } else if (x > 0) {
            label_as_a_number = 6;
            orientation = "landscope right";
        }
        trainingData.push_back({x, y, z, orientation});
        outfile << x << "," << y << "," << z << "," << orientation << "," << label_as_a_number << endl;
    }
    infile.close();
    outfile.close();

    return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^