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

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 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注