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