Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- class CriticalPath{
- private:
- int taskId; //Holds the identification number of the task. e.g. task#1 = 1, task#2 = 2, etc...
- int time; //Holds the time needed to complete the task
- vector<int> prereqs; //holds the prerequisites for each task
- public:
- CriticalPath(); //default constructor
- CriticalPath(int i, int t); //constructor for time and id
- CriticalPath(int i, int t, vector<int> p); //constructor for all fields
- void setTaskID(int i); //id mutator
- void setTime(int t); //time mutator
- void setPrereqs(int p); //prereq mutator
- int getTaskID(); //id accessor
- int getTime(); //time accessor
- vector<int> getPrereqs(); //prereq accessor
- void findNoEdge(vector<int> b, int i); //Checks for prereqs(edges).
- static int topSort(); //Sorts the project tasks topologically.
- static void test(); //function tests various items.
- };
- //default constructor
- CriticalPath::CriticalPath(){
- taskId = 0;
- time = 0;
- }
- //constructor for time and id
- CriticalPath::CriticalPath(int i, int t){
- taskId = i;
- time = t;
- }
- //Mutators
- void CriticalPath::setTaskID(int i){
- taskId = i;
- }
- void CriticalPath::setTime(int t){
- time = t;
- }
- //Needs user input when called
- //received the number p of prereqs for a task, and iterates p times.
- void CriticalPath::setPrereqs(int p){
- int a;
- 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.
- for(int i = 0; i < p; i++){
- cout << "Enter prerequisite #" << i+1 << "for task #" << taskId << "." << endl;
- cin >> a;
- prereqs.push_back(a);
- }
- }
- //Accessors
- int CriticalPath::getTaskID(){
- return taskId;
- }
- int CriticalPath::getTime(){
- return time;
- }
- vector<int> CriticalPath::getPrereqs(){
- return prereqs;
- }
- //***************************************
- //========Functions that do work!========
- //***************************************
- vector<int> noEdge; //vector holds ID of tasks with no prerequisite.
- vector<int> sortedList; //Vector to hold sorted tasks.
- //Function just checks to ensure values are stored in noPrereq
- void CriticalPath::test(){
- cout << "Sorted list: ";
- for(unsigned int q = 0; q < noEdge.size(); q++){
- cout << sortedList.at(q) << " ";
- }
- cout << endl;
- }
- //Function checks each task for incoming edges (prereqs)
- //If there are not, it pushes the taskId onyl noEdge<>
- void CriticalPath::findNoEdge(vector<int> b, int i){
- int r = 0;
- for(unsigned int z = 0; z < b.size(); z++){
- r += b.at(z);
- }
- if(r == 0){
- noEdge.push_back(i);
- }
- }
- int CriticalPath::topSort(){
- while(!noEdge.empty()){
- sortedList.push_back(noEdge.at(0)); //push first element of noEdge<> onto sortedList<>
- int n = noEdge.at(0); //sets the first element of noEdge to n
- noEdge.erase(noEdge.begin()); //erase first element of noEdge
- //check all prereqs, if n is a prereq delete it.
- //Then check if no edges for each again, if no edges add to noEdge and run this again.
- //Run until there are no edges for any tasks.
- return n;
- }
- }
- int main(){
- int numTasks, input, input2;
- cout << "How many tasks are required to complete your project?" << endl;
- cin >> numTasks;
- //Creating an array of objects.
- CriticalPath begin(0,0); //bogus beginning task.
- CriticalPath* task = new CriticalPath[numTasks];
- //For loop to set time and taskID for each object
- for(int i = 0; i < numTasks; i++){
- cout <<"How much time is required for task #" << i+1 << "?" << endl;
- cin >> input;
- task[i].setTaskID(i + 1);
- task[i].setTime(input);
- }
- //For loop to set the prereqs for each object.
- for(int j = 0; j < numTasks; j++){
- cout << "How many prerequisites are there for task #" << j+1 << "?" << endl;
- cin >> input2;
- task[j].setPrereqs(input2);
- }
- //==========THIS IS WHERE IT GETS WEIRD==========
- do{
- //Find tasks with no incoming edges, add them to a list.
- //Note: on second iteration there SHOULD be new items to add to the list.
- for(int k = 0; k < numTasks; k++){
- task[k].findNoEdge(task[k].getPrereqs(), task[k].getTaskID());
- }
- //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
- //Delete it from the prerequisite vector of each task in which is appears.
- for(int l = 0; l < numTasks; l++){
- vector<int> g = task[l].getPrereqs(); //g is assigned the prereq vector of the task being worked on.
- for(unsigned int m = 0; m < g.size(); m++){
- int h = CriticalPath::topSort();
- if(g.at(m) == h){
- //THIS NEEDS TO BE SENT BACK TO THE CLASS OBJECT!!!
- g.erase(g.begin() + m);
- }
- }
- }
- //Run until there are no values in noEdge<>
- }while(!noEdge.empty());
- CriticalPath::test();
- //Used to keep the console open.
- cout << "Enter 1 and then hit enter to exit." << endl;
- cin >> input;
- delete[] task; //Kill'em all!!
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement