-
Notifications
You must be signed in to change notification settings - Fork 13
/
loadData.h
169 lines (165 loc) · 4.35 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <stdlib.h>
#include <string>
#include <math.h>
#include <fstream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
#define ATTR_NUM 1500
struct Data
{
//可以用共用体类型
int id;
string attr_string[ATTR_NUM];//用于字符型属性
double weight;
Data *next;
int len;
};
class dataToMatrix
{
public:
Data *dataSet;
int col;
int row;
public:
int loadData(dataToMatrix *dtm,char *file)
{
int i=0,j=0,pos=0,k;
char ch='/';
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())
{
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_string[j])
{
pos=datatmp->attr_string[j].find(ch,0);
datatmp->attr_string[j]=datatmp->attr_string[j].substr(0,pos);
j++;
}
datatmp->attr_string[j]="#";
datatmp->len=j;
p->next=datatmp;
p=p->next;
dtm->col++;
}
i++;
}
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())
{
getline(infile,tmpstrline,'\n');//读取文件中一行的数据,保存为string类型
stringstream input(tmpstrline);
if(tmpstrline!="\0")////由于读取文件结束符同样会继续该操作
{
while(input>>datatmp->attr_string[j])
{
j++;
}
}
}
p->next=datatmp;
datatmp->len=j;
dtm->row=j;
infile.close();
return 0;
}
int loadData(dataToMatrix *dtm,char *file,char type)
{
int i=0,j=0,pos=0,k;
char ch=' ';
ifstream infile;
string tmpstrline;
Data *p;
dtm->dataSet=new Data;
dtm->dataSet->next=NULL;
p=dtm->dataSet;
Data *datatmp;
dtm->col=0;
dtm->row=ATTR_NUM;
cout<<file<<endl;
infile.open(file,ios::in);
while(!infile.eof()&&i<20)
{
getline(infile,tmpstrline,'\n');//读取文件中一行的数据,保存为string类型
stringstream input(tmpstrline);
if(tmpstrline!="\0")////由于读取文件结束符同样会继续该操作
{
datatmp=new Data;
datatmp->id=i;
datatmp->next=NULL;
k=0,j=0;
int len=tmpstrline.length();
while(j*2<len)
{
if(tmpstrline.substr(j*2,1)!=" ")
{
datatmp->attr_string[k]=tmpstrline.substr(j*2,2);
//cout<<datatmp->attr_string[k]<<"&";
k++;
}
j++;
}
//datatmp->attr_string[k]="#";
datatmp->len=k;
p->next=datatmp;
p=p->next;
dtm->col++;
}
i++;
}
//cout<<"k="<<k<<endl;
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; p->attr_string[j]!="#"&&j<dtm.row; j++)
{
cout<<p->attr_string[j]<<" ";
}
p=p->next;
cout<<endl;
}
return i;
}
};