Advertisement
Guest User

jul_2_2015.c

a guest
Jun 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. typedef struct trougao{
  7.   double ax, ay;
  8.   double bx, by;
  9.   double cx, cy;
  10. } TROUGAO;
  11.  
  12. double duzina(double ax, double ay, double bx, double by){
  13.   return sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by));  
  14. }
  15.  
  16.  
  17. double povrsina(TROUGAO t){
  18.   double s;
  19.   double d1,d2,d3;
  20.   d1=duzina(t.ax,t.ay,t.bx, t.by);
  21.   d2=duzina(t.ax,t.ay,t.cx, t.cy);
  22.   d3=duzina(t.cx,t.cy,t.bx, t.by);
  23.   s=(d1+d2+d3)/2;
  24.   return sqrt(s*(s-d1)*(s-d2)*(s-d3));
  25. }
  26.  
  27. int poredi (const void *a, const void *b){
  28.  TROUGAO x=*(TROUGAO*)a;
  29.  TROUGAO y=*(TROUGAO*)b;
  30.  double px,py;
  31.  px=povrsina(x);
  32.  py=povrsina(y);
  33.  if(px>py)
  34.    return -1;
  35.  else if(px<py)
  36.    return 1;
  37.  else
  38.    return 0;
  39. }
  40.  
  41. int main () {
  42.  
  43.   int n,i;
  44.   FILE *ulaz;
  45.  
  46.   ulaz= fopen ("trouglovi.txt", "r");
  47.   if(ulaz==NULL){
  48.     printf("Greska.\n");
  49.     exit(EXIT_FAILURE);
  50.   }
  51.  
  52.  
  53.   TROUGAO *t;
  54.  
  55.   fscanf(ulaz,"%d", &n);
  56.   if(n<=0){
  57.     fprintf(stderr, "Greska aha greska.\n");
  58.     exit(EXIT_FAILURE);
  59.   }
  60.  
  61.   t=malloc(n*sizeof(TROUGAO));
  62.   if(t==NULL){
  63.     printf("Neuspesna alokacija.\n");
  64.     exit(EXIT_FAILURE);
  65.   }
  66.  
  67.   for(i=0; i<n; i++){
  68.     fscanf(ulaz, "%lf%lf%lf%lf%lf%lf", &t[i].ax, &t[i].ay, &t[i].bx,&t[i].by, &t[i].cx, &t[i].cy);
  69.   }
  70.  
  71.   qsort(t,n, sizeof(TROUGAO), &poredi);
  72.  
  73.   for(i=0; i<n ;i++){
  74.     printf("%.2lf %.2lf %.2lf %.2lf %.2lf %.2lf\n\n", t[i].ax, t[i].ay,t[i].bx,t[i].by, t[i].cx, t[i].cy);
  75.   }
  76.  
  77.   free(t);
  78.   exit(EXIT_SUCCESS);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement