-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadData.h
102 lines (101 loc) · 2.59 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
#include <iostream>
#include <stdlib.h>
#include <string>
#include <math.h>
#include <fstream>
#include <sstream>
#include <stack>
#include <string>
#define ATTR_NUM 2000
using namespace std;
struct Attr_String
{
string attr;
Attr_String *next;
};
struct Data
{
int id;
Attr_String *Attr_All;//用于字符型属性
Data *next;
int len;
};
class dataToMatrix
{
public:
Data *dataSet;
int col;
int row;
public:
int loadData(dataToMatrix *dtm,const char *file)
{
int i=0,j=0,pos=0,k,num;
char ch=' ';
ifstream infile;
string tmpstrline;
Data *p;
Attr_String *as;
dtm->dataSet=new Data;
dtm->dataSet->next=NULL;
p=dtm->dataSet;
Data *datatmp;
Attr_String *astmp;
dtm->row=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;
datatmp->Attr_All=new Attr_String;
datatmp->Attr_All->next=NULL;
j=0;
as=datatmp->Attr_All;
while(input>>as->attr)
{
pos=as->attr.find(ch,0);
as->attr=as->attr.substr(0,pos);
if(as->attr==".")
as->attr=".";
num=as->attr.size();
astmp=new Attr_String;
astmp->next=NULL;
as->next=astmp;
as=as->next;
j++;
}
datatmp->len=j;
p->next=datatmp;
p=p->next;
dtm->row++;
}
i++;
}
infile.close();
return 0;
}
int print(dataToMatrix dtm)
{
//检测数据加载是否正确
int i,j;
Data *p=dtm.dataSet->next;
Attr_String *as;
for(i=0; i<dtm.row&&p!=NULL; i++)
{
as=p->Attr_All;
for(j=0; as!=NULL&&j<p->len; j++)
{
cout<<as->attr<<" ";
as=as->next;
}
p=p->next;
cout<<endl;
}
return i;
}
};