Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<fstream>
  4. using namespace std;
  5. #define maxTask 20
  6. #define maxChild 19
  7. #define inf -1000;
  8. class task{
  9. public:
  10.  
  11.     char TaskName[40];
  12.     double duration;
  13.     double StatrTime;
  14.     int child[maxChild];
  15.     bool visited;
  16.     bool discoverd;
  17.     int parent;
  18.     int count;
  19.     task(char*t,double d){
  20.         strcpy(TaskName,t);
  21.         duration=d;
  22.         StatrTime=inf;
  23.         visited=false;
  24.         parent =-1;
  25.         count=0;
  26.         discoverd=false;
  27.         for (int i=0;i<maxChild;i++){
  28.             child[i]=-1;}
  29.     }};
  30. class graph{
  31. public:
  32.     int size;
  33.     task**g;
  34.     graph(int n);
  35.     ~graph();
  36.     void loadFile(char*FileName,int N);
  37.     void AddTask(int Index,char* tn,double D);
  38.     void AddEdge(int Indix,int Child,int N);
  39.     int StartTask(int n);
  40.     int EndTask(int n);
  41.     void find_printCB(int n,int start,int end);
  42.     void LongestPath(int S,int N);
  43.     void path(int start,int end);
  44. };
  45. graph::graph(int n){
  46.     g=new task*[n];}
  47.  
  48. void graph::loadFile(char*filename,int N){
  49.     ifstream file_in(filename,ios::in);
  50.     int y;
  51.     file_in>>y;
  52.     size=y;
  53.     int index;
  54.     double dur;
  55.     char TN[40];
  56.     for(int i=0;i<N;i++){
  57.         file_in>>index;
  58.         file_in>>TN;
  59.         file_in>>dur;
  60.         AddTask(index,TN,dur);}
  61.     int e;
  62.     int ch;
  63.     file_in>>e;
  64.     for(int j=0;j<e;j++){
  65.         file_in>>index;
  66.         file_in>>ch;
  67.         AddEdge(index,ch,N);}}
  68.  
  69. void graph::AddTask (int Index,char* tn,double D){
  70.  
  71.     g[Index]=new task(tn,D);return;}
  72.  
  73. void graph::AddEdge(int Index,int Child,int N){
  74.     for ( int k=0;k<(N-1);){
  75.         if(g[Index]->child[k]==-1){
  76.             g[Index]->child[k]=Child;
  77.             (g[Index]->count)++;
  78.             g[Child]->visited=true;
  79.  
  80.             break;}
  81.         else k++; }}
  82.  
  83. int graph::StartTask (int n){
  84.     for(int i=0;i<n;i++){
  85.         if(!(g[i]->visited))
  86.            
  87.         return i;}}
  88.  
  89.  
  90. int graph::EndTask (int n){
  91.     int Count=0;
  92.     int index;
  93.     for( int i =0;i<n;i++){
  94.         if (g[i]->count==0){
  95.            
  96.                 return i;
  97. }
  98.        
  99.            
  100.        
  101. }
  102. }
  103.  
  104. void graph::LongestPath (int S,int N){
  105.     bool found;
  106.     int i,j,max,ind,w,st,sum;
  107.     g[S]->discoverd =true;
  108.     for(j=0;j<g[S]->count ;j++){
  109.         w=g[S]->child [j];
  110.         st=g[S]->duration ;
  111.         sum=g[S]->StatrTime +st;
  112.         if(sum>g[w]->StatrTime ){
  113.             g[w]->StatrTime =sum ;
  114.             g[w]->parent =S;}}
  115.     //find undiscovered task
  116.     found=false;
  117.     max=inf;
  118.     for(i=0;i<N;i++){
  119.         if(!g[i]->discoverd)
  120.             if (g[i]->duration > max ){
  121.                 found=true;
  122.                 max=g[i]->duration ;
  123.                 ind=i;}}
  124.     if (found)
  125.         LongestPath (ind, N);}
  126. //print logest path
  127. void graph::path(int start,int end){
  128.     if(g[end]->parent ==start){
  129.         cout<<start<<"=>"<<end;}
  130.     else{
  131.         path(start,g[end]->parent);
  132.         cout<<"=>"<<end;}}
  133. //to find and print              
  134. void graph:: find_printCB(int n,int start,int end){
  135.     g[start]->StatrTime=0;
  136.     LongestPath(start,n);
  137.     path(start,end);}
  138.  
  139.  
  140. graph::~graph (){
  141.     for(int i=0;i<size;i++){
  142.         delete g[i];}}
  143. int main() {
  144.  
  145.     char fileName[40];
  146.     int n;
  147.     cout<<"please enter the name of file"<<endl;
  148.     cin>>fileName;
  149.     ifstream file_in(fileName,ios::in);
  150.     file_in>>n;
  151.     graph z(n);
  152.    
  153.     int x;
  154.     int b;
  155.     int choice;
  156.     while (true){
  157.         cout<<"**********************MENU************************"<<endl;
  158.         cout<<"*     "<<"1-load new file                         *"<<endl;      
  159.         cout<<"*     "<<"2-find the starting task                *"<<endl;      
  160.         cout<<"*     "<<"3-find the ending task                  *"<<endl;              
  161.         cout<<"*     "<<"4-find and print the critical path      *"<<endl;              
  162.         cout<<"*     "<<"5-exit                                  *"<<endl;
  163.         cout<<"please enter your choice"<<endl;
  164.         cin>>choice;
  165.  
  166.         switch(choice){
  167.  
  168.  
  169.         case 1:{
  170.             z.loadFile(fileName,n);
  171.             x=z.StartTask(z.size);
  172.             b=z.EndTask(z.size);
  173.             break;}
  174.         case 2:{
  175.             cout<<z.g[x]->TaskName <<endl;
  176.  
  177.            
  178.             break;
  179.                }
  180.         case 3:{
  181.             cout<<z.g[b]->TaskName<<endl;
  182.  
  183.            
  184.             break;
  185.                }
  186.         case 4:{
  187.             z.find_printCB(z.size,x,b);break;}
  188.         case 5:{
  189.             return 0;
  190.             getch();break;}
  191.         default:{
  192.             cout<<"unknown choice"<<endl; }}}
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement