kej

На вход подаётся матрица смежности (с файла) Реализовать фу

kej
May 5th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<cmath>
  4. #include <stdio.h>
  5. using namespace std;
  6. struct graph
  7. {
  8.     int x;
  9.     int y;
  10.     int weight;
  11. };
  12. struct node
  13. {
  14.     graph G;
  15.     node* next;
  16. };
  17. void push (node* &p, graph G)
  18. {
  19.     node *temp;
  20.     temp = new node;
  21.     temp->G = G;
  22.     temp->next =p;
  23.     p = temp;
  24.    
  25.    
  26. }
  27. void show (node* &p)
  28. {
  29.     while (p)
  30.     {
  31.         cout<<p->G.x <<" "<< p->G.y << " "<<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.x >>g.y >> g.weight)
  40.     {
  41.         push(p, g);
  42.     }
  43. }
  44. void write (node* p,ofstream &out)
  45. {
  46.     while (p)
  47.     {
  48.         out<<p->G.x <<" "<< p->G.y << " "<<p->G.weight<<endl;
  49.                p = p->next;
  50.     }
  51. }
  52.  
  53. void show_matrix (ofstream &fout, int **matrix,int N)
  54. {
  55.     for (int i=0;i<N;i++)
  56.               {
  57.                  
  58.               for (int j=0;j<N;j++)
  59.                   {
  60.                       fout<<matrix[i][j]<<" ";
  61.                   }
  62.                   fout<<endl;
  63.               }
  64. }
  65. void read_matrix (ifstream &fin, int matrix[5][5],int N)
  66. {
  67.    {
  68.     for (int i=0;i<N;i++)
  69.         {
  70.         for (int j=0;j<N;j++)
  71.             {
  72.                 fin>>matrix[i][j];
  73.             }
  74.         }
  75.     }
  76. }
  77. void convert_matrix (int matrix[5][5],int N,ofstream &fout)
  78. {
  79.    
  80.   int col=0, i, j, j_b=0;
  81.      for(i=0; i<N; i++)
  82.          for(j=0; j<N; j++)
  83.              if(matrix[i][j])
  84.                  col++;
  85.      col/=2;
  86.      int **b=new int*[N];
  87.      for(i=0; i<N; i++)
  88.      {
  89.          b[i]=new int[col];
  90.          for(j=0; j<col; j++)
  91.              b[i][j]=0;
  92.      }
  93.      for(i=0; i<N; i++)
  94.          for(j=i+1; j<N; j++)
  95.              if(matrix[i][j])
  96.              {
  97.                  b[i][j_b]=1;
  98.                  b[j][j_b]=1;
  99.                  j_b++;
  100.              }
  101.     show_matrix(fout, b, N);
  102. }
  103. void matrix_to_list (int matrix[5][5],int N,node *p,ofstream &fout)
  104. {
  105.    
  106.     int i, j;
  107.     graph G;
  108.     for (i = 0; i < N; i++)
  109.     {
  110.     for (j = 0; j < N; j++)
  111.     {
  112.     if (matrix[i][j] != 0)
  113.     {
  114.     G.weight = matrix[i][j];
  115.     G.x = i + 1;
  116.     G.y = j + 1;
  117.     push(p, G);
  118.    // write(p, fout);
  119.     matrix[j][i] = 0;
  120.     fout << G.x << " " << G.y << " " << "Weight = " << G.weight << endl;
  121.     }
  122.     }
  123.     }
  124. }
  125. void search (node *p, int n)
  126. {
  127.     node* tmp = p;
  128.     int counter = 0;
  129.     while (p)
  130.     {
  131.         if (tmp->G.x == n|| tmp->G.y ==n)
  132.         {
  133.             counter++;
  134.         }
  135.         tmp = tmp->next;
  136.     }
  137.     cout<<counter;
  138. }
  139. int main()
  140. {
  141.     int n;
  142.     node* first =NULL;
  143.     ifstream fin ("input.txt");
  144.     ofstream fout ("output.txt");
  145.     ofstream fout2 ("output2.txt");
  146.     int N = 5;
  147.     int matrix[5][5];
  148.     read_matrix(fin,matrix,N);
  149.     convert_matrix(matrix,N,fout);
  150.     matrix_to_list(matrix,N,first,fout2);
  151.     cin>>n;
  152.     search (first,n);
  153.    
  154.    
  155.    
  156.     return 0;
  157. }
Add Comment
Please, Sign In to add comment