Seperating C++ Code Into Multiple Files I have the below code in one file. I nee
ID: 3799101 • Letter: S
Question
Seperating C++ Code Into Multiple Files
I have the below code in one file.
I need to seperate it into the below files.
Please tell me how to seperate it and what goes in which file.
FIles that i need to seperate below code into:
1. heap.h - declaration file for heap.
2. heap.cpp - implementation file for heap.
3. pqtype.h - declaration file for priority queue.
4. pqtype.cpp - implementation file for priority queue.
5. test.cpp - driver file.
Code i currently have in one file:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;
struct node
{
int priority;
int snumber;
string jobName;
struct node *next;
};
class PriorityQueue
{
private:
node *front;
public:
PriorityQueue()
{
front = NULL;
}
void addJob(string item, int priority, int number)
{
node *temp, *q;
temp = new node;
temp->jobName = item;
temp->priority = priority;
temp->snumber = number;
//whether queue is empty
if (front == NULL || priority < front->priority)
{
temp->next = front;
front = temp;
}
else
{
q = front;
while (q->next != NULL && q->next->priority <= priority)
q = q->next;
temp->next = q->next;
q->next = temp;
}
}
void printJob()
{
node *temp;
if (front == NULL)
cout << "No print jobs in queue. ";
else
{
temp = front;
cout << "Now printing Job # " << temp->snumber << " " << temp->jobName << endl;
front = front->next;
free(temp);
}
}
void viewJob()
{
node *ptr;
ptr = front;
if (front == NULL)
cout << "No print jobs in queue ";
else
{
while (ptr != NULL)
{
cout << "Job #: " << ptr->snumber << " " << ptr->jobName << endl;
ptr = ptr->next;
}
}
}
};
int main()
{
int choice, priority;
PriorityQueue pq;
char ch;
string jobName;
int number = 0;
cout << "Printer queue" << endl;
cout << "=============" << endl;
do
{
cout << " 1.Add Job ";
cout << "2.Print Job ";
cout << "3.View Job ";
cout << "4.Exit ";
cout << "Enter choice : ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Instructor (I or i), TA (T or t), or Student (S or s)? ";
cin >> ch;
if (ch == 'i' || ch == 'I')
{
priority = 1;
jobName = "Instructor";
}
else if (ch == 't' || ch == 'T')
{
priority = 2;
jobName = "TA";
}
else if (ch == 's' || ch == 'S')
{
priority = 3;
jobName = "Student";
}
number++;
pq.addJob(jobName, priority, number);
break;
case 2:
pq.printJob();
break;
case 3:
pq.viewJob();
break;
case 4:
break;
default:
cout << "Invalid choice. ";
}
}
while (choice != 4);
system("PAUSE");
return 0;
}
Explanation / Answer
1)The below code will go into pqtype.h
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;
struct node
{
int priority;
int snumber;
string jobName;
struct node *next;
};
class PriorityQueue
{
private:
node *front;
public:
PriorityQueue()
{
front = NULL;
}
void addJob(string item, int priority, int number)
{
node *temp, *q;
temp = new node;
temp->jobName = item;
temp->priority = priority;
temp->snumber = number;
//whether queue is empty
if (front == NULL || priority < front->priority)
{
temp->next = front;
front = temp;
}
else
{
q = front;
while (q->next != NULL && q->next->priority <= priority)
q = q->next;
temp->next = q->next;
q->next = temp;
}
}
void printJob()
{
node *temp;
if (front == NULL)
cout << "No print jobs in queue. ";
else
{
temp = front;
cout << "Now printing Job # " << temp->snumber << " " << temp->jobName << endl;
front = front->next;
free(temp);
}
}
void viewJob()
{
node *ptr;
ptr = front;
if (front == NULL)
cout << "No print jobs in queue ";
else
{
while (ptr != NULL)
{
cout << "Job #: " << ptr->snumber << " " << ptr->jobName << endl;
ptr = ptr->next;
}
}
}
};
2) Include the requried headers and #include <pqtype.h> in pqtype.cpp, below is the code of pqtype.cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <pqtype.h>
int main()
{
int choice, priority;
PriorityQueue pq;
char ch;
string jobName;
int number = 0;
cout << "Printer queue" << endl;
cout << "=============" << endl;
do
{
cout << " 1.Add Job ";
cout << "2.Print Job ";
cout << "3.View Job ";
cout << "4.Exit ";
cout << "Enter choice : ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Instructor (I or i), TA (T or t), or Student (S or s)? ";
cin >> ch;
if (ch == 'i' || ch == 'I')
{
priority = 1;
jobName = "Instructor";
}
else if (ch == 't' || ch == 'T')
{
priority = 2;
jobName = "TA";
}
else if (ch == 's' || ch == 'S')
{
priority = 3;
jobName = "Student";
}
number++;
pq.addJob(jobName, priority, number);
break;
case 2:
pq.printJob();
break;
case 3:
pq.viewJob();
break;
case 4:
break;
default:
cout << "Invalid choice. ";
}
}
while (choice != 4);
system("PAUSE");
return 0;
}
Do the same for heap.h and heap.cpp and create test suite as test.cpp
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.