Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define FILE_PATH "D:\\Task5\\Task5.txt"
- //============================== STRUCTURES
- typedef struct {
- char* lines;
- int size;
- } FileLines;
- typedef struct {
- int x;
- int y;
- } Point;
- typedef struct {
- Point p1;
- Point p2;
- int slope;
- } Line;
- //============================== PROTOTYPES
- FILE* getFile();
- FileLines getLines(FILE* file);
- Point *getPointsFromFileLines(FileLines file);
- Point getPointFromString(char* string);
- Line *getAllLines(Point* coords,int size);
- int countParralelLines(Line* lines, int size);
- //============================== FUNCTIONS
- FILE* getFile() { //
- FILE* file;
- file = fopen(FILE_PATH,"r");
- if (file == NULL)
- {
- printf("Failed to open file \n");
- exit(EXIT_FAILURE);
- }
- else
- printf("File opened for reading \n");
- return file;
- }
- FileLines getLines(FILE *file) {
- char c;
- FileLines f = {0,0};
- int fileSize = sizeof(f.lines)/sizeof(f.lines[0]);
- char *line = 0;
- int size = sizeof(line)/sizeof(line[0]);
- while(!feof(file)) {
- while(c != '\n'){
- c = getc(file);
- line = realloc(line, ++size);
- line[size-1] = c;
- }
- f.lines = realloc(f.lines, ++fileSize);
- f.lines[fileSize-1] = *line;
- }
- return f;
- }
- Point *getPointsFromFileLines(FileLines file) {
- Point* p = 0;
- int i;
- int size = (sizeof(p)/sizeof(p[0]));
- for(i = 0; i < file.size; i++) {
- p = realloc(p, (size*(i+1)));
- getPointFromString((char *)file.lines[i]);
- }
- return p;
- }
- Point getPointFromString(char* string) {
- Point p = {0,0};
- while(*string != '\n') { //
- if(*string == 'x') {
- do {
- if(isdigit(*string))
- break;
- string++;
- } while(*string); //
- p.x = (int) strtol(string, &string, 10);
- }
- if(*string == 'y') { //
- do {
- if(isdigit(*string))
- break;
- string++;
- } while(*string); //
- p.y = (int) strtol(string, &string, 10);
- }
- }
- return p;
- }
- Line *getAllLines(Point* coords,int size) {
- int number_of_lines = factorial(size);
- Line l[number_of_lines];
- int i, j;
- for(i = 0; i < number_of_lines; i++) {
- for(j = i; j < number_of_lines; j++) {
- l[j].p1 = coords[i];
- l[j].p2 = coords[j+1];
- }
- }
- return l;
- }
- int factorial(unsigned int i) {
- if(i <= 1)
- return 1;
- return (i + factorial(i-1));
- }
- int countParralelLines(Line* lines,int size) {
- int count;
- int i, j;
- for(i = 0; i < size; i++) {
- for(j = 0; j < size; i++) {
- if(i==j) continue;
- if(lines[i].slope == lines[j].slope)
- count++;
- }
- }
- return count;
- }
- //============================== MAIN PART
- int main()
- {
- FileLines f = {0,0};
- FILE *file;
- file = getFile();
- printf("1 \n");
- f = getLines(file);
- printf("2 \n");
- Point *coords = 0;
- coords = getPointsFromFileLines(f);
- printf("3 \n");
- int size = sizeof(coords)/sizeof(coords[0]);
- int possible_lines = factorial(size);
- Line lines[possible_lines];
- memcpy(lines, getAllLines(coords, size), (sizeof(lines)*sizeof(lines[0])));
- printf("4 \n");
- int count = countParralelLines(lines, possible_lines);
- printf("count: %i", count);
- return 0;
- }
Add Comment
Please, Sign In to add comment