通过函数传递并分配给另一个对象时更改对象属性

时间:2019-05-10 02:41:32

标签: c++

问题在于使用openRemoveHead()传递节点及其在loadProcAry中传递的方式。 当我将从喜欢的列表中删除的节点分配给名为newJob的新对象时,jobID从1更改为13 ... 当我逐行运行调试器时:直到openRemoveHead()的最后一秒,返回的临时对象向我显示正确的ID。 但是当我在loadProcAry()中查看newJob时,它将更改为该随机数。 我不明白为什么会有一个随机数...我检查了我的重载赋值运算符及其罚款。有任何想法吗?

    #include <iostream>
#include <fstream>
using std::ifstream;
using std::ofstream;
using std::cin;
using std::cout;
using std::string;
struct Node{ //because they all public
    int jobID;
    int jobTime;
    int dependentCount;
    Node* next;
    Node()= default;
    Node(int jobID,int jobTime,int dependentCount):jobID(jobID),jobTime(jobTime),dependentCount(dependentCount){next= nullptr;}
    Node(const Node& rhs):jobID(jobID),jobTime(rhs.jobTime),dependentCount(rhs.dependentCount),next(next){}
    Node& operator=(const Node& rhs){
        if(this!=&rhs){
            jobID=rhs.jobID;
            jobTime=rhs.jobTime;
            dependentCount=rhs.dependentCount;
            next=rhs.next;
        }
        return *this;
    }
    ~Node(){
        next= nullptr;
        jobTime=jobID=dependentCount= 0;
    }
};
struct Jobs{
    int jobTime;
    int onWhichProc;
    int onOpen;
    int parentCount;
    int dependentCount;
    Jobs()= default;
    Jobs(int jobTime,int parentCount,int dependentCount):jobTime(jobTime),parentCount(parentCount),dependentCount(dependentCount){onOpen=onWhichProc=0;}
    Jobs(const Jobs& rhs):jobTime(rhs.jobTime),onWhichProc(rhs.onWhichProc),onOpen(rhs.onOpen),parentCount(rhs.parentCount),dependentCount(rhs.dependentCount){}
    Jobs& operator=(const Jobs& rhs){
        if(this!=&rhs){
            jobTime=rhs.jobTime;
            onWhichProc=rhs.onWhichProc;
            onOpen=rhs.onOpen;
            parentCount=rhs.parentCount;
            dependentCount=rhs.dependentCount;
        }
        return *this;
    }
    ~Jobs(){
        jobTime=onOpen=onWhichProc=parentCount=dependentCount=0;
    }
};
class Proc{
public:
    int doWhichJob;
    int timeRemain;
    Proc():doWhichJob(-1){}
    Proc(const Proc& rhs)= delete;
    Proc& operator=(const Proc& rhs)= delete;

};
class Scheduling{
private:
    int numNodes;
    int numProcs;
    int procUsed;
    Jobs* jobAry;
    Proc* procAry;
    Node* OPEN;
    int** adjMatrix;
    int* parentCountAry;
    int* dependentCountAry;
    int* onGraphAry;
    void loadMatrix(ifstream& inFile){
        int i,j;
        while(inFile>>i){
            inFile>>j;
            adjMatrix[i][j]=1;
        }
    }
    int constructJobAry(ifstream& inFile,int** adjMatrix){
        int totalTime=0;
        int nodeID,jobTime;
        inFile>>nodeID;//to get rid of the first entery that we already have from inFile1;
        while(inFile>>nodeID){
            inFile>>jobTime;
            totalTime+=jobTime;
            jobAry[nodeID].jobTime=jobTime;
            jobAry[nodeID].onWhichProc=-1;
            jobAry[nodeID].onOpen=0;
            jobAry[nodeID].parentCount=parentCountAry[nodeID];
            jobAry[nodeID].dependentCount=dependentCountAry[nodeID];
        }
        return totalTime;
    }
    void computeParentCount(int** adjMatrix, int*& parentCountAry){
        parentCountAry = new int[numNodes+1];
        int count = 0;
        for (int i=1;i<=numNodes;i++){
            for (int j=1;j<=numNodes;j++){
                if (adjMatrix[j][i]==1) count++;
            }
            parentCountAry[i]=count;
            count=0;
        }
    }
    void computeDependentCount(int** adjMatrix, int*& dependentCountAry){
        dependentCountAry=new int[numNodes+1];
        int count = 0;
        for (int i=1;i<=numNodes;i++){
            for (int j=1;j<=numNodes;j++){
                if (adjMatrix[i][j]==1) count++;
            }
            dependentCountAry[i]=count;
            count=0;
        }
    }
public:
    int currentTime;
    int totalJobTimes;
    int** scheduleTable;
    Scheduling(int numNodes,int numProcs):numNodes(numNodes),numProcs(numProcs){}
    Scheduling(const Scheduling& rhs)= delete;
    Scheduling& operator=(const Scheduling& rhs)= delete;
    ~Scheduling(){
        Node* it=OPEN;
        Node* it2=it;
        while(it2){
            it2=it->next;
            delete it;
        }
        for(int i = 0;i<=numNodes;i++)
            delete [] adjMatrix[i];
        delete [] adjMatrix;
        delete [] jobAry;
        delete [] procAry;
        for(int i = 0;i<=numNodes;i++)
            delete [] scheduleTable[i];
        delete [] scheduleTable;
        delete [] parentCountAry;
        delete [] dependentCountAry;
        delete [] onGraphAry;
    }
    void initialization(ifstream& inFile1,ifstream&inFile2){
        Node* Dummy = new Node();
        OPEN =Dummy;
        adjMatrix=new int*[numNodes+1];
        for(int i=1;i<=numNodes;i++)
            adjMatrix[i]=new int[numNodes+1];
        for(int i=1;i<=numNodes;i++)
            for(int j=1;j<=numNodes;j++)
                adjMatrix[i][j]=0;
        jobAry=new Jobs[numNodes+1];
        procAry=new Proc[numNodes+1];
        scheduleTable=new int*[numNodes+1];
        for(int i=1;i<=numNodes;i++)
            scheduleTable[i]=new int[numNodes+1];
        for(int i=1;i<=numNodes;i++)
            for(int j=1;j<=numNodes;j++)
                scheduleTable[i][j]=0;
        loadMatrix(inFile1);
        computeParentCount(adjMatrix,parentCountAry);
        computeDependentCount(adjMatrix,dependentCountAry);
        totalJobTimes=constructJobAry(inFile2,adjMatrix);
        onGraphAry=new int[numNodes+1];
        for(int i=1;i<=numNodes;i++)
            onGraphAry[i]=1;
    }
    void setProcUsed(int u){procUsed=u;}
    void loadOpen(){
        int i =1,jobID,jobTime;
        Node* newNode;
        int orphanNode=findOrphan(i);
        while(orphanNode>0){
            jobID=orphanNode;
            jobTime=jobAry[jobID].jobTime;
            newNode = new Node(jobID,jobTime,dependentCountAry[jobID]);
            openInsert(*newNode);
            jobAry[jobID].onOpen=1;
            i++;
            orphanNode=findOrphan(i);
        }
    }
    int findOrphan(int &i){
        while(i<=numNodes) {
            if (jobAry[i].parentCount<=0&&jobAry[i].onOpen==0&&jobAry[i].onWhichProc==-1) return i;
            i++;
        }
        return -1;
    }
    void openInsert( Node& newNode){
        if(!openIsEmpty()) {
            Node *it = OPEN->next;//we want to skip the dummy so it always stays in the front
            Node *it2 = it;
            while (it) {
                if (it->dependentCount > newNode.dependentCount) {
                    it2 = it;
                    it = it->next;
                } else break;
            }
            Node *temp = it2->next;
            it2->next = &newNode;
            newNode.next = temp;
        } else OPEN->next=&newNode;
    }
    Node openRemoveHead(){
        Node temp;
        Node *it;
        if(!openIsEmpty()){
            if(OPEN->next->next){
                temp=*OPEN->next;
                it=OPEN->next;
                OPEN->next=OPEN->next->next;
                delete it;
                return temp;
            }
            else{
                it=OPEN->next;
                temp=*OPEN->next;
                OPEN->next= nullptr;
                delete it;
                return temp;
            }
        }
        else return *OPEN;
    }
    void printList(ofstream& outFile){
        Node* it=OPEN;
        while(it->next){
            outFile<<it->jobID<<" "<<it->jobTime<<" "<<it->dependentCount<<std::endl;
            it=it->next;
        }
        outFile<<it->jobID<<" "<<it->jobTime<<" "<<it->dependentCount<<std::endl;
    }
    bool openIsEmpty(){
        if(OPEN->next) return false;
        return true;
    }
    void loadProcAry(){
        int availProc=findProcessor();
        while(availProc>0&&!openIsEmpty()&&procUsed<numProcs){
            procUsed++;
            Node newJob=openRemoveHead();
            int jobID=newJob.jobID;
            int jobTime=newJob.jobTime;
            procAry[availProc].doWhichJob=jobID;
            procAry[availProc].timeRemain=jobTime;
            putJobOnTable(availProc,currentTime,jobID,jobTime);
            availProc=findProcessor();
        }
    }
    int findProcessor(){
        for(int i = 1;i<=numNodes;i++)
            if(procAry[i].timeRemain<=0&&procUsed<numProcs) return i;
        return -1;
    }
    void putJobOnTable(int availProc,int currentTime,int jobID, int jobTime){
        int time= currentTime;
        int endTime=time+jobTime;
        while(time<endTime){
            scheduleTable[availProc][time]=jobID;
            time++;
        }
    }
    bool graphIsEmpty(){
        for(int i=1;i<=numNodes;i++)
            if(onGraphAry[i]==1) return false;
        return true;
    }
    bool checkCycles(){
        if(openIsEmpty()&&!graphIsEmpty()&&!procsAreDone()) return true;
        return false;
    }
    bool procsAreDone(){
        for(int i=1;i<=numProcs;i++)
            if(scheduleTable[i][currentTime]==0) return true;
        return false;
    }
    void printScheduleTable(ofstream& outFile){
        for(int i=1;i<=numProcs;i++){
            for(int j=0;j<=currentTime;j++)
                outFile<<scheduleTable[i][j]<<"  ";
            outFile<<std::endl;
        }
    }
};
int main(int argsc, char* argsv[]) {
    if(argsc==5) {
        ifstream inFile1(argsv[1]);
        ifstream inFile2(argsv[2]);
        ofstream outFile1(argsv[3]);
        ofstream outFile2(argsv[4]);
        int numNodes,numProcs;
        inFile1>>numNodes;
        cout<<"\nProcess Scheduling AI initialized, my name is siri, please input the number of processors to complete your job\n"; //before the initialization in order to create the object.
        cin>>numProcs;
        while(numProcs<1){
            cout<<"\nerror 666: need one or more procs!";
            cin>>numProcs;
        }
        if(numProcs>numNodes) numProcs=numNodes;
        cout<<"\nok!";
        Scheduling S(numNodes,numProcs);
        S.initialization(inFile1,inFile2);
        S.setProcUsed(0);
        S.currentTime=0;
        S.loadOpen();
        S.printList(outFile2);
        S.loadProcAry();
        if(S.checkCycles()){
            cout<<"\nerror 123: the graph has cycles";
            exit(0);
        }
        S.printScheduleTable(outFile1);
        inFile1.close();
        inFile2.close();
        outFile1.close();
        outFile2.close();
    }
    else{
        cout<<"error 505: not enough files were provided\n";
        exit(0);
    }
    return 0;
}

infile1:

12
1 3
2 6
5 6
10 6
2 7
4 7
1 2
11 7
9 8
1 4
12 8
6 8
3 6
11 8
3 7
8 7

inFile2

12
1 1
2 3
3 1
4 2
5 1
6 3
7 1
8 2
9 3
10 1
11 3
12 1

outfile1和outfile2只是空文件

0 个答案:

没有答案