#include "stdafx.h" #include "math.h" #include #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); }