include stdafx include math include iostream include hasp_api include

  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
#include "stdafx.h"
#include "math.h"
#include <iostream>
#include "hasp_api.h"
#include "hasp_vcode.h"
typedef float (*func6)(float x1, float x2, float x3, float x4, float x5, float x6);
float res[6];
float rounding(float value, int sign)
{
float x = value * (pow(10.0, sign));
float y=floor(x) / (pow(10.0, sign));
return y;
}
float evaluating_the_function_of_six_variables(func6 func, func6 del)
{
const int count_of_variables = 6;
const int count_of_combinations = (int)pow(double(2), count_of_variables);
int sign = 4;
float error = 1.0 / (pow(10, sign));
float ranges[count_of_variables][2] = {
{ -1, 1 },
{ -1, 1 },
{ -1, 1 },
{ -1, 1 },
{ -1, 1 },
{ -1, 1 }
};
float max_res, best_variant[count_of_variables], variants_of_variable[count_of_variables][2];
bool result_found = false, max_res_flag = false;
while(result_found == false)
{
for (int i=0; i < count_of_variables; ++i)
{
float range_size = ranges[i][1] - ranges[i][0];
float range_center = (ranges[i][1] + ranges[i][0])/ 2;
range_center = rounding(range_center, sign);
if (range_size > error)
{
variants_of_variable[i][0] = range_center - error;
variants_of_variable[i][1] = range_center + error;
}
else
{
variants_of_variable[i][0] = range_center;
variants_of_variable[i][1] = range_center;
}
}
float max_res_step;
bool max_res_step_initialized = false;
float best_var_step[count_of_variables];
float prew_best_var[count_of_variables];
for (int i=0; i < count_of_combinations; ++i)
{
float variant[count_of_variables];
for (int j=0; j < count_of_variables; ++j)
{
variant[j] = variants_of_variable[j][(i / ((int)(count_of_combinations / (int)pow(float(2), j+1)))) % 2];
}
if (del(variant[0], variant[1], variant[2], variant[3], variant[4], variant[5]) != 0)
{
float result = func(variant[0], variant[1], variant[2], variant[3], variant[4], variant[5]);
if (max_res_step_initialized == false)
{
max_res_step_initialized = true;
max_res_step = result;
memcpy(best_var_step, variant, sizeof(variant));
}
else
{
if (result < max_res_step)
{
max_res_step = result;
memcpy(best_var_step, variant, sizeof(variant));
}
}
}
}
if (max_res_flag == false)
{
max_res_flag = true;
max_res = max_res_step;
memcpy(best_variant, best_var_step, sizeof(best_variant));
}
else
{
if (memcmp(prew_best_var, best_var_step, sizeof(best_variant)) == 0)
{
result_found = true;
break;
}
else
{
memcpy(prew_best_var, best_var_step, sizeof(best_variant));
if (max_res_step < max_res)
{
max_res = max_res_step;
memcpy(best_variant, best_var_step, sizeof(best_variant));
}
}
}
for (int i=0; i < count_of_variables; ++i)
{
float range_size = ranges[i][1] - ranges[i][0];
float range_center = (ranges[i][1] + ranges[i][0]) / 2;
range_center = rounding(range_center, sign);
if (best_var_step[i] < range_center)
{
ranges[i][1] = range_center;
}
else
{
ranges[i][0] = range_center;
}
}
}
float max=rounding(max_res,sign);
printf("Result == %.3f\n", max);
printf("x1 = %.3f, x2 = %.3f, x3 = %.3f, x4 = %.3f, x5 = %.3f, x6 = %.3f\n",
best_variant[0], best_variant[1], best_variant[2], best_variant[3], best_variant[4], best_variant[5]);
for(int i=0; i < 6; ++i)
{
res[i] = best_variant[i];
}
res[6] = max;
return max;
}
float func_of_6_variables(float x1,float x2,float x3,float x4,float x5,float x6)
{
float y=5*x1+pow(x2,2)+3*x3-4*x4+x5-1+x6;
return y;
}
float delenie(float x1,float x2,float x3,float x4,float x5,float x6)
{
return 1;
}
void main()
{
hasp_handle_t handle = HASP_INVALID_HANDLE_VALUE;
hasp_status_t status;
const hasp_feature_t feature = HASP_DEFAULT_FID;
status = hasp_login(feature, vendor_code, &handle); //открываем сессию
if(status != HASP_STATUS_OK)
{
printf("ERROR! Unlicensed software usage!\n"); //выдаём ошибку в случае неудачи
exit(0); //и прекращаем работу приложения
}
hasp_encrypt(handle, &res, 64); //зашифровываем рабочий массив с помощью аппаратного ключа HASP
hasp_write(handle, HASP_FILEID_RW, 0, 64, res); //записываем в память ключа зашифрованный массив
status = hasp_logout(handle); //закрываем сессию
status = hasp_login(feature, vendor_code, &handle); //снова открываем сессию
if(status != HASP_STATUS_OK)
{
printf("ERROR! Unlicensed software usage!\n");
exit(0);
}
hasp_read(handle, HASP_FILEID_RO, 0, 64, &res); //считываем из памяти ключа зашифрованный массив
hasp_decrypt(handle, &res, 64); //дешифруем его для дальнейшего использования в приложении
status = hasp_logout(handle); //закрываем сессию
evaluating_the_function_of_six_variables(func_of_6_variables,delenie);
}