-
Notifications
You must be signed in to change notification settings - Fork 0
/
PartI_C.cpp
211 lines (164 loc) · 5.54 KB
/
PartI_C.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
/*******************************************************
* PART I.3: Binary and Interpolation search algorithms *
*******************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <chrono> // C++ library to measure the running time of each algorithm using the clock with the highest resolution available
#include <unistd.h>
#define MAX_SIZE 3240 // Max size of the array stored the daily transactions
struct dailyStockData
{
char Date[11];
float Open, High, Low, Close;
int Volume, OpenInt;
};
typedef struct dailyStockData dataItem;
int (*searchAlgPtr)(dataItem arr[], int l, int r, char x[]); // Pointer to functions implementing binarySearch and interpolationSearch algorithms
int steps; // Number of steps made by algorithm
// Declaration of functions
int readFile(dataItem arr[], int argc, char *argv[]);
int binarySearch(dataItem arr[], int l, int r, char x[]);
int interpolationSearch(dataItem arr[], int l, int r, char x[]);
unsigned long val(char s[]);
int main(int argc, char *argv[])
{
dataItem S[MAX_SIZE];
if (argc >= 2)
{
if (strcmp(argv[1], "binarySearch") == 0)
searchAlgPtr = &binarySearch;
else if (strcmp(argv[1], "interpolationSearch") == 0)
searchAlgPtr = &interpolationSearch;
else
{
printf("Invalid algorithm specified. Please use 'binarySearch' or 'interpolationSearch'.\n");
return 1;
}
}
else
{
printf("Please specify the sorting algorithm to use as a command line argument. Options: 'binarySearch', 'interpolationSearch'.\n");
return 1;
}
int N = readFile(S, argc, argv); // N is the number of daily transactions read from file
char x[11];
int pos; // Position of search key in the array or -1 if not found
printf("Give the date to search for (yyyy-mm-dd): ");
scanf("%s", x);
typedef std :: chrono :: high_resolution_clock clock;
// Start measuring running time
auto startTime = clock :: now();
pos = (*searchAlgPtr)(S, 0, N-1, x);
// Stop measuring running time and calculate the elapsed time
auto endTime = clock :: now();
auto elapsedTime = std :: chrono :: duration_cast<std :: chrono :: nanoseconds>(endTime - startTime).count();
if (searchAlgPtr == &binarySearch)
printf("\n\n[BINARY SEARCH]\n");
if (searchAlgPtr == &interpolationSearch)
printf("\n\n[INTERPOLATION SEARCH]\n");
if (pos == -1)
printf("\nDate %s not found.\nNumber of steps: %d\n", x, steps);
else
printf("\nDate %s found at array position %d. Volume: %d\nNumber of steps: %d\n", x, pos, S[pos].Volume, steps);
printf("Running time measured: %g seconds\n", (double)elapsedTime*1e-9);
return 0;
}
// Open the file, read data records, store them to array arr and return the number of records read
int readFile(dataItem arr[], int argc, char *argv[])
{
FILE *fp;
char *fileName;
char line[80];
int numLines = 0; // Number of lines read from file
dataItem dt;
if (argc >= 3) // Data filename passed as a command line argument
fileName = strdup(argv[2]);
else
{
printf("Give the stock data filename: "); // Data filename asked by user
scanf("%ms", &fileName);
printf("\n\n");
}
// Check if the file exists
if (access(fileName, F_OK) == -1)
{
printf("\nERROR: File '%s' not found\n", fileName);
free(fileName);
exit(1);
}
fp = fopen(fileName, "r");
if (!fp) // fp == NULL
{
printf("\nERROR: can't open file\n");
free(fileName);
exit(1);
}
fgets(line, 80, fp); // Get the first line
while (fgets(line, 80, fp))
{
sscanf(line, "%10s,%f,%f,%f,%f,%d,%d", dt.Date, &dt.Open, &dt.High, &dt.Low, &dt.Close, &dt.Volume, &dt.OpenInt);
arr[numLines] = dt;
numLines++;
}
free(fileName);
fclose(fp);
return numLines; // Number of daily transactions read
}
// Binary search algorithm
int binarySearch(dataItem arr[], int l, int r, char x[])
{
int m;
steps = 0;
while (l <= r)
{
steps++;
m = (l+r)/2; // probe index
if (strcmp(arr[m].Date, x) == 0)
return m; // Search successful
else if (strcmp(arr[m].Date, x) < 0)
l = m+1;
else
r = m-1;
}
return -1; // Search unsuccessful
}
// Helping function to convert a Date field to its integer value
unsigned long val(char s[])
{
int i, j, k = 0;
char d[strlen(s)-2];
i = strlen(s);
for (j=0; j<i; j++)
{
if (isdigit(s[j]))
{
d[k] = s[j];
k++;
}
}
d[k] = '\0';
return (unsigned long)atoi(d);
}
// Interpolation search algorithm
int interpolationSearch(dataItem arr[], int l, int r, char x[])
{
int m;
steps = 1;
while (strcmp(arr[r].Date, x) >= 0 && strcmp(x, arr[l].Date) > 0)
{
steps++;
m = l + (r-l) * (val(x) - val(arr[l].Date)) / (val(arr[r].Date) - val(arr[l].Date)); // probe index
if (strcmp(arr[m].Date, x) < 0)
l = m+1;
else if (strcmp(arr[m].Date, x) > 0)
r = m-1;
else
l = m;
}
if (strcmp(x, arr[l].Date) == 0)
return l; // Search successful
else
return -1; // Search unsuccessful
}