Created
December 9, 2013 15:28
-
-
Save a60814billy/7873967 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
#include <iostream> | |
#include <string> | |
#include <fstream> | |
#include <algorithm> | |
#include <vector> | |
using namespace std; | |
typedef struct _node{ | |
string jobName; | |
int priority; | |
int length; | |
friend bool operator<(const _node& x, const _node&y){ | |
return x.priority > y.priority; | |
} | |
} node; | |
typedef node* pnode; | |
int main(int argc, char *argv[]){ | |
vector<node> pqueue; | |
ifstream ifs; | |
ifs.open("input.txt"); | |
if (ifs.is_open()){ | |
while (!ifs.eof()) | |
{ | |
pnode newNode = new node; | |
ifs >> newNode->jobName >> newNode->priority >> newNode->length; | |
pqueue.push_back((*newNode)); | |
} | |
} | |
else{ | |
cerr << "open file error!"; | |
} | |
ifs.close(); | |
make_heap(pqueue.begin(), pqueue.end()); | |
sort_heap(pqueue.begin(), pqueue.end()); | |
cout << "%Current queue status:" << endl; | |
int action; | |
int cputime = 0; | |
pnode pExecute = 0; | |
cout << "%"; | |
for (unsigned int i = 0; i < pqueue.size(); i++){ | |
cout << pqueue[i].jobName << " "; | |
} | |
cout << endl << endl; | |
while (true){ | |
cout << "For the following action," << endl; | |
cout << "1. execute" << endl; | |
cout << "2. insert a job" << endl; | |
cout << "3. quit" << endl; | |
cout << "please select an action: "; | |
cin >> action; | |
switch (action) | |
{ | |
case 1: | |
cout << endl; | |
if (pqueue.size() != 0){ | |
cputime += pqueue.back().length; | |
cout << pqueue.back().jobName << " finished at time " << cputime; | |
make_heap(pqueue.begin(), pqueue.end()); | |
pop_heap(pqueue.begin(), pqueue.end()); | |
pqueue.pop_back(); | |
} | |
cout << endl << endl; | |
break; | |
case 2: | |
pExecute = new node; | |
cout << "Your new job:" << endl; | |
cin >> pExecute->jobName >> pExecute->priority >> pExecute->length; | |
pqueue.push_back((*pExecute)); | |
cout << "%New queue status:" << endl; | |
cout << "%"; | |
for (unsigned int i = 0; i < pqueue.size(); i++){ | |
cout << pqueue[i].jobName << " "; | |
} | |
cout << endl << endl; | |
make_heap(pqueue.begin(), pqueue.end()); | |
sort_heap(pqueue.begin(), pqueue.end()); | |
break; | |
case 3: | |
while (true) | |
{ | |
if (pqueue.size() != 0){ | |
make_heap(pqueue.begin(), pqueue.end()); | |
sort_heap(pqueue.begin(), pqueue.end()); | |
cputime += pqueue.back().length; | |
cout << pqueue.back().jobName << " finished at time " << cputime; | |
make_heap(pqueue.begin(), pqueue.end()); | |
pop_heap(pqueue.begin(), pqueue.end()); | |
pqueue.pop_back(); | |
cout << endl; | |
}else{ | |
break; | |
} | |
} | |
break; | |
default: | |
break; | |
} | |
if (action == 3) break; | |
} | |
system("PAUSE"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment