Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define INTERVALLO "lim.txt"
- #define EQUAZIONI "eqs.txt"
- #define STRLEN 256
- #define SUDDIVISIONE 1000
- typedef struct
- {
- float v1,v2,v3;
- char tipo,segno;
- }equazione;
- typedef equazione* pequazione;
- typedef FILE* pfile;
- int main (void)
- {
- pfile pf;
- float xmax,xmin,ymax,ymin,as,dx,dy,x,y,v,area;
- char s[STRLEN];
- int ne,i,np;
- pequazione pe;
- if ((pf=fopen(INTERVALLO,"r"))==NULL) goto file_err;
- if (fscanf(pf,"%s",s)!=1) goto file_err;
- xmin=atof(s);
- if (fscanf(pf,"%s",s)!=1) goto file_err;
- ymin=atof(s);
- if (fscanf(pf,"%s",s)!=1) goto file_err;
- xmax=atof(s);
- if (fscanf(pf,"%s",s)!=1) goto file_err;
- ymax=atof(s);
- as=(xmax-xmin)*(ymax-ymin);
- 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);
- fclose(pf);
- if ((pf=fopen(EQUAZIONI,"r"))==NULL) goto file_err;
- if (fscanf(pf,"%s",s)!=1) goto file_err;
- ne=atoi(s);
- if ((pe=(pequazione)malloc(sizeof(equazione)*ne))==NULL) goto mem_err;
- printf("nel file inserito hai %d equazioni\n",ne);
- for(i=0;i<ne;i++)
- {
- if (fscanf(pf,"%s",s)!=1) goto file_err;
- pe[i].v1=atof(s);
- if (fscanf(pf,"%s",s)!=1) goto file_err;
- pe[i].v2=atof(s);
- if (fscanf(pf,"%s",s)!=1) goto file_err;
- pe[i].v3=atof(s);
- if (fscanf(pf,"%s",s)!=1) goto file_err;
- pe[i].tipo=s[0];
- if (fscanf(pf,"%s",s)!=1) goto file_err;
- pe[i].segno=s[0];
- if(pe[i].tipo=='r')
- {
- printf("Equazione: %fx+%fy+%f",pe[i].v1,pe[i].v2,pe[i].v3);
- }
- else
- {
- printf("Equazione: (x-%f)^2 + (y-%f)^2 -%f",pe[i].v1,pe[i].v2,(pe[i].v3)*(pe[i].v3));
- }
- if(pe[i].segno=='+')
- {
- printf(">=0\n");
- }
- else
- {
- printf("<=0\n");
- }
- }
- fclose(pf);
- np=0;
- dx=(xmax-xmin)/SUDDIVISIONE;
- dy=(ymax-ymin)/SUDDIVISIONE;
- for(x=xmin;x<=xmax;x=x+dx)
- {for(y=ymin;y<=ymax;y=y+dy)
- {
- for(i=0;i<ne;i++)
- {
- if(pe[i].tipo=='r')
- {
- v=((x-pe[i].v1)*(x-pe[i].v1)+(y-pe[i].v2)*(y-pe[i].v2)-(pe[i].v3)*(pe[i].v3));
- }
- else
- {
- v=x*pe[i].v1+y*pe[i].v2+pe[i].v3;
- }
- if(pe[i].segno=='+' && v<0 || pe[i].segno=='-' && v>0)
- {
- break;
- }
- }
- if(i==ne)
- {
- np=np+1;
- }
- }
- }
- area=(np*as)/((SUDDIVISIONE+1)*(SUDDIVISIONE+1));
- printf("area totale: %.5f",area);
- return EXIT_SUCCESS;
- file_err: printf("File Error!\n"); return EXIT_FAILURE;
- mem_err: printf("Mem Error!\n"); return EXIT_FAILURE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement