Advertisement
filip710

PROG2 LV10 Z1

Jun 29th, 2016
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. /* Napišite C program koji učitava točke i trokute iz datoteke model.txt. Program iz
  2. datoteke učitava prvo broj točaka n, zatim broj trokuta m, a zatim točke i trokute.
  3. Potrebno je dinamički alocirati memoriju za unos točaka i trokuta. Pronaći i ispisati
  4. opseg trokuta koji ima najveći opseg u formatu „%.2f“. */
  5.  
  6. // MAIN
  7.  
  8. #include<math.h>
  9. #include<stdio.h>
  10. #include<stdlib.h>
  11. #include "myheader.h"
  12.  
  13.  
  14. int main()
  15. {
  16.     FILE *f1;
  17.     int i, ind1, ind2, ind3, n, m;
  18.     float trenutniOpseg, maxOpseg=0;
  19.     Tocka *tocke;
  20.     Trokut *trokuti;
  21.     float l1, l2, l3;
  22.     f1 = fopen("model.txt", "r");
  23.  
  24.  
  25.     fscanf(f1, "%d", &n);
  26.     tocke = (Tocka*)malloc(n*sizeof(Tocka));
  27.     fscanf(f1, "%d", &m);
  28.     trokuti = (Trokut*)malloc(m*sizeof(Trokut));
  29.     printf("%d\n%d\n", n, m);
  30.     for (i = 0; i<n; i++)
  31.     {
  32.         fscanf(f1, "%f%f%f", &tocke[i].x, &tocke[i].y, &tocke[i].z);
  33.     }
  34.  
  35.     for (i = 0; i<n; i++)
  36.     {
  37.         printf("%f %f %f\n", tocke[i].x, tocke[i].y, tocke[i].z);
  38.     }
  39.  
  40.     for (i = 0; i<m; i++)
  41.     {
  42.         fscanf(f1, "%d%d%d", &ind1, &ind2, &ind3);
  43.         trokuti[i].t1 = &tocke[ind1];
  44.         trokuti[i].t2 = &tocke[ind2];
  45.         trokuti[i].t3 = &tocke[ind3];
  46.     }
  47.  
  48.     for (i = 0; i<m; i++)
  49.     {
  50.         trenutniOpseg = 0;
  51.         l1 = len3d(trokuti[i].t1->x, trokuti[i].t1->y, trokuti[i].t1->z, trokuti[i].t2->x, trokuti[i].t2->y, trokuti[i].t2->z);
  52.         l2 = len3d(trokuti[i].t2->x, trokuti[i].t2->y, trokuti[i].t2->z, trokuti[i].t3->x, trokuti[i].t3->y, trokuti[i].t3->z);
  53.         l3 = len3d(trokuti[i].t3->x, trokuti[i].t3->y, trokuti[i].t3->z, trokuti[i].t1->x, trokuti[i].t1->y, trokuti[i].t1->z);
  54.         trenutniOpseg = l1 + l2 + l3;
  55.         if (trenutniOpseg>maxOpseg) maxOpseg = trenutniOpseg;
  56.     }
  57.     printf(".*REZULTATI:\n%.2f.*\n", maxOpseg);
  58.  
  59.     return 0;
  60. }
  61.  
  62. //FUNKCIJE
  63.  
  64. #include "myheader.h"
  65. #include<math.h>
  66.  
  67. float len3d(float x1, float y1, float z1, float x2, float y2, float z2)
  68. {
  69.     return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) + pow(z2 - z1, 2));
  70. }
  71.  
  72. //HEADER
  73.  
  74. #ifndef MYHEADER_H
  75. #define MYHEADER_H
  76.  
  77. float len3d(float x1, float y1, float z1, float x2, float y2, float z2);
  78.  
  79. typedef struct tocka{
  80.  
  81.     float x;
  82.     float y;
  83.     float z;
  84. }Tocka;
  85.  
  86. typedef struct trokut{
  87.  
  88.     struct tocka *t1;
  89.     struct tocka *t2;
  90.     struct tocka *t3;
  91. }Trokut;
  92.  
  93. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement