Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- int main()
- {
- FILE *input_fd = NULL;
- input_fd = fopen("./input.txt", "r");
- if (input_fd == NULL)
- {
- printf("Error file pointer null.\n");
- exit(1);
- }
- ssize_t read;
- size_t len = 0;
- char *line = NULL;
- // First line is the number of tests;
- int ntests;
- // On success, getline() and getdelim() return the number of characters read, including the delimiter character, but not including the terminating null byte. This value can be used to handle embedded null bytes in the line read.
- read = getline(&line, &len, input_fd);
- // getline() reads an entire line from stream, storing the address of the buffer containing the text into *lineptr. The buffer is null-terminated and includes the newline character, if one was found.
- line[read] = '\0';
- if (!(ntests = atoi(line)))
- {
- printf("Error: Invalid number of tests\n");
- exit(1);
- }
- // Skip a line
- read = getline(&line, &len, input_fd);
- // We will get burst of NKNOWN points separated by a white space, we need to determine the fourth such that the four points make a rectangle.
- int x[4][ntests];
- memset(x, 0, sizeof(x));
- int y[4][ntests];
- memset(y, 0, sizeof(y));
- int cnt = 0;
- int i = 0;
- while ((read = getline(&line, &len, input_fd)) != -1)
- {
- line[read] = '\0';
- char *token = strtok(line, " ");
- x[i][cnt] = atoi(token);
- token = strtok(NULL, " ");
- y[i][cnt] = atoi(token);
- // When the coordinate matrix is full we perform calculations.
- if ((i++ == 2))
- {
- // The result is the non common X and non common y
- // 1 7
- // 4 7
- // 1 5
- // -> 4 5
- if (x[0][cnt] == x[1][cnt])
- {
- x[4][cnt] = x[2][cnt];
- }
- else if (x[0][cnt] == x[2][cnt])
- {
- printf("Cnt before line 68 %d\n", cnt);
- x[4][cnt] = x[1][cnt];
- printf("Cnt after line 68 %d\n", cnt);
- }
- else
- {
- x[4][cnt] = x[0][cnt];
- }
- if (y[0][cnt] == y[1][cnt])
- {
- y[4][cnt] = y[2][cnt];
- }
- else if (y[0][cnt] == y[2][cnt])
- {
- y[4][cnt] = y[1][cnt];
- }
- else
- {
- y[4][cnt] = y[0][cnt];
- }
- printf("Case #%d: %d %d\n\n", cnt, x[4][cnt], y[4][cnt]);
- // skip line
- getline(&line, &len, input_fd);
- cnt++;
- i = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement