disaste0_0 2023-04-14 21:16 采纳率: 81.6%
浏览 56
已结题

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 <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文件判断后的数据

  • 写回答

3条回答 默认 最新

  • 「已注销」 2023-04-15 00:16
    关注

    修改后的代码如下:

    
    #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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月23日
  • 已采纳回答 4月15日
  • 赞助了问题酬金15元 4月14日
  • 创建了问题 4月14日

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)