Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define ARRAY_AMOUNT 500
- // structs type
- struct Paragraph {
- int index;
- char buff[ARRAY_AMOUNT];
- };
- struct ParagraphArray {
- struct Paragraph *buffpoint;
- int size;
- };
- // This is my function
- int itemIndex(char *point);
- int itemIndex2(const char *point, int curIndex);
- struct Paragraph firstPar(char *point);
- struct ParagraphArray *getAllParagraphsArrayNew(const char *str);
- void arrayFree(struct ParagraphArray *array);
- void bufChange(const char *point, char *buffer, int size);
- bool isEqualPar(struct Paragraph temp1, struct Paragraph temp2);
- bool isEqualParArr(struct ParagraphArray temp1, struct ParagraphArray temp2);
- int main(void) {
- // First task // main
- char str[] = "1 - First\n\n2 - Second\n\n3 - Third\n\n4 - Fourth\n\n5 - Fifth";
- //char str2[] = "1 - First\n\n2 - Second";
- char buffer[] = {"Bufer\n\nI am so cool\n\nI am a Buffer"};
- int size = strlen(buffer);
- assert(itemIndex(str) == 11);
- assert((itemIndex2(&buffer[0], 10)) == 21);
- assert(itemIndex2(str, 0) == 11);
- bufChange(&str[0], &buffer[0], size);
- // Second task // main
- struct Paragraph x = {1, "jalhsdfj"};
- struct Paragraph y = {1, "jalhsdfj"};
- // This struct created only to cheak "isEqualPar"
- struct Paragraph comparison = {.index = 0, .buff = "1 - First"};
- assert(isEqualPar(x, y) == 1);
- assert(isEqualPar(firstPar(str), x) == 0);
- assert(isEqualPar(firstPar(str), comparison) == 1);
- // This structs created to check isEqualParArr
- struct Paragraph buf[3] = {
- {2, "Buffer filling"}, {15, "Bla - Bla - Bool"}, {300, "(x^2)` = 2*x"}};
- struct ParagraphArray temp1 = {.size = 3, .buffpoint = &buf[0]};
- struct Paragraph buf2[3] = {
- {2, "Buffer filling"}, {15, "Bla - Bla - Bool"}, {300, "(x^2)` = 2*x"}};
- struct ParagraphArray temp2 = {.size = 3, .buffpoint = &buf2[0]};
- struct Paragraph buf3[3] = {{4, "Tra - Ta - Ta "},
- {15, "Bla - Bla - Bool swimming pool"},
- {12, "I can in C "}};
- struct ParagraphArray temp3 = {.size = 3, .buffpoint = &buf3[0]};
- assert(isEqualParArr(temp1, temp2) == 1);
- assert(isEqualParArr(temp1, temp3) == 0);
- struct ParagraphArray *d = getAllParagraphsArrayNew(str);
- for (int i = 0; i < d->size; i++) {
- printf("%s\n", d->buffpoint[i].buff);
- }
- arrayFree(d);
- return 0;
- }
- // FIRST TASK
- int itemIndex(char *point) {
- int curr = 0;
- for (char *i = point; *i != '\0'; i++, curr++) {
- if (*i == '\n' && *(i + 1) == '\n' && *(i + 2) != '\0') {
- return curr + 2;
- }
- }
- return -1;
- }
- int itemIndex2(const char *point, int curIndex) {
- for (const char *i = point + curIndex; *i != '\0'; i++, curIndex++) {
- if (*i == '\n' && *(i + 1) == '\n' && *(i + 2) != '\0') {
- return curIndex + 2;
- }
- }
- return -1;
- }
- void bufChange(const char *point, char *buffer, int size) {
- for (int i = 0; i < size - 1; i++) {
- if ( point[i + 1] == '\0' || (point[i] == '\n' && point[i + 1] == '\n') )
- break;
- *(buffer + i) = *(point + i);
- };
- }
- // SECOND TASK
- bool isEqualPar(struct Paragraph temp1, struct Paragraph temp2) {
- if (temp1.index == temp2.index && !strcmp(temp1.buff, temp2.buff))
- return true;
- return false;
- }
- bool isEqualParArr(struct ParagraphArray temp1, struct ParagraphArray temp2) {
- if (temp1.size == temp2.size) {
- for (int i = 0; i < temp1.size; i++) {
- if (isEqualPar(temp1.buffpoint[i], temp2.buffpoint[i]) == 0)
- return false;
- }
- return true;
- }
- return false;
- }
- struct Paragraph firstPar(char *point) {
- struct Paragraph CurrPar = {.index = 0, .buff = "\0"};
- for (int i = 0; point != NULL && i < ARRAY_AMOUNT; i++) {
- if ((point[i] == '\n' && point[i + 1] == '\n') || (i == ARRAY_AMOUNT - 1)) {
- CurrPar.buff[i] = '\0';
- return CurrPar;
- }
- CurrPar.buff[i] = point[i];
- }
- return CurrPar;
- }
- // THIRD TASK
- struct ParagraphArray * getAllParagraphsArrayNew(const char *str) {
- int counter = 0;
- int index = 0;
- for (int i = 0; i < strlen(str); i++) {
- if (str[i + 1] == '\0' || (str[i] == '\n' && str[i + 1] == '\n'))
- counter++;
- }
- struct ParagraphArray * masspoint = malloc(sizeof(struct ParagraphArray));
- masspoint->size = counter;
- masspoint->buffpoint = calloc(sizeof(struct Paragraph), counter);
- for (int i = 0; i < counter; i++) {
- bufChange(str + index, masspoint->buffpoint[i].buff, ARRAY_AMOUNT);
- index = itemIndex2(str, index);
- }
- return masspoint;
- }
- // remember *(name).data == name->data
- void arrayFree(struct ParagraphArray *array) {
- if (array->buffpoint != NULL)
- free(array->buffpoint);
- free(array);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement