kej

Описать граф в виде списка и посчитать его вес.

kej
Apr 28th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<cmath>
  4. using namespace std;
  5. struct graph
  6. {
  7.     int x1,x2;
  8.     int y1,y2;
  9.     double weight;
  10. };
  11. struct node
  12. {
  13.     graph G;
  14.     node* next;
  15. };
  16. void push (node* &p, graph G)
  17. {
  18.     node *temp;
  19.     temp = new node;
  20.     temp->G = G;
  21.     temp->next =p;
  22.     p= temp;
  23.    
  24.    
  25. }
  26.  
  27. void show (node* &p)
  28. {
  29.     while (p)
  30.     {
  31.         cout<<p->G.x1 <<" "<< p->G.y1 <<" "<< p->G.x2<<" " << p->G.y2 << " "<<p->G.weight<<endl;
  32.         p = p->next;
  33.     }
  34. }
  35. void read (node* &p,ifstream &file)
  36. {
  37.     p = NULL;
  38.     graph G;
  39.     while (file >> G.x1 >>G.y1 >> G.y2 >>G.x2)
  40.     {
  41.        
  42.         G.weight = sqrt((pow(G.x1 - G.x2, 2)+pow(G.y1 - G.y2,2)));
  43.         push(p, G);
  44.        
  45.     }
  46. }
  47. void write (node* &p,ofstream &out)
  48. {
  49.     while (p)
  50.     {
  51.         out<<p->G.x1 <<" "<< p->G.y1 <<" "<< p->G.x2<<" " << p->G.y2 << " "<<p->G.weight<<endl;
  52.                p = p->next;
  53.     }
  54. }
  55. int main()
  56. {
  57.     node *list;
  58.     ifstream f ("input.txt");
  59.     ofstream out ("output.txt");
  60.     read(list, f);
  61.     write(list, out);
  62.    
  63.    
  64. }
Add Comment
Please, Sign In to add comment