#include #include #include typedef void (*FunctionPointer)(int, int); typedef FunctionPointer* FunctionArray; void addFunction(FunctionArray* delegate, int* size, FunctionPointer func) { *delegate = realloc(*delegate, (*size + 1) * sizeof(FunctionPointer)); if (*delegate != NULL) { (*delegate)[*size] = func; (*size)++; } } void removeFunction(FunctionArray* delegate, int* size, FunctionPointer func) { int index = -1; for (int i = 0; i < *size; i++) { if ((*delegate)[i] == func) { index = i; break; } } if (index != -1) { for (int i = index; i < *size - 1; i++) { (*delegate)[i] = (*delegate)[i + 1]; } *delegate = realloc(*delegate, (*size - 1) * sizeof(FunctionPointer)); (*size)--; } } void invokeAllFunctions(FunctionArray delegate, int size,int a, int b) { for (int i = 0; i < size; i++) { delegate[i](a,b); } } void removeDuplicates(FunctionArray* delegate, int* size) { for (int i = 0; i < *size; i++) { for (int j = i + 1; j < *size; j++) { if ((*delegate)[i] == (*delegate)[j]) { removeFunction(delegate, size, (*delegate)[j]); j--; } } } } void summ(int a, int b) { printf("Сумма равна %d\n",(a+b)); } void subtraction(int a, int b) { printf("Разница равна %d\n", (a - b)); } void multiplications(int a, int b) { printf("Произведение равна %d\n", (a * b)); } int main() { setlocale(LC_ALL, "Rus"); FunctionArray delegate = NULL; int size = 0; int a = 6; int b = 5; addFunction(&delegate, &size, summ); addFunction(&delegate, &size, subtraction); addFunction(&delegate, &size, summ); addFunction(&delegate, &size, summ); addFunction(&delegate, &size, subtraction); addFunction(&delegate, &size, multiplications); addFunction(&delegate, &size, multiplications); addFunction(&delegate, &size, multiplications); addFunction(&delegate, &size, multiplications); addFunction(&delegate, &size, multiplications); addFunction(&delegate, &size, multiplications); addFunction(&delegate, &size, multiplications); addFunction(&delegate, &size, multiplications); addFunction(&delegate, &size, multiplications); addFunction(&delegate, &size, multiplications); addFunction(&delegate, &size, multiplications); addFunction(&delegate, &size, multiplications); addFunction(&delegate, &size, multiplications); addFunction(&delegate, &size, summ); addFunction(&delegate, &size, summ); addFunction(&delegate, &size, summ); addFunction(&delegate, &size, summ); addFunction(&delegate, &size, summ); addFunction(&delegate, &size, summ); invokeAllFunctions(delegate, size,a,b); removeFunction(&delegate, &size, summ); removeFunction(&delegate, &size, subtraction); removeFunction(&delegate, &size, multiplications); printf("\nПосле удаления\n"); invokeAllFunctions(delegate, size,a,b); printf("\nПосле удаления дубликатов\n"); removeDuplicates(&delegate, &size); invokeAllFunctions(delegate, size, a, b); free(delegate); return 0; }