Advertisement
Infidelis

Lab1

Mar 2nd, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. typedef struct point
  6. {
  7.     int x;
  8.     int y;
  9. } point;
  10.  
  11. typedef struct line
  12. {
  13.     point p1;
  14.     point p2;
  15. } line;
  16.  
  17. typedef struct reads
  18. {
  19.     line *p;
  20.     int n;
  21.     FILE *in;
  22.     FILE *out;
  23.     float s;
  24. } reads;
  25.  
  26. void read(reads *c)
  27. {
  28.     fopen_s(&c->in, "C:\\Users\\Infidelis\\Desktop\\input.txt", "r");
  29.     fscanf_s(c->in, "%d", &c->n);
  30.     c->p = (line*)malloc(c->n * sizeof(line));
  31.     for (int i = 0; i < c->n; i++)
  32.         fscanf_s(c->in, "%d%d%d%d", &c->p[i].p1.x, &c->p[i].p1.y, &c->p[i].p2.x, &c->p[i].p2.y);
  33.     fclose(c->in);
  34. }
  35.  
  36. float sum(reads *c)
  37. {
  38.     c->s = 0;
  39.     for (int i = 0; i < c->n; i++)
  40.         c->s = c->s + sqrt(pow(c->p[i].p1.x - c->p[i].p2.x, 2) + pow(c->p[i].p1.y - c->p[i].p2.y, 2));
  41.     return c->s;
  42. }
  43.  
  44. void end(reads *c)
  45. {
  46.     fopen_s(&c->out, "C:\\Users\\Infidelis\\Desktop\\output.txt", "w");
  47.     fprintf_s(c->out, "%f", sum(c));
  48.     fclose(c->out);
  49.     free(c);
  50. }
  51.  
  52. void main()
  53. {
  54.     reads *c = (reads*)malloc(sizeof(reads));
  55.     read(c);
  56.     sum(c);
  57.     end(c);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement