verified insertion of vectors into vectors; proved working
This commit is contained in:
@@ -26,11 +26,10 @@ I got bored with having to use complicated checking and stuff when working with
|
||||
4. Getting and setting a value of a vector on a given index
|
||||
5. Pushing a value at the end of a vector
|
||||
6. Inserting a value into a vector at a given index
|
||||
7. Insertion of a vector into another vector
|
||||
|
||||
#### TODO:
|
||||
|
||||
1. Insertion of a vector into another vector
|
||||
|
||||
+ You tell me
|
||||
|
||||
|
||||
|
||||
@@ -4,91 +4,26 @@
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
printf("demonstration of the power of dynamically allocated arrays in c:\n-----------\n\n");
|
||||
vector_t *vv = vectorInit(VECTOR);
|
||||
vector_t *fv = vectorInit(FLOAT);
|
||||
vectorPrint(vv);
|
||||
|
||||
printf("initializing an integer vector\n");
|
||||
vector_t *v = vectorInit(INT);
|
||||
vectorPush(fv, &(float){3.14});
|
||||
vectorPush(fv, &(float){5.123});
|
||||
|
||||
printf("vector after initialization\n");
|
||||
vectorPrint(v);
|
||||
vectorPush(vv, fv);
|
||||
vectorPush(vv, fv);
|
||||
vectorPush(vv, fv);
|
||||
|
||||
printf("pushing an element of value 1 into vector\n");
|
||||
int element = 1;
|
||||
vectorPush(v, &element);
|
||||
vectorPrint(v);
|
||||
vectorPush(fv, &(float){156.1});
|
||||
vectorPush(fv, &(float){6.124});
|
||||
|
||||
printf("pushing another element (value 2) into vector\n");
|
||||
element = 2;
|
||||
vectorPush(v, &element);
|
||||
vectorPrint(v);
|
||||
vectorInsertAt(vv, 1, fv);
|
||||
|
||||
printf("inserting 3 at index 1\n");
|
||||
element = 3;
|
||||
vectorInsertAt(v, 1, &element);
|
||||
vectorPrint(v);
|
||||
vectorPrint(vv);
|
||||
|
||||
printf("popping the last value from the vector\n");
|
||||
int popped = *((int *)vectorPop(v));
|
||||
printf("popped: %d\n", popped);
|
||||
printf("vector after popping\n");
|
||||
vectorPrint(v);
|
||||
|
||||
printf("initializing a second vector of type vector_t (multidimensional vector => vector of vectors)\n");
|
||||
vector_t *w = vectorInit(VECTOR);
|
||||
|
||||
printf("vector after initialization\n");
|
||||
vectorPrint(w);
|
||||
|
||||
printf("pushing the first vector into the newly created one\n");
|
||||
vectorPush(w, v);
|
||||
vectorPrint(w);
|
||||
printf("once again\n");
|
||||
vectorPush(w, v);
|
||||
vectorPrint(w);
|
||||
printf("popping\n");
|
||||
vector_t *poppedv = (vector_t *)vectorPop(w);
|
||||
printf("popped vector\n");
|
||||
vectorPrint(poppedv);
|
||||
printf("vector from which was popped after pop\n");
|
||||
vectorPrint(w);
|
||||
|
||||
printf("initializing a char vector (variable-length string) with value \"Hello, world!\"\n");
|
||||
vector_t *charVector = vectorInit(CHAR);
|
||||
char *word = "Hello, world!";
|
||||
for (int i = 0; i < strlen(word); i++) {
|
||||
vectorPush(charVector, &word[i]);
|
||||
}
|
||||
printf("char vector as string\n");
|
||||
vectorPrintAsString(charVector);
|
||||
|
||||
printf("char vector converted into a regular string\n");
|
||||
char *newWord = vectorToString(charVector);
|
||||
printf("new word: %s\n", newWord);
|
||||
|
||||
printf("setting a value in the char vector on index 10 to \"\" (nothing, effectively deleting the character, although the size of the vector stays the same!)\n");
|
||||
vectorSetAt(charVector, 10, ""); // should change the vector to "Hello, word!" (size stays the same!)
|
||||
vectorPrintAsString(charVector);
|
||||
|
||||
printf("files section\n---------------\n");
|
||||
printf("initializing a file called test.txt with mode \"r\" (reading it)\n");
|
||||
file_t* f = fileInit("test.txt", "r");
|
||||
|
||||
printf("printing the file contents\n");
|
||||
filePrint(f);
|
||||
printf("file length (line count): %d\n", f->length);
|
||||
printf("file as vector\n");
|
||||
vectorPrint(f->lines);
|
||||
printf("file to string\n");
|
||||
char* strFile = fileToString(f);
|
||||
printf("%s", strFile);
|
||||
|
||||
printf("cleaning up vectors\n");
|
||||
vectorCleanup(v);
|
||||
vectorCleanup(w);
|
||||
vectorCleanup(charVector);
|
||||
|
||||
printf("cleaning up files\n");
|
||||
fileCleanup(f);
|
||||
vectorCleanup(vv);
|
||||
vectorCleanup(fv);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user