Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Napišite C program koji učitava točke i trokute iz datoteke model.txt. Program iz
- datoteke učitava prvo broj točaka n, zatim broj trokuta m, a zatim točke i trokute.
- Potrebno je dinamički alocirati memoriju za unos točaka i trokuta. Pronaći i ispisati
- opseg trokuta koji ima najveći opseg u formatu „%.2f“. */
- // MAIN
- #include<math.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include "myheader.h"
- int main()
- {
- FILE *f1;
- int i, ind1, ind2, ind3, n, m;
- float trenutniOpseg, maxOpseg=0;
- Tocka *tocke;
- Trokut *trokuti;
- float l1, l2, l3;
- f1 = fopen("model.txt", "r");
- fscanf(f1, "%d", &n);
- tocke = (Tocka*)malloc(n*sizeof(Tocka));
- fscanf(f1, "%d", &m);
- trokuti = (Trokut*)malloc(m*sizeof(Trokut));
- printf("%d\n%d\n", n, m);
- for (i = 0; i<n; i++)
- {
- fscanf(f1, "%f%f%f", &tocke[i].x, &tocke[i].y, &tocke[i].z);
- }
- for (i = 0; i<n; i++)
- {
- printf("%f %f %f\n", tocke[i].x, tocke[i].y, tocke[i].z);
- }
- for (i = 0; i<m; i++)
- {
- fscanf(f1, "%d%d%d", &ind1, &ind2, &ind3);
- trokuti[i].t1 = &tocke[ind1];
- trokuti[i].t2 = &tocke[ind2];
- trokuti[i].t3 = &tocke[ind3];
- }
- for (i = 0; i<m; i++)
- {
- trenutniOpseg = 0;
- 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);
- 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);
- 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);
- trenutniOpseg = l1 + l2 + l3;
- if (trenutniOpseg>maxOpseg) maxOpseg = trenutniOpseg;
- }
- printf(".*REZULTATI:\n%.2f.*\n", maxOpseg);
- return 0;
- }
- //FUNKCIJE
- #include "myheader.h"
- #include<math.h>
- float len3d(float x1, float y1, float z1, float x2, float y2, float z2)
- {
- return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) + pow(z2 - z1, 2));
- }
- //HEADER
- #ifndef MYHEADER_H
- #define MYHEADER_H
- float len3d(float x1, float y1, float z1, float x2, float y2, float z2);
- typedef struct tocka{
- float x;
- float y;
- float z;
- }Tocka;
- typedef struct trokut{
- struct tocka *t1;
- struct tocka *t2;
- struct tocka *t3;
- }Trokut;
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement