📄 sort.cpp
字号:
// Sort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Sort.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include<fstream>
#include<vector>
#include<math.h>
#include<string>
// The one and only application object
CWinApp theApp;
using namespace std;
struct Iris
{
double Attribute[4];//SepalLength,SepalWidth,PetalLength,PetalWidth
string ClassName;
};
const int K = 20;
double IrisDistance(Iris a, Iris b)
{
double d = 0;
for (int i = 0; i < 4; i++)
d += pow(a.Attribute[i] - b.Attribute[i],2);
return d;
}
void OutPut(int c1, int c2, int c3, Iris TestData)
{
string result;
if (c1 >= c2 && c1 >= c3)
result = "Iris-setosa";
else if (c2 >= c1 && c2 >= c3)
result = "Iris-versicolor";
else
result = "Iris-virginica";
cout<<TestData.Attribute[0]<<'\t'<<TestData.Attribute[1]<<'\t'<<TestData.Attribute[2]<<'\t'
<<TestData.Attribute[3]<<'\t'<<TestData.ClassName<<'\t'<<"Sort:"<<result<<'\t';
if (result == TestData.ClassName)
cout<<"T";
else
cout<<"F";
cout<<endl;
}
void Sort(vector<Iris> DataSet, Iris TestData)
{
vector<int> KPos;//K个训练数据的数组下标号
for (int i = 0; i != K ;i++)
KPos.push_back(i);//初始化
for (vector<int>::size_type i = K; i != DataSet.size() ; i++)//遍历其他训练数据
{
double m_distance = IrisDistance(DataSet[i], TestData);
int m_MaxDistancePos = KPos[0],m_MaxDistanceKPos = 0;
double m_MaxDistance = IrisDistance(DataSet[m_MaxDistancePos], TestData);
for (vector<int>::size_type j = 1; j != KPos.size();j++)
{
if (m_MaxDistance <= IrisDistance(DataSet[KPos[j]], TestData))
{
m_MaxDistancePos = KPos[j];
m_MaxDistanceKPos = j;
}
}
if (m_distance <= IrisDistance(DataSet[m_MaxDistancePos], TestData))
KPos[m_MaxDistanceKPos] = i;
}
int countSetosa = 0, countVersicolor = 0, countVirginica = 0;
for (vector<int>::size_type i = 0; i != KPos.size(); i++)
{
if (DataSet[KPos[i]].ClassName == "Iris-setosa")
countSetosa++;
else if (DataSet[KPos[i]].ClassName == "Iris-versicolor")
countVersicolor++;
else
countVirginica++;
}
OutPut(countSetosa, countVersicolor, countVirginica, TestData);
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
/**********************************读入训练数据**********************************/
ifstream infileDataSet;
infileDataSet.open("iris.data");
vector<Iris> DataSet;//训练数据
Iris Data;
int i = 0;//i记录对应的属性位置
char ch, chPrior = '1';//ch为读入的字符
double m_Attribute = 0;//将读入的字符转换为double型数据
while(infileDataSet.get(ch))//读入字符
{
if (ch != '.')
{
if ('0' <= ch && ch <= '9')
{
if (chPrior != '.')
m_Attribute = m_Attribute*10 + (ch-48);
else
m_Attribute += 0.1*(ch-48);
}
else//, 字母 _ 换行符
{
if (ch == ',' )
{
Data.Attribute[i] = m_Attribute;
i++;
m_Attribute = 0;//清零
}
else if (ch == '\n')
{
i = 0;
DataSet.push_back(Data);
Data.ClassName.clear();
}
else
Data.ClassName.append(1,ch);
}
}
chPrior = ch;
}
infileDataSet.close();
/**********************************读入测试数据**********************************/
ifstream infileTestDataSet;
infileTestDataSet.open("TestDataSet.txt");
vector<Iris> TestDataSet;//测试数据
Iris TestData;
i = 0;
chPrior = '1';
m_Attribute = 0;
while(infileTestDataSet.get(ch))//读入字符
{
if (ch != '.')
{
if ('0' <= ch && ch <= '9')
{
if (chPrior != '.')
m_Attribute = m_Attribute*10 + (ch-48);
else
m_Attribute += 0.1*(ch-48);
}
else//, 字母 _ 换行符
{
if (ch == ',' )
{
TestData.Attribute[i] = m_Attribute;
i++;
m_Attribute =0;//清零
}
else if (ch == '\n')
{
i = 0;
TestDataSet.push_back(TestData);
TestData.ClassName.clear();
}
else
TestData.ClassName.append(1,ch);
}
}
chPrior = ch;
}
infileTestDataSet.close();
cout<<"-------------------------------------Result-------------------------------------";
for (vector<int>::size_type k = 0 ; k != TestDataSet.size(); k++)
Sort(DataSet, TestDataSet[k]);
}
return nRetCode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -