-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_add.cpp
365 lines (315 loc) · 11.2 KB
/
function_add.cpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include"head.h"
std::string Upper(std::string input)
{
//std::string x;//作为最后返回的大写模式
for (int i = 0; i < input.size(); i++)
{
if (input[i] >= 'a' && input[i] <= 'z')
input[i] = input[i] + 'A' - 'a';
}
return input;
}
void MyCorrect(std::string input)
{
//当已经确定输入有这个judge点错误的时候
std::string standard[STANDARD] = {"CREATE","DROP","TABLE","INSERT","DELETE",
"UPDATE","SELECT","QUIT","FROM","WHERE",
"ORDER","BY","SET","ASC","DESC",
"DISTINCT","INTO","VALUES"};
for (int which_standard = 0; which_standard < STANDARD; which_standard++)//将所有的标准进行遍历
{
int weight = 0;
for (int i = 0; i < input.length() && i < standard[which_standard].length(); i++)//将这个作为input遍历的点
{
if (input[i] == standard[which_standard][i])
weight++;
}
//这时候要对权数进行计算
if ((double)weight >= (double)(standard[which_standard].length() * 0.5))//这时候有超过50%的相似度
{
std::cout<<"您输入的“"<<input<<"”可能是想输入:“"<<standard[which_standard]<<"”"<<std::endl;
return;
}
}
return;//未完成
}
void Data_Table::compute(char *input)//计算功能的实现
//所希望的格式:COMPUTE 属性 FROM name TO 模式
{
//这个时候的第一个单词“COMPUTE”已经进入了之前的判定
//所以现在进入的是第二个单词
//所希望的格式:COMPUTE 属性 FROM name TO 模式
char *order1, *order2;
std::string judge;
std::string name;
std::string which_get;//作为计算哪一个的依据
std::vector<std::string> mode;
Table *p = HEAD;
order1 = OrderDivide(input, ' ', 1);
which_get = input;//将属性写入
order2 = OrderDivide(order1, ' ', 1);
judge = order1;
judge = Upper(judge);
if (judge != "FROM")
{
std::cout<<"语法错误"<<std::endl;
MyCorrect(judge);
return;
}
order1 = OrderDivide(order2, ' ', 1);
name = order2;
order2 = OrderDivide(order1, ' ', 1);
judge = order1;
judge = Upper(judge);
if (judge != "TO")
{
std::cout<<"语法错误"<<std::endl;
MyCorrect(judge);
return;
}
mode = ListDivide(order2, '|');
//这时候所有的语法点是正确的
for (; p != NULL; p = p->next)
if (p->table_name == name)
break;
if (p == NULL)
{
std::cout<<"表“"<<name<<"”不存在"<<std::endl;
return;
}
//这时候已经找到了这一个表
int which = -1;//这里是标注出哪一个属性的数字
for (int i = 0; i < p->table_head.size(); i++)
{
if (p->table_head[i] == which_get)
{
which = i;
break;
}
}
if (which == -1)//还是初始化的值
{
std::cout<<"属性“"<<which_get<<"”不存在"<<std::endl;
return;
}
//这里就找到了哪一列的数据进行计算
std::vector<double> data;//作为储存数据的容器
double mid;//作为中间的变量进行操作
for (int i = 0; i < p->all_data.size(); i++)
{
int point_count = 0;
int j = 0;
for (; j < p->all_data[i][which].length(); j++)
{
if (p->all_data[i][which][j] >= '0' && p->all_data[i][which][j] <= '9')
continue;
else if (p->all_data[i][which][j] == '.')
point_count++;
else
break;
}
if (point_count > 1 || j < p->all_data[i][which].length())
{
std::cout<<"该属性下的数据不全为数字!"<<std::endl;
return;
}
else
{
mid = atof(p->all_data[i][which].c_str());//将这一个数据变成double
data.push_back(mid);
}
}
//下面进行模式的判断
for (int t = 0; t < mode.size(); t++)
{
if (mode[t] == "MAX")
{
double max = -10000000;
for (int i = 0; i < data.size(); i++)
{
if (data[i] > max)
max = data[i];
}
std::cout<<which_get<<" 最大值:"<<max<<std::endl;
}
else if (mode[t] == "MIN")
{
double min = 10000000;
for (int i = 0; i < data.size(); i++)
{
if (data[i] < min)
min = data[i];
}
std::cout<<which_get<<" 最小值:"<<min<<std::endl;
}
else if (mode[t] == "AVERAGE")
{
double sum = 0;
double average = 0;
for (int i = 0; i < data.size(); i++)
sum += data[i];
average = sum / data.size();
std::cout<<which_get<<" 平均值:"<<average<<std::endl;
}
else if (mode[t] == "VARIANCE")
{
double sum = 0;
double average = 0;
for (int i = 0; i < data.size(); i++)
sum += data[i];
average = sum / data.size();
//先计算出这一组数据的平均值
double variance = 0;//作为方差的储存值
for (int i = 0; i < data.size(); i++)
variance += (data[i] - average) * (data[i] - average);//算出没有进行除法之前的值
variance = variance / data.size();
std::cout<<which_get<<" 方差:"<<variance<<std::endl;
}
else if (mode[t] == "RANGE")//极差的计算
{
double max = -10000000;
for (int i = 0; i < data.size(); i++)
{
if (data[i] > max)
max = data[i];
}
double min = 10000000;
for (int i = 0; i < data.size(); i++)
{
if (data[i] < min)
min = data[i];
}
double range = max - min;
std::cout<<which_get<<" 极差:"<<std::endl;
}
else
{
std::cout<<"没有“"<<mode[t]<<"”这一种模式"<<std::endl;
}
}
return;
}
void Data_Table::search(char *input)
//所希望的格式:SEARCH 关键词 FROM name 属性 个数
{
char *order1, *order2;
std::string judge, name;
std::string key;//作为搜索的关键词
std::vector<std::string> which_to_search;//捕获最后的所有属性
Table *p = HEAD;
order1 = OrderDivide(input, ' ', 1);
key = input;
key = Upper(key);//强制转换为大写
order2 = OrderDivide(order1, ' ', 1);
judge = order1;
judge = Upper(judge);
if (judge != "FROM")
{
std::cout<<"语法错误"<<std::endl;
MyCorrect(judge);
return;
}
order1 = OrderDivide(order2, ' ', 1);
name = order2;//抓取到表的名字
order2 = OrderDivide(order1, ' ', 1);
//order2 = OrderDivide(order1, ' ', 1);
//std::cout<<order1<<std::endl;
which_to_search = ListDivide(order1, '|');
//得到所有属性的名字
int num = atoi(order2);//作为一共有多少个搜索结果的标记
std::cout<<num<<std::endl;
for (; p != NULL; p = p->next)
if (p->table_name == name)
break;
if (!p)
{
std::cout<<"表“"<<name<<"”不存在"<<std::endl;
return;
}
//这就已经找到了这个表
std::vector<int> which;//作为查找哪些属性的标记,int型
for (int i = 0; i < which_to_search.size(); i++)
{
for (int j = 0; j < p->table_head.size(); j++)
{
if (p->table_head[j] == which_to_search[i])
which.push_back(j);//将这个属性的位置压入
}
}
if (which.size() < which_to_search.size())
{
std::cout<<"属性不对等"<<std::endl;
return;
}
std::vector<int> which_to_print;//作为打印数据标记
for (int condition = 10; which_to_print.size() < num && condition > 0; condition--)
//这就是标记权的限度以及决定函数终止与否的函数
{
for (int i = 0; i < p->all_data.size(); i++)//对于原数据行的遍历
{
//std::cout<<condition<<" ";
bool flag = true;
for (int ii = 0; ii < which_to_print.size(); ii++)
{
if (which_to_print[ii] == i)//已经存在于这之中的一个数据
{
flag = false;
break;
}
}
if (!flag)
continue;
else
//这时候就可以进行相似的比较
{
//std::cout<<"?";
for (int j = 0; j < which.size(); j++)//作为每一列的遍历
{
int like = 0;//标记一共有多少个相似的权
std::string mid = Upper(p->all_data[i][which[j]]);//将数据全部转化为大写
std::vector<int> record;//记录原来数据的哪些位次被记录了
for (int key_i = 0; key_i < key.length(); key_i++)//对关键词的每一个字符进行遍历
{
for (int data_i = 0; data_i < p->all_data[i][which[j]].length(); data_i++)//作为对原来的数据每一位进行的遍历
{
bool x = true;
for (int record_i = 0; record_i < record.size(); record_i++)
//观察这一位的字符是否经过了遍历
{
if (data_i == record[record_i])
{
x = false;
break;//这种情况就是说明已经重复
}
}
if (!x)
continue;
//下面的操作就是说明已经确定了这个字母没有被选中过
else
{
if (mid[data_i] == key[key_i])//如果这时候的这一位等于key中的某一位
{
record.push_back(data_i);
break;
}
}
}
}
like = record.size();
if ((double)like / (double)key.length() >= (double)condition / 10.0)//计算权比例是否超过预期
{
//std::cout<<(double)like / (double)key.length()<<" "<<(double)condition / 10.0<<"|";
which_to_print.push_back(i);
}
}
}
}
}
std::vector<std::vector<std::string> > data_to_print;//作为最后进行输出的数据表
for (int i = 0; i < which_to_print.size(); i++)
{
data_to_print.push_back(p->all_data[which_to_print[i]]);
}
this->print_data(p->table_head, data_to_print);
return;
}