1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Flauer [41]
1 year ago
6

write a program to evaluate the following postfix expression. the value of each variable must be asked from the user during the

execution time. suppose the postfix expression is tom jerry mickey 23 * - $ and variables tom, jerry, and mickey are int type
Computers and Technology
1 answer:
ikadub [295]1 year ago
8 0

In this exercise we have to use the knowledge of computational language in C++ to write a code that program to evaluate the following postfix expression. the value of each variable must be asked from the user during the execution time.

<h3>Writting the code:</h3>

<em>#include <bits/stdc++.h></em>

<em>using namespace std;</em>

<em>// CREATED A </em><em>CLASS </em><em>STACK</em>

<em>class Stack</em>

<em>{</em>

<em>public:</em>

<em> int top;</em>

<em> unsigned sz;</em>

<em> int* ar;</em>

<em>};</em>

<em>// VARIOUS STACK </em><em>FUNCTIONS</em>

<em>Stack* makeStack( unsigned sz )</em>

<em>{</em>

<em> Stack* stack = new Stack();</em>

<em> // BASE </em><em>CONDITION</em>

<em> if (!stack) return NULL;</em>

<em> stack->top = -1;</em>

<em> stack->sz = sz;</em>

<em> stack->ar = new int[(stack->sz * sizeof(int))];</em>

<em> if (!stack->ar) return NULL;</em>

<em> // RETURNING THE STACK</em>

<em> return stack;</em>

<em>}</em>

<em>// CHECKING IF THE STACK IS </em><em>EMPTY</em>

<em>int isEmpty(Stack* stack)</em>

<em>{</em>

<em> return stack->top == -1 ;</em>

<em>}</em>

<em>// CHECKING THE TOP OF THE STACK</em>

<em>int whatAtTop(Stack* stack)</em>

<em>{</em>

<em> return stack->ar[stack->top];</em>

<em>}</em>

<em>// </em><em>POPPING </em><em>OUT OF A STACK</em>

<em>int pop(Stack* stack)</em>

<em>{</em>

<em> if (!isEmpty(stack))</em>

<em>  return stack->ar[stack->top--] ;</em>

<em> return '$';</em>

<em>}</em>

<em>// PUSHING IN THE STACK</em>

<em>void push(Stack* stack, int op)</em>

<em>{</em>

<em> stack->ar[++stack->top] = op;</em>

<em>}</em>

<em>// EVALUATING </em><em>POSTFIX </em><em>STARTS HERE</em>

<em>int postfixEvaluator(string res)</em>

<em>{</em>

<em> // CREATING STACK</em>

<em> Stack* stack = makeStack(res.length());</em>

<em> int i;</em>

<em> // BASE </em><em>CONDITION</em>

<em> if (!stack) return -1;</em>

<em> for (i = 0; res[i]; ++i)</em>

<em> {</em>

<em>  // CHECK FOR SPACES</em>

<em>  if (res[i] == ' ')continue;</em>

<em>  // CHECK FOR THE DIGIT</em>

<em>  else if (isdigit(res[i]))</em>

<em>  {</em>

<em>   int N = 0;</em>

<em>   // EXTRACT THE </em><em>NUMBER </em><em>OUT OF STACK</em>

<em>   while (isdigit(res[i]))</em>

<em>   {</em>

<em>    N = N * 10 + (int)(res[i] - '0');</em>

<em>    i++;</em>

<em>   }</em>

<em>   i--;</em>

<em>   // </em><em>PUSH </em><em>THE NUMBER IN STACK</em>

<em>   push(stack, N);</em>

<em>  }</em>

<em>  // POPPING AND </em><em>ARITHMETIC </em><em>OPERATIONS</em>

<em>  else</em>

<em>  {</em>

<em>   int pos1 = pop(stack);</em>

<em>   int pos2 = pop(stack);</em>

<em>   switch (res[i])</em>

<em>   {</em>

<em>   case '+': push(stack, pos2 + pos1); break;</em>

<em>   case '-': push(stack, pos2 - pos1); break;</em>

<em>   case '*': push(stack, pos2 * pos1); break;</em>

<em>   case '/': push(stack, pos2 / pos1); break;</em>

<em>   }</em>

<em>  }</em>

<em> }</em>

<em> return pop(stack);</em>

<em>}</em>

<em>// MAIN CODE</em>

<em>int main()</em>

<em>{</em>

<em> string tes;</em>

<em> getline(cin, tes);</em>

<em> string tmp = "";</em>

<em> string res = "";</em>

<em> for (int i = 0; i < tes.length(); i++) {</em>

<em>  if (tes[i] <= 'z' and tes[i] >= 'a') {</em>

<em>   tmp += tes[i];</em>

<em>  } else if (tes[i] == ' ' and (tes[i - 1] <= 'z' and tes[i - 1] >= 'a')) {</em>

<em>   cout << "Enter the value of " << tmp << ": ";</em>

<em>   int x; cin >> x;</em>

<em>   res += (to_string(x));</em>

<em>   res += ' ';</em>

<em>   tmp = "";</em>

<em>  } else {</em>

<em>   res += tes[i];</em>

<em>  }</em>

<em> }</em>

<em> cout << "\nThe postfix expression is : " << res;</em>

<em> cout << "\nThe result is : " << postfixEvaluator(res);</em>

<em> return 0;</em>

<em>}</em>

See more about C++ at brainly.com/question/19705654

#SPJ1

You might be interested in
What can multivariable calculus show for a data set.
dalvyx [7]

Answer:

They can help to explain the relationship between the output and input variables.

Hope this Helps!

5 0
2 years ago
What is the difference between the animation with shape hints and the animation without the shape hints?
soldier1979 [14.2K]
Animation with shape is 3D animation
Animation without shape is 2D animation
4 0
3 years ago
An investigator obtains consent and HIPAA authorization from subjects to review their medical records and HIV status. He plans t
matrenka [14]

Answer:D. A HIPAA violation

Explanation:

The Health Insurance Portability and Accountability Act of 1996 is a legislation that was designed to protect or safeguard medical records of patients, to eliminate and prevent healthcare fraud and protect health care coverage of employees .

It helps to ensure that patients' information remains private and their record are secured.

The updated rules of this act includes the HIPAA Privacy Rule, HIPAA Security Rule, HIPAA Omnibus Rule, and the HIPAA Breach Notification Rule.

A HIPAA violation is a failure to comply with any aspect of HIPAA standards and provisions and in this case it was theft of records; he should have ensured that the computer is placed in a secured place or the information is locked even when the computer get stolen.

7 0
3 years ago
5. Write few lines of code that creates two arrays with malloc. Then write a statement that can create a memory leak. Discuss wh
Darina [25.2K]

Answer:

 // function with memory leak  

void func_to_show_mem_leak()  {  

int *pointer;

pointer = malloc(10 * sizeof(int));

*(pointer+3) = 99;}  

 

// driver code  

int main()  

{  

    // Call the function  

   // to get the memory leak  

   func_to_show_mem_leak();  

     return 0;  }

Explanation:

Memory leakage occurs when programmers allocates memory by using new keyword and forgets to deallocate the memory by using delete() function or delete[] operator. One of the most memory leakage occurs by using wrong delete operator. The delete operator should be used to free a single allocated memory space, whereas the delete [] operator should be used to free an array of data values.

3 0
3 years ago
How is an orthographic drawing similar or different from an isometric drawing
Savatey [412]
An Isometric drawing<span> is a quasi 3d </span>drawing<span> that shows the height width and depth of the object in a single view where the viewpoint is at a 45 degree angle from each of the perpendicular planes of the </span>orthographic<span> view. </span>Isometric<span> differs from a perspective view in that all lengths are shown true length.</span>
6 0
3 years ago
Other questions:
  • Without exception, you must always stop when ______________.
    14·1 answer
  • Similarities between human and computer​
    12·1 answer
  • What statement should you use to print the value of total with a precision of 5 digits?
    7·1 answer
  • John’s grandparents make wine for special occasions. They add a pinch of yeast to crushed grapes. Over time, this action release
    15·2 answers
  • Read the scenario below, and then answer the question.
    13·1 answer
  • The foundation of secure communication on the internet replies on asymmetric encryption, with the use of Public and Private keys
    10·1 answer
  • WILL GIVE MORE POINTS, PLEASE HELP!!!
    11·1 answer
  • Aaron is stating the main idea of what he read in his own words. He is _____.
    11·2 answers
  • 3.6 Code Practice Edhesive. (PYTHON LANGUAGE)
    13·1 answer
  • This isn't an academic question, but it's a question about something I am consistently seeing on Brainly, so please don't delete
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!