hi there,
I have writeen these three files from notes given in class and by taking bits and peices from books and online. I am compiling these in borland 5.01 and im getting an error saying:
"Heap.cpp(5,2):Cannot Creat Pre-compiled header: code in header"
I think all the code is correct but any advice would be greatly rercieved.
The program should turn the given array into a heap and then show how a char is added and removed from the heap.
Thanking You,
ps. Hope i posted the code ok....
DAVE.
heapTest1.cpp
Code:
#include "heap.cpp"
void main()
{
Heap h;
int i = 0;
char s[] = "EASYQUESTION";
while( s[i] != '\0')
{
h.insert( (int) s[i]);
++i;
}
h.displayAsChars();
cout << "\nRemoving " << (char) h.remove() << "\n";
h.displayAsChars();
cout << "\nInserting X and B into heap\n";
h.insert( (int) 'X'); h.insert( (int) 'B');
h.displayAsChars();
}
Heap.hpp
Code:
#ifndef _heap_hpp
#define _haep_hpp
#include <iostream.h>
#include <limits.h>
#include <math.h>
#define maxH 100
class Heap
{
private:
int *a;
int N;
void siftUp(int k);
void siftDown(int k);
public:
Heap();
Heap(int);
Heap(int[], int);
void insert(int x);
int remove();
void display();
void displayAsChars();
};
#endif
Heap.cpp
Code:
#include "heap.hpp"
#include <iostream.h>
Heap::Heap()
{
N = 0;
a = new int[maxH+1];
}
Heap::Heap(int size)
{
N = 0;
a = new int[size + 1];
}
Heap::Heap(int b[], int size)
{
N = size;
a = b;
for ( int i = N/2 ; i >= 1 ; --i)
{
siftDown(i);
}
}
void Heap::siftUp( int k)
{
int v = a[k];
a[0] = INT_MAX;
while(v > a[k/2])
{
a[k] = a[k/2];
k = k/2;
}
a[k] = v;
}
void Heap::siftDown (int k)
{
int v, j;
v = a[k];
while(k <= N/2)
{
j = 2 * k;
if(j < N && a[j] < a[j+1])
{
++j;
}
if (v >= a[j])
{
break;
}
a[k] = a[j];
k = j;
}
a[k] = v;
}
void Heap::insert(int x)
{
a[++N] = x;
siftUp(N);
}
int Heap::remove()
{
a[0] = a[1];
a[1] = a[N--];
siftDown(1);
return a[0];
}
void Heap::displayAsChars()
{
cout << (char)a[1] << endl;
for( int i = 1 ; i <= N/2 ; i = i * 2)
{
for( int j = 2*i ; j < 4*i && j <= N ; ++j)
{
cout << (char) a[j] << " ";
}
cout << endl;
}
}
void Heap::display()
{
cout << a[1] << endl;
for( int i = 1 ; i<= N/2 ; i = i * 2)
{
for( int j = 2*i ; j < 4*i && j <= N ; ++j)
{
cout << a[j] << " ";
}
cout << endl;
}
}