Advertisement
Guest User

Untitled

a guest
Apr 8th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<conio.h>
  4.  
  5. using namespace std;
  6. FILE *graph;
  7. int n,//количество вершин
  8.     m;//количество ребер
  9. int **ms;
  10. int v,u,w;//начальная вершина,конечная, вес ребра
  11.  
  12. void read()
  13. {
  14.     if(fopen("graph.txt","r")==NULL)cout<<"ERROR\n";
  15.     graph=fopen("graph.txt","r");
  16.     fscanf(graph,"%i",n);
  17.     fscanf(graph,"%i",m);
  18.     ms=new int*[n];
  19.     for(int i=0;i<n;i++) ms[i]=new int[n];
  20.     for(int i=0;i<n;i++)
  21.         for(int j=0;j<n;j++)
  22.             ms[i][j]=0;
  23.     while(!feof(graph))
  24.     {
  25.         fscanf(graph,"%i",v);fscanf(graph,"%i",u);fscanf(graph,"%i",w);
  26.         if (w<0) cout<<"This program can't work right with weight<0";
  27.         else
  28.             ms[v][u]=w;
  29.     }
  30.     //вывод матрицы
  31.     for(int i=0;i<n;i++)
  32.         for(int j=0;j<n;j++)
  33.             cout<<ms[i][j]<<" ";
  34.     cout<<endl;
  35. }
  36.  
  37. void main()
  38. {
  39.     int key;
  40.     system("cls");
  41.     do{
  42.     cout<<"MENU\n";
  43.     cout<<"1.Read your graph\n";
  44.     cin>>key;
  45.     switch(key)
  46.         {case 1:{ read();break;}
  47.         }
  48.     }while(key!=0);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement