Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with stack template project for C++ please and thank you. This is prob

ID: 666217 • Letter: N

Question

Need help with stack template project for C++ please and thank you. This is probaly like the 5th time I posted this question and I received the same copy and pasted incorrect answer. I am wasting my money and posting my limited questions to get these incorrect answers. Please read in the input from the input.txt below and send correct code by the directions with the correct screenshot output. The input.txt is in the dropbox link below. The input file uses parenthesis and the code should act off when it hits the right parenthesis. Please implement the parenthesis in the code.

https://www.dropbox.com/sh/36sxpfkktf7rawv/AABiPMaEmnPVSsVlR65Jr7S6a?dl=0

For this program, we'll be writing a simple expression evaluator that uses a stack template. We use the usual rules about precedence to resolve which operation should be done first if an expression is potentially ambiguous:

For this program we're writing the second part of this: Given a string containing a fully parenthesized algebraic expression, evaluate the expression.

For this program, we'll make a few simplifying assumptions; primarily, all numbers will be single-digit integers >= 0, so they'll be single characters, and the expected results will be integers. (So all operations, including division, will be integer division, with fractions dropped).

So how's this done? To carry this out, 2 stacks are needed, 1 for numbers (ints) and the other for operators (chars). Thus, a template stack is needed. Input is a fully-parenthesized algebraic expression.

The evaluation algorithm is as follows: For each character in the expression: if the character is any of: ( + - * / Push this character on to the operators stack If the character is any of: 0 1 2 3 4 5 6 7 8 9 Convert to an int and push onto the numbers stack (remember that int(‘0’) is NOT the integer 0!) If the character is: ) Pop right operand from numbers stack If the numbers stack is empty, declare an error and end processing of this expression Pop left operand from numbers stack If the numbers stack is empty, declare an error and end processing of this expression Pop operator from operator stack If the operator stack is empty, declare an error and end processing of this expression Carry out operation Push result onto numbers stack Top of operator stack should be the corresponding ( If so, remove it If it's not a left-parenthesis, declare an error and end processing of this expression At end of expression, the following conditions should hold: The operator stack is empty The numbers stack has exactly one value: the value of the expression

You are given an input file containing several expressions, some of them valid, some not. Output is to the screen; for each expression, either the value of the expression or an “invalid expression” message.

Development notes: • Write a template List class. The class should contain the following methods:

push_back(item) push_front(item) insert(item) // assumes list is already sorted, places in order sort() size() clear() pop_front() // removes first item, returns nothing pop_back() // removes last item, returns nothing front() // returns first item, does not alter list back() // returns last item, does not alter list remove(item) // removes 1st occurrence found in list remove_all(item) // removes all occurrences in list is_present(item) const // returns true if item is anywhere in list, false otherwise

As part of the List class, declare a ListException class that can be thrown on a list error (such as attempting to remove something from an empty list, etc) • The stack class will be a wrapper for the list class, exposing only a limited set of of this functionality: push(item), which calls push_back top(), which calls back() pop(), which calls pop_back() size(), clear(), and is_present() all call the corresponding list methods.

• As part of your stack class, define a StackException class that can be thrown on stack overflow or underflow. This can simply be a catch block that catches a ListException and throws a StackException as its action.

• Your program should print error messages for mismatched parentheses (at end of processing a right-parenthesis, the top character on the operator stack is anything other than the corresponding left parenthesis), invalid character in input, or “invalid expression” for anything else.

Example of input and final output should look like this:

Input (input.txt):

(1 + 1)

((7+5)/2) (

(3*4)+ 1)

(1 + * 1)

((7+5)/2))

(((3*4)+ 1)

((7+5)/2 3)

((7$5)/2)

(((9+3)*((3+1)/(1+1)))/4)

Output (to screen):

2

6

13

Mismatched parentheses in expression. Invalid expression.

Stack error in Evaluate(): Stack error on item access Invalid expression.

Invalid expression.

Invalid expression.

Invalid character in input: $ Invalid expression.

6

Press any key to continue . . .

Explanation / Answer

The file can be read using function below

List<string> getItems(string fileName)
{
List<string> list = new ArrayList<string>();

ifstream in_stream;

string line;

in_stream.open(fileName);

while(!in_stream.eof())
{
    in_stream >> line;
    list.add(line);
}

in_stream.close();

return list;

}

In the Stack, we can have push method which should be as below,

public class Stack<char>
{
private:
  std::list<char> list;
  
public:
  Stack()
  {
   list = new ArrayList<char.();
   
  }
  void push(char item);
  char pop();

}

void Stack::push(char item)
{
list.push_back(item);

  
}

char Stack::pop()
{
if(list.size()>0)
  list.pop_back(item);
return '-1'
  
}

The expression evaluator can be as below.

class Evaluator()
{
public:
  int evaluate(char[] items);
private:
  Stack<char> stack;
  int calc(int num1, char op, int num2);
}

int Evaluator:: evaluate(char[] items)
{
int index = 0;
while(index < items.size())
{
  if(items[index] != ')')
  {
   stack.push(items[index]);
  }
  else
  {
   int el1 = (int)stack.pop()
   char op = stack.pop()
   int el2 = (int)stack.pop()
   int calcItem = calc(el1,op,el2);
   index++;
   cout<<calcitem
   
  }
}
}

int Evaluator:: calc(int item1, char op, int item2)
{
switch(op)
{
  case '+':
   return item+item2;
  case '-'
   return item1-item2;
  case '*'
   return item1*item2;
  case '/'
   return item1/item2;
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote