fork(2) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. // Storing two 2D points and their distance.
  6. typedef struct PointPair_t
  7. {
  8. double x1, y1, x2, y2, dist;
  9. }PointPair, *PPointPair;
  10.  
  11. // Storing pointer to PointPair in a linked list.
  12. typedef struct PointPairNode_t
  13. {
  14. PPointPair value;
  15. struct PointPairNode_t *next;
  16. }PointPairNode, *PPointPairNode;
  17.  
  18. double distance(double x1, double y1, double x2, double y2)
  19. {
  20. double dx = x2 - x1;
  21. double dy = y2 - y1;
  22. return sqrt((dx * dx) + (dy * dy));
  23. }
  24.  
  25. // Sort array of pointer to PointPair using insertion sort.
  26. void sortPointPairByDistance(PPointPair *arr, unsigned int count)
  27. {
  28. int top = count - 2;
  29. int i;
  30.  
  31. while(top >= 0)
  32. {
  33. PPointPair temp = arr[top];
  34. for(i = top; i < count - 1; ++i)
  35. {
  36. if(arr[i + 1]->dist >= temp->dist)
  37. break;
  38. arr[i] = arr[i + 1];
  39. }
  40. arr[i] = temp;
  41. --top;
  42. }
  43. }
  44.  
  45. int main(int argc, char *argv[])
  46. {
  47. PPointPairNode list = NULL;
  48. unsigned int pointPairCount = 0;
  49. PPointPair *arr = NULL;
  50. int i;
  51. PPointPairNode listPtr;
  52.  
  53. FILE *fp;
  54.  
  55. //fp = fopen("point.txt", "r");
  56. fp = stdin;
  57. if(fp)
  58. {
  59. while(!feof(fp))
  60. {
  61. double x1, y1, x2, y2;
  62. if(fscanf(fp, "%lf %lf %lf %lf", &x1, &y1, &x2, &y2) == 4)
  63. {
  64. // Allocate a node and a PointPair to store points and distances.
  65. PPointPairNode node = (PPointPairNode)malloc(sizeof(PointPairNode));
  66. node->value = (PPointPair)malloc(sizeof(PointPair));
  67. node->value->x1 = x1;
  68. node->value->y1 = y1;
  69. node->value->x2 = x2;
  70. node->value->y2 = y2;
  71. node->value->dist = distance(x1, y1, x2, y2);
  72. node->next = list;
  73. list = node;
  74. ++pointPairCount;
  75. printf("Pair %u : { (%5.2lf, %5.2lf), (%5.2lf, %5.2lf) } distance : %9.6lf\n",
  76. pointPairCount, x1, y1, x2, y2, node->value->dist);
  77. }
  78. }
  79. fclose(fp);
  80. }
  81.  
  82. // Allocate an array and copy pointers into it for sorting.
  83. arr = (PPointPair *)malloc(pointPairCount * sizeof(PPointPair));
  84. for(i = pointPairCount - 1, listPtr = list; i >= 0; --i, listPtr = listPtr->next)
  85. {
  86. arr[i] = listPtr->value;
  87. }
  88.  
  89. sortPointPairByDistance(arr, pointPairCount);
  90.  
  91. //fp = fopen("point2.txt", "w");
  92. fp = stdout;
  93. if(fp)
  94. {
  95. for(i = 0; i < pointPairCount; ++i)
  96. {
  97. fprintf(fp, "%lf %lf %lf %lf\n", arr[i]->x1, arr[i]->y1, arr[i]->x2, arr[i]->y2);
  98. }
  99. fclose(fp);
  100. }
  101.  
  102. // Free memory.
  103. free(arr);
  104. while(list)
  105. {
  106. listPtr = list->next;
  107. free(list->value);
  108. free(list);
  109. list = listPtr;
  110. }
  111.  
  112. return 0;
  113. }
  114.  
Success #stdin #stdout 0.02s 1848KB
stdin
25.3 3.4 23.2 44.2
23.4 56.4 34 22
53.2 33.4 45.3 54.3
3.4 5.5 3.5 6.7
2.4 7.5 3.5 3.5
stdout
Pair 1 : { (25.30,  3.40), (23.20, 44.20) }  distance : 40.854008
Pair 2 : { (23.40, 56.40), (34.00, 22.00) }  distance : 35.996111
Pair 3 : { (53.20, 33.40), (45.30, 54.30) }  distance : 22.343232
Pair 4 : { ( 3.40,  5.50), ( 3.50,  6.70) }  distance :  1.204159
Pair 5 : { ( 2.40,  7.50), ( 3.50,  3.50) }  distance :  4.148494
3.400000 5.500000 3.500000 6.700000
2.400000 7.500000 3.500000 3.500000
53.200000 33.400000 45.300000 54.300000
23.400000 56.400000 34.000000 22.000000
25.300000 3.400000 23.200000 44.200000