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()
- {
- double flaecheninhalt;
- // 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("polygon.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 n=0;
- int a[999];
- int b[999];
- 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);
- }
- printf("Lese Eckpunkt: %6.2f/%6.2f\n", x, y);
- a[n] = x;
- b[n] = y;
- n++;
- }
- flaecheninhalt=0;
- for(int i=0; i<n;i++){
- if(i==(n-1)){
- flaecheninhalt +=((b[i]+b[0])*(a[i]-a[0]));
- }
- else{
- flaecheninhalt += ((b[i]+b[i+1])*(a[i]-a[i+1]));
- }
- }
- flaecheninhalt = flaecheninhalt/2;
- double xschwer, yschwer;
- for(int j=0;j<n;j++){
- if(j==(n-1)){
- xschwer += (a[j] + a[0]) * (a[j] * b[0] - a[0] * b[j]);
- yschwer += (b[j] + b[0]) * (a[j] * b[0] - a[0] * b[j]);
- }
- else {
- xschwer += (a[j] + a[j + 1]) * (a[j] * b[j + 1] - a[j + 1] * b[j]);
- yschwer += (b[j] + b[j + 1]) * (a[j] * b[j + 1] - a[j + 1] * b[j]);
- }
- }
- xschwer = xschwer / (6*flaecheninhalt);
- yschwer = yschwer / (6*flaecheninhalt);
- // output results
- printf("\nErgebnisse:\n");
- printf("-----------\n\n");
- printf("A = %.2f\n", flaecheninhalt);
- printf("Schwerpunkt = (%.2f|%.2f) \n", xschwer, yschwer);
- // 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