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
Romashka [77]
3 years ago
8

in C++ Please Specification Algorithms for expression evaluation Expression evaluation contains two major stages: Converting fro

m infix notation to postfix notation, using a stack to store the operators (+, -, etc.) Evaluating the postfix expression, using a stack to store the operands (numbers being operated on) Infix notation is the standard expression form with which you're familiar. Operators with higher precedence (* and /) evaluate before lower-precedence operators (+ and -), with parentheses used to override that precedence. Examples of infix expressions include: 5 + 3 * 2 (4 + 2) / (3 - 1) (5 + 6 * (2 - 1)) / 4 In postfix notation, operators are written after the values, or operands, on which they operate. Operator precedence doesn't matter, so parentheses are unnecessary. Expressions are evaluated left to right. Each time you encounter an operator, it operates on the two values before it, with the operation result then replacing the numbers and operator in the expression. Here are the postfix equivalents of the infix expressions above: 5 3 2 * + 4 2 + 3 1 - / 5 6 2 1 - * + 4 / Each of the two evaluation stages can be broken down further, as described below. This link also contains an explanation of these steps, with slightly different examples. Infix to postfix conversion Parse the expression from left to right, using a stack to store the operators. As each token (piece of the input--in this case, a number, operator, or parenthesis) is encountered, do the following: If the token is a number, pass it directly to the "output" ("Output" = postfix expression. See Section 3: Hints on how to store this expression) If the token is an operator or parenthesis, change the operator stack as follows: If the token is a left parenthesis (, just push it onto the stack If the token is a right parenthesis ), check the top of the stack until you find the corresponding left parenthesis. Pop every other operator off the stack and pass each one to the output. Pop the left parenthesis but do not pass it to the output. If the token is an operator (+ - * /), check the top of the stack until you find an operator of lower priority than the current operator (or the stack is empty). Pop everything of equal or higher priority, sending each popped operator to the output. Once your program has read the entire input expression, pop every remaining operator off the stack, sending each one to the output as it's removed from the stack. Postfix expression evaluation Parse the postfix expression from left to right, using a stack to store the numbers this time. Handle each token as follows: If the token is a number, push it on the stack. If the token is an operator, pop the top two values off the stack, perform the specified operation, then push the result on the stack Once your program has parsed the entire postfix expression, the stack holds the result of evaluating the expression. Print this value, then pop it off the stack. Input specification Your program should repeatedly print the message Enter expression (or exit to end):, then read the line the user inputs. The input follows the rules below: Every valid line of input begins with a number, a left parenthesis (, or the letter e in exit Your program should print Invalid expression in all other cases. You can assume any line that starts with a number or left parenthesis is a valid expression--there are no errors in the middle of an expression. The input only contains single-digit, non-negative integers (0-9) Output specification and detailed grading rubric Your program should print the prompt described above, then print the following information in response to each type of input line (includes grading rubric that corresponds to test cases) If the user enters "exit", print Exiting program ... and end the program (5 points) If the user enters an invalid expression, print Invalid expression (10 points) For example: Enter expression (or exit to end): )1 + 2) * 3 Invalid expression If the user enters a valid expression (a line beginning with a number or ( character), print the following Print Expression:, followed by the original expression (10 points) On the next line, print Postfix form:, followed by the expression converted to postfix form (15 points) On the next line, print Result:, followed by the result of evaluating the postfix form of the expression (15 points) For example: Enter expression (or exit to end): (5 + 6 * (2 - 1)) / 4 Expression: (5 + 6 * (2 - 1)) / 4 Postfix form: 5 6 2 1 - * + 4 / Result: 2 If the user enters anything other than "exit," repeat the prompt and read another expression (5 points) For example: Enter expression (or exit to end): 5 + 3 * 2 Expression: 5 + 3 * 2 Postfix form: 5 3 2 * + Result: 11 Enter expression (or exit to end): (4 + 2) / (3 - 1) Expression: (4 + 2) / (3 - 1) Postfix form: 4 2 + 3 1 - / Result: 3 Enter expression (or exit to end): exit Exiting program ...
Engineering
1 answer:
Sladkaya [172]3 years ago
4 0

Answer:

See explaination

Explanation:

#include<iostream>

#include<stdlib.h>

#include<string.h>

#include<math.h>

using namespace std;

const int MAX = 100;

//stack structure

struct stack{

string st;

int top = -1;

//push an item to the stack

void push(char x)

{

if(top==MAX-1){

cout << "Stack overflow\n";

return;

}

st[++top] = x;

}

//delete the topmost element

char pop()

{

if(top==-1){

cout << "Stack underflow\n";

return 0;

}

return st[top--];

}

//return topmost element

char peek()

{

return st[top];

}

};

//function prototypes

int precedence(char x);

string convert(string infixExpression);

int evaluate(string postFix);

//main function

int main()

{

string infix, postfix;

do{

//prompt and read infix expression

cout<< "Enter expression (or exit to end):" <<endl;

getline(cin, infix);

if(infix=="exit") break;

if(infix[0]=='(' || isdigit(infix[0]))

{

cout << "Expression: " << infix << endl;

string postfix = convert(infix);

cout << "Postfix form: " << postfix <<endl;

int result = evaluate(postfix);

cout << "Result:" << result << endl;

}

else

cout << "Invalid expression" <<endl;

}while(1);

return 0;

}

//function to return precedence

int precedence(char x)

{

switch(x)

{

case '(':

return 0;

case '+':

case '-':

return 1;

case '*':

case '/':

return 2;

case '^':

return 3;

default : return 999;

}

}

//function to convert from infix to postfix expression

string convert(string infix)

{

stack stk;

string postfix = "";

stk.push('(');

int count = 0;

for(int i=0; i<infix.size(); i++)

{

if(isalpha(infix[i]))

count++;

}

infix = infix + ")";

for(int i=0; i<infix.size(); i++)

{

char ch = infix[i];

if(ch==' ') continue;

if(isalpha(ch))

{

postfix = postfix + " " + ch;

}

else if(ch=='(')

{

stk.push('(');

}

else if(ch==')')

{

while(stk.peek()!='(')

{

postfix = postfix + " " + stk.pop();

}

stk.pop();

}

else

{

int p1 = precedence(ch);

int p2 = precedence(stk.peek());

while(p1<=p2)

{

postfix = postfix + " " + stk.pop();

p2=precedence(stk.peek());

}

stk.push(ch);

}

}

return postfix;

}

//function to evaluate postfix expression

int evaluate(string postFix)

{

stack stk;

for(int i=0; i<postFix.size(); i++)

{

char c = postFix[i];

if(c==' ') continue;

if(isdigit(c))

{

int val = c - '0';

stk.push(val);

}

else

{

int x = stk.pop();

int y = stk.pop();

switch(c)

{

case '+':

stk.push(y+x);

break;

case '-':

stk.push(y-x);

break;

case '*':

stk.push(y*x);

break;

case '/':

stk.push(y/x);

break;

case '^':

stk.push(pow(y,x));

break;

}

}

}

return stk.pop();

}

You might be interested in
The two boxcars A and B have a weight of 20000lb and 30000lb respectively. If they coast freely down the incline when the brakes
kolbaska11 [484]

Answer:

T=5.98 kips

Explanation:

First, introduce forces, acting on both cars:

on car A there are 4 forces acting: gravity force mA*g, normal reaction force, friction force and force T- it represents the interaction between cars A and B. On car B, there are three forces acting: gravity force, normal reaction force and force T. Note, that force T is acting on both cars, but it has opposite direction. Force T, acting on car A has direction, opposite to the friction force, whether the T, acting on B, is directed backwards- in the same direction with the friction force. Note, that both cars have the same acceleration, which is directed backwards.

Once the forces were established, we can write components of the Second Newtons Law on vertical and horizontal axes, considering that horizontal axis is directed backwards- in the same direction with the acceleration:

For car A on the vertical axis the equation is: -mAg+NA=0

For car A on the horizontal axis, the equation is: Ffr-T=mAa

For car B, on the vertical axis the equation is: -mBg+NB=0

For car B, on the horizontal axis, the equation is: T=mBa

We need to solve these equations to find force T, knowing that Ffr=μmAg, where

After the transformations, the equations for acceleration and force in the coupling will be:

a=(μmAg)/(mA+mB)=6.43 ft/s2- note, that the given answer is not correct for the given numerical values;

and force T: T=μmAmBg/(mA+mB)=6.0 kips- note, that the force answer is in line with the given numerical value

5 0
4 years ago
You are designing the access control policies for a Web-based retail store. Customers access the store via the Web, browse produ
PolarNik [594]

Answer:

Object-Oriented Software Engineering Using UML, Patterns, and Java, 3e, shows readers how to use both the principles of software engineering and the practices of various object-oriented tools, processes, and products.

3 0
3 years ago
A(n) _____________ is used commonly in open split-phase motors to disconnect the start winding from the electrical circuit when
Alenkasestr [34]

A <u>centrifugal switch</u> is used commonly in open split-phase motors to disconnect the start winding from the electrical circuit when the motor reaches approximately 75% of its rated speed.

Hope that helps!

7 0
3 years ago
Which is not required when working in a manufacturing facility?
Artyom0805 [142]
Flip flops are not required
5 0
3 years ago
As cylinder pressure and heat increase due to an increased load condition, the fuel injection management system must ___________
Temka [501]

possible Answers:

Compensate ⭐⭐⭐⭐⭐

Adjust            ⭐⭐⭐⭐⭐

regulate         ⭐⭐⭐⭐

tune               ⭐⭐⭐

calibrate        ⭐⭐⭐

balance         ⭐⭐

correct           ⭐

6 0
3 years ago
Other questions:
  • When the outside temperature is 5.2 ⁰C, a steel beam of cross-sectional area 52 cm2 is installed in a building with the ends of
    8·1 answer
  • Can anybody teach me how to make an app with flask and pygame together?​
    10·1 answer
  • One type of illumination system consists of rows of strip fluorescents and a ceiling that will transmit light. For this system t
    15·1 answer
  • Verify the below velocity distribution describes a fluid in a state of pure rotation. What is the angular Velocity? (a)-Vx = -1/
    7·1 answer
  • The specific gravity of a substance that has mass of 10 kg and occupies a volume of 0.02 m^3 is a) 0.5 b) 1.5 c) 2.5 d) 3.5 e) n
    11·1 answer
  • Problem: design the following rectangular floor beam for a building.
    15·2 answers
  • g A smooth pipeline with a diameter of 5 cm carries glycerin at 20 degrees Celsius. The flow rate in the pipe is 0.01 m3/s. What
    15·1 answer
  • Please read and an<br><br> 3. Many Jacks use hydraullc power.<br> A) O True<br> B) O False
    13·1 answer
  • In the figure show, what's the distance from point H to point C?
    14·1 answer
  • Tech A says that wiring diagrams are essentially a map of all of the electrical components and their connections. Tech B says th
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!