123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <locale.h>
- 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;
- }
|