Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. #define INTERVALLO "lim.txt"
  5. #define EQUAZIONI "eqs.txt"
  6. #define STRLEN 256
  7. #define SUDDIVISIONE 1000
  8. typedef struct
  9. {
  10.  float v1,v2,v3;
  11.  char tipo,segno;
  12. }equazione;
  13. typedef equazione* pequazione;
  14.  
  15. typedef FILE* pfile;
  16.  
  17. int main (void)
  18.    {
  19.        pfile pf;
  20.        float xmax,xmin,ymax,ymin,as,dx,dy,x,y,v,area;
  21.        char s[STRLEN];
  22.        int ne,i,np;
  23.        pequazione pe;
  24.  
  25.        if ((pf=fopen(INTERVALLO,"r"))==NULL)                   goto file_err;
  26.        if (fscanf(pf,"%s",s)!=1)                   goto file_err;
  27.        xmin=atof(s);
  28.        if (fscanf(pf,"%s",s)!=1)                   goto file_err;
  29.        ymin=atof(s);
  30.        if (fscanf(pf,"%s",s)!=1)                   goto file_err;
  31.        xmax=atof(s);
  32.        if (fscanf(pf,"%s",s)!=1)                   goto file_err;
  33.        ymax=atof(s);
  34.        as=(xmax-xmin)*(ymax-ymin);
  35.        printf("Gli estremi del nostro intervallo sono  (%.2f , %.2f)  e  (%.2f , %.2f)  e l'area della sezione considerata รจ as=%.2f\n",xmin,ymin,xmax,ymax,as);
  36.        fclose(pf);
  37.        if ((pf=fopen(EQUAZIONI,"r"))==NULL)                   goto file_err;
  38.        if (fscanf(pf,"%s",s)!=1)                   goto file_err;
  39.        ne=atoi(s);
  40.        if ((pe=(pequazione)malloc(sizeof(equazione)*ne))==NULL)    goto mem_err;
  41.        printf("nel file inserito hai %d equazioni\n",ne);
  42.        for(i=0;i<ne;i++)
  43.           {
  44.                if (fscanf(pf,"%s",s)!=1)       goto file_err;
  45.                pe[i].v1=atof(s);
  46.                if (fscanf(pf,"%s",s)!=1)       goto file_err;
  47.                pe[i].v2=atof(s);
  48.                if (fscanf(pf,"%s",s)!=1)   goto file_err;
  49.                pe[i].v3=atof(s);
  50.                if (fscanf(pf,"%s",s)!=1)   goto file_err;
  51.                pe[i].tipo=s[0];
  52.                if (fscanf(pf,"%s",s)!=1)   goto   file_err;
  53.                pe[i].segno=s[0];
  54.                if(pe[i].tipo=='r')
  55.                   {
  56.                       printf("Equazione: %fx+%fy+%f",pe[i].v1,pe[i].v2,pe[i].v3);
  57.                   }
  58.                else
  59.                   {
  60.                       printf("Equazione: (x-%f)^2 + (y-%f)^2 -%f",pe[i].v1,pe[i].v2,(pe[i].v3)*(pe[i].v3));
  61.                   }
  62.                if(pe[i].segno=='+')
  63.                   {
  64.                       printf(">=0\n");
  65.                   }
  66.                else
  67.                   {
  68.                       printf("<=0\n");
  69.                   }
  70.           }
  71.        fclose(pf);
  72.        np=0;
  73.        dx=(xmax-xmin)/SUDDIVISIONE;
  74.        dy=(ymax-ymin)/SUDDIVISIONE;
  75.      
  76.        for(x=xmin;x<=xmax;x=x+dx)
  77.           {for(y=ymin;y<=ymax;y=y+dy)
  78.              {
  79.                  for(i=0;i<ne;i++)
  80.                     {
  81.                         if(pe[i].tipo=='r')
  82.                            {
  83.                                v=((x-pe[i].v1)*(x-pe[i].v1)+(y-pe[i].v2)*(y-pe[i].v2)-(pe[i].v3)*(pe[i].v3));
  84.                            }
  85.                         else
  86.                            {
  87.                                v=x*pe[i].v1+y*pe[i].v2+pe[i].v3;  
  88.                            }
  89.                         if(pe[i].segno=='+' && v<0 || pe[i].segno=='-' && v>0)
  90.                            {
  91.                                break;
  92.                            }
  93.                        
  94.                     }
  95.                  if(i==ne)
  96.                     {
  97.                         np=np+1;
  98.                     }
  99.                
  100.              }
  101.             }
  102.        area=(np*as)/((SUDDIVISIONE+1)*(SUDDIVISIONE+1));
  103.        printf("area totale: %.5f",area);
  104.  
  105.  
  106.        return EXIT_SUCCESS;
  107.  
  108.        file_err: printf("File Error!\n");  return EXIT_FAILURE;
  109.        mem_err:  printf("Mem  Error!\n");  return EXIT_FAILURE;
  110.    
  111.  
  112.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement