Skip to content

Instantly share code, notes, and snippets.

@dertst
Created August 8, 2019 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dertst/182ac23c958d2bb75ad3c7f0af6b5a5d to your computer and use it in GitHub Desktop.
Save dertst/182ac23c958d2bb75ad3c7f0af6b5a5d to your computer and use it in GitHub Desktop.
#include "pch.h"
#include <iostream>
#include <stdio.h>
#include <Windows.h>
#include <ctime>
#include <cstdlib>
using namespace std;
struct Branch
{
int value;
Branch *left, *right;
};
int result;
int show(Branch *&Tree,int a)
{
if (Tree != NULL)
{
if (a < Tree->value)
{
result++;
}
show(Tree->left,a);
printf("%d ",Tree->value);
show(Tree->right,a);
}
return result;
}
void add_node(int value, Branch *&MyTree)
{
if (NULL == MyTree)
{
MyTree = new Branch;
MyTree->value = value;
MyTree->left = MyTree->right = NULL;
}
if (value < MyTree->value)
{
if (MyTree->left != NULL) add_node(value, MyTree->left);
else
{
MyTree->left = new Branch;
MyTree->left->left = MyTree->left->right = NULL;
MyTree->left->value = value;
}
}
if (value > MyTree->value)
{
if (MyTree->right != NULL) add_node(value, MyTree->right);
else
{
MyTree->right = new Branch;
MyTree->right->left = MyTree->right->right = NULL;
MyTree->right->value = value;
}
}
}
int* InputArray(int n)
{
FILE *f;
fopen_s(&f, "C:\\temp\\intut.txt", "wt");
int *collection = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++)
{
collection[i] = rand() % 100 + 1;
fprintf(f, "%d", collection[i]);
}
return collection;
fclose(f);
}
int CalculateArray(int* collection, int a,int n)
{
int result=0;
for (int i = 0; i < n; i++)
{
if (collection[i] > a)
{
result++;
}
}
return result;
}
struct Node {
int value;
struct Node *next;
struct Node *prev;
};
struct List {
struct Node* first;
struct Node* last;
};
struct List* create() {
struct List* list = (struct List*)malloc(sizeof(struct List));
list->first = NULL;
list->last = NULL;
return list;
}
void insert( struct List* list)
{
if (list->first == NULL && list->last == NULL)
{
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->value = rand() % 100 + 1;
node->next = NULL;
node->prev = NULL;
list->first = node;
list->last = node;
}
else {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->value = rand() % 100 + 1;
node->prev = list->last;
node->next = NULL;
list->last = node;
}
}
int ScrollList(int limit, struct List* list)
{
if (list == NULL || list->first == NULL)
{
return 0;
}
Node* curentNode = list->last;
int result = 0;
printf("коллекция");
do {
if (curentNode->value > limit)
{
result++;
}
printf("%d ", curentNode->value);
curentNode = curentNode->prev;
} while (curentNode != list->last && curentNode != NULL);
printf("\n");
return result;
}
int main()
{
setlocale(LC_ALL, "Russian");
int StartTime = clock();
printf("Выбирете тип коллекции\nМассив(1)\nДвусвязный список(2)\nДвоичное дерево(3)\nДля выбора нужного типа коллекции введите номер соответствующего типа\n");
int Type;
int n;
int Result;
scanf_s("%d", &Type);
printf("Введите размер коллекции");
printf("\n");
scanf_s("%d", &n);
Branch *Tree = NULL;
if (Type == 1)
{
int* collection=InputArray(n);
printf("Введите число с кототорым хотите сравнить элементы коллекции!");
printf("\n");
int a;
scanf_s("%d", &a);
Result=CalculateArray(collection, a, n);
}
else if(Type == 2)
{
printf("Введите число с кототорым хотите сравнить элементы коллекции!");
printf("\n");
int a;
scanf_s("%d", &a);
struct List* list = create();
for (int i = 0; i < n; i++)
{
insert(list);
}
Result = ScrollList(a, list);
}
else if(Type == 3)
{
int val;
printf("Введите число с кототорым хотите сравнить элементы коллекции!");
printf("\n");
int a;
scanf_s("%d", &a);
printf("коллекция\n");
for (int i = 0; i < n; i++)
{
val = rand() % 100 + 1;
add_node(val, Tree);
}
Result=show(Tree,a);
}
int Member;
if (Type == 1)
{
Member = sizeof(int) * 8 + sizeof(int)*n;
}
else if(Type == 2)
{
Member = sizeof(int) * 8 + sizeof(List);
}
else if (Type == 3)
{
Member = sizeof(int) * 9 + sizeof(Branch);
}
int EndTime = clock();
int Time = EndTime - StartTime;
printf("Количество чисел больше введённого: %d\nКоличество памяти требоемое для хранения данных: %d\nВремя выполнения операции: %d", Result,Member,Time);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment