disaste0_0 2023-04-13 08:32 采纳率: 81.6%
浏览 131
已结题

C++代码实现,已有大部分代码

这是头文件
#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"CLASSIFIER.h"



using namespace std;

int main() {
    vector<CLASSIFIER> trainingData;
    ifstream file("trainingData.txt");
    if (!file.is_open()) {
        cerr << "Error: Could not open file" << 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();

    cout << "Choose a classifier: " << endl;
    cout << "1. DummyClassifier" << endl;
    cout << "2. Nearest Neighbor (NN) Classifier" << endl;
    cout << "3. k-NN Classifier" << endl;
    int choice;
    cin >> choice;

    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 phone orientation is: " << orientation << endl;
        if (z < 0)
        {
            cout << "face up";
        }
        if (z > 0)
        {
            cout << "face down";
        }
        if (y < 0)
        {
            cout << "face portrail";
        }
        if (y > 0)
        {
            cout << "portrail upside down";
        }
        if (x < 0)
        {
            cout << "landscope left";
        }
        if (x > 0)
        {
            cout << "landscope right";
        }
    }
    else if (choice == 3) {
        printKNNClassifier();
    }
    else {
        cout << "Invalid choice" << endl;
    }

    return 0;
}

这是要求

img

  • 写回答

3条回答 默认 最新

  • 「已注销」 2023-04-13 09:25
    关注

    引用new bing部分回答作答:
    以下是根据您提供的代码修改后的程序,可以让用户输入数据文件名,并将结果输出到一个名为result.txt的文件中:

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include "CLASSIFIER.h"
    
    using namespace std;
    
    int main() {
        vector<CLASSIFIER> trainingData;
        string fileName;
        cout << "Enter the name of the data file: ";
        cin >> fileName;
        ifstream file(fileName);
        if (!file.is_open()) {
            cerr << "Error: Could not open file" << 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();
    
        cout << "Choose a classifier: " << endl;
        cout << "1. DummyClassifier" << endl;
        cout << "2. Nearest Neighbor (NN) Classifier" << endl;
        cout << "3. k-NN Classifier" << endl;
        int choice;
        cin >> choice;
    
        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 phone orientation is: " << orientation << endl;
            if (z < 0)
            {
                cout << "face up";
            }
            if (z > 0)
            {
                cout << "face down";
            }
            if (y < 0)
            {
                cout << "face portrail";
            }
            if (y > 0)
            {
                cout << "portrail upside down";
            }
            if (x < 0)
            {
                cout << "landscope left";
            }
            if (x > 0)
            {
                cout << "landscope right";
            }
        }
        else if (choice == 3) {
            printKNNClassifier();
        }
        else {
            cout << "Invalid choice" << endl;
        }
        system("pause");
        return 0;
    }
    

    此程序会提示用户输入一个数据文件名,然后会打开该文件并读取其中的数据。然后,它会将每个数据点传递给nearestNeighbor函数,并将结果写入一个名为result.txt的文件中。

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

报告相同问题?

问题事件

  • 系统已结题 4月21日
  • 已采纳回答 4月13日
  • 修改了问题 4月13日
  • 创建了问题 4月13日

悬赏问题

  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了