Guest User

Untitled

a guest
Jan 11th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct _SLine
  5. {
  6.  double start;
  7.  double end;
  8. }Line;
  9.  
  10.  
  11. void swap(double *x,double *y)
  12. {
  13.  double p=0;
  14.  p=*x;*x=*y;*y=p;
  15. }
  16.  
  17.  
  18. int read(const char *sf,Line **mas,int *leng)
  19. {
  20.   int i;
  21.   FILE *f;
  22.   double p;
  23.   f=fopen(sf,"r");
  24.      if(fscanf(f,"%d",leng)!=1) return -1;
  25.   else
  26.   {
  27.    (*leng)=(*leng)/2;
  28.    (*mas)=(Line*)malloc((*leng)*sizeof(Line));
  29.    for(i=0;i<(*leng);i++)
  30.    {
  31.     fscanf(f,"%lf",&p);
  32.     (*mas+i)->start=p;
  33.     fscanf(f,"%lf",&p);
  34.     (*mas+i)->end=p;
  35.     if((*mas+i)->start>(*mas+i)->end) swap(&((*mas+i)->end),&((*mas+i)->start));
  36.    }
  37.   }
  38.   fclose(f);
  39.   return 0;
  40. }
  41.  
  42.  
  43. Line joined_lines(Line *mas,int leng)
  44. {
  45.  int i=0;
  46.  double min_start=mas[1].start,max_end=mas[1].end;
  47.  Line l;
  48.  for(i=2;i<leng;i++)
  49.  {
  50.   if(mas[i].start<=min_start) min_start=mas[i].start;
  51.   if(mas[i].end>=max_end) max_end=mas[i].end;
  52.  }
  53.  l.start=min_start;
  54.  l.end=max_end;
  55.  return l;
  56. }
  57.  
  58.  
  59.  
  60. int if_belong(Line l1,Line l2)
  61. {
  62.  if(((l1.start>=l2.start)&&(l1.end<=l2.end))) return 1;else return 0;
  63. }
  64.  
  65.  
  66.  
  67.  
  68. int main(int __argc,char *__argv[])
  69. {
  70.  if(__argc>1)
  71.  {
  72.   int leng;
  73.   Line *mas;
  74.   FILE *f;
  75.      if(read(__argv[1],&mas,&leng)==0)
  76.   {
  77.    f=fopen(__argv[2],"w");
  78.    fprintf(f,"%d",if_belong(mas[0],joined_lines(mas,leng)));
  79.    fclose(f);
  80.   }
  81.  }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment