Menu

[Solved]Instruction Look Complete Maincpp Instruction Look Complete Program Output Match Hw1stackd Q37250238

/INSTRUCTION

//Look for ** to complete all ofthem

main.cpp

//INSTRUCTION
//Look for ** to complete this program
//The output should match my hw1stackDemo.out

//=========================================================
//HW#: HW4P1 stack
//Your name: **
//Complier: **
//File type: client program
//===========================================================

using namespace std;

#include
#include
#include “stack.h”

//Purpose of the program: **
//Algorithm: **
int main()
{
stack postfixstack; // integer stack
string expression; // user entered expression
  
cout << “type a postfix expression: ” ;
cin >> expression;
  
int i = 0; // character position within expression
char item; // one char out of the expression
  
int box1; // receive things from pop
int box2; // receive things from pop
  
while (expression[i] != ”)
{
try
   {
   item = expression[i]; // current char
  
   //2. if it is an operand (number),
   // push it (you might get Overflow exception)
   // **
  
   //3. else if it is an operator,
   // pop the two operands (you might get Underflowexception), and
   //   apply the operator to the two operands,and
   // push the result.
   else if ( (item == ‘+’) || (item == ‘-‘) || (item ==’*’))
   {
   postfixstack.pop(box1);
   postfixstack.pop(box2);
   //cases for different operators follow:
   if (item == ‘-‘) result = box2-box1;
   // ** also do the + and * cases
   // ** Finally push the result
   }
   else throw “invalid item”;
   } // this closes try
  
// Catch exceptions and report problems and quit the programnow.
// Error messages describe what is wrong with the expression.
catch (stack::Overflow)
   {** }
catch (stack::Underflow) // for too few operands
   {** }
catch (char const* errorcode) // for invalid item
   {** }
// go back to the loop after incrementing i
**
}// end of while
  
// After the loop successfully completes:
// The result will be at the top of the stack. Pop it and showit.
**
// If anything is left on the stack, an incomplete expression wasfound.
// Inform the user.
**

}// end of the program

___________________________________________________________________________________________________________________________________________

stack.cpp

//INSTRUCTION
//Look for **. (do control-S)
//Update the comments using the HOW TO COMMENT file,
// complete exception handling and
// finish ClearIt to make this complete – Yoshii (CS311)
// When done, compile stack.cpp to make sure there are no syntaxerrors.
  
//=========================================================
//HW#: HW4P1 stack
//Your name: **
//Complier: **
//File type: stack implementation file
//=========================================================

using namespace std;
#include
#include “stack.h”

//PURPOSE: Constructor must initialize top to be -1 to beginwith.
stack::stack()
{ top = -1; } // indicate an empty stack
  
//PURPOSE: Destructor does not have to do anything since this is astatic array.
stack::~stack()
{ }// nothing to do }
  
//PURPOSE: checks top and returns true if empty, falseotherwise.
// ** In the comment, make sure you define “empty” in terms oftop.
bool stack::isEmpty()
{ if (top == -1) return true; else return false; )
  
//PURPOSE: checks top and returns true if full, falseotherwise.
//** In the comment, make sure you define “full” in terms oftop.
bool stack::isFull()
{ if (top == MAX-1 ) return true; else return false; }
  
//PURPOSE: calls isFull and if true, throws an exception(Overflow)
// Otherwise, adds an element to el after incrementing top.
//PARAMETER: pass the element (elem) to be pushed
void stack::push(el_t elem)
{ if (isFull()) ??**
else { top++; el[top] = elem; }}

//PURPOSE: calls isEmpty and if true, throws an exception(Underflow)
// Otherwise, removes an element from el and gives it back.
//PARAMETER: provide a holder (elem) for the element popped (passby ref)
void stack::pop(el_t& elem)
{ if (isEmpty()) ??**
else { elem = el[top]; top–;}}

// PURPOSE: calls isEmpty and if true, throws an exception(underflow)
// Otherwise, gives back the top element from el.
//PARAMETER: provide a holder (elem) for the element (pass byref)
void stack::topElem(el_t& elem)
{ if (isEmpty()) ??**
else { elem = el[top]; } }

//PURPOSE: dislayAll calls isEmpty and if true, displays [ empty].
// Otherwise, diplays the elements vertically.
void stack::displayAll()
{ if (isEmpty()) cout << .[ empty ]. << endl;
else for (int i=top; i>=0; i–)
{ cout << el[i] << endl; }
cout << “————–” << endl;
}

//PURPOSE: pops all elements from the stack to make it empty if itis not empty yet.
void stack::clearIt()
{// ** comment a local variable
??** }
_________________________________________________________________________________________________________________________________________

stack.h

//INSTRUCTION:

complete the ** parts and then complete stack.cpp
//EMACS HINT:
// control-S does searches
// Tab on each line will indent perfectly for C++

// =======================================================
// HW#: HW4P1 stack
// Your name: **
// Compiler: g++
// File type: headher file stack.h
//=======================================================

//—– Globally setting up the aliases —————-

const int MAX = 10; // The MAX number of elements for thestack
// MAX is unknown to the client

typedef ** el_t; // the el_t type is ** for now
// el_t is unknown to the client
//——————————————————-

class stack
{
  
private: // to be hidden from the client
  
el_t el[MAX]; // el is an array of el_t’s
int top; // top is index to the top of stack
  
public: // prototypes to be used by the client
// Note that no parameter variables are given
  
// exception handling classes
class Overflow{}; // thrown when the stack overflows
class Underflow{}; // thrown when the stack underflows
  
stack(); // constructor to create an object
~stack(); // destructor to destroy an object
  
// PURPOSE: if not full, enters an element at the top;
// otherwise throws an exception – Overflow
// PARAMETER: pass the element to be pushed
void push(el_t);
  
// PURPOSE: if not empty, removes and gives back the topelement;
// otherwise throws an exception – Underflow
// PARAMETER: provide variable to receive the popped element (passby ref)
void pop(el_t&);
  
// PURPOSE: if not empty, gives the top element without removingit;
// otherwise, throws an exception – Underflow
// PARAMETER: ***
void topElem(el_t&);
  
// ** Must add good comments for each function – See Notes1B

//PURPOSE: **
bool isEmpty();
//PURPOSE: **
bool isFull();
//PURPOSE: **
void displayAll();
//PURPOSE: **
void clearIt();
  
};

// Note: semicolon is needed at the end of the header file

Write a stack application program to evaluate post-fix expressions The user will enter a post-fix expression of the form 34+

Write a stack application program to evaluate post-fix expressions The user will enter a post-fix expression of the form 34+ 345+* 722+- as a sting. Operators are +-, and, Single digit mbers only. No blanks. which means 3+4 which means 3*(4+5) which means 7-(2+2) Please note that the expression could be wrong too few operands (i.e. cannot pop operand) incomplete expression (i.e. more than the result at the end) 3+ 345 Your program will display the evaluated result (a number) or an eTor message describing what is wrong (the algorithm is in Notes-1, so use it as given) .You may assue that no expre Since each element of the string is a character, you will need to convert it to an integer to ssion will be longer than 12 characters. perform arithmetic operations. Required Test Cases (MUST TEST IN THIS ORDER): which means 3+4 which means 3*(4+5) which means 7-(2+2) which means (3+4)+(5+6) which means (1+2)+ ((3 4 expression too long too few operands too few operands invalid element incomplete expression 34+ 2. 345+ 3. 722+ 4. 34+56++ 5, 12+34 45+-+ 6. 1234567891234 expression t (4+5)) 8. 3+ 9, 3# 10. 2345+ NOTE: Do you think I have listed all possible cases? Always ask yourselfif there are other cases you should test to make sure your program is bug free Show transcribed image text Write a stack application program to evaluate post-fix expressions The user will enter a post-fix expression of the form 34+ 345+* 722+- as a sting. Operators are +-, and, Single digit mbers only. No blanks. which means 3+4 which means 3*(4+5) which means 7-(2+2) Please note that the expression could be wrong too few operands (i.e. cannot pop operand) incomplete expression (i.e. more than the result at the end) 3+ 345 Your program will display the evaluated result (a number) or an eTor message describing what is wrong (the algorithm is in Notes-1, so use it as given) .You may assue that no expre Since each element of the string is a character, you will need to convert it to an integer to ssion will be longer than 12 characters. perform arithmetic operations. Required Test Cases (MUST TEST IN THIS ORDER): which means 3+4 which means 3*(4+5) which means 7-(2+2) which means (3+4)+(5+6) which means (1+2)+ ((3 4 expression too long too few operands too few operands invalid element incomplete expression 34+ 2. 345+ 3. 722+ 4. 34+56++ 5, 12+34 45+-+ 6. 1234567891234 expression t (4+5)) 8. 3+ 9, 3# 10. 2345+ NOTE: Do you think I have listed all possible cases? Always ask yourselfif there are other cases you should test to make sure your program is bug free

Expert Answer


Answer to /INSTRUCTION //Look for ** to complete all of them main.cpp //INSTRUCTION //Look for ** to complete this program //The … . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *