Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- int main()
- {
- // variables for reading the measurements from file
- FILE* fp;
- size_t len = 0;
- char resp;
- double x, y;
- // open the input file "messwerte.txt" for reading
- fp = fopen("polygon1.txt", "r");
- if (fp == NULL)
- {
- // if file could not be opened (wrong path, not readable, ...)
- // output a short message and immediately exit
- printf("Eingabedatei kann nicht geoeffnet werden.\n");
- scanf("%c", &resp);
- exit(EXIT_FAILURE);
- }
- // print program header
- printf("\n\nProgramm zur Berechnung eines Polygons\n");
- printf("--------------------------------------------------------\n");
- // the following loop reads a new value in every iteration
- // until the end of the file or some invalid token is found
- int i = 1;
- int n = 0;
- double xarray[50];
- double yarray[50];
- while (1)
- {
- len = fscanf(fp, "%lf %lf", &x, &y);
- if (len == EOF)
- break;
- else if (len == 0) {
- printf("Unerwartetes Zeichen in der Eingabedatei.");
- scanf("%c", &resp);
- exit(EXIT_FAILURE);
- }
- xarray[i] = x;
- yarray[i] = y;
- printf("Lese Eckpunkt: %6.2f/%6.2f\n", x, y);
- i++;
- n++;
- }
- double sum = 0;
- double temp;
- i = 0;
- //if n= 4
- if (n == 4) {
- for (i = 1;i < n;i++) {
- temp = (yarray[i] + yarray[i + 1]) * (xarray[i] - xarray[i + 1]) / 2;
- printf("\nx = %.1f |x+1 = %.1f | y = %.1f |y+1 = %.1f | i = %d | temp = %.1f", xarray[i], xarray[i + 1], yarray[i], yarray[i + 1], i, temp);
- sum = sum + temp;
- }
- }
- //if n = 3 (xarray[1]-xarray[2])*(yarray[3]-yarray[2]
- if (n == 3) {
- sum = 0.5 * (xarray[2] - xarray[1]) * (yarray[3] - yarray[2]);
- }
- // output results
- printf("\nErgebnisse:\n");
- printf("-----------\n\n");
- printf("\nFlaecheninhalt = %.5f", sum);
- // finally close the input file and clean up memory
- fclose(fp);
- // wait for user input before closing terminal
- scanf("%c", &resp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement