Question

In: Computer Science

Please complete it in C++ Part 2: Queue Create a New Project and give your project...

Please complete it in C++

Part 2: Queue

  1. Create a New Project and give your project a name, say Lab6b.

  1. Download the given source files StackArr.h, StackArr.cpp, QueueArr.h and QueueArr.cpp from Canvas and save them to your Lab6b folder. Also import them to your project.

  1. Add a source file to your project, called QueueMain.cpp and implement your program according to the following:
    1. prompt the user to input a string;
    2. change each uppercase letter to lowercase;
    3. place each letter both in a queue and onto a stack;
    4. verify whether the input string is a palindrome (i.e. a set of letters or numbers that is the same whether read from left to right or right to left).

The following shows a number of program's sample input / output sessions.

Input a string: aibohphobia

aibohphobia is a palindrome

Input a string: level

level is a palindrome

Input a string: desmond

desmond is not a palindrome

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////StackArr.h

#ifndef STACKARR_H
#define STACKARR_H

class StackArr {
private:
   int maxTop;
   int stackTop;
   char *values;

public:
   StackArr(int);
   ~StackArr();
   bool isEmpty() const;
   bool isFull() const;
   char top() const;
   void push(const char& x);
   char pop();
   void displayStack() const;
};

#endif
#pragma once
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////StackArr.cpp

#include "StackArr.h"
#include <string>
#include <iostream>

using namespace std;

StackArr::StackArr(int size) {
   maxTop = size;
   values = new char[size];
   stackTop = -1;
}

StackArr::~StackArr() {
   delete[] values;
}

bool StackArr::isEmpty() const {


   return stackTop == -1;
}

bool StackArr::isFull() const {
   return stackTop == maxTop;
}

void StackArr::push(const char& x) {
   if (isFull())
       cout << "Error! The stack is full!" << endl;
   else
       values[++stackTop] = x;
}

char StackArr::pop() {
   if (isEmpty()) {
       cout << "Error! The stack is empty!" << endl;
       return -1;
   }
   else
       return values[stackTop--];
}

char StackArr::top() const {
   if (isEmpty()) {
       cout << "Error! The stack is empty!" << endl;
       return -1;
   }
   else
       return values[stackTop];
}

void StackArr::displayStack() const {
   cout << "Top -->";
   for (int i = stackTop; i >= 0; i--)
       cout << "\t|\t" << values[i] << "\t|" << endl;
   cout << "\t|---------------|" << endl << endl;
  
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////QueueArr.h

#ifndef QUEUEARR_H
#define QUEUEARR_H

class QueueArr {

private:
   int front;
   int back;
   int counter;
   int maxSize;
   char* values;

public:
   QueueArr(int);
   ~QueueArr();

   bool isEmpty() const;
   bool isFull() const;
   bool enqueue(char x);
   char dequeue();
   void displayQueue() const;
};

#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////QueueArr.cpp

#include "QueueArr.h"
#include <string>
#include <iostream>

using namespace std;

QueueArr::QueueArr(int size) {

   values = new char[size];
   maxSize = size;
   front = 0;
   back = -1;
   counter = 0;
}

QueueArr::~QueueArr() {
   delete[] values;
}

bool QueueArr::isEmpty() const {
  
   if (counter)
       return false;
   else
       return true;
}

bool QueueArr::isFull() const {
   if (counter < maxSize)
       return false;
   else
       return true;
}

bool QueueArr::enqueue(char x) {
   if (isFull()) {
       cout << "Error! The queue is full." << endl;
       return false;
   }
   else {
       back = (back + 1) % maxSize;
       values[back] = x;
       counter++;
       return true;
   }
}

char QueueArr::dequeue() {
   char x = -1;
   if (isEmpty()) {
       cout << "Error! The queue is empty." << endl;
       return -1;
   }
   else {
       x = values[front];
       front = (front + 1) % maxSize;
       counter--;
       return x;
   }
}

void QueueArr::displayQueue() const {
   cout << "Front -->";
   for (int i = 0; i < counter; i++) {
       if (i == 0)
           cout << "\t";
       else
           cout << "\t\t";
       cout << values[(front + i) % maxSize];
       if (i != counter - 1)
           cout << endl;
       else
           cout << "\t<--Back" << endl;
   }
   cout << endl;
}

Solutions

Expert Solution

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////StackArr.h

#ifndef STACKARR_H
#define STACKARR_H

class StackArr {
private:
   int maxTop;
   int stackTop;
   char *values;

public:
   StackArr(int);
   ~StackArr();
   bool isEmpty() const;
   bool isFull() const;
   char top() const;
   void push(const char& x);
   char pop();
   void displayStack() const;
};

#endif
#pragma once
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////StackArr.cpp

#include "StackArr.h"
#include <string>
#include <iostream>

using namespace std;

StackArr::StackArr(int size) {
   maxTop = size;
   values = new char[size];
   stackTop = -1;
}

StackArr::~StackArr() {
   delete[] values;
}

bool StackArr::isEmpty() const {


   return stackTop == -1;
}

bool StackArr::isFull() const {
   return stackTop == maxTop;
}

void StackArr::push(const char& x) {
   if (isFull())
       cout << "Error! The stack is full!" << endl;
   else
       values[++stackTop] = x;
}

char StackArr::pop() {
   if (isEmpty()) {
       cout << "Error! The stack is empty!" << endl;
       return -1;
   }
   else
       return values[stackTop--];
}

char StackArr::top() const {
   if (isEmpty()) {
       cout << "Error! The stack is empty!" << endl;
       return -1;
   }
   else
       return values[stackTop];
}

void StackArr::displayStack() const {
   cout << "Top -->";
   for (int i = stackTop; i >= 0; i--)
       cout << "\t|\t" << values[i] << "\t|" << endl;
   cout << "\t|---------------|" << endl << endl;
  
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////QueueArr.h

#ifndef QUEUEARR_H
#define QUEUEARR_H

class QueueArr {

private:
   int front;
   int back;
   int counter;
   int maxSize;
   char* values;

public:
   QueueArr(int);
   ~QueueArr();

   bool isEmpty() const;
   bool isFull() const;
   bool enqueue(char x);
   char dequeue();
   void displayQueue() const;
};

#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////QueueArr.cpp

#include "QueueArr.h"
#include <string>
#include <iostream>

using namespace std;

QueueArr::QueueArr(int size) {

   values = new char[size];
   maxSize = size;
   front = 0;
   back = -1;
   counter = 0;
}

QueueArr::~QueueArr() {
   delete[] values;
}

bool QueueArr::isEmpty() const {
  
   if (counter)
       return false;
   else
       return true;
}

bool QueueArr::isFull() const {
   if (counter < maxSize)
       return false;
   else
       return true;
}

bool QueueArr::enqueue(char x) {
   if (isFull()) {
       cout << "Error! The queue is full." << endl;
       return false;
   }
   else {
       back = (back + 1) % maxSize;
       values[back] = x;
       counter++;
       return true;
   }
}

char QueueArr::dequeue() {
   char x = -1;
   if (isEmpty()) {
       cout << "Error! The queue is empty." << endl;
       return -1;
   }
   else {
       x = values[front];
       front = (front + 1) % maxSize;
       counter--;
       return x;
   }
}

void QueueArr::displayQueue() const {
   cout << "Front -->";
   for (int i = 0; i < counter; i++) {
       if (i == 0)
           cout << "\t";
       else
           cout << "\t\t";
       cout << values[(front + i) % maxSize];
       if (i != counter - 1)
           cout << endl;
       else
           cout << "\t<--Back" << endl;
   }
   cout << endl;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////QueueMain.cpp

#include <cstring>
#include <iostream>

#include "QueueArr.cpp"

#include "StackArr.cpp"

using namespace std;

int main()
{
string str;/*a string to take input from user*/
cout<<"Enter the string : ";
cin>>str;
int n=str.size();/*storing the size of string*/
StackArr s(n);/*declare a stack object*/
QueueArr q(n);/*declare a queue object*/
for(int i=0;i<n;i++)/*converting each letter to lowercase*/
{
q.enqueue(tolower(str[i]);/*enqueue the input string to queue in order*/
s.push(tolower(str[i]);/*push the string in order on stack*/
}

/*display the content of stack and queue*/
q.displayQueue();
s.displayStack();
bool flg=1;/*flg to check if the input string is palindrome*/
while((n--)/2)/*half the size of the string needs to be checked if it's a palindrome*/
{
if(q.dequeue()!=s.pop())/*using both queue and stack to determine if it's a palindrome, stack will start popping the string from end and queue will dequeue from beginning and both will move to the middle of input*/
{
flg=0;
break;/*break if any unmatched character occurs*/
}
}
if(flg)/*string was palindrome if all characters from beginning to mid(first half) and from end to mid(second half) matched and flg remained 1 else it will be set to 0*/
{
cout<<str<<" is a palindrome";
}
else
{
cout<<str<<" is not a palindrome";
}
return 0;
}

//output


Related Solutions

Please complete it in C++ Part 1: Stack Create a New Project and give your project...
Please complete it in C++ Part 1: Stack Create a New Project and give your project a name, say Lab6a. Download the given source files StackArr.h and StackArr.cpp from Moodle and save them to your Lab6a folder. Also import them to your project. Add a source file to your project, called StackMain.cpp and implement your program according to the following: Prompt the user to input a program filename. Open the file and check if every right brace (i.e. }), bracket...
Part 1: Stack Create a New Project and give your project a name, say Lab6a. Download...
Part 1: Stack Create a New Project and give your project a name, say Lab6a. Download the given source files StackArr.h and StackArr.cpp from Moodle and save them to your Lab6a folder. Also import them to your project. Add a source file to your project, called StackMain.cpp and implement your program according to the following: Prompt the user to input a program filename. Open the file and check if every right brace (i.e. }), bracket (i.e. ]), and parenthesis (i.e....
Please follow the instructions and solve it by c++ Close the project and create a new...
Please follow the instructions and solve it by c++ Close the project and create a new one called 'Lab0-Part3' with the same options as in the previous step. Write a C++ program that does the following: Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1 and 100 inclusive Then, creates two more 100-element arrays, one holding odd values and the other holding even values. Prints both of the new arrays to the console.
Stack Create a New Project and give your project a name, say. Download the given source...
Stack Create a New Project and give your project a name, say. Download the given source files StackArr.h and StackArr.cpp from Moodle and save them to your Lab6a folder. Also import them to your project. Add a source file to your project, called StackMain.cpp and implement your program according to the following: Prompt the user to input a program filename. Open the file and check if every right brace (i.e. }), bracket (i.e. ]), and parenthesis (i.e. )) in the...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following two methods(headers provided) as described below: 1. A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range...
PART 2 Please answer these with true and false answers, but please give your explainations, use...
PART 2 Please answer these with true and false answers, but please give your explainations, use diagrams if necessary 5. Suppose the government funds the provision of a pure public good from tax revenue. The total burden to the economy of providing the good exceeds the amount spent on the good. 6. A decrease in posted (nominal) interest rates necessarily means a decrease in real interest rates. 7. At the end of 2016 the Canadian dollar exchange rate with the...
Please create an Event-Based JavaScript Program of your choice. Please write new code for this project...
Please create an Event-Based JavaScript Program of your choice. Please write new code for this project (starting from scratch). Thank you!!
C++ Project The following is a simple implementation of the circular queue Important: for this project,...
C++ Project The following is a simple implementation of the circular queue Important: for this project, you must complete the work based on this initial code. 1. Fix the full() method. 2. Fix the empty() method. 3. Place "X" in the front position using a statement in the constructor. 4. Adjust the DEQ() method so that the front position in the array would always have "X" in it and the previous X should be blanked out. 5. Write the code...
Part 2: You will create five tables in your ColonialAdventureTours database. Please do not write your...
Part 2: You will create five tables in your ColonialAdventureTours database. Please do not write your own SQL Commands for this task, use data found in the following Colonial_create.txt file and copy and paste the commands into MySQL workbench. Then add Primary key, Foreign key, and not null constraints appropriately. Then run your codes. Note: Remember that since you enforced referential integrity (foreign key constraints) that you must create the "primary" tables before you can create the "related" tables in...
I HAVE ALREADY CORRECTLY COMPLETED PART A AND B PLEASE COMPLETE PART C ONLY Part A...
I HAVE ALREADY CORRECTLY COMPLETED PART A AND B PLEASE COMPLETE PART C ONLY Part A In late 2020, the Nicklaus Corporation was formed. The corporate charter authorizes the issuance of 6,000,000 shares of common stock carrying a $1 par value, and 2,000,000 shares of $5 par value, noncumulative, nonparticipating preferred stock. On January 2, 2021, 4,000,000 shares of the common stock are issued in exchange for cash at an average price of $10 per share. Also on January 2,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT