Solve.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. // 1.
  5. int triple(int x) {
  6. return x * 3;
  7. }
  8. int divide(int x) {
  9. return x / 3;
  10. }
  11. int* func(int (*f)(int), int* arr, int* size) {
  12. int* result = (int*)malloc(sizeof(int) * (*size));
  13. for (int i = 0; i < *size; i++) {
  14. result[i] = f(arr[i]);
  15. }
  16. return result;
  17. }
  18. // 2.
  19. void printTriangleMatrix(int n) {
  20. for (int i = n; i >= 0; i--) {
  21. for (int j = i; j >= 0; j--) {
  22. printf("%d ", j);
  23. }
  24. printf("\n");
  25. }
  26. }
  27. // 3.
  28. typedef struct Node {
  29. int data;
  30. struct Node* next;
  31. } Node;
  32. Node* createList(int n) {
  33. Node* head = NULL;
  34. Node* current = NULL;
  35. for (int i = 0; i < n; i++) {
  36. Node* newNode = (Node*)malloc(sizeof(Node));
  37. newNode->data = i + 1;
  38. newNode->next = NULL;
  39. if (head == NULL) {
  40. head = newNode;
  41. current = newNode;
  42. }
  43. else {
  44. current->next = newNode;
  45. current = newNode;
  46. }
  47. }
  48. return head;
  49. }
  50. void printList(Node* head) {
  51. Node* current = head;
  52. while (current != NULL) {
  53. printf("%d ", current->data);
  54. current = current->next;
  55. }
  56. printf("\n");
  57. }
  58. void deleteList(Node* head) {
  59. Node* current = head;
  60. while (current != NULL) {
  61. Node* next = current->next;
  62. free(current);
  63. current = next;
  64. }
  65. }
  66. void replaceElement(Node* head, int index, int value) {
  67. Node* current = head;
  68. int i = 0;
  69. if (index < 0) { // Ïðîâåðêà íà îòðèöàòåëüíîå çíà÷åíèå
  70. printf("Íåäîïóñòèìîå çíà÷åíèå: îòðèöàòåëüíûå çíà÷åíèÿ íåäîïóñòèìû\n");
  71. return;
  72. }
  73. while (current != NULL) {
  74. if (i == index) {
  75. current->data = value;
  76. return;
  77. }
  78. current = current->next;
  79. i++;
  80. }
  81. printf("Èíäåêñ âûøåë çà ãðàíèöû\n");
  82. }
  83. int main() {
  84. system("chcp 1251 > null");
  85. printf("Çàäàíèå1 - '1'\n"
  86. "Çàäàíèå2 - '2'\n"
  87. "Çàäàíèå3 - '3'\n");
  88. int input = 0;
  89. // 1.
  90. int arr[] = { 1, 2, 3, 4, 5 };
  91. int size = sizeof(arr) / sizeof(arr[0]);
  92. int* result1 = func(triple, arr, &size);
  93. int* result2 = func(divide, arr, &size);
  94. // 2.
  95. int n = 0;
  96. // 3.
  97. int listSize = 0;
  98. Node* head = createList(listSize);
  99. scanf_s("%d", &input);
  100. switch (input)
  101. {
  102. case 1:
  103. printf("Original array: ");
  104. for (int i = 0; i < size; i++) {
  105. printf("%d ", arr[i]);
  106. }
  107. printf("\nTripled array: ");
  108. for (int i = 0; i < size; i++) {
  109. printf("%d ", result1[i]);
  110. }
  111. printf("\nDivided array: ");
  112. for (int i = 0; i < size; i++) {
  113. printf("%d ", result2[i]);
  114. }
  115. printf("\n");
  116. free(result1);
  117. free(result2);
  118. break;
  119. case 2:
  120. // 2. Ôóíêöèÿ printTriangleMatrix
  121. printf("\nÂâåäèòå ÷èñëî: ");
  122. scanf_s("%d", &n);
  123. printf("\nÒðåóãîëüíàÿ ìàòðèöà: \n");
  124. printTriangleMatrix(n);
  125. break;
  126. case 3:
  127. // 3. Ôóíêöèè ðàáîòû ñî ñïèñêîì
  128. printf("\nÂâåäèòå ÷èñëî: ");
  129. scanf_s("%d", &listSize);
  130. printf("\nÑîçäàíèå ñïèñêà...");
  131. printList(head);
  132. deleteList(head);
  133. printf("Óäàëåíèå ñïèñêà...\n");
  134. head = createList(listSize);
  135. printf("Ñîçäàííûé ñïèñîê: ");
  136. printList(head);
  137. replaceElement(head, 2, 100);
  138. printf("Ñïèñîê ïîñëå çàìåíû ýëåìåíòà:");
  139. printList(head);
  140. deleteList(head);
  141. printf("Óäàëåíèå ñïèñêà...\n");
  142. break;
  143. }
  144. return 0;
  145. }