-
Notifications
You must be signed in to change notification settings - Fork 10
/
loadData.h
118 lines (115 loc) · 2.95 KB
/
loadData.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <stdlib.h>
#include <string>
#include <math.h>
#include <fstream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
#define MAX_SIZE_OF_TRAINING_SET 10000
#define ATTR_NUM 1000
struct Data
{
//可以用共用体类型
int id;
double attr_double[ATTR_NUM];//用于数值型属性
string attr_string[ATTR_NUM];//用于数值型属性
double weight;
Data *next;
};
class dataToMatrix
{
public:
Data *dataSet;
int col;
int row;
public:
/**
加载数值型样本数据
*/
int loadData(dataToMatrix *dtm,const char *file)
{
int i=0,j=0;
ifstream infile;
string tmpstrline;
Data *p;
dtm->dataSet=new Data;
dtm->dataSet->next=NULL;
p=dtm->dataSet;
Data *datatmp;
dtm->col=0;
cout<<file<<endl;
infile.open(file,ios::in);
while(!infile.eof()&&i<MAX_SIZE_OF_TRAINING_SET)
{
getline(infile,tmpstrline,'\n');//读取文件中一行的数据,保存为string类型
stringstream input(tmpstrline);
if(tmpstrline!="\0")////由于读取文件结束符同样会继续该操作
{
datatmp=new Data;
datatmp->id=i;
datatmp->next=NULL;
j=0;
while(input>>datatmp->attr_double[j])j++;
p->next=datatmp;
p=p->next;
dtm->col++;
}
}
dtm->row=j;
infile.close();
return 0;
}
/**
加载字符型样本数据
*/
int loadData(dataToMatrix *dtm,char *file,int type)
{
int i=0,j=0;
ifstream infile;
string tmpstrline;
Data *p;
dtm->dataSet=new Data;
dtm->dataSet->next=NULL;
p=dtm->dataSet;
Data *datatmp;
dtm->col=1;
cout<<file<<endl;
infile.open(file,ios::in);
datatmp=new Data;
datatmp->next=NULL;
while(!infile.eof()&&i<MAX_SIZE_OF_TRAINING_SET)
{
getline(infile,tmpstrline,'\n');//读取文件中一行的数据,保存为string类型
stringstream input(tmpstrline);
if(tmpstrline!="\0")////由于读取文件结束符同样会继续该操作
{
while(input>>datatmp->attr_string[j]){j++;};
}
}
p->next=datatmp;
dtm->row=j;
infile.close();
return 0;
}
int print(dataToMatrix dtm)
{
//检测数据加载是否正确
int i,j;
Data *p=dtm.dataSet->next;
for(i=0; i<dtm.col&&p!=NULL; i++)
{
for(j=0; j<dtm.row; j++)
{
if(p->attr_string[j]!="")
cout<<p->attr_string[j]<<" ";
else
cout<<p->attr_double[j]<<" ";
}
p=p->next;
cout<<endl;
}
return i;
}
};