Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1. /**
  2. * @author: Arman Boskailo <e01228166@student.tuwien.ac.at>
  3. *
  4. *
  5. *
  6. *
  7. */
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <math.h>
  13. #include <unistd.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include <sys/wait.h>
  17. char *progname;
  18.  
  19. int countFirst = 0;
  20.  
  21. int countSecond = 0;
  22.  
  23. typedef struct points{
  24. float x,y;
  25. }point;
  26.  
  27. /**
  28. *
  29. *
  30. *
  31. *
  32. */
  33.  
  34. float square(float x){
  35. return x*x;
  36. }
  37.  
  38. /**
  39. *
  40. *
  41. *
  42. *
  43. */
  44.  
  45. float getDistance(point a, point b){
  46. float x1 = a.x;
  47. float y1 = a.y;
  48.  
  49. float x2 = b.x;
  50. float y2 = b.y;
  51.  
  52. return sqrt(square(x2-x1) + square(y2-y1));
  53. }
  54. /**
  55. *
  56. *
  57. *
  58. *
  59. *
  60. */
  61.  
  62. float mean(point *list,int counter){
  63. float sum = 0;
  64. for(int i = 0; i < counter;i++){
  65. sum += list[i].x;
  66. }
  67. sum /= counter;
  68.  
  69. return sum;
  70. }
  71.  
  72. /**
  73. *
  74. *
  75. *
  76. *
  77. */
  78.  
  79. void splitList(point *p,point *firstPart, point *secondPart,int counter,float avg){
  80.  
  81. for(int i = 0; i < counter;i++){
  82. if(p[i].x <= avg ){
  83. countFirst++;
  84. firstPart = realloc(firstPart,sizeof(point) * countFirst);
  85. firstPart[countFirst - 1] = p[i];
  86. }else{
  87. countSecond++;
  88. secondPart = realloc(secondPart,sizeof(point) * countSecond);
  89. secondPart[countSecond - 1] = p[i];
  90. }
  91. }
  92. }
  93.  
  94.  
  95. int main(int argc, char *argv[]){
  96.  
  97. progname = argv[0];
  98. int status;
  99. char input[100];
  100. point *list;
  101. list = malloc(sizeof(point));
  102. memset(list,0,sizeof(point));
  103. int counter = 0;
  104. float avg;
  105.  
  106. while(fgets(input,100,stdin)){
  107. counter++;
  108. char *p;
  109. float x = strtof(input,&p);
  110. float y = strtof(p,NULL);
  111. fprintf(stderr,"from gets: %f %f\n",x,y);
  112. point newPoint;
  113. newPoint.x = x;
  114. newPoint.y = y;
  115. list = realloc(list,sizeof(point) * counter);
  116. // for index
  117. *(list+counter-1) = newPoint;
  118. }
  119.  
  120. if(counter == 1){
  121. exit(EXIT_SUCCESS);
  122. }
  123.  
  124. if(counter == 2){
  125. fprintf(stdout,"%f %f\n",list[0].x,list[0].y);
  126. fprintf(stdout,"%f %f\n",list[1].x,list[1].y);
  127. exit(EXIT_SUCCESS);
  128. }
  129.  
  130. avg = mean(list,counter);
  131.  
  132. point *firstPart;
  133. firstPart = malloc(sizeof(point));
  134. memset(firstPart,0,sizeof(point));
  135. point *secondPart;
  136. secondPart = malloc(sizeof(point));
  137. memset(secondPart,0,sizeof(point));
  138. splitList(list,firstPart,secondPart,counter,avg);
  139.  
  140. int toPipefd[2];
  141. int fromPipefd[2];
  142.  
  143. if(pipe(toPipefd) == -1){
  144. fprintf(stderr,"%s: Pipe Failed\n",progname);
  145. exit(EXIT_FAILURE);
  146. }
  147.  
  148. if(pipe(fromPipefd) == -1){
  149. fprintf(stderr,"%s: Pipe Failed\n",progname);
  150. exit(EXIT_FAILURE);
  151. }
  152.  
  153.  
  154. pid_t firstChildPid = fork();
  155.  
  156. switch (firstChildPid) {
  157. case -1:
  158. exit(EXIT_FAILURE);
  159. break;
  160.  
  161. case 0:
  162. close(STDIN_FILENO);
  163. close(STDOUT_FILENO);
  164. dup2(toPipefd[0],STDIN_FILENO);
  165. close(toPipefd[0]);
  166. close(toPipefd[1]);
  167. dup2(fromPipefd[1],STDOUT_FILENO);
  168. close(fromPipefd[0]);
  169. close(fromPipefd[1]);
  170. execlp(progname,progname,NULL);
  171. fprintf(stderr,"%s : Exec failed!",progname);
  172. break;
  173. default:
  174. break;
  175. }
  176.  
  177. close(toPipefd[0]);
  178. close(fromPipefd[1]);
  179.  
  180. int toPipefd1[2];
  181. int fromPipefd1[2];
  182.  
  183. pipe(toPipefd1);
  184. pipe(fromPipefd1);
  185.  
  186. pid_t secondChildPid = fork();
  187.  
  188.  
  189. switch (secondChildPid) {
  190. case -1:
  191. exit(EXIT_FAILURE);
  192. break;
  193.  
  194. case 0:
  195. close(STDIN_FILENO);
  196. close(STDOUT_FILENO);
  197. dup2(toPipefd1[0],STDIN_FILENO);
  198. close(toPipefd1[0]);
  199. close(toPipefd1[1]);
  200. dup2(fromPipefd1[1],STDOUT_FILENO);
  201. close(fromPipefd1[0]);
  202. close(fromPipefd1[1]);
  203. execlp(progname,progname,NULL);
  204. fprintf(stderr,"%s : Exec failed!",progname);
  205. break;
  206. default:
  207. break;
  208. }
  209.  
  210. close(toPipefd1[0]);
  211. close(fromPipefd1[1]);
  212.  
  213. int i;
  214. for(i = 0; i < countFirst; i++){
  215. char buf[100];
  216. sprintf(buf, "%f %f\n", firstPart[i].x, firstPart[i].y);
  217. write(toPipefd[1], buf, strlen(buf));
  218. memset(&buf, 0, sizeof(buf));
  219. }
  220.  
  221. for(i = 0; i < countSecond; i++){
  222. char buf[100];
  223. sprintf(buf, "%f %f\n", secondPart[i].x, secondPart[i].y);
  224. write(toPipefd1[1], buf, strlen(buf));
  225. memset(&buf, 0, sizeof(buf));
  226. }
  227. close(toPipefd[1]);
  228. close(toPipefd1[1]);
  229.  
  230. char buffer1[256];
  231. char buffer2[256];
  232.  
  233. point fromFirstChild[2];
  234. point fromSecondChild[2];
  235.  
  236. int counterFirst = 0;
  237. int counterSecond = 0;
  238.  
  239.  
  240. FILE *fp1 = fdopen(fromPipefd[0], "r");
  241.  
  242. if(fp1 == NULL){
  243. exit(EXIT_FAILURE);
  244. }
  245.  
  246. while(fgets(buffer1,sizeof(buffer1),fp1)){
  247. char *p;
  248. float x = strtof(buffer1,&p);
  249. float y = strtof(p,&p);
  250.  
  251. point newPoint;
  252. newPoint.x = x;
  253. newPoint.y = y;
  254. fromFirstChild[counterFirst] = newPoint;
  255. counterFirst++;
  256. memset(&buffer1, 0, sizeof(buffer1));
  257.  
  258. }
  259. fclose(fp1);
  260.  
  261. FILE *fp2 = fdopen(fromPipefd1[0], "r");
  262. if(fp2 == NULL){
  263. exit(EXIT_FAILURE);
  264. }
  265.  
  266. while(fgets(buffer2,sizeof(buffer2), fp2)){
  267. char *p;
  268. float x = strtof(buffer2,&p);
  269. float y = strtof(p,NULL);
  270.  
  271. point newPoint;
  272. newPoint.x = x;
  273. newPoint.y = y;
  274. fromSecondChild[counterSecond] = newPoint;
  275. counterSecond++;
  276. memset(&buffer2, 0, sizeof(buffer2));
  277. }
  278.  
  279.  
  280. fclose(fp2);
  281. float distanceOfPoints;
  282. point result[2];
  283. float distanceFromFirstChild = getDistance(fromFirstChild[0],fromFirstChild[1]);
  284. float distanceFromSecondChild = getDistance(fromSecondChild[0],fromSecondChild[1]);
  285.  
  286.  
  287. if(distanceFromFirstChild <= distanceFromSecondChild){
  288. distanceOfPoints = distanceFromFirstChild;
  289. result[0] = fromFirstChild[0];
  290. result[1] = fromFirstChild[1];
  291. }else{
  292. distanceOfPoints = distanceFromSecondChild;
  293. result[0] = fromSecondChild[0];
  294. result[1] = fromSecondChild[1];
  295. }
  296.  
  297.  
  298. for(int i = 0; i < 2 ; i++){
  299. for(int j = 0 ; j < 2; j++){
  300. float resultDistance = getDistance(fromFirstChild[i],fromSecondChild[j]);
  301.  
  302. if(resultDistance <= distanceOfPoints){
  303. distanceOfPoints = resultDistance;
  304. result[0] = fromFirstChild[i];
  305. result[1] = fromSecondChild[j];
  306. }
  307. }
  308. }
  309.  
  310.  
  311. fprintf(stdout,"%f %f\n",result[0].x,result[0].y);
  312.  
  313. fprintf(stdout,"%f %f\n",result[1].x,result[1].y);
  314.  
  315. (void) wait(&status);
  316.  
  317.  
  318. return EXIT_SUCCESS;
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement