Advertisement
Adytzu04

examen prog

May 22nd, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. //header
  2.  
  3. #ifndef EXAMEN_H_
  4. #define EXAMEN_H_
  5.  
  6. struct _PUNCT{
  7.     double x,y;
  8. };
  9. typedef struct _PUNCT PUNCT;
  10. struct _PITICI{
  11.     unsigned int np;
  12.     PUNCT *cc;
  13. };
  14. typedef struct _PITICI PITICI;
  15. PITICI citireStructura(FILE*in);
  16. void afisarePitici(PITICI P);
  17. int semn(PUNCT P,double a,double b, double c);
  18. int verificare(PITICI P,double a, double b, double c);
  19.  
  20. #endif /* EXAMEN_H_ */
  21.  
  22. //c
  23.  
  24. #include<stdio.h>
  25. #include<stdlib.h>
  26. #include<string.h>
  27. #include"examen.h"
  28.  
  29. PITICI citireStructura(FILE*in)
  30. {
  31.     PITICI P;
  32.     int i;
  33.     fscanf(in,"%d",&P.np);
  34.     P.cc=(PUNCT *)calloc(P.np,sizeof(PUNCT));
  35.     if(P.cc==0)
  36.     {
  37.         fprintf(stderr,"Memorie insuficenta.\n");
  38.         exit(EXIT_FAILURE);
  39.     }
  40.     for(i=0;i<P.np;i++)
  41.     {
  42.         fscanf(in,"%lf",&P.cc[i].x);
  43.         fscanf(in,"%lf",&P.cc[i].y);
  44.     }
  45.     return P;
  46. }
  47. void afisarePitici(PITICI P)
  48. {
  49.     int i;
  50.     for(i=0;i<P.np;i++)
  51.     {
  52.         fprintf(stdout,"(%.1lf ,%.1lf)   ",P.cc[i].x,P.cc[i].y);
  53.     }
  54.     fprintf(stdout,"\n");
  55. }
  56. int semn(PUNCT P,double a,double b, double c)
  57. {
  58.     int ok;
  59.     ok=a*P.x+b*P.y+c;
  60.     if(ok>0)
  61.     {
  62.         return 1;
  63.     }
  64.     else
  65.     {
  66.         if(ok==0)
  67.         {
  68.             return 0;
  69.         }
  70.         else
  71.         {
  72.             return -1;
  73.         }
  74.     }
  75. }
  76. int verificare(PITICI P,double a, double b, double c)
  77. {
  78.     int ok,i;
  79.     for(i=0;i<P.np;i++)
  80.     {
  81.         if(semn(P.cc[i],a,b,c)==semn(P.cc[i+1],a,b,c))
  82.             ok=1;
  83.         else
  84.             ok=0;
  85.     }
  86.     return ok;
  87. }
  88.  
  89. //main
  90.  
  91. #include<stdio.h>
  92. #include<stdlib.h>
  93. #include<string.h>
  94. #include"examen.h"
  95. int main(void)
  96. {
  97.     FILE*in;
  98.     int s,i,ok;
  99.     double x1,y1,x2,y2;
  100.     double a,b,c;
  101.     in=fopen("intrare.txt","r");
  102.     if(in==0)
  103.     {
  104.         fprintf(stderr,"eroare la deschiderea fisierului intrare.txt.\n");
  105.         exit(EXIT_FAILURE);
  106.     }
  107.     FILE*out;
  108.     out=fopen("iesire.txt","w");
  109.     if(out==0)
  110.     {
  111.         fprintf(stderr,"eroare la deschiderea fisieerului iesire.txt.\n");
  112.         exit(EXIT_FAILURE);
  113.     }
  114.     PITICI r,al;
  115.     r=citireStructura(in);
  116.     fprintf(stdout,"casutele piticilor cu scufii rosii se gasesc la urm coordonate.\n");
  117.     afisarePitici(r);
  118.     al=citireStructura(in);
  119.     fprintf(stdout,"casutele piticilor cu scufii albastre se gasesc la urm coordonate.\n");
  120.     afisarePitici(al);
  121.     fscanf(in,"%d",&s);
  122.     for(i=1;i<s;i++)
  123.     {
  124.  
  125.             fscanf(in,"%lf",&x1);
  126.             fscanf(in,"%lf",&y1);
  127.             fscanf(in,"%lf",&x2);
  128.             fscanf(in,"%lf",&y2);
  129.  
  130.         a=y1-y2;
  131.         b=x2-x1;
  132.         c=x1*y2-x2*y1;
  133.         ok=verificare(r,a,b,c);
  134.         if(ok==1)
  135.         {
  136.             fprintf(stdout,"da-sfetnicul %d ia dat o solutie valida.\n",i);
  137.         }
  138.         else
  139.         {
  140.             fprintf(stdout,"nu-sfetnicul %d nu ia dat o solutie valida.\n",i);
  141.         }
  142.  
  143.     }
  144.  
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement