Source.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <locale.h>
  4. typedef void (*FunctionPointer)(int, int);
  5. typedef FunctionPointer* FunctionArray;
  6. void addFunction(FunctionArray* delegate, int* size, FunctionPointer func) {
  7. *delegate = realloc(*delegate, (*size + 1) * sizeof(FunctionPointer));
  8. if (*delegate != NULL) {
  9. (*delegate)[*size] = func;
  10. (*size)++;
  11. }
  12. }
  13. void removeFunction(FunctionArray* delegate, int* size, FunctionPointer func) {
  14. int index = -1;
  15. for (int i = 0; i < *size; i++) {
  16. if ((*delegate)[i] == func) {
  17. index = i;
  18. break;
  19. }
  20. }
  21. if (index != -1) {
  22. for (int i = index; i < *size - 1; i++) {
  23. (*delegate)[i] = (*delegate)[i + 1];
  24. }
  25. *delegate = realloc(*delegate, (*size - 1) * sizeof(FunctionPointer));
  26. (*size)--;
  27. }
  28. }
  29. void invokeAllFunctions(FunctionArray delegate, int size,int a, int b) {
  30. for (int i = 0; i < size; i++) {
  31. delegate[i](a,b);
  32. }
  33. }
  34. void removeDuplicates(FunctionArray* delegate, int* size) {
  35. for (int i = 0; i < *size; i++) {
  36. for (int j = i + 1; j < *size; j++) {
  37. if ((*delegate)[i] == (*delegate)[j]) {
  38. removeFunction(delegate, size, (*delegate)[j]);
  39. j--;
  40. }
  41. }
  42. }
  43. }
  44. void summ(int a, int b) {
  45. printf("Ñóììà ðàâíà %d\n",(a+b));
  46. }
  47. void subtraction(int a, int b) {
  48. printf("Ðàçíèöà ðàâíà %d\n", (a - b));
  49. }
  50. void multiplications(int a, int b) {
  51. printf("Ïðîèçâåäåíèå ðàâíà %d\n", (a * b));
  52. }
  53. int main() {
  54. setlocale(LC_ALL, "Rus");
  55. FunctionArray delegate = NULL;
  56. int size = 0;
  57. int a = 6;
  58. int b = 5;
  59. addFunction(&delegate, &size, summ);
  60. addFunction(&delegate, &size, subtraction);
  61. addFunction(&delegate, &size, summ);
  62. addFunction(&delegate, &size, summ);
  63. addFunction(&delegate, &size, subtraction);
  64. addFunction(&delegate, &size, multiplications);
  65. addFunction(&delegate, &size, multiplications);
  66. addFunction(&delegate, &size, multiplications);
  67. addFunction(&delegate, &size, multiplications);
  68. addFunction(&delegate, &size, multiplications);
  69. addFunction(&delegate, &size, multiplications);
  70. addFunction(&delegate, &size, multiplications);
  71. addFunction(&delegate, &size, multiplications);
  72. addFunction(&delegate, &size, multiplications);
  73. addFunction(&delegate, &size, multiplications);
  74. addFunction(&delegate, &size, multiplications);
  75. addFunction(&delegate, &size, multiplications);
  76. addFunction(&delegate, &size, multiplications);
  77. addFunction(&delegate, &size, summ);
  78. addFunction(&delegate, &size, summ);
  79. addFunction(&delegate, &size, summ);
  80. addFunction(&delegate, &size, summ);
  81. addFunction(&delegate, &size, summ);
  82. addFunction(&delegate, &size, summ);
  83. invokeAllFunctions(delegate, size,a,b);
  84. removeFunction(&delegate, &size, summ);
  85. removeFunction(&delegate, &size, subtraction);
  86. removeFunction(&delegate, &size, multiplications);
  87. printf("\nÏîñëå óäàëåíèÿ\n");
  88. invokeAllFunctions(delegate, size,a,b);
  89. printf("\nÏîñëå óäàëåíèÿ äóáëèêàòîâ\n");
  90. removeDuplicates(&delegate, &size);
  91. invokeAllFunctions(delegate, size, a, b);
  92. free(delegate);
  93. return 0;
  94. }