-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayDoubleSample.mq5
102 lines (102 loc) · 4.1 KB
/
ArrayDoubleSample.mq5
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
//+------------------------------------------------------------------+
//| ArrayDoubleSample.mq5 |
//| Copyright 2000-2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
//---
#include <Arrays\ArrayDouble.mqh>
#include <Files\FileBin.mqh>
//---
const int ExtArraySize=10000;
const int ExtArrayAdd=100;
string ExtFileName="ArrayDoubleSample.bin";
//+------------------------------------------------------------------+
//| Example class CArrayDouble |
//+------------------------------------------------------------------+
int OnStart(void)
{
int i,pos;
double key;
CFileBin File;
CArrayDouble ArrayDouble;
//---
printf("Start sample %s.",__FILE__);
//--- fill an array of background information
//--- open file for reading
if(File.Open(ExtFileName,FILE_READ)!=INVALID_HANDLE)
{
//--- read array from file
if(!ArrayDouble.Load(File.Handle()))
{
//--- error reading from file
printf("%s (%4d): error %d",__FILE__,__LINE__,GetLastError());
}
//--- do not forget close file
File.Close();
}
//--- check whether enough information in the array
if(ArrayDouble.Total()<ExtArraySize)
{
//--- information in the file is not enough, or it is not at all
//--- reserve position in the array for the missing information
if(!ArrayDouble.Reserve(ExtArraySize-ArrayDouble.Total()))
{
//--- displaying the log error information
printf("%s (%4d): reserve error",__FILE__,__LINE__);
//--- remove a previously created array
return(__LINE__);
}
//--- additional fill an array of "random" values
for(i=ArrayDouble.Total();i<ExtArraySize;i++)
ArrayDouble.Add(MathRand()*MathPow(10,MathRand()%100));
}
//--- sort array
ArrayDouble.Sort();
//--- inserts the additional data without violating sorting (ExtArrayAdd items)
for(i=0;i<ExtArrayAdd;i++)
ArrayDouble.InsertSort(MathRand()*MathPow(10,MathRand()%100));
//--- set tolerance "fuzzy" comparison for the search
ArrayDouble.Delta(0.1);
//--- produce some of the search in sorted array
key=MathRand()*MathPow(10,MathRand()%100);
if((pos=ArrayDouble.SearchGreat(key))==-1)
printf("Search for items greater than %f, not found",key);
else
{
printf("Search for items greater than %f, found %f in the position %d",key,ArrayDouble.At(pos),pos);
//--- your actions have found the element
//--- ...
//---
}
key=MathRand()*MathPow(10,MathRand()%100);
if((pos=ArrayDouble.SearchLess(key))==-1)
printf("Search for items less than %f, not found",key);
else
{
printf("Search for items less than %f, found %f in the position %d",key,ArrayDouble.At(pos),pos);
//--- your actions have found the element
//--- ...
//---
}
//--- Remove from the array of extra data (ExtArrayAdd/2 largest and ExtArrayAdd/2 lowest)
ArrayDouble.DeleteRange(ArrayDouble.Total()-ExtArrayAdd/2-1,ArrayDouble.Total());
ArrayDouble.DeleteRange(0,ExtArraySize/2);
//--- save the modified array of file
//--- open file for writing
if(File.Open(ExtFileName,FILE_WRITE)!=INVALID_HANDLE)
if(ArrayDouble.Save(File.Handle()))
{
//--- normal completion
//--- because when you call the destructor, an open file is closed automatically
//--- and explicitly close the file is not necessary but desirable
printf("End of sample %s. OK!",__FILE__);
return(0);
}
//--- error with file
//--- displaying the log error information
printf("%s (%4d): error %d",__FILE__,__LINE__,GetLastError());
return(__LINE__);
}
//+------------------------------------------------------------------+