Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define FILE_IN "punti.txt"
- #define LUNGHEZZA 10
- int main(void)
- {
- char s[LUNGHEZZA];
- FILE* pf;
- int x, y, x1 = 0, y1 = 0, i=0;
- float area = 0;
- printf("il programma calcola aree di spezzate, acquisendo i punti da file di testo\n");
- pf = fopen(FILE_IN, "r");
- if (pf == NULL)
- {
- printf("Non ho potuto aprire il file %s\n", FILE_IN);
- return EXIT_FAILURE;
- }
- else
- {
- // il ciclo while conta quante righe sono presenti nel file
- while (fgets(s, sizeof(s), pf))
- {
- i++;
- }
- //il file deve essere chiuso e riaperto altrimenti continuerebbe a leggere dall'ultima riga
- fclose(pf);
- pf = fopen(FILE_IN, "r");
- //i deve essere >0 affinchè l'area possa essere positiva
- if (i == 0)
- printf("nel file non c'e' alcun punto!");
- else
- {
- do
- {
- fscanf(pf, "%s", s);
- x = atoi(s);
- fscanf(pf, "%s", s);
- y = atoi(s);
- area += ((float)((y + y1) * (x - x1)) / 2);
- x1 = x;
- y1 = y;
- i = i - 2;
- }
- while (i != 0);
- fclose(pf);
- printf("L'area della spezzata e': %f", area);
- return EXIT_SUCCESS;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement