我一直在努力尋找作業之間的逆向關係。更具體地說,我會用一個例子來講述它。如何找到作業之間的相反關係?
假設我有n個工作,即{0,1,2,3,4,...n}
。我也有工作之間的關係。我知道只有後繼工作,即2
之後是4,5
。 5
後面跟着7,8
等。我把它放在一個文本文件中。我想獲得作業之間的優先關係(什麼是5
的前任作業?)。
有文本輸出會很好。我有一些代碼,但它不起作用。
#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();
}
下面是一個簡單的輸入:
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
輸出應該是這樣的:
Id Predecesor1 Predecesor2 Predecesor3
........................................
...........................................
...........................................
你需要舉一個更好的例子。請發佈示例輸入文件以及您期望的輸出。 –
好的。如何添加一個txt文件 – furkan
僅供參考'void main()'不正確;無論是C還是C++。 –