Toggle navigation
Home
Ask A Question
Answer Questions
Users
Support/FAQ
Login
Register
343Program7.241.pdf
Details
Question Title:
241 Assignment 7
View the Question
Preview - Click download on the question page for the full document.
prec3 ? "^ higher than *" : "^ lower than *") << endl << (prec1 > prec4 ? "^ higher than /" : "^ lower than /") << endl << (prec1 > prec5 ? "^ higher than +" : "^ lower than +") << endl << (prec1 > prec6 ? "^ higher than -" : "^ lower than -") << endl << (prec1 > prec7 ? "^ higher than $" : "^ lower than $") << endl << endl << endl; cout << (prec3 == prec4 ? "* / equal precedence" : "* / not equal precedence") << endl << (prec3 > prec5 ? "* higher than +" : "* lower than +") << endl << (prec3 > prec6 ? "* higher than -" : "* lower than -") << endl << (prec3 > prec7 ? "* higher than $" : "* lower than $") << endl << endl << endl; cout << (prec5 == prec6 ? "+ - equal precedence" : "+ - not equal precedence") << endl << (prec5 > prec7 ? "+ higher than $" : "+ lower than $") << endl << endl << endl; Compile the code by executing make at the command line. If there are any compiler errors, fix them. Execute the program ./inpost If the functions were written correctly, the output should be: ^ is an operator ~ is an operator + is an operator - is an operator * is an operator / is an operator $ is not an operator ^ ~ equal precedence ^ higher than * ^ higher than / ^ higher than + ^ higher than - ^ higher than $ * / equal precedence * higher than + * higher than - * higher than $ + - equal precedence + higher than $ Once the output matches, delete the code that was added (the cout statements and integer variables). Un- comment the code that was commented out at the beginning of this step. 6. Some useful things from the string class: The length() method returns the number of characters in a string object. The += operator can be used to append a character to a string object. The [subscript] operator can be used to access a character at a specific location in a string object. 7. In the inpost.cpp file, add code for the convert function. This function takes a reference to a constant string that holds the infix expression as its argument and returns a string (the postfix expression). The goal right now is not to do the conversion from infix to postfix. It’s just to make sure that all the characters in the infix string can be processed one at a time and properly identified. The function will eventually need a few variables, but for now just create a string to hold the resulting postfix expression. Nothing is going to be put in it right now but it can be used to satisfy the return type for the function. In a loop that executes for each character in the infix string (i.e. for the length of the infix string), write a cascading decision statement that tests: - If the character is an alphabetic character, display the character and some message that says the character is alphabetic - If the character is a digit character, display the character and some message that says the character is a digit - If the character is a left parenthesis, display the character and some message that says the character is a left parenthesis - If the character is a right parenthesis, display the character and some message that says the character is a right parenthesis - If the character is an arithmetic operator, display the character and some message that says the character is an operator. This is where the function written in step 3 can be used. - Otherwise, display the character and that it is being ignored Return the postfix string. Note: make sure to put the curly braces in for each portion of the cascading decision statement. 8. Compile and execute the code at the command line. However, when executing the code, make sure to use input redirection to direct the infix.in file that was copied by the setup command to the program: ./inpost < infix.in If the convert function processes the characters correctly, the output should resemble: infix: 2 2 is a digit postfix: infix: 2+a 2 is a digit + is an operator a is alphabetic postfix: infix: (1+a) ^2 ( is a left parenthesis 1 is a digit + is an operator a is alphabetic ) is a right parenthesis is ignored ^ is an operator 2 is a digit postfix: infix: ~(1+a) ^2 ~ is an operator ( is a left parenthesis 1 is a digit + is an operator a is alphabetic ) is a right parenthesis is ignored ^ is an operator 2 is a digit postfix: infix: ~((1+a)^2) ~ is an operator ( is a left parenthesis ( is a left parenthesis 1 is a digit + is an operator a is alphabetic ) is a right parenthesis ^ is an operator 2 is a digit ) is a right parenthesis postfix: infix: 0/0 0 is a digit / is an operator 0 is a digit postfix: infix: 1/0 1 is a digit / is an operator 0 is a digit postfix: infix: 0/1 0 is a digit / is an operator 1 is a digit postfix: infix: c^a c is alphabetic ^ is an operator a is alphabetic postfix: infix: ~c^a ~ is an operator c is alphabetic ^ is an operator a is alphabetic postfix: infix: c^d c is alphabetic ^ is an operator d is alphabetic postfix: infix: ~c^d ~ is an operator c is alphabetic ^ is an operator d is alphabetic postfix: infix: d+1 d is alphabetic + is an operator 1 is a digit postfix: infix: d +1 *2 is ignored is ignored d is alphabetic is ignored + is an operator 1 is a digit is ignored * is an operator 2 is a digit postfix: infix: ( d +1) *2 ( is a left parenthesis is ignored d is alphabetic is ignored + is an operator 1 is a digit ) is a right parenthesis is ignored * is an operator 2 is a digit postfix: infix: a-e-a a is alphabetic - is an operator e is alphabetic - is an operator a is alphabetic postfix: infix: (a-e-a)/( ~d + 1) ( is a left parenthesis a is alphabetic - is an operator e is alphabetic - is an operator a is alphabetic ) is a right parenthesis / is an operator ( is a left parenthesis is ignored is ignored ~ is an operator d is alphabetic is ignored + is an operator is ignored 1 is a digit ) is a right parenthesis postfix: infix: (a^2 + ~b ^ 2) * (5 - c) ( is a left parenthesis a is alphabetic ^ is an operator 2 is a digit is ignored + is an operator is ignored ~ is an operator b is alphabetic is ignored ^ is an operator is ignored 2 is a digit ) is a right parenthesis is ignored * is an operator is ignored ( is a left parenthesis 5 is a digit is ignored - is an operator is ignored c is alphabetic ) is a right parenthesis postfix: infix: ~ 3 * ~( a + 1) - b / c ^ 2 is ignored is ignored ~ is an operator is ignored 3 is a digit is ignored * is an operator is ignored ~ is an operator ( is a left parenthesis is ignored a is alphabetic is ignored + is an operator is ignored 1 is a digit ) is a right parenthesis is ignored - is an operator is ignored b is alphabetic is ignored / is an operator is ignored c is alphabetic is ignored ^ is an operator is ignored 2 is a digit postfix: infix: 246 + b /123 2 is a digit 4 is a digit 6 is a digit is ignored + is an operator is ignored b is alphabetic is ignored / is an operator 1 is a digit 2 is a digit 3 is a digit postfix: infix: ( 246 + (( b /123) ) ) ( is a left parenthesis is ignored is ignored is ignored 2 is a digit 4 is a digit 6 is a digit is ignored + is an operator is ignored ( is a left parenthesis ( is a left parenthesis is ignored is ignored b is alphabetic is ignored / is an operator 1 is a digit 2 is a digit 3 is a digit ) is a right parenthesis is ignored is ignored is ignored ) is a right parenthesis is ignored is ignored is ignored ) is a right parenthesis postfix: infix: ( ( 246 + b) /123) ( is a left parenthesis is ignored is ignored ( is a left parenthesis is ignored 2 is a digit 4 is a digit 6 is a digit is ignored + is an operator is ignored is ignored b is alphabetic ) is a right parenthesis is ignored is ignored / is an operator 1 is a digit 2 is a digit 3 is a digit ) is a right parenthesis is ignored is ignored is ignored postfix: infix: a+b/c^d-~e*f a is alphabetic + is an operator b is alphabetic / is an operator c is alphabetic ^ is an operator d is alphabetic - is an operator ~ is an operator e is alphabetic * is an operator f is alphabetic postfix: Note: it might be easier to make a copy of the infix.in file to your account. The copy of the file can be edited to remove some of the infix expressions so there isn’t as much data to be processed right away. If you do this, substitute the name of the copy of the file after the < when executing the code. 9. In the inpost.cpp file, add code to the convert function. Remove the else statement that handles the characters that will be ignored. This should help to shorten the output. Now, handle the case of alphabetic characters first. After the cout statement that displays the character and the message about the character being alphabetic, determine if a space should be added to the postfix string (this needs to be done because the postfix string should have the different values separated by a single space). One way to do this is to check the length of the postfix string. If the postfix string has characters, append a space to the postfix string Finally, append the character from the infix string to the postfix string. 10. Compile and execute the code at the command line. Don’t forget to use input redirection! 11. Notice in the output that the postfix string that is displayed should now contain alphabetic characters and if there is more than one, they should be separated by a single space. infix: 2 2 is a digit postfix: infix: 2+a 2 is a digit + is an operator a is alphabetic postfix: a infix: (1+a) ^2 ( is a left parenthesis 1 is a digit + is an operator a is alphabetic ) is a right parenthesis ^ is an operator 2 is a digit postfix: a infix: ~(1+a) ^2 ~ is an operator ( is a left parenthesis 1 is a digit + is an operator a is alphabetic ) is a right parenthesis ^ is an operator 2 is a digit postfix: a infix: ~((1+a)^2) ~ is an operator ( is a left parenthesis ( is a left parenthesis 1 is a digit + is an operator a is alphabetic ) is a right parenthesis ^ is an operator 2 is a digit ) is a right parenthesis postfix: a infix: 0/0 0 is a digit / is an operator 0 is a digit postfix: infix: 1/0 1 is a digit / is an operator 0 is a digit postfix: infix: 0/1 0 is a digit / is an operator 1 is a digit postfix: infix: c^a c is alphabetic ^ is an operator a is alphabetic postfix: c a infix: ~c^a ~ is an operator c is alphabetic ^ is an operator a is alphabetic postfix: c a infix: c^d c is alphabetic ^ is an operator d is alphabetic postfix: c d infix: ~c^d ~ is an operator c is alphabetic ^ is an operator d is alphabetic postfix: c d infix: d+1 d is alphabetic + is an operator 1 is a digit postfix: d infix: d +1 *2 d is alphabetic + is an operator 1 is a digit * is an operator 2 is a digit postfix: d infix: ( d +1) *2 ( is a left parenthesis d is alphabetic + is an operator 1 is a digit ) is a right parenthesis * is an operator 2 is a digit postfix: d infix: a-e-a a is alphabetic - is an operator e is alphabetic - is an operator a is alphabetic postfix: a e a infix: (a-e-a)/( ~d + 1) ( is a left parenthesis a is alphabetic - is an operator e is alphabetic - is an operator a is alphabetic ) is a right parenthesis / is an operator ( is a left parenthesis ~ is an operator d is alphabetic + is an operator 1 is a digit ) is a right parenthesis postfix: a e a d infix: (a^2 + ~b ^ 2) * (5 - c) ( is a left parenthesis a is alphabetic ^ is an operator 2 is a digit + is an operator ~ is an operator b is alphabetic ^ is an operator 2 is a digit ) is a right parenthesis * is an operator ( is a left parenthesis 5 is a digit - is an operator c is alphabetic ) is a right parenthesis postfix: a b c infix: ~ 3 * ~( a + 1) - b / c ^ 2 ~ is an operator 3 is a digit * is an operator ~ is an operator ( is a left parenthesis a is alphabetic + is an operator 1 is a digit ) is a right parenthesis - is an operator b is alphabetic / is an operator c is alphabetic ^ is an operator 2 is a digit postfix: a b c infix: 246 + b /123 2 is a digit 4 is a digit 6 is a digit + is an operator b is alphabetic / is an operator 1 is a digit 2 is a digit 3 is a digit postfix: b infix: ( 246 + (( b /123) ) ) ( is a left parenthesis 2 is a digit 4 is a digit 6 is a digit + is an operator ( is a left parenthesis ( is a left parenthesis b is alphabetic / is an operator 1 is a digit 2 is a digit 3 is a digit ) is a right parenthesis ) is a right parenthesis ) is a right parenthesis postfix: b infix: ( ( 246 + b) /123) ( is a left parenthesis ( is a left parenthesis 2 is a digit 4 is a digit 6 is a digit + is an operator b is alphabetic ) is a right parenthesis / is an operator 1 is a digit 2 is a digit 3 is a digit ) is a right parenthesis postfix: b infix: a+b/c^d-~e*f a is alphabetic + is an operator b is alphabetic / is an operator c is alphabetic ^ is an operator d is alphabetic - is an operator ~ is an operator e is alphabetic * is an operator f is alphabetic postfix: a b c d e f 12. In the inpost.cpp file, add code to the convert function. Handle the case of digit characters. After the cout statement that displays the character and the message about the character being a digit, determine if a space should be added to the postfix string. This can be done in the same way as in step 9. It’s possible that the numeric values in the infix string have more than one digit. Because of this a loop is needed to make sure that all the digits are added to the postfix string. As long as the character in the infix string is a digit character, append the character to the postfix string and increment the subscript for the infix string by 1. Note: depending on how the loop that is processing the entire infix string (the one that was originally written in step 7) is written, you might have to decrement the subscript for the infix string by 1. 13. Compile and execute the code at the command line. Don’t forget to use input redirection! 14. Notice in the output that the postfix string that is displayed now contains alphabetic and digit characters and they should be separated by a single space. Also notice that the numeric values that have more than one digit do not have a space between the digits. infix: 2 2 is a digit postfix: 2 infix: 2+a 2 is a digit + is an operator a is alphabetic postfix: 2 a infix: (1+a) ^2 ( is a left parenthesis 1 is a digit + is an operator a is alphabetic ) is a right parenthesis ^ is an operator 2 is a digit postfix: 1 a 2 infix: ~(1+a) ^2 ~ is an operator ( is a left parenthesis 1 is a digit + is an operator a is alphabetic ) is a right parenthesis ^ is an operator 2 is a digit postfix: 1 a 2 infix: ~((1+a)^2) ~ is an operator ( is a left parenthesis ( is a left parenthesis 1 is a digit + is an operator a is alphabetic ) is a right parenthesis ^ is an operator 2 is a digit ) is a right parenthesis postfix: 1 a 2 infix: 0/0 0 is a digit / is an operator 0 is a digit postfix: 0 0 infix: 1/0 1 is a digit / is an operator 0 is a digit postfix: 1 0 infix: 0/1 0 is a digit / is an operator 1 is a digit postfix: 0 1 infix: c^a c is alphabetic ^ is an operator a is alphabetic postfix: c a infix: ~c^a ~ is an operator c is alphabetic ^ is an operator a is alphabetic postfix: c a infix: c^d c is alphabetic ^ is an operator d is alphabetic postfix: c d infix: ~c^d ~ is an operator c is alphabetic ^ is an operator d is alphabetic postfix: c d infix: d+1 d is alphabetic + is an operator 1 is a digit postfix: d 1 infix: d +1 *2 d is alphabetic + is an operator 1 is a digit * is an operator 2 is a digit postfix: d 1 2 infix: ( d +1) *2 ( is a left parenthesis d is alphabetic + is an operator 1 is a digit ) is a right parenthesis * is an operator 2 is a digit postfix: d 1 2 infix: a-e-a a is alphabetic - is an operator e is alphabetic - is an operator a is alphabetic postfix: a e a infix: (a-e-a)/( ~d + 1) ( is a left parenthesis a is alphabetic - is an operator e is alphabetic - is an operator a is alphabetic ) is a right parenthesis / is an operator ( is a left parenthesis ~ is an operator d is alphabetic + is an operator 1 is a digit ) is a right parenthesis postfix: a e a d 1 infix: (a^2 + ~b ^ 2) * (5 - c) ( is a left parenthesis a is alphabetic ^ is an operator 2 is a digit + is an operator ~ is an operator b is alphabetic ^ is an operator 2 is a digit ) is a right parenthesis * is an operator ( is a left parenthesis 5 is a digit - is an operator c is alphabetic ) is a right parenthesis postfix: a 2 b 2 5 c infix: ~ 3 * ~( a + 1) - b / c ^ 2 ~ is an operator 3 is a digit * is an operator ~ is an operator ( is a left parenthesis a is alphabetic + is an operator 1 is a digit ) is a right parenthesis - is an operator b is alphabetic / is an operator c is alphabetic ^ is an operator 2 is a digit postfix: 3 a 1 b c 2 infix: 246 + b /123 2 is a digit + is an operator b is alphabetic / is an operator 1 is a digit postfix: 246 b 123 infix: ( 246 + (( b /123) ) ) ( is a left parenthesis 2 is a digit + is an operator ( is a left parenthesis ( is a left parenthesis b is alphabetic / is an operator 1 is a digit ) is a right parenthesis ) is a right parenthesis ) is a right parenthesis postfix: 246 b 123 infix: ( ( 246 + b) /123) ( is a left parenthesis ( is a left parenthesis 2 is a digit + is an operator b is alphabetic ) is a right parenthesis / is an operator 1 is a digit ) is a right parenthesis postfix: 246 b 123 infix: a+b/c^d-~e*f a is alphabetic + is an operator b is alphabetic / is an operator c is alphabetic ^ is an operator d is alphabetic - is an operator ~ is an operator e is alphabetic * is an operator f is alphabetic postfix: a b c d e f 15. In the inpost.cpp file, add code to the convert function. Remove all the cout statements that were added to display the characters and their types. Add another variable to the function. This one should be for a mystack object. Now, handle the case of a left parenthesis by pushing the character onto the mystack object. 16. Compile and execute the code at the command line. Don’t forget to use input redirection! The output should now resemble: infix: 2 postfix: 2 infix: 2+a postfix: 2 a infix: (1+a) ^2 postfix: 1 a 2 infix: ~(1+a) ^2 postfix: 1 a 2 infix: ~((1+a)^2) postfix: 1 a 2 infix: 0/0 postfix: 0 0 infix: 1/0 postfix: 1 0 infix: 0/1 postfix: 0 1 infix: c^a postfix: c a infix: ~c^a postfix: c a infix: c^d postfix: c d infix: ~c^d postfix: c d infix: d+1 postfix: d 1 infix: d +1 *2 postfix: d 1 2 infix: ( d +1) *2 postfix: d 1 2 infix: a-e-a postfix: a e a infix: (a-e-a)/( ~d + 1) postfix: a e a d 1 infix: (a^2 + ~b ^ 2) * (5 - c) postfix: a 2 b 2 5 c infix: ~ 3 * ~( a + 1) - b / c ^ 2 postfix: 3 a 1 b c 2 infix: 246 + b /123 postfix: 246 b 123 infix: ( 246 + (( b /123) ) ) postfix: 246 b 123 infix: ( ( 246 + b) /123) postfix: 246 b 123 infix: a+b/c^d-~e*f postfix: a b c d e f 17. In the inpost.cpp file, add code to the convert function. Add another variable to the function. This one should be for a character that will hold the top value in the mystack object. Handle the case of a right parenthesis. Use the top method from the mystack class to get the character that is at the top of the mystack object. Make sure to save the value returned from the top method in the character variable that was declared above. Pop the mystack object to remove the top character. As long as the stack is not empty (use the empty method from the mystack class) and the character variable is not equal to a left parenthesis: o Determine if a space should be added to the postfix string. This can be done in the same way as in step 9. o Append the character variable to the postfix string o Get the top character from the mystack object and save it in the character variable o Pop the mystack object 18. Compile and execute the code at the command line. Don’t forget to use input redirection! The output should now resemble: infix: 2 postfix: 2 infix: 2+a postfix: 2 a infix: (1+a) ^2 postfix: 1 a 2 infix: ~(1+a) ^2 postfix: 1 a 2 infix: ~((1+a)^2) postfix: 1 a 2 infix: 0/0 postfix: 0 0 infix: 1/0 postfix: 1 0 infix: 0/1 postfix: 0 1 infix: c^a postfix: c a infix: ~c^a postfix: c a infix: c^d postfix: c d infix: ~c^d postfix: c d infix: d+1 postfix: d 1 infix: d +1 *2 postfix: d 1 2 infix: ( d +1) *2 postfix: d 1 2 infix: a-e-a postfix: a e a infix: (a-e-a)/( ~d + 1) postfix: a e a d 1 infix: (a^2 + ~b ^ 2) * (5 - c) postfix: a 2 b 2 5 c infix: ~ 3 * ~( a + 1) - b / c ^ 2 postfix: 3 a 1 b c 2 infix: 246 + b /123 postfix: 246 b 123 infix: ( 246 + (( b /123) ) ) postfix: 246 b 123 infix: ( ( 246 + b) /123) postfix: 246 b 123 infix: a+b/c^d-~e*f postfix: a b c d e f 19. In the inpost.cpp file, add code to the convert function. Handle the case of an operator. Use the function coded in step 4 to determine the precedence of the infix character. This value should be saved in an integer variable. As long as the stack is not empty and the precedence of the top character on the stack is greater than or equal to the precedence that is saved in the integer variable: o Determine if a space should be added to the postfix string. This can be done in the same way as in step 9. o Get the top character from the mystack object and save it in the character variable o Append the character variable to the postfix string o Pop the mystack object Push the infix character onto the mystack object. 20. Compile and execute the code at the command line. Don’t forget to use input redirection! Notice that some of the postfix strings are missing some of the operators. That’s okay right now. infix: 2 postfix: 2 infix: 2+a postfix: 2 a infix: (1+a) ^2 postfix: 1 a + 2 infix: ~(1+a) ^2 postfix: 1 a + ~ 2 infix: ~((1+a)^2) postfix: 1 a + 2 ^ infix: 0/0 postfix: 0 0 infix: 1/0 postfix: 1 0 infix: 0/1 postfix: 0 1 infix: c^a postfix: c a infix: ~c^a postfix: c ~ a infix: c^d postfix: c d infix: ~c^d postfix: c ~ d infix: d+1 postfix: d 1 infix: d +1 *2 postfix: d 1 2 infix: ( d +1) *2 postfix: d 1 + 2 infix: a-e-a postfix: a e - a infix: (a-e-a)/( ~d + 1) postfix: a e - a - d ~ 1 + infix: (a^2 + ~b ^ 2) * (5 - c) postfix: a 2 ^ b ~ 2 ^ + 5 c - infix: ~ 3 * ~( a + 1) - b / c ^ 2 postfix: 3 ~ a 1 + ~ * b c 2 infix: 246 + b /123 postfix: 246 b 123 infix: ( 246 + (( b /123) ) ) postfix: 246 b 123 / + infix: ( ( 246 + b) /123) postfix: 246 b + 123 / infix: a+b/c^d-~e*f postfix: a b c d ^ / + e ~ f 21. In the inpost.cpp file, add the last bit of code to the convert function. This code should be placed after the loop that processed the characters in the infix string. As long as the stack is not empty: o Determine if a space should be added to the postfix string. This can be done in the same way as in step 9. o Get the top character from the mystack object and save it in the character variable o Append the character variable to the postfix string o Pop the mystack object 22. Compile and execute the code at the command line. Don’t forget to use input redirection! The output should be correctly formatted now. infix: 2 postfix: 2 infix: 2+a postfix: 2 a + infix: (1+a) ^2 postfix: 1 a + 2 ^ infix: ~(1+a) ^2 postfix: 1 a + ~ 2 ^ infix: ~((1+a)^2) postfix: 1 a + 2 ^ ~ infix: 0/0 postfix: 0 0 / infix: 1/0 postfix: 1 0 / infix: 0/1 postfix: 0 1 / infix: c^a postfix: c a ^ infix: ~c^a postfix: c ~ a ^ infix: c^d postfix: c d ^ infix: ~c^d postfix: c ~ d ^ infix: d+1 postfix: d 1 + infix: d +1 *2 postfix: d 1 2 * + infix: ( d +1) *2 postfix: d 1 + 2 * infix: a-e-a postfix: a e - a - infix: (a-e-a)/( ~d + 1) postfix: a e - a - d ~ 1 + / infix: (a^2 + ~b ^ 2) * (5 - c) postfix: a 2 ^ b ~ 2 ^ + 5 c - * infix: ~ 3 * ~( a + 1) - b / c ^ 2 postfix: 3 ~ a 1 + ~ * b c 2 ^ / - infix: 246 + b /123 postfix: 246 b 123 / + infix: ( 246 + (( b /123) ) ) postfix: 246 b 123 / + infix: ( ( 246 + b) /123) postfix: 246 b + 123 / infix: a+b/c^d-~e*f postfix: a b c d ^ / + e ~ f * - 23. Professor McMahon included a link to the expected output as one of the files that was copied to your account by the setup command. The output from this file (postfix.key) can be electronically compared to the output from your program by executing the code and redirecting the output to an output file and then using the diff command to compare the two output files. ./inpost < infix.in > output.txt diff output.txt postfix.key You can use any name for the output file that you want (just make sure that it’s not the same as any of the files that already exist). If the output files are the same, the diff command will not produce any output. ">