Source.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. typedef int (*delegate_func)(int, int);
  6. typedef struct
  7. {
  8. delegate_func* funcs;
  9. int count;
  10. } Delegate;
  11. int summ(int a, int b)
  12. {
  13. return a + b;
  14. }
  15. int diff(int a, int b)
  16. {
  17. return a - b;
  18. }
  19. void zadanie1() //çàäàíèå 1
  20. {
  21. int (*FS)(int,int);
  22. FS = summ;
  23. printf("Ðåçóëüòàò ôóíêöèè summ %d\n", FS(10,5));
  24. int(*FArr[2])(int, int);
  25. FArr[0] = summ;
  26. FArr[1] = diff;
  27. for (int i = 0; i < 2; i++)
  28. {
  29. printf("Ðåçóëüòàò ôóíêöèè %d = %d\n", i+1, FArr[i](10,3));
  30. }
  31. }
  32. void add_function_to_delegate(Delegate* d, delegate_func func) //çàäàíèå 2
  33. {
  34. d->count++;
  35. d->funcs = (delegate_func*)realloc(d->funcs, d->count * sizeof(delegate_func));
  36. d->funcs[d->count - 1] = func;
  37. }
  38. void invoke_delegate(Delegate* d, int a, int b) //çàäàíèå 4
  39. {
  40. for (int i = 0; i < d->count; i++)
  41. {
  42. printf("Ðåçóëüòàò ôóíêöèè %d: %d\n", i + 1, d->funcs[i](a, b));
  43. }
  44. }
  45. void remove_delegate(Delegate d)
  46. {
  47. free(d.funcs);
  48. d.count = 0;
  49. }
  50. void removeFunction(Delegate* delegate, delegate_func func);
  51. void zadanie2() //çàäàíèå 2
  52. {
  53. Delegate d = { .count = 0 };
  54. printf("Äîáàâëåíèå ôóíêöèé\n");
  55. add_function_to_delegate(&d, summ);
  56. add_function_to_delegate(&d, diff);
  57. invoke_delegate(&d, 5, 3);
  58. printf("Äîáàâëåíèå ôóíêöèé\n");
  59. removeFunction(&d, summ);
  60. removeFunction(&d, diff);
  61. add_function_to_delegate(&d, diff);
  62. invoke_delegate(&d, 5, 3);
  63. }
  64. void removeFunction(Delegate* delegate, delegate_func func) //çàäàíèå 3
  65. {
  66. for (int i = 0; i < delegate->count; i++)
  67. {
  68. if (delegate->funcs[i] == func)
  69. {
  70. for (int j = i; j < delegate->count - 1; j++) {
  71. delegate->funcs[j] = delegate->funcs[j + 1];
  72. }
  73. delegate->count--;
  74. delegate->funcs = (delegate_func*)realloc(delegate->funcs, delegate->count * sizeof(delegate_func));
  75. break;
  76. }
  77. }
  78. }
  79. void zadanie3()//çàäàíèå 3
  80. {
  81. Delegate d = { .count = 0 };
  82. printf("Äîáàâëåíèå ôóíêöèé\n");
  83. add_function_to_delegate(&d, summ);
  84. add_function_to_delegate(&d, diff);
  85. invoke_delegate(&d, 5, 3);
  86. printf("Ïîñëå óäàëåíèå ôóíêöèè diff(2)\n");
  87. removeFunction(&d, diff);
  88. invoke_delegate(&d, 5, 3);
  89. }
  90. void remove_duplicate_functions(Delegate* delegate) {
  91. for (int i = 0; i < delegate->count; i++) {
  92. for (int j = i + 1; j < delegate->count; j++) {
  93. if (delegate->funcs[i] == delegate->funcs[j]) {
  94. removeFunction(delegate, delegate->funcs[j]);
  95. j--;
  96. }
  97. }
  98. }
  99. }
  100. void zadanie4()
  101. {
  102. Delegate d = { .count = 0 };
  103. printf("Äîáàâëåíèå ôóíêöèé\n");
  104. add_function_to_delegate(&d, summ);
  105. add_function_to_delegate(&d, diff);
  106. invoke_delegate(&d, 5, 3);
  107. printf("Äîáàâëåíèå äóáëèðóþùèõ ôóíêöèé\n");
  108. add_function_to_delegate(&d, diff);
  109. add_function_to_delegate(&d, summ);
  110. add_function_to_delegate(&d, diff);
  111. add_function_to_delegate(&d, summ);
  112. add_function_to_delegate(&d, summ);
  113. add_function_to_delegate(&d, summ);
  114. add_function_to_delegate(&d, summ);
  115. add_function_to_delegate(&d, diff);
  116. add_function_to_delegate(&d, summ);
  117. add_function_to_delegate(&d, summ);
  118. add_function_to_delegate(&d, diff);
  119. add_function_to_delegate(&d, diff);
  120. add_function_to_delegate(&d, summ);
  121. add_function_to_delegate(&d, summ);
  122. add_function_to_delegate(&d, summ);
  123. invoke_delegate(&d, 5, 3);
  124. printf("Ïîñëå óäàëåíèå äóáëèðóþùèõ ôóíêöèé\n");
  125. remove_duplicate_functions(&d, diff);
  126. remove_duplicate_functions(&d, summ);
  127. invoke_delegate(&d, 5, 3);
  128. remove_delegate(d);
  129. }
  130. int main(void)
  131. {
  132. system("chcp 1251>nul");
  133. printf("1. Îïðåäåëèòü óêàçàòåëü íà ôóíêöèþ è ìàññèâ óêàçàòåëåé íà ôóíêöèþ êàê íîâûé òèï äàííûõ\n");
  134. printf("2. Ðåàëèçîâàòü ôóíêöèþ äîáàâëåíèÿ ôóíêöèé â äåëåãàò\n");
  135. printf("3. Ðåàëèçîâàòü ôóíêöèþ óäàëåíèÿ ôóíêöèé èç äåëåãàòà\n");
  136. printf("4. Ðåàëèçîâàòü ôóíêöèþ ïî óäàëåíèþ âñåõ ïîâòîðÿþùèõñÿ ôóíêöèé â äåëåãàòå\n");
  137. int n, zadanie = 0;
  138. while (zadanie == 0)
  139. {
  140. printf("\nÂûáåðèòå íîìåð çàäàíèÿ (1-4): ");
  141. if (scanf_s("%d", &n)!=1)
  142. {
  143. printf("Îøèáêà ââîäà, íåîáõîäèìî ââåñòè ÷èñëî\n");
  144. zadanie = 1;
  145. }
  146. switch (n)
  147. {
  148. case 1:
  149. zadanie1();
  150. break;
  151. case 2:
  152. zadanie2();
  153. break;
  154. case 3:
  155. zadanie3();
  156. break;
  157. case 4:
  158. zadanie4();
  159. break;
  160. default:
  161. printf("Òàêîãî çàäàíèÿ íåò");
  162. break;
  163. }
  164. int c = -1;
  165. printf("\n\nÂû õîòèòå âûáðàòü äðóãîå çàäàíèå? 0 - íåò, 1 - äà\n");
  166. scanf_s("%d", &c);
  167. if (c == 0) zadanie = 1;
  168. }
  169. return 0;
  170. }