123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- // 1.
- int triple(int x) {
- return x * 3;
- }
- int divide(int x) {
- return x / 3;
- }
- int* func(int (*f)(int), int* arr, int* size) {
- int* result = (int*)malloc(sizeof(int) * (*size));
- for (int i = 0; i < *size; i++) {
- result[i] = f(arr[i]);
- }
- return result;
- }
- // 2.
- void printTriangleMatrix(int n) {
- for (int i = n; i >= 0; i--) {
- for (int j = i; j >= 0; j--) {
- printf("%d ", j);
- }
- printf("\n");
- }
- }
- // 3.
- typedef struct Node {
- int data;
- struct Node* next;
- } Node;
- Node* createList(int n) {
- Node* head = NULL;
- Node* current = NULL;
- for (int i = 0; i < n; i++) {
- Node* newNode = (Node*)malloc(sizeof(Node));
- newNode->data = i + 1;
- newNode->next = NULL;
- if (head == NULL) {
- head = newNode;
- current = newNode;
- }
- else {
- current->next = newNode;
- current = newNode;
- }
- }
- return head;
- }
- void printList(Node* head) {
- Node* current = head;
- while (current != NULL) {
- printf("%d ", current->data);
- current = current->next;
- }
- printf("\n");
- }
- void deleteList(Node* head) {
- Node* current = head;
- while (current != NULL) {
- Node* next = current->next;
- free(current);
- current = next;
- }
- }
- void replaceElement(Node* head, int index, int value) {
- Node* current = head;
- int i = 0;
- if (index < 0) { // Ïðîâåðêà íà îòðèöàòåëüíîå çíà÷åíèå
- printf("Íåäîïóñòèìîå çíà÷åíèå: îòðèöàòåëüíûå çíà÷åíèÿ íåäîïóñòèìû\n");
- return;
- }
- while (current != NULL) {
- if (i == index) {
- current->data = value;
- return;
- }
- current = current->next;
- i++;
- }
- printf("Èíäåêñ âûøåë çà ãðàíèöû\n");
- }
- int main() {
- system("chcp 1251 > null");
- printf("Çàäàíèå1 - '1'\n"
- "Çàäàíèå2 - '2'\n"
- "Çàäàíèå3 - '3'\n");
- int input = 0;
- // 1.
- int arr[] = { 1, 2, 3, 4, 5 };
- int size = sizeof(arr) / sizeof(arr[0]);
- int* result1 = func(triple, arr, &size);
- int* result2 = func(divide, arr, &size);
- // 2.
- int n = 0;
- // 3.
- int listSize = 0;
- Node* head = createList(listSize);
- scanf_s("%d", &input);
- switch (input)
- {
- case 1:
- printf("Original array: ");
- for (int i = 0; i < size; i++) {
- printf("%d ", arr[i]);
- }
- printf("\nTripled array: ");
- for (int i = 0; i < size; i++) {
- printf("%d ", result1[i]);
- }
- printf("\nDivided array: ");
- for (int i = 0; i < size; i++) {
- printf("%d ", result2[i]);
- }
- printf("\n");
- free(result1);
- free(result2);
- break;
- case 2:
- // 2. Ôóíêöèÿ printTriangleMatrix
- printf("\nÂâåäèòå ÷èñëî: ");
- scanf_s("%d", &n);
- printf("\nÒðåóãîëüíàÿ ìàòðèöà: \n");
- printTriangleMatrix(n);
- break;
- case 3:
- // 3. Ôóíêöèè ðàáîòû ñî ñïèñêîì
-
- printf("\nÂâåäèòå ÷èñëî: ");
- scanf_s("%d", &listSize);
- printf("\nÑîçäàíèå ñïèñêà...");
- printList(head);
- deleteList(head);
- printf("Óäàëåíèå ñïèñêà...\n");
- head = createList(listSize);
- printf("Ñîçäàííûé ñïèñîê: ");
- printList(head);
- replaceElement(head, 2, 100);
- printf("Ñïèñîê ïîñëå çàìåíû ýëåìåíòà:");
- printList(head);
- deleteList(head);
- printf("Óäàëåíèå ñïèñêà...\n");
- break;
- }
- return 0;
- }
|