• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

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

0
Jaziar
Asked:
Jaziar
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
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
0
 
ozoCommented:
int cmpPoint2D ( const void *p1, const void *p2) {
  return lessThan((struct Point2D *)p2,(struct Point2D *)p1)-lessThan((struct Point2D *)p1,(struct Point2D *)p2);
}


int main (void) {
struct Point2D list[]={
  {1,2},
  {1,4},
  {1,3},
  {2,4},
  {1,6},
  {2,6},
  {3,1},
};
 qsort(list,sizeof(list)/sizeof(list[0]),sizeof(list[0]),cmpPoint2D);
 for( int i=0;i<sizeof(list)/sizeof(list[0]); i++ ){
   printf("%d,%d\n",list[i].x,list[i].y);
 }
}

0
 
Infinity08Commented:
>> 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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now