almost final

This commit is contained in:
Valér Jakubčo 2021-12-02 16:17:47 +01:00
parent f37d69ab7b
commit 7b86b6ba5f

View File

@ -1,67 +1,113 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <math.h>
#define SIZE 20 #define SIZE 100
int checkMinHeap(const long* arr, const int size){ int checkMinHeap(const long* arr, const int size){
//printf("size: %d\n",size);
if(size == 1){ if(size == 1){
return 1; return 1;
} }
if(size == 2){
if(arr[0] < arr[1]){
return 1;
}else {
return -1;
}
}
for (int i = 0; i<=(size-2)/2; i++) { for (int i = 0; i<=(size-2)/2; i++) {
if(arr[2*i] < arr[i]) return -1; if(arr[2*i+1] < arr[i]) return -1;
if(arr[2*i+1] < arr[i]) return -1; if(arr[2*i] < arr[i]) return -1;
} }
return 1; return 1;
} }
long int* strToInt(const char* chArr){ long int* strToInt(const char* chArr, int* counter){
int j = strlen(chArr); int j = strlen(chArr);
//int i = 0; //int i = 0;
int counter=0; *counter=0;
//printf("dlength %d\n",j/2-1); //printf("dlength %d\n",j);
long int* tmp = (long int*)calloc(j,sizeof(long)); long int* tmp = (long int*)calloc(j,sizeof(long));
char* pEND; char* pEND;
for(int i=0; i<j; i++){ for(int i=0; i<j; i++){
tmp[i] = strtol(chArr, &pEND, 10); tmp[i] = strtol(chArr, &pEND, 10);
//printf("%ld \n", dArr[i]); //printf("%ld \n", dArr[i]);
if(tmp[i] == 0) break; if(tmp[i] == 0) break;
counter++; *counter = *counter+1;
chArr = pEND; chArr = pEND;
} }
/* for(int i = 0; i<j/2-1; i++){ //printf("counter %d\n", *counter);
printf("%ld ", dArr[i]); long* dArr = (long*)calloc(*counter, sizeof(long));
}*/ for(int i; i < *counter; i++){
//printf("counter %d\n", counter);
long* dArr = (long*)calloc(counter, sizeof(long));
for(int i; i < counter; i++){
dArr[i] = tmp[i]; dArr[i] = tmp[i];
} }
free(tmp); free(tmp);
return dArr; return dArr;
} }
void print_tree(const long* arr){ void print_tree(const long* arr, int* size){
long size = sizeof(&arr)/sizeof(arr[0]); for(int i=0; i<*size; i++){
for(int i = 0; i < size; i++){ switch (i) {
printf("%ld\n", arr[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;
}
}
} }
int main(){ int main(){
char chArr[SIZE]; char chArr[SIZE];
int counter;
int* counterPtr = &counter;
while(1){ while(1){
char* r = fgets(chArr, SIZE, stdin); char* r = fgets(chArr, SIZE, stdin);
if(r == NULL){ if(r == NULL){
break; break;
} }
} }
long int* dArr = strToInt(chArr); long int* dArr = strToInt(chArr, counterPtr);
int heap = checkMinHeap(dArr, *counterPtr);
int heap = checkMinHeap(dArr, sizeof(&dArr)/sizeof(long));
if(heap == 1){ if(heap == 1){
printf("Je to taka kopa:\n"); printf("Je to taka kopa:\n");
print_tree(dArr); print_tree(dArr, counterPtr);
}else {
printf("Nie je kopa.\n");
} }
free(dArr); free(dArr);
return 0; return 0;