Advertisement
Skadwick

Critical path

Nov 17th, 2012
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class CriticalPath{
  7. private:
  8. int taskId; //Holds the identification number of the task. e.g. task#1 = 1, task#2 = 2, etc...
  9. int time; //Holds the time needed to complete the task
  10. vector<int> prereqs; //holds the prerequisites for each task
  11.  
  12. public:
  13. CriticalPath(); //default constructor
  14. CriticalPath(int i, int t); //constructor for time and id
  15. CriticalPath(int i, int t, vector<int> p); //constructor for all fields
  16. void setTaskID(int i); //id mutator
  17. void setTime(int t); //time mutator
  18. void setPrereqs(int p); //prereq mutator
  19. int getTaskID(); //id accessor
  20. int getTime(); //time accessor
  21. vector<int> getPrereqs(); //prereq accessor
  22.  
  23. void findNoEdge(vector<int> b, int i); //Checks for prereqs(edges).
  24. static int topSort(); //Sorts the project tasks topologically.
  25. static void test(); //function tests various items.
  26. };
  27.  
  28. //default constructor
  29. CriticalPath::CriticalPath(){
  30. taskId = 0;
  31. time = 0;
  32. }
  33. //constructor for time and id
  34. CriticalPath::CriticalPath(int i, int t){
  35. taskId = i;
  36. time = t;
  37. }
  38.  
  39. //Mutators
  40. void CriticalPath::setTaskID(int i){
  41. taskId = i;
  42. }
  43.  
  44. void CriticalPath::setTime(int t){
  45. time = t;
  46. }
  47.  
  48. //Needs user input when called
  49. //received the number p of prereqs for a task, and iterates p times.
  50. void CriticalPath::setPrereqs(int p){
  51. int a;
  52. prereqs.push_back(0); //making the 'beginning' task a prerequisite for all. It's time is 0, so there is no effect on the final path.
  53.  
  54. for(int i = 0; i < p; i++){
  55. cout << "Enter prerequisite #" << i+1 << "for task #" << taskId << "." << endl;
  56. cin >> a;
  57. prereqs.push_back(a);
  58. }
  59. }
  60.  
  61. //Accessors
  62. int CriticalPath::getTaskID(){
  63. return taskId;
  64. }
  65.  
  66. int CriticalPath::getTime(){
  67. return time;
  68. }
  69.  
  70. vector<int> CriticalPath::getPrereqs(){
  71. return prereqs;
  72. }
  73.  
  74. //***************************************
  75. //========Functions that do work!========
  76. //***************************************
  77.  
  78. vector<int> noEdge; //vector holds ID of tasks with no prerequisite.
  79. vector<int> sortedList; //Vector to hold sorted tasks.
  80.  
  81. //Function just checks to ensure values are stored in noPrereq
  82. void CriticalPath::test(){
  83. cout << "Sorted list: ";
  84. for(unsigned int q = 0; q < noEdge.size(); q++){
  85. cout << sortedList.at(q) << " ";
  86. }
  87. cout << endl;
  88. }
  89.  
  90. //Function checks each task for incoming edges (prereqs)
  91. //If there are not, it pushes the taskId onyl noEdge<>
  92. void CriticalPath::findNoEdge(vector<int> b, int i){
  93. int r = 0;
  94. for(unsigned int z = 0; z < b.size(); z++){
  95. r += b.at(z);
  96. }
  97. if(r == 0){
  98. noEdge.push_back(i);
  99. }
  100. }
  101.  
  102. int CriticalPath::topSort(){
  103. while(!noEdge.empty()){
  104. sortedList.push_back(noEdge.at(0)); //push first element of noEdge<> onto sortedList<>
  105. int n = noEdge.at(0); //sets the first element of noEdge to n
  106. noEdge.erase(noEdge.begin()); //erase first element of noEdge
  107. //check all prereqs, if n is a prereq delete it.
  108. //Then check if no edges for each again, if no edges add to noEdge and run this again.
  109. //Run until there are no edges for any tasks.
  110. return n;
  111. }
  112. }
  113.  
  114. int main(){
  115. int numTasks, input, input2;
  116.  
  117. cout << "How many tasks are required to complete your project?" << endl;
  118. cin >> numTasks;
  119.  
  120. //Creating an array of objects.
  121. CriticalPath begin(0,0); //bogus beginning task.
  122. CriticalPath* task = new CriticalPath[numTasks];
  123.  
  124. //For loop to set time and taskID for each object
  125. for(int i = 0; i < numTasks; i++){
  126. cout <<"How much time is required for task #" << i+1 << "?" << endl;
  127. cin >> input;
  128. task[i].setTaskID(i + 1);
  129. task[i].setTime(input);
  130. }
  131.  
  132. //For loop to set the prereqs for each object.
  133. for(int j = 0; j < numTasks; j++){
  134. cout << "How many prerequisites are there for task #" << j+1 << "?" << endl;
  135. cin >> input2;
  136. task[j].setPrereqs(input2);
  137. }
  138.  
  139. //==========THIS IS WHERE IT GETS WEIRD==========
  140.  
  141. do{
  142. //Find tasks with no incoming edges, add them to a list.
  143. //Note: on second iteration there SHOULD be new items to add to the list.
  144. for(int k = 0; k < numTasks; k++){
  145. task[k].findNoEdge(task[k].getPrereqs(), task[k].getTaskID());
  146. }
  147.  
  148. //Take the first number in the list created above. Add it to another list, and then find all tasks that it is a prerequisite for
  149. //Delete it from the prerequisite vector of each task in which is appears.
  150. for(int l = 0; l < numTasks; l++){
  151. vector<int> g = task[l].getPrereqs(); //g is assigned the prereq vector of the task being worked on.
  152. for(unsigned int m = 0; m < g.size(); m++){
  153. int h = CriticalPath::topSort();
  154. if(g.at(m) == h){
  155. //THIS NEEDS TO BE SENT BACK TO THE CLASS OBJECT!!!
  156. g.erase(g.begin() + m);
  157. }
  158. }
  159. }
  160. //Run until there are no values in noEdge<>
  161. }while(!noEdge.empty());
  162.  
  163.  
  164.  
  165. CriticalPath::test();
  166.  
  167. //Used to keep the console open.
  168. cout << "Enter 1 and then hit enter to exit." << endl;
  169. cin >> input;
  170.  
  171. delete[] task; //Kill'em all!!
  172. return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement