Guest User

Task 5

a guest
Dec 15th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #define FILE_PATH "D:\\Task5\\Task5.txt"
  5.  
  6. //==============================    STRUCTURES
  7.  
  8. typedef struct {
  9.     char* lines;
  10.     int size;
  11. } FileLines;
  12.  
  13. typedef struct {
  14.     int x;
  15.     int y;
  16. } Point;
  17.  
  18. typedef struct {
  19.     Point p1;
  20.     Point p2;
  21.     int slope;
  22. } Line;
  23.  
  24. //==============================    PROTOTYPES
  25. FILE* getFile();
  26. FileLines getLines(FILE* file);
  27. Point *getPointsFromFileLines(FileLines file);
  28. Point getPointFromString(char* string);
  29. Line *getAllLines(Point* coords,int size);
  30. int countParralelLines(Line* lines, int size);
  31. //==============================    FUNCTIONS
  32.  
  33. FILE* getFile() {                               //
  34.     FILE* file;
  35.     file = fopen(FILE_PATH,"r");
  36.     if (file == NULL)
  37.     {
  38.         printf("Failed to open file \n");
  39.         exit(EXIT_FAILURE);
  40.     }
  41.     else
  42.         printf("File opened for reading \n");
  43.     return file;
  44. }
  45.  
  46. FileLines getLines(FILE *file) {
  47.     char c;
  48.     FileLines f = {0,0};
  49.     int fileSize = sizeof(f.lines)/sizeof(f.lines[0]);
  50.     char *line = 0;
  51.     int size = sizeof(line)/sizeof(line[0]);
  52.  
  53.     while(!feof(file)) {
  54.         while(c != '\n'){
  55.             c = getc(file);
  56.             line = realloc(line, ++size);
  57.             line[size-1] = c;
  58.         }
  59.         f.lines = realloc(f.lines, ++fileSize);
  60.         f.lines[fileSize-1] = *line;
  61.     }
  62.     return f;
  63. }
  64.  
  65. Point *getPointsFromFileLines(FileLines file) {
  66.     Point* p = 0;
  67.     int i;
  68.     int size = (sizeof(p)/sizeof(p[0]));
  69.     for(i = 0; i < file.size; i++) {
  70.         p = realloc(p, (size*(i+1)));
  71.         getPointFromString((char *)file.lines[i]);
  72.     }
  73.     return p;
  74. }
  75.  
  76. Point getPointFromString(char* string) {
  77.     Point p = {0,0};
  78.     while(*string != '\n') { //
  79.         if(*string == 'x') {
  80.             do {
  81.                 if(isdigit(*string))
  82.                     break;
  83.                 string++;
  84.             } while(*string); //
  85.             p.x = (int) strtol(string, &string, 10);
  86.  
  87.         }
  88.         if(*string == 'y') { //
  89.             do {
  90.                 if(isdigit(*string))
  91.                     break;
  92.                 string++;
  93.             } while(*string); //
  94.             p.y = (int) strtol(string, &string, 10);
  95.         }
  96.     }
  97.     return p;
  98. }
  99.  
  100. Line *getAllLines(Point* coords,int size) {
  101.     int number_of_lines = factorial(size);
  102.      Line l[number_of_lines];
  103.     int i, j;
  104.     for(i = 0; i < number_of_lines; i++) {
  105.         for(j = i; j < number_of_lines; j++) {
  106.             l[j].p1 = coords[i];
  107.             l[j].p2 = coords[j+1];
  108.         }
  109.     }
  110.     return l;
  111. }
  112.  
  113. int factorial(unsigned int i) {
  114.     if(i <= 1)
  115.         return 1;
  116.     return (i + factorial(i-1));
  117. }
  118.  
  119. int countParralelLines(Line* lines,int size) {
  120.     int count;
  121.     int i, j;
  122.     for(i = 0; i < size; i++) {
  123.         for(j = 0; j < size; i++) {
  124.             if(i==j) continue;
  125.             if(lines[i].slope == lines[j].slope)
  126.                 count++;
  127.         }
  128.     }
  129.     return count;
  130. }
  131.  
  132. //==============================    MAIN PART
  133.  
  134. int main()
  135. {
  136.     FileLines f = {0,0};
  137.     FILE *file;
  138.  
  139.     file = getFile();
  140.  
  141.     printf("1 \n");
  142.  
  143.     f = getLines(file);
  144.  
  145.     printf("2 \n");
  146.  
  147.     Point *coords = 0;
  148.     coords = getPointsFromFileLines(f);
  149.  
  150.     printf("3 \n");
  151.  
  152.     int size = sizeof(coords)/sizeof(coords[0]);
  153.     int possible_lines = factorial(size);
  154.     Line lines[possible_lines];
  155.     memcpy(lines, getAllLines(coords, size), (sizeof(lines)*sizeof(lines[0])));
  156.  
  157.     printf("4 \n");
  158.  
  159.     int count = countParralelLines(lines, possible_lines);
  160.     printf("count: %i", count);
  161.  
  162.     return 0;
  163. }
Add Comment
Please, Sign In to add comment