Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<conio.h>
- #include<fstream>
- using namespace std;
- #define maxTask 20
- #define maxChild 19
- #define inf -1000;
- class task{
- public:
- char TaskName[40];
- double duration;
- double StatrTime;
- int child[maxChild];
- bool visited;
- bool discoverd;
- int parent;
- int count;
- task(char*t,double d){
- strcpy(TaskName,t);
- duration=d;
- StatrTime=inf;
- visited=false;
- parent =-1;
- count=0;
- discoverd=false;
- for (int i=0;i<maxChild;i++){
- child[i]=-1;}
- }};
- class graph{
- public:
- int size;
- task**g;
- graph(int n);
- ~graph();
- void loadFile(char*FileName,int N);
- void AddTask(int Index,char* tn,double D);
- void AddEdge(int Indix,int Child,int N);
- int StartTask(int n);
- int EndTask(int n);
- void find_printCB(int n,int start,int end);
- void LongestPath(int S,int N);
- void path(int start,int end);
- };
- graph::graph(int n){
- g=new task*[n];}
- void graph::loadFile(char*filename,int N){
- ifstream file_in(filename,ios::in);
- int y;
- file_in>>y;
- size=y;
- int index;
- double dur;
- char TN[40];
- for(int i=0;i<N;i++){
- file_in>>index;
- file_in>>TN;
- file_in>>dur;
- AddTask(index,TN,dur);}
- int e;
- int ch;
- file_in>>e;
- for(int j=0;j<e;j++){
- file_in>>index;
- file_in>>ch;
- AddEdge(index,ch,N);}}
- void graph::AddTask (int Index,char* tn,double D){
- g[Index]=new task(tn,D);return;}
- void graph::AddEdge(int Index,int Child,int N){
- for ( int k=0;k<(N-1);){
- if(g[Index]->child[k]==-1){
- g[Index]->child[k]=Child;
- (g[Index]->count)++;
- g[Child]->visited=true;
- break;}
- else k++; }}
- int graph::StartTask (int n){
- for(int i=0;i<n;i++){
- if(!(g[i]->visited))
- return i;}}
- int graph::EndTask (int n){
- int Count=0;
- int index;
- for( int i =0;i<n;i++){
- if (g[i]->count==0){
- return i;
- }
- }
- }
- void graph::LongestPath (int S,int N){
- bool found;
- int i,j,max,ind,w,st,sum;
- g[S]->discoverd =true;
- for(j=0;j<g[S]->count ;j++){
- w=g[S]->child [j];
- st=g[S]->duration ;
- sum=g[S]->StatrTime +st;
- if(sum>g[w]->StatrTime ){
- g[w]->StatrTime =sum ;
- g[w]->parent =S;}}
- //find undiscovered task
- found=false;
- max=inf;
- for(i=0;i<N;i++){
- if(!g[i]->discoverd)
- if (g[i]->duration > max ){
- found=true;
- max=g[i]->duration ;
- ind=i;}}
- if (found)
- LongestPath (ind, N);}
- //print logest path
- void graph::path(int start,int end){
- if(g[end]->parent ==start){
- cout<<start<<"=>"<<end;}
- else{
- path(start,g[end]->parent);
- cout<<"=>"<<end;}}
- //to find and print
- void graph:: find_printCB(int n,int start,int end){
- g[start]->StatrTime=0;
- LongestPath(start,n);
- path(start,end);}
- graph::~graph (){
- for(int i=0;i<size;i++){
- delete g[i];}}
- int main() {
- char fileName[40];
- int n;
- cout<<"please enter the name of file"<<endl;
- cin>>fileName;
- ifstream file_in(fileName,ios::in);
- file_in>>n;
- graph z(n);
- int x;
- int b;
- int choice;
- while (true){
- cout<<"**********************MENU************************"<<endl;
- cout<<"* "<<"1-load new file *"<<endl;
- cout<<"* "<<"2-find the starting task *"<<endl;
- cout<<"* "<<"3-find the ending task *"<<endl;
- cout<<"* "<<"4-find and print the critical path *"<<endl;
- cout<<"* "<<"5-exit *"<<endl;
- cout<<"please enter your choice"<<endl;
- cin>>choice;
- switch(choice){
- case 1:{
- z.loadFile(fileName,n);
- x=z.StartTask(z.size);
- b=z.EndTask(z.size);
- break;}
- case 2:{
- cout<<z.g[x]->TaskName <<endl;
- break;
- }
- case 3:{
- cout<<z.g[b]->TaskName<<endl;
- break;
- }
- case 4:{
- z.find_printCB(z.size,x,b);break;}
- case 5:{
- return 0;
- getch();break;}
- default:{
- cout<<"unknown choice"<<endl; }}}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement