Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include<cmath>
- #include <stdio.h>
- using namespace std;
- struct graph
- {
- int x;
- int y;
- int weight;
- };
- struct node
- {
- graph G;
- node* next;
- };
- void push (node* &p, graph G)
- {
- node *temp;
- temp = new node;
- temp->G = G;
- temp->next =p;
- p = temp;
- }
- void show (node* &p)
- {
- while (p)
- {
- cout<<p->G.x <<" "<< p->G.y << " "<<p->G.weight<<endl;
- p = p->next;
- }
- }
- void read (node* &p,ifstream &file)
- {
- p = NULL;
- graph g;
- while (file >> g.x >>g.y >> g.weight)
- {
- push(p, g);
- }
- }
- void write (node* p,ofstream &out)
- {
- while (p)
- {
- out<<p->G.x <<" "<< p->G.y << " "<<p->G.weight<<endl;
- p = p->next;
- }
- }
- void show_matrix (ofstream &fout, int **matrix,int N)
- {
- for (int i=0;i<N;i++)
- {
- for (int j=0;j<N;j++)
- {
- fout<<matrix[i][j]<<" ";
- }
- fout<<endl;
- }
- }
- void read_matrix (ifstream &fin, int matrix[5][5],int N)
- {
- {
- for (int i=0;i<N;i++)
- {
- for (int j=0;j<N;j++)
- {
- fin>>matrix[i][j];
- }
- }
- }
- }
- void convert_matrix (int matrix[5][5],int N,ofstream &fout)
- {
- int col=0, i, j, j_b=0;
- for(i=0; i<N; i++)
- for(j=0; j<N; j++)
- if(matrix[i][j])
- col++;
- col/=2;
- int **b=new int*[N];
- for(i=0; i<N; i++)
- {
- b[i]=new int[col];
- for(j=0; j<col; j++)
- b[i][j]=0;
- }
- for(i=0; i<N; i++)
- for(j=i+1; j<N; j++)
- if(matrix[i][j])
- {
- b[i][j_b]=1;
- b[j][j_b]=1;
- j_b++;
- }
- show_matrix(fout, b, N);
- }
- void matrix_to_list (int matrix[5][5],int N,node *p,ofstream &fout)
- {
- int i, j;
- graph G;
- for (i = 0; i < N; i++)
- {
- for (j = 0; j < N; j++)
- {
- if (matrix[i][j] != 0)
- {
- G.weight = matrix[i][j];
- G.x = i + 1;
- G.y = j + 1;
- push(p, G);
- // write(p, fout);
- matrix[j][i] = 0;
- fout << G.x << " " << G.y << " " << "Weight = " << G.weight << endl;
- }
- }
- }
- }
- void search (node *p, int n)
- {
- node* tmp = p;
- int counter = 0;
- while (p)
- {
- if (tmp->G.x == n|| tmp->G.y ==n)
- {
- counter++;
- }
- tmp = tmp->next;
- }
- cout<<counter;
- }
- int main()
- {
- int n;
- node* first =NULL;
- ifstream fin ("input.txt");
- ofstream fout ("output.txt");
- ofstream fout2 ("output2.txt");
- int N = 5;
- int matrix[5][5];
- read_matrix(fin,matrix,N);
- convert_matrix(matrix,N,fout);
- matrix_to_list(matrix,N,first,fout2);
- cin>>n;
- search (first,n);
- return 0;
- }
Add Comment
Please, Sign In to add comment