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
This is going to get taken down but I dont care add me on discord cause ima bored<br> -Red-#9847
Alex777 [14]

Answer:

I WILL

Explanation:

6 0
3 years ago
Social media applications' main purpose is to allow users to watch movies and TV shows, listen to music, and read books online.
Novosadov [1.4K]

Answer: I dont really know but im guessing it depends on the type of assignment you have. im thinking True.

Explanation:

4 0
3 years ago
Demote the "Arrive and leave at the same time each day." List item, and then promote the "Respect" list item.
garri49 [273]

Answer:

Dear

<h3>You should wear something professional. A tie, suit, classy dress, heels. You are more likely to be hired if you make it look like you take pride in your appearance. Also, depending on what kind of job you are applying for, the outfits can vary as well.</h3>

Explanation:

Black is too formal for interviews, and earth tones are too casual. Two-button suits are the professional standard.

7 0
3 years ago
Default tab stops are set in word every _______ inch.<br> a. ¼<br> b. ¾<br> c. ½<br> d. 1
UNO [17]
Default tab stops are set in word every 1/2 inch so your correct answer is C :)
5 0
3 years ago
A single instruction carried out by a computer is called a what?
pochemuha

Answer:

in computer science, an instruction is a single operation of a processor defined by the instruction set

Explanation:

if you need anymore help let me know :)

6 0
3 years ago
Other questions:
  • You are attempting to update a Windows image that has been mounted under C:\mount on your local system. What command must you us
    13·1 answer
  • What is safe mode?
    9·1 answer
  • Texture fills are available to add as a slide background true or false
    7·2 answers
  • What does psychologist Edward Spector mean when he says “ We have an entire generation of guinea pigs in an experiment”?
    14·1 answer
  • This is a wise and hard question.....What is the hardest question in the world? I know the answer do you?
    10·1 answer
  • Declare a variable temperature and initialize it to 98.6. Instructor Notes: Note that "initialize" means "declare and assign a v
    5·1 answer
  • Can someone start me off with a short 2 or 3 paragraphs about the pros and cons of Microsoft Word, and if you can recommend a si
    12·1 answer
  • Ten examples of an interpreter
    8·1 answer
  • What do mobile platforms utilize to stream voice, IP telephony, mobile internet access, video calling, gaming services, cloud co
    15·1 answer
  • To delete unnecessary files on a hard disk use software
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!