Advertisement
Duhan

Mo2

Dec 5th, 2019
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.97 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6.  
  7. int main()
  8. {
  9.  
  10.     // variables for reading the measurements from file
  11.     FILE* fp;
  12.     size_t len = 0;
  13.     char resp;
  14.  
  15.     double x, y;
  16.  
  17.     // open the input file "messwerte.txt" for reading
  18.     fp = fopen("polygon1.txt", "r");
  19.     if (fp == NULL)
  20.     {
  21.         // if file could not be opened (wrong path, not readable, ...)
  22.         // output a short message and immediately exit
  23.         printf("Eingabedatei kann nicht geoeffnet werden.\n");
  24.         scanf("%c", &resp);
  25.         exit(EXIT_FAILURE);
  26.     }
  27.  
  28.     // print program header
  29.     printf("\n\nProgramm zur Berechnung eines Polygons\n");
  30.     printf("--------------------------------------------------------\n");
  31.  
  32.     // the following loop reads a new value in every iteration
  33.     // until the end of the file or some invalid token is found
  34.     int i = 1;
  35.     int n = 0;
  36.     double xarray[50];
  37.     double yarray[50];
  38.  
  39.  
  40.     while (1)
  41.     {
  42.         len = fscanf(fp, "%lf %lf", &x, &y);
  43.  
  44.         if (len == EOF)
  45.             break;
  46.         else if (len == 0) {
  47.             printf("Unerwartetes Zeichen in der Eingabedatei.");
  48.             scanf("%c", &resp);
  49.             exit(EXIT_FAILURE);
  50.         }
  51.  
  52.         xarray[i] = x;
  53.         yarray[i] = y;
  54.  
  55.         printf("Lese Eckpunkt: %6.2f/%6.2f\n", x, y);
  56.         i++;
  57.         n++;
  58.     }
  59.  
  60.     double sum = 0;
  61.     double temp;
  62.  
  63.     i = 0;
  64.     //if n= 4
  65.     if (n == 4) {
  66.         for (i = 1;i < n;i++) {
  67.  
  68.             temp = (yarray[i] + yarray[i + 1]) * (xarray[i] - xarray[i + 1]) / 2;
  69.  
  70.             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);
  71.             sum = sum + temp;
  72.         }
  73.  
  74.     }
  75.  
  76.     //if n = 3 (xarray[1]-xarray[2])*(yarray[3]-yarray[2]
  77.     if (n == 3) {
  78.  
  79.         sum = 0.5 * (xarray[2] - xarray[1]) * (yarray[3] - yarray[2]);
  80.  
  81.  
  82.     }
  83.     // output results
  84.     printf("\nErgebnisse:\n");
  85.     printf("-----------\n\n");
  86.     printf("\nFlaecheninhalt = %.5f", sum);
  87.     // finally close the input file and clean up memory
  88.     fclose(fp);
  89.  
  90.     // wait for user input before closing terminal
  91.     scanf("%c", &resp);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement