Question: Hello, I've been stuck on this code for awhile now and desperatly need help. Here is what you must do: 1.In this question, you are
Hello,
I've been stuck on this code for awhile now and desperatly need help. Here is what you must do:
1.In this question, you are going to implement a stack data structure in part 1 and then use it to create postfix mini-calculator in section 2.
1.1. Use the myStack.h to implement a stack template. It should be able to work for arbitrary data types (char, int, double, etc.).
1.2. Postfix calculator: Complete the provided main script to implement a mini postfix calculator for
integer numbers. We use infix notation in regular calculations like 2+4*3, while its postfix notation is 2 4 3 * +. Use your stack data structure from part 1.1 and the following algorithm to to create the postfix calculator:
Read a line from terminal as the user input.
If the input is a valid integer, you push that integer onto the stack.
If the input is a valid operator (+, *, /, - ), pop two integers off the stack,
perform the requested operation, and push the result back onto the stack. If there were less than 2 integers remained in the stack, print a meaningful error message.
If the user enters c, you need to print the final result. If the user input line was not in a correct format, you should print a meaningful message.
If the user enters H, you halt the program without performing any additional computations.
For any other input characters, print a meaningful error message.
Use the myQueue.cpp and myQueue.h to implement a queue data structure for integer data type :
To keep track of the front and rear of the queue, you can use two integer indicators.
Be careful about the empty and full state. You can use one of the three implementations that distinguish these two states.
Complete the main script of this question.
Here is Main.cpp for question 1:
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
cout<<"***************************************************************** "< cout<<"*************** postfix mini calculator ************************* "< cout<<"***************************************************************** "< //Todo: Create your stack char inputItem; //User input bool flag=true; while(true) { cout<<"Enter your input items. Then enter 'c' at the end of your input line!"< cout<<"Enter 'H' to halt the program at any time!"< while(flag) { cin>>inputItem; if(inputItem=='H') { cout<<"***************************************************************** "< cout<<"***************************************************************** "< cout<<"***************************************************************** "< return 0; } else if(inputItem=='c') { //Todo: //1) Check if the input line is in the correct format. //2) If true, compute the results based on the algorithm and print for the user. //3) Otherwise, print a meaningful error message and pop all items of the stack. flag=false; } else if(isspace(inputItem)) //skip space characters { continue; } else if((inputItem=='+')||(inputItem=='/')||(inputItem=='-')||(inputItem=='*')) { //Todo: //Follow the steps mentioned in the algorithm for operators. } else { int num= inputItem-'0'; if((num>=0)&&(num<=9)) { //Todo: //Follow the steps mentioned in the algorithm for integer values. } else { cout<<"Invalid input line!!!"< flag=false; //Todo: //Pop all input items while ((getchar() != ' ')); //flush the buffer } } } flag=true; } } Here is myStack.h for question 1: #ifndef STACK_H #define STACK_H #define MAX 1000 template class myStack { private: int CurrentSize; //An indicator that contains the current size of the stack userType holder[MAX]; //Maximum size of the stack public: myStack(); bool push(userType x); userType pop(); bool isEmpty(); }; template myStack::myStack() { //Todo: initialize the value of the currentSize. } template bool myStack::push(userType x) { if(isEmpty()) { //Todo //Push the item x to myStack. //Update the value of the currentSize. return true; } else { //Todo: //print a meaningful error message return false; } } template userType myStack::pop() { //Todo: //Pop the currentSize element of the stack (e.g. x). //update the value of the currentSize. //return x. } template bool myStack::isEmpty() { //Check if the size of the stack is still less than the MAX value. } #endif // STACK_H Here is main.cpp for Q2: #include #include using namespace std; int main() { char choice; cout<<"***************************************************************** "< cout<<"****** Testing the implemeted queue data structure *************** "< cout<<"***************************************************************** "< //Todo: Create your queue do { cout<<"Choose your task from the following options:"< cout<<"1: Push items?"< cout<<"2: Pop items?"< cout<<"3: Print items?"< cout<<"4: exit?"< cin>>choice; //Todo: check if the choice has a valid value. if(choice=='1') { char toCheck; bool flag=true; int inputItem; cout<<"Enter input items, C to cancel"< while(flag) { cin>>toCheck; if(toCheck=='c') flag=false; if(flag) { inputItem = toCheck-'0'; //Todo: push the inputItem into queue. //Print an error message if the queue is full. //Set the value of the flag to false if it is already full. } } cout<<"Back to the main menu!"< } else if(choice=='2') { char toCheck; bool flag=true; int count; cout<<"Enter the number of items you want to pop-out, C to cancel"< cin>>toCheck; if(toCheck=='c') flag=false; while(flag) { count = toCheck-'0'; //Todo: //Check if the queue is already empty or not //If true, pop from the front, update the count value and print the poped item for the //user. //Otherwise, print a meaningfull error message and set the flag value to false. } cout<<"Back to the main menu!"< } else if(choice=='3') { //Todo: Print all items of the queue. } else if(choice=='4') { cout<<"***************************************************************** "< cout<<"***************************************************************** "< cout<<"***************************************************************** "< return 0; } else { cout<<"INVALID CHOICE "< } }while(true); } Here is myQueue for Q2: #include "myQueue.h" myQueue::myQueue() { //initialize the value of the rear and front indicator based on your implementation. } bool myQueue::push(int x) { if(!isFull()) { //Todo: //Check if the queue is full or not. //If so, insert an item into the back of the myQueue. //Update the value of the front indicator //Otherwise, print a meaningful error message return true; } else { //Todo: print a meaningfull message return false; } } int myQueue::pop() { //Todo: //Remove the front item from the myQueue //Update the value of the rear indicator } bool myQueue::isFull() { //Check if the myQueue still has empty space at its rear or the front. } bool myQueue::isEmpty() { //Check if the myQueue is empty based on the value of the front and rear indicator. } void myQueue::print() { //Todo: print all items of the myQueue. //Hint: use a variable to store a copy of the front value. itterate over the queue until you reached //to the rear element. } Here is myQueue.h for Q2: #ifndef myQueue_H #define myQueue_H #define MAX 1000 class myQueue { public: myQueue(); bool push(int x); int pop(); bool isFull(); bool isEmpty(); void print(); private: int rear, front; int holder[MAX]; }; #endif // myQueue_H Hopefully one of y'all can help me with this i've been trying to understand this for awhile
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
