Created
December 7, 2015 21:08
-
-
Save Gelio/ecaef493566907e8c516 to your computer and use it in GitHub Desktop.
Ruchoma tablica w C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #include <time.h> | |
| #define ACHUNK_LEN 5 | |
| #define RCHUNK_LEN 4 | |
| int len = 0; | |
| int *pini(int *len, int firstvalue); | |
| void wypisz(int **ppmymovingtable, int len); | |
| void zwolnij(int **ppmymovingtable, int *len); | |
| void chunq(int **ppmymovingtable, int iwhat, int *len); | |
| void qdbl(int **ppmymovingtable, int *len); | |
| void chundq(int **ppmymovingtable, int *len); | |
| int main() | |
| { | |
| int *ppMyMovingTable, firstValue; | |
| scanf_s("%d", &firstValue); | |
| ppMyMovingTable = pini(&len, firstValue); | |
| wypisz(&ppMyMovingTable, len); | |
| chunq(&ppMyMovingTable, 5, &len); | |
| wypisz(&ppMyMovingTable, len); | |
| qdbl(&ppMyMovingTable, &len); | |
| wypisz(&ppMyMovingTable, len); | |
| chundq(&ppMyMovingTable, &len); | |
| wypisz(&ppMyMovingTable, len); | |
| zwolnij(&ppMyMovingTable, &len); | |
| system("PAUSE"); | |
| return 0; | |
| } | |
| // ================================================================================ | |
| int *pini(int *len, int firstvalue) | |
| { | |
| int *tablica = (int*)malloc(sizeof(int)); | |
| if (tablica == NULL) | |
| return NULL; | |
| *tablica = firstvalue; | |
| *len = 1; | |
| return tablica; | |
| } // pini | |
| void wypisz(int **ppmymovingtable, int len) | |
| { | |
| int i; | |
| if (*ppmymovingtable == NULL) | |
| { | |
| printf("Nie mozna bylo zaalokowac pamieci na tablice.\n"); | |
| return; | |
| } | |
| printf("Dlugosc: %d\n", len); | |
| for (i = 0; i < len; i++) | |
| printf("%d ", *(*ppmymovingtable + i)); | |
| printf("\n"); | |
| return; | |
| } // wypisz | |
| void zwolnij(int **ppmymovingtable, int *len) | |
| { | |
| if (*ppmymovingtable == NULL) | |
| return; | |
| free(*ppmymovingtable); | |
| *len = 0; | |
| return; | |
| } // zwolnij | |
| void chunq(int **ppmymovingtable, int iwhat, int *len) | |
| { | |
| int i; | |
| if (*ppmymovingtable == NULL) | |
| { | |
| printf("Tablica nie istnieje i nie mozna nic dodac.\n"); | |
| return; | |
| } | |
| *ppmymovingtable = (int*)realloc((void*)*ppmymovingtable, (*len + ACHUNK_LEN) * sizeof(int)); | |
| if (*ppmymovingtable == NULL) | |
| { | |
| printf("Nie mozna zreallokowac danych\n"); | |
| return; | |
| } | |
| for (i = 0; i < ACHUNK_LEN; i++, *len = *len + 1) | |
| *(*ppmymovingtable + *len) = iwhat; | |
| return; | |
| } //chunq | |
| void qdbl(int **ppmymovingtable, int *len) | |
| { | |
| int i, baseLen = len; | |
| if (*ppmymovingtable == NULL) | |
| { | |
| printf("Tablica nie istnieje i nie mozna nic dodac.\n"); | |
| return; | |
| } | |
| *ppmymovingtable = (int*)realloc((void*)*ppmymovingtable, (*len * 2) * sizeof(int)); | |
| if (*ppmymovingtable == NULL) | |
| { | |
| printf("Nie mozna zreallokowac danych\n"); | |
| return; | |
| } | |
| for (i = 0; i < *len; i++) | |
| *(*ppmymovingtable + *len + i) = *(*ppmymovingtable + i); | |
| *len = *len * 2; | |
| return; | |
| } // qdbl | |
| void chundq(int **ppmymovingtable, int *len) | |
| { | |
| int i, newLen = max(*len - RCHUNK_LEN, 0); | |
| if (*ppmymovingtable == NULL) | |
| { | |
| printf("Tablica nie istnieje i nie mozna nic dodac.\n"); | |
| return; | |
| } | |
| *ppmymovingtable = (int*)realloc((void*)*ppmymovingtable, newLen * sizeof(int)); | |
| if (*ppmymovingtable == NULL) | |
| { | |
| printf("Nie mozna zreallokowac danych\n"); | |
| return; | |
| } | |
| *len = newLen; | |
| return; | |
| } // chundq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment