IT HAS TO BE DONE IN C++ I ONLY UNDERSTAND C++ THANKS ! ***It is not checking nu
ID: 3844402 • Letter: I
Question
IT HAS TO BE DONE IN C++ I ONLY UNDERSTAND C++ THANKS !
***It is not checking number of characters! I have to check character by character and see if sentence is identical***
===============================
#ifndef QUEUE_H
#define QUEUE_H
#include
#include
using namespace std;
class Queue
{
private:
char *qArray;
int qSize;
int front;
int rear;
int numItems;
public:
Queue(int);
Queue(const Queue &);
~Queue();
void enqueue(char);
void dequeue(char &);
bool isEmpty() const;
bool isFull() const;
void clear();
};
#endif
================================
#include "Queue.h"
Queue::Queue(int size)
{
qArray = new char[size];
qSize = size;
front = -1;
rear = -1;
numItems = 0;
}
Queue::Queue(const Queue &obj)
{
qArray = new char[obj.qSize];
qSize = obj.qSize;
front = obj.front;
rear = obj.rear;
numItems = obj.numItems;
for(int i=0; i qArray[i] = obj.qArray[i];
}
Queue::~Queue()
{
delete [] qArray;
}
void Queue::enqueue(char num)
{
if(isFull())
cout << "Queue is full ";
else
{
rear = (rear + 1) % qSize;
qArray[rear] = num;
numItems++;
}
}
void Queue::dequeue(char &num)
{
if(isEmpty())
cout << "Queue is empty ";
else
{
front = (front + 1) % qSize;
num = qArray[front];
numItems--;
}
}
bool Queue::isEmpty() const
{
bool status;
if(numItems > 0)
status = false;
else
status = true;
return status;
}
bool Queue::isFull() const
{
bool status;
if(numItems < qSize)
status = false;
else
status = true;
return status;
}
void Queue::clear()
{
front = qSize - 1;
rear = qSize - 1;
numItems = 0;
}
This is what i have so far. I am having hard time how to demonstrate it in the main. After reading two sentenes from the user and putting it in seperate queues in the array one by one and comparing it. I just need help with main. Above picture is the problem.
Write a program that reads two sentences and reads them into two separate queues. The program should then determine whether the sentences are identical by comparing the characters in the queues. When two non-identical characters are encountered, the program should display a message indicating that the sentences are not the same. If both queues contain the same set of characters, message should be displayed indicating that the sentences are identical.Explanation / Answer
SOLUTION:
I CHANGED for(int i=0; i to for(int i=0; i <obj.qSize;i++)
RUNNING CODE (WITH COMMETS) IS :
===================================================
#ifndef QUEUE_H
#define QUEUE_H
#include<bits/stdc++.h>
using namespace std;
class Queue
{
private:
char *qArray;
int qSize;
int front;
int rear;
int numItems;
public:
Queue(int);
Queue(const Queue &);
~Queue();
void enqueue(char);
void dequeue(char &);
bool isEmpty() const;
bool isFull() const;
void clear();
};
#endif
#include "Queue.h"
#include<bits/stdc++.h>
using namespace std;
Queue::Queue(int size)
{
qArray = new char[size];
qSize = size;
front = -1;
rear = -1;
numItems = 0;
}
Queue::Queue(const Queue &obj)
{
qArray = new char[obj.qSize];
qSize = obj.qSize;
front = obj.front;
rear = obj.rear;
numItems = obj.numItems;
for(int i=0; i <obj.qSize;i++) qArray[i] = obj.qArray[i];
}
Queue::~Queue()
{
delete [] qArray;
}
void Queue::enqueue(char num)
{
if(isFull())
cout << "Queue is full ";
else
{
rear = (rear + 1) % qSize;
qArray[rear] = num;
numItems++;
}
}
void Queue::dequeue(char &num)
{
if(isEmpty())
cout << "Queue is empty ";
else
{
front = (front + 1) % qSize;
num = qArray[front];
numItems--;
}
}
bool Queue::isEmpty() const
{
bool status;
if(numItems > 0)
status = false;
else
status = true;
return status;
}
bool Queue::isFull() const
{
bool status;
if(numItems < qSize)
status = false;
else
status = true;
return status;
}
void Queue::clear()
{
front = qSize - 1;
rear = qSize - 1;
numItems = 0;
}
int main(int argc, char const *argv[]) {
/* code */
string s1,s2;
//take input two sentances s1 and s2 from user
/*{NOTE HERE TWO SENTANCES FORMATE IN TERMINAL:
sentance one
sentance two
that means it will read a line as a sentance
}
*/
getline(cin, s1);
getline(cin, s2);
//now put these two sentances s1 and s2 in queue
//first i am puting s1 into queue Q1
Queue *Q1;
int size1=s1.size();
Q1=new Queue(size1);
for(int i=0;i<size1;i++){ //inserting s1 into queue Q1
Q1->enqueue(s1[i]);
}
//now s2 into queue Q2
Queue *Q2;
int size2=s2.size();
Q2=new Queue(size2);
for(int i=0;i<size2;i++){ //inserting s1 into queue Q1
Q2->enqueue(s2[i]);
}
//Here Compare two Queue Q1 and Q2
char c1,c2;
int flag=0;
while(!(Q1->isEmpty())&&!(Q1->isEmpty())){
Q1->dequeue(c1);
Q2->dequeue(c2);
if(c1!=c2){
flag=1;
break;
}
}
if(flag&&(!(Q1->isEmpty())||!(Q1->isEmpty()))){
cout<<"These two sentances are not identical"<<endl;
}else{
cout<<"These two sentances are identical"<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.