Third
This commit is contained in:
v.ignatkin 2024-11-19 09:24:07 +04:00
parent a4ad6c8f1d
commit 3f9a7eb976

View File

@ -1,240 +1,69 @@
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <Windows.h>
struct Node {
int data;
struct Node* next;
};
struct Node* first = NULL; int* pa; //Óêàçàòåëü íà íà÷àëüíûé ýëåìåíò ìàññèâà.
int n; // Ðåàëüíîå êîëè÷åñòâî ýëåìåíòîâ â ìàññèâå
void printList() { void Load() {
struct Node* ptr = first; // Îòêðûòèå âõîäíîãî ôàéëà
while (ptr != NULL) { FILE* fin = fopen("c:\\Temp\\Lection14\\in3.txt", "rt");
printf("(%d) -> ", ptr->data); if (fin == NULL) {
ptr = ptr->next; printf("Âõîäíîé ôàéë íå íàéäåí\n");
return;
} }
printf("NULL\n"); // Çàãðóçêà ìàññèâà èç âõîäíîãî ôàéëà
} fscanf(fin, "%d", &n);
void addToHead(int value) { // Âûäåëåíèå ïàìÿòè ïîä äèíàìè÷åñêèé ìàññèâ
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); pa = (int*)malloc(sizeof(int) * n);
newNode->next = first; for (int i = 0; i < n; i++) {
newNode->data = value; fscanf(fin, "%d", &pa[i]);
}
// Çàêðûòèå âõîäíîãî ôàéëà
fclose(fin);
}
void SaveResult() {
// Âû÷èñëåíèå ñðåäíåãî àðèôìåòè÷åñêîãî
float sa = 0;
float s = 0;
for (int i = 0; i < n; i++) {
s += pa[i];
}
sa = s / n;
first = newNode; // Âûÿñíåíèå, ñêîëüêî ýëåìåíòîâ ìåíüøå ñð àðèôìåòè÷åñêîãî è áîëüøå 0
} int m = 0;
int deleteFromHead() { for (int i = 0; i < n; i++) {
int value = first->data; if (pa[i] < sa and pa[i] > 0) {
struct Node* delNode = first; m++;
first = first->next;
free(delNode);
return value;
}
int contains(int value) {
struct Node* ptr = first;
while (ptr != NULL) {
if (ptr->data == value) {
return 1;
} }
ptr = ptr->next;
} }
return 0; // Îòêðûòèå âûõîäíîãî ôàéëà
} FILE* fout = fopen("c:\\Temp\\Lection14\\out3.txt", "wt");
void clearList() { if (fout == NULL) {
while (first != NULL) printf("Âûõîäíîé ôàéë íå íàéäåí\n");
{ return;
struct Node* delNode = first;
first = first->next;
free(delNode);
} }
}
int sum() { // Ñîõðàíåíèå ýëåìåíòîâ íèæå ñð àðèôìåòè÷åñêîãî è áîëüøå 0
struct Node* ptr = first; fprintf(fout, "%d\n", m);
int s = 0; for (int i = 0; i < n; i++) {
while (ptr != NULL) { if (pa[i] < sa and pa[i] > 0) {
s += ptr->data; fprintf(fout, "%d ", pa[i]);
ptr = ptr->next;
}
return s;
}
int evenCount() {
struct Node* ptr = first;
int s = 0;
while (ptr != NULL) {
if (ptr->data % 2 == 0) {
s++;
} }
ptr = ptr->next;
}
return s;
}
int oddsX10() {
struct Node* ptr = first;
int s = 0;
while (ptr != NULL) {
if (ptr->data % 2 != 0) {
ptr->data = ptr->data * 10;
}
ptr = ptr->next;
}
return s;
}
void elementIx100(int i) {
struct Node* ptr = first;
int Index = 0;
while (ptr != NULL) {
if (Index == i) {
ptr->data = ptr->data * 100;
return;
}
ptr = ptr->next;
Index++;
}
}
void elementLeftIx10(int i) {
struct Node* ptr = first;
int Index = 0;
while (ptr != NULL and Index < i) {
if (Index >= 0) {
ptr->data *= 10;
}
ptr = ptr->next;
Index++;
} }
// Çàêðûòèå ôàéëà
fclose(fout);
} }
void main() { void main() {
printf("\nTask 1!\n"); SetConsoleCP(1251);
first = NULL; SetConsoleOutputCP(1251);
printList(); printf("Hello! It is Task3!\n");
addToHead(400); Load();
addToHead(300); SaveResult();
addToHead(200);
addToHead(100);
printList();
int x1 = deleteFromHead();
printf("x1 = %d\n", x1);
printList();
printf("contains(100) = %d\n", contains(100));
printf("contains(150) = %d\n", contains(150));
printf("contains(200) = %d\n", contains(200));
clearList();
printList();
printf("contains(100) = %d\n", contains(100));
printf("contains(150) = %d\n", contains(150));
printf("contains(200) = %d\n", contains(200));
printf("\nTask 2!\n");
printList();
addToHead(1);
addToHead(3);
addToHead(6);
addToHead(9);
printList();
printf("sum = %d\n", sum());
clearList();
printList();
printf("sum = %d\n", sum());
{
int x;
scanf_s("%d", &x);
}
printf("\nTask 3!\n");
printList();
addToHead(1);
addToHead(3);
addToHead(6);
addToHead(8);
printList();
printf("even numbers = %d\n", evenCount());
clearList();
printList();
printf("even numbers = %d\n", evenCount());
{
int x;
scanf_s("%d", &x);
}
printf("\nTask 4!\n");
printList();
addToHead(1);
addToHead(3);
addToHead(6);
addToHead(8);
printList();
oddsX10();
printList();
clearList();
{
int x;
scanf("%d", &x);
}
printf("\nTask 5!\n");
printList();
addToHead(1);
addToHead(3);
addToHead(6);
addToHead(8);
printList();
elementIx100(0);
printList();
elementIx100(2);
printList();
elementIx100(0);
printList();
clearList();
{
int x;
scanf("d", &x);
}
printf("\nTask 6!\n");
printList();
addToHead(1);
addToHead(3);
addToHead(6);
addToHead(8);
printList();
elementLeftIx10(1);
printList();
elementLeftIx10(2);
printList();
elementLeftIx10(4);
printList();
clearList();
{
int x;
scanf("%d", &x);
}
}
free(pa);
}