How to find reverse relations between jobs?
I've been trying to find reverse relations between jobs. To be more specific I will tell it with an example.
Suppose I have n jobs i.e {0,1,2,3,4,...n}
. I also have relations between jobs. I know only successor jobs, i.e., 2
is followed by 4,5
. 5
is followed by 7,8
etc. I have it in a text file. I want to obtaing precedence relations between jobs (what is the predecessor job of 5
?).
Having text output would be great. I have some code but it does not work.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
#define TOTAL_ACTIVITY 123
void readFile();
void change ();
void writeFile ();
struct Activity {
int precedessor [3];
int successor [3];
int id;
};
Activity activityList[TOTAL_ACTIVITY];
void main() {
readFile();
change();
writeFile();
}
void readFile() {
ifstream myReadFile;
myReadFile.open("pre.txt");
if (!myReadFile) { //check whether the file can be opened
cerr << "Unable to open file"; // terminate with error
}
while (!myReadFile.eof()) {
int Id,suc1, suc2, suc3;
int t = 0;
while (myReadFile >> Id >> suc1 >> suc2 >> suc3) //data should be in this order
{
activityList[t].id = Id;
activityList[t].successor [0] = suc1;
activityList[t].successor [1] = suc2;
activityList[t].successor [2] = suc3;
t++;
}
}
return;
}
void change() {
int act;
for (int i=1;i<TOTAL_ACTIVITY;i++){
for (int j=0;j<TOTAL_ACTIVITY;j++){
for (int k=0;k<3;k++) {
if (activityList[j].successor[k]==i;)
}
}
}
}
void writeFile() {
ofstream out("out.txt");
out << "id\t" << "Pre1\t" << "Pre2\t" << "Pre3\t"<<"\n";
for (int j = 0; j < TOTAL_ACTIVITY; j++) {
out << activityList[j].id << "\t";
out << activityList[j].precedessor[0]<< "\t";
out << activityList[j].precedessor[1] << "\t";
out << activityList[j].precedessor[2] << "\t";
out << "\n";
}
out.close();
}
Here is a sample input:
ID Successor1 Successor2 Successor3
1 2 3 4
2 6 11 15
3 7 8 13
4 5 9 10
5 20
6 30
7 27
8 12 19 27
9 14
10 16 25
11 20 26
12 14
13 17 18
14 17
15 25
16 21 22
17 22
18 20 22
19 24 29
20 23 25
21 28
22 23
23 24
24 30
25 30
26 31
27 28
28 31
29 32
30 32
31 32
Output should be something like this:
Id开发者_如何学JAVA Predecesor1 Predecesor2 Predecesor3
........................................
...........................................
...........................................
You are given the successors of a job, but you do not need to retain this information.
For example, if I say: 5 -> 6, 7
meaning that 5
is followed by 6
and 7
, then it is equivalent to saying that 6
and 7
are preceded by 5
, right.
You can then either:
- directly output the precedence when reading the successors
- store in an associative container, using the job id as the key and the predecessor as value
Specifics details... are for you to work out to conclude your homework.
精彩评论