Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author: Arman Boskailo <e01228166@student.tuwien.ac.at>
- *
- *
- *
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- char *progname;
- int countFirst = 0;
- int countSecond = 0;
- typedef struct points{
- float x,y;
- }point;
- /**
- *
- *
- *
- *
- */
- float square(float x){
- return x*x;
- }
- /**
- *
- *
- *
- *
- */
- float getDistance(point a, point b){
- float x1 = a.x;
- float y1 = a.y;
- float x2 = b.x;
- float y2 = b.y;
- return sqrt(square(x2-x1) + square(y2-y1));
- }
- /**
- *
- *
- *
- *
- *
- */
- float mean(point *list,int counter){
- float sum = 0;
- for(int i = 0; i < counter;i++){
- sum += list[i].x;
- }
- sum /= counter;
- return sum;
- }
- /**
- *
- *
- *
- *
- */
- void splitList(point *p,point *firstPart, point *secondPart,int counter,float avg){
- for(int i = 0; i < counter;i++){
- if(p[i].x <= avg ){
- countFirst++;
- firstPart = realloc(firstPart,sizeof(point) * countFirst);
- firstPart[countFirst - 1] = p[i];
- }else{
- countSecond++;
- secondPart = realloc(secondPart,sizeof(point) * countSecond);
- secondPart[countSecond - 1] = p[i];
- }
- }
- }
- int main(int argc, char *argv[]){
- progname = argv[0];
- int status;
- char input[100];
- point *list;
- list = malloc(sizeof(point));
- memset(list,0,sizeof(point));
- int counter = 0;
- float avg;
- while(fgets(input,100,stdin)){
- counter++;
- char *p;
- float x = strtof(input,&p);
- float y = strtof(p,NULL);
- fprintf(stderr,"from gets: %f %f\n",x,y);
- point newPoint;
- newPoint.x = x;
- newPoint.y = y;
- list = realloc(list,sizeof(point) * counter);
- // for index
- *(list+counter-1) = newPoint;
- }
- if(counter == 1){
- exit(EXIT_SUCCESS);
- }
- if(counter == 2){
- fprintf(stdout,"%f %f\n",list[0].x,list[0].y);
- fprintf(stdout,"%f %f\n",list[1].x,list[1].y);
- exit(EXIT_SUCCESS);
- }
- avg = mean(list,counter);
- point *firstPart;
- firstPart = malloc(sizeof(point));
- memset(firstPart,0,sizeof(point));
- point *secondPart;
- secondPart = malloc(sizeof(point));
- memset(secondPart,0,sizeof(point));
- splitList(list,firstPart,secondPart,counter,avg);
- int toPipefd[2];
- int fromPipefd[2];
- if(pipe(toPipefd) == -1){
- fprintf(stderr,"%s: Pipe Failed\n",progname);
- exit(EXIT_FAILURE);
- }
- if(pipe(fromPipefd) == -1){
- fprintf(stderr,"%s: Pipe Failed\n",progname);
- exit(EXIT_FAILURE);
- }
- pid_t firstChildPid = fork();
- switch (firstChildPid) {
- case -1:
- exit(EXIT_FAILURE);
- break;
- case 0:
- close(STDIN_FILENO);
- close(STDOUT_FILENO);
- dup2(toPipefd[0],STDIN_FILENO);
- close(toPipefd[0]);
- close(toPipefd[1]);
- dup2(fromPipefd[1],STDOUT_FILENO);
- close(fromPipefd[0]);
- close(fromPipefd[1]);
- execlp(progname,progname,NULL);
- fprintf(stderr,"%s : Exec failed!",progname);
- break;
- default:
- break;
- }
- close(toPipefd[0]);
- close(fromPipefd[1]);
- int toPipefd1[2];
- int fromPipefd1[2];
- pipe(toPipefd1);
- pipe(fromPipefd1);
- pid_t secondChildPid = fork();
- switch (secondChildPid) {
- case -1:
- exit(EXIT_FAILURE);
- break;
- case 0:
- close(STDIN_FILENO);
- close(STDOUT_FILENO);
- dup2(toPipefd1[0],STDIN_FILENO);
- close(toPipefd1[0]);
- close(toPipefd1[1]);
- dup2(fromPipefd1[1],STDOUT_FILENO);
- close(fromPipefd1[0]);
- close(fromPipefd1[1]);
- execlp(progname,progname,NULL);
- fprintf(stderr,"%s : Exec failed!",progname);
- break;
- default:
- break;
- }
- close(toPipefd1[0]);
- close(fromPipefd1[1]);
- int i;
- for(i = 0; i < countFirst; i++){
- char buf[100];
- sprintf(buf, "%f %f\n", firstPart[i].x, firstPart[i].y);
- write(toPipefd[1], buf, strlen(buf));
- memset(&buf, 0, sizeof(buf));
- }
- for(i = 0; i < countSecond; i++){
- char buf[100];
- sprintf(buf, "%f %f\n", secondPart[i].x, secondPart[i].y);
- write(toPipefd1[1], buf, strlen(buf));
- memset(&buf, 0, sizeof(buf));
- }
- close(toPipefd[1]);
- close(toPipefd1[1]);
- char buffer1[256];
- char buffer2[256];
- point fromFirstChild[2];
- point fromSecondChild[2];
- int counterFirst = 0;
- int counterSecond = 0;
- FILE *fp1 = fdopen(fromPipefd[0], "r");
- if(fp1 == NULL){
- exit(EXIT_FAILURE);
- }
- while(fgets(buffer1,sizeof(buffer1),fp1)){
- char *p;
- float x = strtof(buffer1,&p);
- float y = strtof(p,&p);
- point newPoint;
- newPoint.x = x;
- newPoint.y = y;
- fromFirstChild[counterFirst] = newPoint;
- counterFirst++;
- memset(&buffer1, 0, sizeof(buffer1));
- }
- fclose(fp1);
- FILE *fp2 = fdopen(fromPipefd1[0], "r");
- if(fp2 == NULL){
- exit(EXIT_FAILURE);
- }
- while(fgets(buffer2,sizeof(buffer2), fp2)){
- char *p;
- float x = strtof(buffer2,&p);
- float y = strtof(p,NULL);
- point newPoint;
- newPoint.x = x;
- newPoint.y = y;
- fromSecondChild[counterSecond] = newPoint;
- counterSecond++;
- memset(&buffer2, 0, sizeof(buffer2));
- }
- fclose(fp2);
- float distanceOfPoints;
- point result[2];
- float distanceFromFirstChild = getDistance(fromFirstChild[0],fromFirstChild[1]);
- float distanceFromSecondChild = getDistance(fromSecondChild[0],fromSecondChild[1]);
- if(distanceFromFirstChild <= distanceFromSecondChild){
- distanceOfPoints = distanceFromFirstChild;
- result[0] = fromFirstChild[0];
- result[1] = fromFirstChild[1];
- }else{
- distanceOfPoints = distanceFromSecondChild;
- result[0] = fromSecondChild[0];
- result[1] = fromSecondChild[1];
- }
- for(int i = 0; i < 2 ; i++){
- for(int j = 0 ; j < 2; j++){
- float resultDistance = getDistance(fromFirstChild[i],fromSecondChild[j]);
- if(resultDistance <= distanceOfPoints){
- distanceOfPoints = resultDistance;
- result[0] = fromFirstChild[i];
- result[1] = fromSecondChild[j];
- }
- }
- }
- fprintf(stdout,"%f %f\n",result[0].x,result[0].y);
- fprintf(stdout,"%f %f\n",result[1].x,result[1].y);
- (void) wait(&status);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement