OK, I'm going to bite the bullet and post my code. I would like to preface to say the original deadline is passed so I tried to put a lot of related material in before turning in (I know bad, but I needed something), next deadline is tomorrow I haven't procrastinated I've just struggled with this tremendously.
If someone can tell me what's excess, or what flow I need, anything, I just need to figure this out and make it click.
Code:
#include <string>
#include <queue>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <queue>
#include <map>
using namespace std;
struct queue_element {
string data;
float priority;
//friend istream& operator >> (istream& comm, queue_element& elem);
};
/*istream& operator >> (istream& instr, queue_element& elem) {
queue_element result;
if (comm >> result.data >> result.priority) {
elem = result;
}
return comm;
}*/
int main(int argc,const char *argv[])
{
priority_queue map<string, float> pq;
int i = 0;
if (argc < 3)
{
std::cerr << "Usage: myprog inputfile outputfile\n";
return 1;
}
ifstream inFile(argv[1]);
ofstream outFile(argv[2]);
char command;
string line;
int counter;
while(getline(inFile, line)){
stringstream buffer(line);
buffer >> command;
queue_element elem;
while (buffer >> elem){
}
}
while (command != /0)
{
switch(command)
{
case B:
//.build_heap;
break;
case A:
//insert function for new objects
break;
case U:
//update objects and order in heap
break;
case R:
//remove best object
break;
case D:
//dump heap (print contents in order)
break;
case P:
for ( i = 1; i+=)
{
x = pq.find[i];
if (x != pq.end()) //found key
cout << p->first << p-> second << endl;
else
cout << "Item not found" << endl;
break;
}
}
return 0;
}
Code:
#ifndef PRIORITY_QUEUE_H
#define PRIORITY_QUEUE_H
#include <string>
#include <iostream>
#include <map>
#include <vector>
#include <iterator>
using namespace std;
class priority_queue
{
public:
priority_queue(int); //constructor
~priority_queue(); //destructor
void build_heap(struct, map); //take objects and put into min-heap
void extract_max(); //remove the string of "best object" and delete from heap array
void heapsort()
void insert(vector);
bool leaf(size i) const;
void setheapsize (int);
void setpair (struct);
size parent_index(size i) const; //do I even need these?
size big_child_index(size i) const;
private:
struct queue_element;
map<string, float> pq;
};
#endif
Code:
#include <assert.h> // Provides assert function
#include <iomanip> // Provides setw
#include <iostream> // Provides cin, cout
#include "PriorityQueue.h"
using namespace std;
//constructor
PriorityQueue::PriorityQueue(map)
{
}
void heapify(map)
{
int left=(2*priority);
int right=(2*priority)+1;
int large;
if((left<=hs)&&(a[left]>a[num]))
{
large=left;
}
else
{
large=priority;
}
if((right<=hs)&&(a[right]>a[large]))
{
large=right;
}
if(num!=large)
{
swap(a[num],a[large]);
heapify(large);
}
}
void PriorityQueue::build_heap()
{
for(int i=n/2;i>0;i--)
{
heapify(i);
}
}
void PriorityQueue::heapsort()
{
build_heap();
hs=n;
for(int i=hs;i>1;i--)
{
swap(a[1],a[i]);
hs--;
heapify(1);
}
}
void PriorityQueue::insert(const Item& entry, int priority)
{
if(pair==0)
{
heap[pair].data= entry;
heap[pair].priority= priority;
pair++;
}
else
{
heap[pair].data= entry;
heap[pair].priority= priority;
i= pair;
pair++;
while(parent_priority(i)<priority)
{
swap_with_parent(i);
i=parent_index(i);
}
}
}
PriorityQueue::Item PriorityQueue::extract_max( )
{
assert(pair>0);
if(pair==1)
{
max = heapelem[1];
heapelem[1] = heapelem[size];
size = size -1;
MAX-HEAPIFY (heapelem, 1)
return max;
}
}
bool PriorityQueue::leaf(size i) const
{
if(((2*i)+1)>pair)
return 1;
else
return 0;
}
size PriorityQueue::parent_index(size i) const
{
return (i-1)/2;
}
size PriorityQueue::big_child_index(size i) const
{
assert(!leaf(i));
if((2*i)+2<pair)
{
if(heap[(2*i)+1].priority>heap[(2*i)+2].priority)
{
return (2*i)+1;
}
else
{
return (2*i)+2;
}
}
else
{
return(2*i)+1;
}
}