



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: DATA STR ALG GEN PRO; Subject: COMPUTER PROGRAMMING; University: Florida State University; Term: Fall 2006;
Typology: Assignments
1 / 6
This page cannot be seen from the preview
Don't miss anything!




A Stack is a type of data container/ data structure that implements the LAST-IN-FIRST- OUT (LIFO) strategy for inserting and recovering data. This is a very useful strategy, related to many types of natural programming tasks. For instance:
The interface of GenericStack is specified below. It provides for the following public functionality, all of which can be efficiently achieved using an internal array representation for the data. GenericStack(): no-argument constructor. Initializes the Stack to some pre-specified capacity. ~GenericStack (): destructor. GenericStack (const GenericStack
(5 + 3) * 12 - 7 is an infix arithmetic expression that evaluates to 89 5 + 3 * 12 – 7 is an infix arithmetic expression that evaluates to 34 For the sake of comparison, postfix arithmetic expressions (also known as reverse Polish notation) equivalent to the above examples are: 5 3 + 12 * 7 – 5 3 12 * + 7 – Two characteristics of the Postfix notation are (1) any operator, such as ‘+’ or ‘/’ is applied to the two prior operand values, and (2) it does not require ever the use of parenthesis. More examples: a + b1 * c + ( dd * e + f ) * G in Infix notation becomes a b1 c * + dd e * f + G * + in Postfix notation To implement Infix to Postfix conversion with a stack, one parses the expression as sequence of space-separated strings. When an operand (i.e., an alphanumeric string) is read in the input, it is immediately output. Operators (i.e., ‘-‘, ‘*’) may have to be saved by placement in the operator_stack. We also stack left parentheses. Start with an initially empty operator_stack. Follow these 4 rules for processing operators/parentheses:
After converting a given expression in Infix notation to Postfix notation, you will evaluate the resulting arithmetic expression IF all the operands are numeric (int, float, etc.) values. Otherwise, if the resulting Infix expression contains characters, your output should be equal to the input. Example inputs: 5 3 + 12 * 7 – 5 3 12 * + 7 – 3 5 * c – 10 / Example outputs: 89 34 3 5 * c – 10 / To achieve this, you will have an operand_stack, initially empty. Assume that the expression contains only numeric operands (no variable names). Operands are pushed into the stack as they are ready from the input. When an operator is read from the input, remove the two values on the top of the stack, apply the operator to them, and push the result onto the stack. If an operator is read and the stack has fewer than two elements in it, report an error. If end of input is reached and the stack has more than one operand left in it, report an error. If end of input is reached and the stack has exactly one operand in it, print that as the final result, or 0 if the stack is empty. For more information on the evaluation of Infix notation arithmetic expressions, look up section 3.6 of the textbook. We provide scripts with example input and output values. Summarizing task 2. Your program should expect as input from (possibly re-directed) stdin a series of space-separated strings. If you read a1 (no space) this is the name of the variable a and not ‘a’ followed by ‘ 1 ’. Similarly, if you read ‘bb 12’, this is a variable ‘bb’ followed by the number ‘ 12 ’ and not ‘b’ ,‘b’, ‘ 12 ’ or ‘bb’, ‘ 1 ’ ,‘ 2 ’. Your program should convert all Infix expressions to Postfix expressions, including expressions that contain variable names. The resulting Postfix expression should be printed to stdout. Your program should evaluate the computed Postfix expressions that contain only numeric operands, using the above algorithm, and print the results to stdout.