C++读取MAGIC gamma telescope data并求数据的多元平均向量
话不多说记录以下成果
multi_mean_vector.h头文件:
#include#include<string> #define MAXSIZE 19020 using namespace std;
struct dataset { double data_first =0; double data_second = 0; double data_third = 0; double data_forth = 0; double data_fifth = 0; double data_sixth = 0; double data_seventh = 0; double data_eighth = 0; double data_ninth = 0; double data_tenth = 0; string data_var = ""; }data_exp[MAXSIZE - 1]; //求多元平均向量 dataset* Multi_Mean_Vector(dataset* x) { //第一个属性 double total_first = 0; double mean_first = 0; for (int i = 0; i < 19020; i++) { total_first = total_first + x[i].data_first; } mean_first = total_first / 19020; //第二个属性 double total_second = 0; double mean_second = 0; for (int i = 0; i < 19020; i++) { total_second = total_second + x[i].data_second; } mean_second = total_second / 19020; //第三个属性 double total_third = 0; double mean_third = 0; for (int i = 0; i < 19020; i++) { total_third = total_third + x[i].data_third; } mean_third = total_third / 19020; //第四个属性 double total_forth = 0; double mean_forth = 0; for (int i = 0; i < 19020; i++) { total_forth = total_forth + x[i].data_forth; } mean_forth = total_forth / 19020; //第五个属性 double total_fifth = 0; double mean_fifth = 0; for (int i = 0; i < 19020; i++) { total_fifth = total_fifth + x[i].data_fifth; } mean_fifth = total_fifth / 19020; //第六个属性 double total_sixth = 0; double mean_sixth = 0; for (int i = 0; i < 19020; i++) { total_sixth = total_sixth + x[i].data_sixth; } mean_sixth = total_sixth / 19020; //第七个属性 double total_seventh = 0; double mean_seventh = 0; for (int i = 0; i < 19020; i++) { total_seventh = total_seventh + x[i].data_seventh; } mean_seventh = total_seventh / 19020; //第八个属性 double total_eighth = 0; double mean_eighth = 0; for (int i = 0; i < 19020; i++) { total_eighth = total_eighth + x[i].data_eighth; } mean_eighth = total_eighth / 19020; //第九个属性 double total_ninth = 0; double mean_ninth = 0; for (int i = 0; i < 19020; i++) { total_ninth = total_ninth + x[i].data_ninth; } mean_ninth = total_ninth / 19020; //第十个属性 double total_tenth = 0; double mean_tenth = 0; for (int i = 0; i < 19020; i++) { total_tenth = total_tenth + x[i].data_tenth; } mean_tenth = total_tenth / 19020; //结构体初始化 dataset * multi_mean_vector; multi_mean_vector = new dataset; multi_mean_vector->data_first = mean_first; multi_mean_vector->data_second = mean_second; multi_mean_vector->data_third = mean_third; multi_mean_vector->data_forth = mean_forth; multi_mean_vector->data_fifth = mean_fifth; multi_mean_vector->data_sixth = mean_sixth; multi_mean_vector->data_seventh = mean_seventh; multi_mean_vector->data_eighth = mean_eighth; multi_mean_vector->data_ninth = mean_ninth; multi_mean_vector->data_tenth = mean_tenth; return multi_mean_vector; }
data_mining.cpp:
#include#include"multi_mean_vector.h" #pragma warning(disable :4996) using namespace std; void data_output(dataset* s) { cout << s->data_first << "|" << s->data_second << "|" << s->data_third << "|" << s->data_forth << "|" << s->data_fifth << "|" << s->data_sixth << "|" << s->data_seventh << "|" << s->data_eighth << "|" << s->data_ninth << "|" << s->data_tenth << endl; } int main() { string line; ifstream ifs; ifs.open("C:/Users/涂小康/Desktop/数据挖掘/magic04.txt"); string temp[11];//!!!!!!!!!数组数量多1 int j = 0; while(!ifs.eof()) { getline(ifs, line); char str[150]; strcpy(str, line.c_str()); char* p=NULL; char* buf=NULL; p = strtok_s(str, ",", &buf); int i = 0; while (p) { temp[i] = p; p = strtok_s(NULL, ",", &buf); i++; } data_exp[j].data_first = atof(temp[0].c_str()); data_exp[j].data_second = atof(temp[1].c_str()); data_exp[j].data_third = atof(temp[2].c_str()); data_exp[j].data_forth = atof(temp[3].c_str()); data_exp[j].data_fifth = atof(temp[4].c_str()); data_exp[j].data_sixth = atof(temp[5].c_str()); data_exp[j].data_seventh = atof(temp[6].c_str()); data_exp[j].data_eighth = atof(temp[7].c_str()); data_exp[j].data_ninth = atof(temp[8].c_str()); data_exp[j].data_tenth = atof(temp[9].c_str()); data_exp[j].data_var = temp[10]; j++; } dataset* ptr; ptr = Multi_Mean_Vector(data_exp); data_output(ptr); delete ptr; ifs.close(); return 0; }