subject

Infix to Postfix Write a C++ program that will accept infix expressions (like 5*(4+8)) and
convert them to postfix. You are to use Dijkstra's algorithm for converting.
Dijkstra's Algorithm:
Read in 1 line into a string
Print the string
while there are characters left to process in the string

| get a token (skip over blanks)
| if the token is a digit then output(token)
| else
|
| | if the token is '(' then push(token)
| | else
| |
| | | if the token is ')' then
| | |
| | | | while the top item is not '('
| | | | pop(temp) and output(temp);
| | | | pop(temp)
| | |
| | | else
| | |
| | | | if the stack is empty then push(token)
| | | | else
| | | |
| | | | | while the stack is not empty
| | | | | and the priority (token) <= priority (top item on the stack)
| | | | | pop(temp) and output(temp)
| | | | | push(token)
| | | |
| | |
| |
|

while the stack is not empty do pop(temp) and output(temp)
Precedence of the Operators:
operators : ^ * / + - (
precedence: 3 2 2 1 1 0
where ^ means exponentiation
input for the assignment:
2 + 3 * 5
2 + 3 * 5 ^ 6
2 + 3 - 5 + 6 - 4 + 2 - 1
2 + 3 * (5 - 6) - 4
2 * 3 ^ 5 * 6 - 4
(2 + 3) * 6 ^ 2
Output for the assignment
1: 2 + 3 * 5
235*+
2: 2 + 3 * 5 ^ 6
2356^*+
3: 2 + 3 - 5 + 6 - 4 + 2 - 1
23+5-6+4-2+1-
4: 2 + 3 * (5 - 6) - 4
2356-*+4-
5: 2 * 3 ^ 5 * 6 - 4
235^*6*4-
6: (2 + 3) * 6 ^ 2
23+62^*
You might also try:
7: ( ( ( ( 2 + 3 - 4 ) / 2 + 8 ) * 3 * ( 4 + 5 ) / 2 / 3 + 9 ) )
23+4-2/8+3*45+*2/3/9+
Programming Notes:
You are to write a well-composed program. The stack routines are to be
defined as methods in a class and are to be in a separate file.
//A sample c++ program to read a file 1 line at a time
#include
#include
#include
using namespace std;
int main()
{
string aline;
ifstream inData;
inData. open("infix. data");
while ( getline(inData, aline) )
{
cout << aline << endl;
}
inData. close();
}

ansver
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 15:00, miracle9704
Atool that matches persoal skills qualities interests and talets to a career is called a
Answers: 1
image
Computers and Technology, 23.06.2019 13:00, dimondqueen511
Which one of the following voltages should never be measured directly with a vom? a. 1200 v b. 500 v c. 800 v d. 100v
Answers: 2
image
Computers and Technology, 23.06.2019 17:30, cxttiemsp021
Per the municipal solid waste report, what are the most common sources of waste (trash
Answers: 3
image
Computers and Technology, 23.06.2019 19:00, amayax77
Write a program that displays the following menu: geometry calculator 1. calculate the area of a circle 2. calculate the area of a rectangle 3. calculate the area of a triangle 4. quit enter your choice (1-4): if the user enters 1, the program should ask for the radius of the circle and then display its area. use the following formula: area = ď€(the square of r) use 3.14159 for ď€ and the radius of the circle for r. if the user enters 2, the program should ask for the length and width of the rectangle and then display the rectangle’s area. use the following formula: area = length * width if the user enters 3, the program should ask for the length of the triangle’s base and its height, and then display its area. use the following formula: area = base * height * .5 if the user enters 4, the program should end. input validation: display an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu. do not accept negative values for the circle’s radius, the rectangle’s length or width, or the triangle’s base or height. note: if the user enters an improper menu choice (1-4), the program prints "the valid choices are 1 through 4. run the program again and select one of those." if the user enters a negative radius, the program prints "the radius can not be less than zero." if the user enters a negative value for height or base, the program prints "only enter positive values for base and height."
Answers: 1
You know the right answer?
Infix to Postfix Write a C++ program that will accept infix expressions (like 5*(4+8)) and
co...

Questions in other subjects:

Konu
Mathematics, 10.06.2020 17:57