123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- #include<stdio.h>
- #include<math.h>
- #include <malloc.h>
- #include <string.h>
- typedef int (*delegate_func)(int, int);
- typedef struct
- {
- delegate_func* funcs;
- int count;
- } Delegate;
- int summ(int a, int b)
- {
- return a + b;
- }
- int diff(int a, int b)
- {
- return a - b;
- }
- void zadanie1() //çàäàíèå 1
- {
- int (*FS)(int,int);
- FS = summ;
- printf("Ðåçóëüòàò ôóíêöèè summ %d\n", FS(10,5));
- int(*FArr[2])(int, int);
- FArr[0] = summ;
- FArr[1] = diff;
- for (int i = 0; i < 2; i++)
- {
- printf("Ðåçóëüòàò ôóíêöèè %d = %d\n", i+1, FArr[i](10,3));
- }
- }
- void add_function_to_delegate(Delegate* d, delegate_func func) //çàäàíèå 2
- {
- d->count++;
- d->funcs = (delegate_func*)realloc(d->funcs, d->count * sizeof(delegate_func));
- d->funcs[d->count - 1] = func;
- }
- void invoke_delegate(Delegate* d, int a, int b) //çàäàíèå 4
- {
- for (int i = 0; i < d->count; i++)
- {
- printf("Ðåçóëüòàò ôóíêöèè %d: %d\n", i + 1, d->funcs[i](a, b));
- }
- }
- void remove_delegate(Delegate d)
- {
- free(d.funcs);
- d.count = 0;
- }
- void removeFunction(Delegate* delegate, delegate_func func);
- void zadanie2() //çàäàíèå 2
- {
- Delegate d = { .count = 0 };
- printf("Äîáàâëåíèå ôóíêöèé\n");
- add_function_to_delegate(&d, summ);
- add_function_to_delegate(&d, diff);
- invoke_delegate(&d, 5, 3);
- printf("Äîáàâëåíèå ôóíêöèé\n");
- removeFunction(&d, summ);
- removeFunction(&d, diff);
- add_function_to_delegate(&d, diff);
- invoke_delegate(&d, 5, 3);
-
- }
- void removeFunction(Delegate* delegate, delegate_func func) //çàäàíèå 3
- {
- for (int i = 0; i < delegate->count; i++)
- {
- if (delegate->funcs[i] == func)
- {
- for (int j = i; j < delegate->count - 1; j++) {
- delegate->funcs[j] = delegate->funcs[j + 1];
- }
- delegate->count--;
- delegate->funcs = (delegate_func*)realloc(delegate->funcs, delegate->count * sizeof(delegate_func));
- break;
- }
- }
- }
- void zadanie3()//çàäàíèå 3
- {
- Delegate d = { .count = 0 };
- printf("Äîáàâëåíèå ôóíêöèé\n");
- add_function_to_delegate(&d, summ);
- add_function_to_delegate(&d, diff);
- invoke_delegate(&d, 5, 3);
- printf("Ïîñëå óäàëåíèå ôóíêöèè diff(2)\n");
- removeFunction(&d, diff);
- invoke_delegate(&d, 5, 3);
- }
- void remove_duplicate_functions(Delegate* delegate) {
- for (int i = 0; i < delegate->count; i++) {
- for (int j = i + 1; j < delegate->count; j++) {
- if (delegate->funcs[i] == delegate->funcs[j]) {
- removeFunction(delegate, delegate->funcs[j]);
- j--;
- }
- }
- }
- }
- void zadanie4()
- {
- Delegate d = { .count = 0 };
- printf("Äîáàâëåíèå ôóíêöèé\n");
- add_function_to_delegate(&d, summ);
- add_function_to_delegate(&d, diff);
- invoke_delegate(&d, 5, 3);
- printf("Äîáàâëåíèå äóáëèðóþùèõ ôóíêöèé\n");
- add_function_to_delegate(&d, diff);
- add_function_to_delegate(&d, summ);
- add_function_to_delegate(&d, diff);
- add_function_to_delegate(&d, summ);
- add_function_to_delegate(&d, summ);
- add_function_to_delegate(&d, summ);
- add_function_to_delegate(&d, summ);
- add_function_to_delegate(&d, diff);
- add_function_to_delegate(&d, summ);
- add_function_to_delegate(&d, summ);
- add_function_to_delegate(&d, diff);
- add_function_to_delegate(&d, diff);
- add_function_to_delegate(&d, summ);
- add_function_to_delegate(&d, summ);
- add_function_to_delegate(&d, summ);
- invoke_delegate(&d, 5, 3);
- printf("Ïîñëå óäàëåíèå äóáëèðóþùèõ ôóíêöèé\n");
- remove_duplicate_functions(&d, diff);
- remove_duplicate_functions(&d, summ);
- invoke_delegate(&d, 5, 3);
- remove_delegate(d);
- }
- int main(void)
- {
- system("chcp 1251>nul");
- printf("1. Îïðåäåëèòü óêàçàòåëü íà ôóíêöèþ è ìàññèâ óêàçàòåëåé íà ôóíêöèþ êàê íîâûé òèï äàííûõ\n");
- printf("2. Ðåàëèçîâàòü ôóíêöèþ äîáàâëåíèÿ ôóíêöèé â äåëåãàò\n");
- printf("3. Ðåàëèçîâàòü ôóíêöèþ óäàëåíèÿ ôóíêöèé èç äåëåãàòà\n");
- printf("4. Ðåàëèçîâàòü ôóíêöèþ ïî óäàëåíèþ âñåõ ïîâòîðÿþùèõñÿ ôóíêöèé â äåëåãàòå\n");
- int n, zadanie = 0;
- while (zadanie == 0)
- {
- printf("\nÂûáåðèòå íîìåð çàäàíèÿ (1-4): ");
- if (scanf_s("%d", &n)!=1)
- {
- printf("Îøèáêà ââîäà, íåîáõîäèìî ââåñòè ÷èñëî\n");
- zadanie = 1;
- }
- switch (n)
- {
- case 1:
- zadanie1();
- break;
- case 2:
- zadanie2();
- break;
- case 3:
- zadanie3();
- break;
- case 4:
- zadanie4();
- break;
- default:
- printf("Òàêîãî çàäàíèÿ íåò");
- break;
- }
- int c = -1;
- printf("\n\nÂû õîòèòå âûáðàòü äðóãîå çàäàíèå? 0 - íåò, 1 - äà\n");
- scanf_s("%d", &c);
- if (c == 0) zadanie = 1;
- }
- return 0;
- }
|