Computers and Technology

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();
}

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 02:00, rah45
Which of the following is not a source of sustainable raw materials? a) coal mine b) flick of sheep c) cotton plantation d) line forest.
Answers: 2
image
Computers and Technology, 23.06.2019 09:30, Princessirisperez0
Given a link with a maximum transmission rate of 32.8 mbps. only two computers, x and y, wish to transmit starting at time t = 0 seconds. computer x sends filex (4 mib) and computer y sends filey (244 kib), both starting at time t = 0. statistical multiplexing is used, with details as follows packet payload size = 1000 bytes packet header size = 24 bytes (overhead) ignore processing and queueing delays assume partial packets (packets consisting of less than 1000 bytes of data) are padded so that they are the same size as full packets. assume continuous alternating-packet transmission. computer x gets the transmission medium first. at what time (t = ? ) would filey finish transmitting? give answer in milliseconds, without units, and round to one decimal places (e. g. for an answer of 0.013777 seconds you would enter "13.8" without the quotes)
Answers: 3
image
Computers and Technology, 23.06.2019 19:30, sallybob0
What are loans to a company or government for a set amount of time
Answers: 1
image
Computers and Technology, 24.06.2019 00:40, iamsecond235p318rq
To maintain clarity and focus lighting might be needed
Answers: 2
Do you know the correct answer?
Infix to Postfix Write a C++ program that will accept infix expressions (like 5*(4+8)) and
co...

Questions in other subjects: