Link to home
Start Free TrialLog in
Avatar of Jaziar
Jaziar

asked on

Inputing a File of 2d points and then sorting

I have a program that will take in 2d points and then compare the points. Example in the code

struct Point2D p1 = {1,2};
struct Point2D p2 = {1,2};

if (lessThan(&p1, &p2)) {
       puts("one point is less than the other");
   }
   else {
    puts("Equal");
   }

This code is working great.  But what I need is to take a file of points.  
Example:
1,2
1,4
1,3
2,4
1,6
2,6
3,1

As the file is parsed the values are ran through the  lesThan function.  
When the file is finsihed the values are in order from the smalles to the largest.

Example  
1,2
1,3
1,4
1,6
2,4
2,6
3,1


I am very rusty with C so I am not sure how I would do this.  I can parse a file of chars or strings, but not sure how to handle struct point values.
struct Point2D {
   int x;
   int y;
};
 
 int comparePoint2D ( struct Point2D *p1, struct Point2D *p2) {
   int result = p1->x == p2->x && p1->y == p2->y;
   return result;
}
 
int lessThan(const struct Point2D *p1, const struct Point2D *p2) {
   int result = (p1->x < p2->x) || (p1->x == p2->x && p1->y < p2->y);
   return result;   
} 
 
 
 
int main (void) {
   struct Point2D p1 = {1,2};
   struct Point2D p2 = {1,2};
  
   if (comparePoint2D(&p1, &p2)) {
       puts("Points are equal");
   } 
   else {
    puts("Points are not equal");
   }
   
   if (lessThan(&p1, &p2)) {
       puts("one point is less than the other");
   } 
   else {
    puts("Equal");
   }
   return 0;
}

Open in new window

Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi Jaziar,

Sorting a linked list is a bit challenging.  The program will typically waste a lot of cycles with list management and list position for most kinds of sorts.  (Sorting a linked list can certainly be done.)

It might be easiest to generate a table of Point2D structures and populate it with the points.  Then call one of the available sort routines (like qsort) to sort the points.  Then it's a simple matter of putting the points back into list form.


Kent
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> I can parse a file of chars or strings, but not sure how to handle struct point values.

Write a function that reads one Point2D struct from the file. The function reads one line from the input file, and then parses that line to get two integer values from it, separated by a comma. It uses those two integer values to construct a Point2D object, and returns it.

This function can be called in a loop until the whole file is read. Each Point2D object read from the file can be stored into an array.

You can make use of fgets to read a line from the file, and sscanf or strtok (with atoi) or strtol to extract the integer values from the line :

        http://www.cplusplus.com/reference/clibrary/cstdio/fgets.html

        http://www.cplusplus.com/reference/clibrary/cstdio/sscanf.html

        http://www.cplusplus.com/reference/clibrary/cstring/strtok.html
        http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html

        http://www.cplusplus.com/reference/clibrary/cstdlib/strtol.html