Академический Документы
Профессиональный Документы
Культура Документы
Introduction
The purpose of this lab was to review the stack, queue, and linked list data structures by implementing
them and utilizing them to solve practical problems such as arithmetic evaluation and element
counting. Although seeming as completely rudimentary at first glance, these data structures serve as the
building blocks of many contemporary algorithms that allow programmers to solve modern problems,
like complex graph-theory problems such as network mapping, with surprising ease.
In order to parse a correct-syntax arithmetic expression, the programmer must convert it into its
post-fix notation to properly evaluate it. Since we are, indeed, reading numbers with more than
one digit, we will use a queue that holds objects that can represent either a number
(Double)or an operator (Character). Such approach is possible due to the dynamic
versatility of an object-oriented programming language – namely the usage of the data type
Object. The algorithm converts the expression by pushing and popping the characters
according to their precedence whilst appending them to a queue that contains the numbers read.
The algorithm evaluates the expression by using a queue that helps it read the post-fix
expression regardless of how long it may be.
However, there is a drawback to this approach. Since the algorithm depends on reading a digit
in order to begin assuming it might be a number, the instance of typing .5 as the representation
of one-half will be parsed incorrectly, consequently ending with an error. To correctly represent
numbers below 1, the user must provide a leading 0 to denote the entity as a number, i.e. 0.5 for
last example. Similarly, the same thing goes for whole numbers that are interpreted with
insignificant zeros past the decimal. If, for instance, the algorithm reads a 2.0 in the
expression , it does not account for the extra 2 characters. To fix it, the user must omit the zero;
in other words, 2.0 must be typed as 2. Nevertheless, both issues could have easily been
resolved by using a specialized StringTokenizer method that also returns the delimiter as
a token. Such approach would have allowed us to rid ourselves from keeping track of where in
the expression we were reading at and allow us to focus more on the content.
The entire application is structured in three packages. Labone package contains methods that read
inputs whilst handling wrong input. LabTwo package contains the source files that wrap the logic for
the application, such as input reading and execution. Labtwo.structures package contains all the
structures utilized in lab 2. The main source file Main.java wraps both parts in an easy-to-follow code
that displays a simple menu.
Experimental Results
{0,4}
{0,5}
{0,4,5}
{0}
{1,2,3,6,7,8,9}
{(0,0),(4,0)}
1 The set { 1 2 3 5 6 7 8 9 } is simply the compliment of D and should be omitted when comparing the anticipated results
with the actual results.
Arithmetic Parsing and Evaluation
If the restrictions described in the previous section are followed, the algorithm correctly evaluates
arithmetic expressions including decimals and powers:
Addition
Subtraction
Multiplication
Division
Exponential
Parenthesis
Table 2.2