usaa21/a4/program.c

114 lines
2.8 KiB
C
Raw Normal View History

2021-12-01 19:33:09 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2021-12-02 15:17:47 +00:00
#include <math.h>
2021-12-01 19:33:09 +00:00
2021-12-02 15:17:47 +00:00
#define SIZE 100
2021-12-01 19:33:09 +00:00
int checkMinHeap(const long* arr, const int size){
2021-12-02 15:17:47 +00:00
//printf("size: %d\n",size);
2021-12-02 02:31:27 +00:00
if(size == 1){
return 1;
}
2021-12-02 15:17:47 +00:00
if(size == 2){
if(arr[0] < arr[1]){
return 1;
}else {
return -1;
}
}
2021-12-01 19:33:09 +00:00
for (int i = 0; i<=(size-2)/2; i++) {
2021-12-02 15:17:47 +00:00
if(arr[2*i+1] < arr[i]) return -1;
if(arr[2*i] < arr[i]) return -1;
2021-12-01 19:33:09 +00:00
}
return 1;
}
2021-12-02 15:17:47 +00:00
long int* strToInt(const char* chArr, int* counter){
2021-12-01 19:33:09 +00:00
int j = strlen(chArr);
//int i = 0;
2021-12-02 15:17:47 +00:00
*counter=0;
//printf("dlength %d\n",j);
2021-12-02 02:31:27 +00:00
long int* tmp = (long int*)calloc(j,sizeof(long));
2021-12-01 19:33:09 +00:00
char* pEND;
for(int i=0; i<j; i++){
2021-12-02 02:31:27 +00:00
tmp[i] = strtol(chArr, &pEND, 10);
2021-12-01 19:33:09 +00:00
//printf("%ld \n", dArr[i]);
2021-12-02 02:31:27 +00:00
if(tmp[i] == 0) break;
2021-12-02 15:17:47 +00:00
*counter = *counter+1;
2021-12-01 19:33:09 +00:00
chArr = pEND;
}
2021-12-02 15:17:47 +00:00
//printf("counter %d\n", *counter);
long* dArr = (long*)calloc(*counter, sizeof(long));
for(int i; i < *counter; i++){
2021-12-02 02:31:27 +00:00
dArr[i] = tmp[i];
}
free(tmp);
2021-12-01 19:33:09 +00:00
return dArr;
}
2021-12-02 15:17:47 +00:00
void print_tree(const long* arr, int* size){
for(int i=0; i<*size; i++){
switch (i) {
case 0:
printf("%ld\n",arr[i]);
continue;;
case 1:
printf(" ");
printf("%ld\n",arr[i]);
continue;;
case 2:
printf(" ");
printf("%ld\n",arr[i]);
continue;
case 3:
printf("%ld\n",arr[i]);
continue;
case 4:
printf(" ");
printf("%ld\n",arr[i]);
continue;
case 5:
printf(" ");
printf("%ld\n",arr[i]);
break;
case '6':
printf("%ld",arr[i]);
break;
case '7':
printf("%ld",arr[i]);
break;
case '8':
printf("%ld",arr[i]);
break;
case '9':
printf("%ld",arr[i]);
break;
}
}
2021-12-01 19:33:09 +00:00
}
int main(){
char chArr[SIZE];
2021-12-02 15:17:47 +00:00
int counter;
int* counterPtr = &counter;
2021-12-01 19:33:09 +00:00
while(1){
char* r = fgets(chArr, SIZE, stdin);
if(r == NULL){
break;
}
}
2021-12-02 15:17:47 +00:00
long int* dArr = strToInt(chArr, counterPtr);
int heap = checkMinHeap(dArr, *counterPtr);
2021-12-01 19:33:09 +00:00
if(heap == 1){
printf("Je to taka kopa:\n");
2021-12-02 15:17:47 +00:00
print_tree(dArr, counterPtr);
}else {
printf("Nie je kopa.\n");
2021-12-01 19:33:09 +00:00
}
free(dArr);
return 0;
}