
Patrick B. answered 07/13/20
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <string.h>
#define MAX_QUEUE_SIZE (4096)
typedef
class Queue
{
private:
char str[MAX_QUEUE_SIZE];
int count;
public:
Queue()
{
memset(str,0,MAX_QUEUE_SIZE);
count=0;
}
Queue(char * inbuff) // initializes the queue with a string rather than doing multiple pushes
{
memset(str,0,MAX_QUEUE_SIZE);
strncpy(str,inbuff,MAX_QUEUE_SIZE);
count = strlen(inbuff);
}
Queue ( const Queue & q) //copy constructor
{
memset(this->str,0,MAX_QUEUE_SIZE);
strncpy(this->str,q.str,MAX_QUEUE_SIZE);
this->count = strlen(q.str) ;
}
int GetCount() { return(count); }
void Enqueue( char kod)
{
if ( count < MAX_QUEUE_SIZE)
{
str[count++] = kod;
}
}
void Dequeue()
{
if (count<=0) { return; } //cannot pop empty queue
str[0]=0;
for (int iLoop=0; iLoop<count; iLoop++)
{
str[iLoop]=str[iLoop+1];
}
count--;
}
char QueueFront() { return(str[0]); }
char QueueRear() { return(str[count-1]); }
bool Empty() { return(count==0); }
bool Full() { return(count==MAX_QUEUE_SIZE); }
void Display(char * msg=NULL)
{
if (msg!=NULL)
{
cout << "************************************************************"<< endl;
cout << msg << endl;
}
cout << "*********************************************************************" << endl;
cout << str << endl;
}
} *TQueue;
#define QUEUE_SIZE ( sizeof(Queue))
TQueue Decode( TQueue qEncodedMessage, int K)
{
char alphabet[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
TQueue qReturn = new Queue();
int N = qEncodedMessage->GetCount();
for (int iLoop=0; iLoop<N; iLoop++)
{
char chChar = qEncodedMessage->QueueFront();
cout << "chChar = " << chChar << endl;
qEncodedMessage->Dequeue();
char decodedChar = chChar+K;
if (decodedChar<65) { decodedChar+=65; }
if (decodedChar>97) { decodedChar-=65; }
qReturn->Enqueue(chChar+K);
}
return(qReturn);
}
int main()
{
int K=-5;
//Demonstrates that the Queue is functioning properly
Queue myQ;
for (char chChar='A'; chChar<'H'; chChar++)
{
myQ.Enqueue(chChar);
}
myQ.Display((char*) "Queue Test" ); //ABCDEFG
int count = myQ.GetCount();
for (int iLoop=0; iLoop<count; iLoop++)
{
cout << " front = " << myQ.QueueFront() << " : rear = " << myQ.QueueRear() << endl;
myQ.Dequeue();
}
if (myQ.Empty()) { cout << "Good to the last drop " << endl; }
myQ.Display();
/*****************************************
// H T S L W F Y Z Q F Y N T S X decodes to C O N G R A T U L A T I O N S for k=-5
**********************************************/
char encodedBuff[25];
strcpy(encodedBuff,"HTSLWFYZQFYNTSX");
Queue qEncoded(encodedBuff);
qEncoded.Display((char*)"Encoded Queue Buffer");
cout << "qEncoded count = " << qEncoded.GetCount() << endl;
Queue qAlphabet((char*)"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
TQueue qDecoded = Decode(&qEncoded,K) ;
qDecoded->Display((char *)"Decoded Message");
}