Need help with the following steps for a few programs using C++. I have the step
ID: 3885674 • Letter: N
Question
Need help with the following steps for a few programs using C++. I have the steps that I still need completed. Following the steps I need still will the code that I have so far which is basically the basic level of what I need to completed in these steps.
1. Modify the Stack class to include appropriate error messages if invalid conditions occur—for example, trying to pop an item when the stack is empty.
2. Implement a program that can input an expression in postfix notation and output its value.
3. Modify the Queue class to include appropriate error messages if invalid conditions occur—for example, trying to dequeue an item when the queue is empty.
4. Write an application to simulate an automatic call distributor or ACD. ACDs are used in call centers to route incoming calls to available agents. The application should consist of
an automatic call distributor (ACD) object; and
calls to be handled (each call is an object of the type Call).
The ACD object should contain a queue to store the information of the calls (therefore, the item type stored in your Queue class should be Call). Each Call object contains the associated phone number, date it was made, time it was made, a random ID, and a language chosen by the customer who made the call. Test your classes by deciding in main() the next action randomly: a call leaves the queue (call has been routed to an agent), a new call arrives to the queue, or simply do nothing (the state of the system remains the same).
A SIMPLE STACK CLASS
main.cpp
/*******************************
* Week 2 lesson: *
* a simple Stack class *
*******************************/
#include <iostream>
#include "stack.h"
using namespace std;
int main()
{
Stack s;
cout << "Insertion of 10 characters in s" << endl;
for (int i = 0; i < s.getSize(); i++)
{
char x = 32 + rand()%95;
cout << x << endl;
s.push(x);
}
cout << endl
<< "Displaying and deleting elements from s" << endl;
while(!s.isEmpty())
{
cout << "Item at the top: " << s.peek() << endl;
s.pop();
}
return 0;
}
Stack.cpp
/*******************************
* Week 2 lesson: *
* a simple Stack class *
*******************************/
#include "Stack.h"
/*
* Constructor. Initializes the stack.
*/
Stack::Stack()
{
top = 0;
}
/*
* Determines whether the stack is empty.
*
* Returns true if the stack is empty, false otherwise.
*/
bool Stack::isEmpty()
{
return top == 0;
}
/*
* Adds an element to the top of the stack.
*
* x: element to be added to the stack.
*/
void Stack::push(char x)
{
list[top] = x;
top++;
}
/*
* Removes the element at the top of the stack.
*/
void Stack::pop()
{
top--;
}
/*
* Returns the element at the top of the stack. Does not remove it.
*/
char Stack::peek()
{
return list[top-1];
}
/*
* Returns the size of the stack.
*/
int Stack::getSize()
{
return SIZE;
}
Stack.h
/*******************************
* Week 2 lesson: *
* a simple Stack class *
*******************************/
/*
* Class implementing a Stack ADT.
*/
class Stack
{
public:
Stack();
bool isEmpty();
void push(char);
void pop();
char peek();
int getSize();
private:
static const int SIZE = 10; //size of the queue array
char list[SIZE]; //array to store the stack items
int top; //amount of elements in the array
};
A SIMPLE QUEUE CLASS
main.cpp
/*******************************
* Week 2 lesson: *
* a simple Queue class *
*******************************/
#include "Queue.h"
#include <iostream>
using namespace std;
int main()
{
Queue q;
cout << "Insertion of 10 characters in q" << endl;
for(int i=0; i < q.getSize(); i++)
{
char x = 32 + rand()%95;
cout << x << endl;
q.enqueue(x);
}
cout << endl
<< "Displaying and deleting elements from q" << endl;
while(!q.isEmpty())
{
cout << "Item at the front: " << q.getFront() << endl;
q.dequeue();
}
return 0;
}
Queue.cpp
/*******************************
* Week 2 lesson: *
* a simple Queue class *
*******************************/
#include "Queue.h"
/*
* Constructor. Initializes the queue.
*/
Queue::Queue()
{
front = 0;
back = SIZE-1;
count = 0;
}
/*
* Determines whether the queue is empty.
*
* Returns true if the queue is empty, false otherwise.
*/
bool Queue::isEmpty()
{
return count == 0;
}
/*
* Adds an element to the back of the queue.
*
* x: element to be added to the queue.
*/
void Queue::enqueue(char x)
{
back = (back+1)%SIZE;
list[back] = x;
count++;
}
/*
* Removes the element in the front of the queue.
*/
void Queue::dequeue()
{
front = (front+1)%SIZE;
count--;
}
/*
* Returns the element in the front of the queue. Does not remove it.
*/
char Queue::getFront()
{
return list[front];
}
/*
* Returns the size of the queue.
*/
int Queue::getSize()
{
return SIZE;
}
Queue.h
/*******************************
* Week 2 lesson: *
* a simple Queue class *
*******************************/
/*
* Class implementing a Queue ADT.
*/
class Queue
{
public:
Queue();
bool isEmpty();
void enqueue(char);
void dequeue();
char getFront();
int getSize();
private:
static const int SIZE = 10; //size of the queue array
char list[SIZE]; //array to store the queue items
int count; //number of items in the queue
int front, back; //front and back locations
};
Explanation / Answer
hey this is the program for the second question.
#include<iostream>
#include<cctype>
#include<stack>
using namespace std;
// this function evel returns the value when a specific operator
// operates on two operands
int eval(int op1, int op2, char operate) {
switch (operate) {
case '*': return op2 * op1;
case '/': return op2 / op1;
case '+': return op2 + op1;
case '-': return op2 - op1;
default : return 0;
}
}
// this function evaluates the postfix operation
//it can be easily modified and some additional
// code can be added to overcome the above mentioned limitations
// it's a simple function which implements the basic logic to
// evaluate postfix operations using stack
int evalPostfix(char postfix[], int size) {
stack<int> s;
int i = 0;
char ch;
int val;
while (i < size) {
ch = postfix[i];
if (isdigit(ch)) {
// we saw an operand
// push the digit onto stack
s.push(ch-'0');
}
else {
// we saw an operator
// pop off the top two operands from the
// stack and evalute them using the current
// operator
int op1 = s.top();
s.pop();
int op2 = s.top();
s.pop();
val = eval(op1, op2, ch);
// push the value obtained after evaluating
// onto the stack
s.push(val);
}
i++;
}
return val;
}
// main
int main() {
char postfix[] = {'5','6','8','+','*','2','/'};
int size = sizeof(postfix);
int val = evalPostfix(postfix, size);
cout<<" Expression evaluates to "<<val;
cout<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.