• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stack Assignment  RSS feed

 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I've beginning a new assignment and would like some feedback on my outline for the program. All of this just seems like overkill to me and I'm a little confused as to how I can implement everything. Hopefully this will make sense...I know its probably vague.

Assignment: You are to design a simple calculator using the stack data structure to perform additions, subtractions, multiplications and divisions. The user may enter an arithmetic expression in infix using numbers, parentheses and arithmetic operations (+, -, *, /). After an expression is entered, its postfix or prefix notation is displayed and then the result of the expression. You are required to design your own infix to postfix and infix to prefix conversions.

The professors example includes: SinglyLinkedList implementation, a Stack that implements Stack interface and extends SinglyLinkedList, a Node class, and StackApp -- in addition to the main class and the prefix/post-fix classes.
(The equation is entered/stored as a string variable)


(1.) Get the user input
- evaluate the user input and use a stack to determine the correct prefix/post-fix notation.
- from the stack, pop each item: a. account for '(' and ')' to push all operators and write all operands until ')', then pop/write
b. store new equation into a list (in prefix/postfix notation)

(2.) Display the list in both notations to the user.

(3.) Perform the calculation
Do you think it will be alright to calculate the original equation that was entered? It seems too much to then design a class that will then cycle through the polish notation and calculate according to that format.

Thanks for any help!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Nickerson wrote: All of this just seems like overkill to me and I'm a little confused as to how I can implement everything.


Not sure what you mean by overkill -- as infix to prefix/postfix conversion, and expression calculation are standard homework assignments.

Henry
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Derek Nickerson wrote: All of this just seems like overkill to me and I'm a little confused as to how I can implement everything.


Not sure what you mean by overkill -- as infix to prefix/postfix conversion, and expression calculation are standard homework assignments.

Henry


I was talking about the implementation of all the different classes -- not about the topic of the assignment itself.
List Interface, LinkedList implementation, Stack interface, Stack implementation that extends LinkedList, Stack app, and a Node class --- that is what I'm referring to as overkill.

I guess my main question is, which class will I define a method that will evaluate the infix equation?
 
Paul Clapham
Sheriff
Posts: 22831
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overkill for this one application, perhaps. You could write it with fewer components, I expect. But you're in a situation where you're supposed to be learning things, right? So this assignment is intended to teach you something.

And if you wanted to write this application, a stack would be a useful tool. So why not build one? (Since you're learning things, that is. Otherwise don't build one, use one which somebody already wrote.) By building a stack you're learning something, too.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Overkill for this one application, perhaps. You could write it with fewer components, I expect. But you're in a situation where you're supposed to be learning things, right? So this assignment is intended to teach you something.

And if you wanted to write this application, a stack would be a useful tool. So why not build one? (Since you're learning things, that is. Otherwise don't build one, use one which somebody already wrote.) By building a stack you're learning something, too.


Yes, I see why the stack is good way of accomplishing the purpose of the program. What I'm a little unsure of is how I am supposed to implement the linkedlist? I don't see the relationship between the two -- the stack and linkedlist.

Initially, I was going to use an array to hold the equation and evaluate each character and push the operators to a stack, according to precedence, and write the operands. A similar method to how it was presented to us in class.
 
Paul Clapham
Sheriff
Posts: 22831
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right that the linked list is the basis for the whole assignment. You didn't do a linked list as an earlier assignment, by any chance? I would have thought that's how your teacher would have arranged things -- but then teaching definitely isn't one of my best skills.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With this method style, I want to evaluate the array of characters and use a stack to transform the infix equation to a post/prefix equation. My push() method will addToFront of a linkedlist and pop() will get the data from the list. My question is, should I use 2 stacks, one to hold the operators and another to hold the operands? Is there a better method to do this?

For example: (8 * 9) + 4 / 6
operand stack = stack1
operator stack = stack2

'(' push to stack2
8 push to stack1
* push to stack2
9 push to stack1
')' pop stack1,then pop stack2 --- result = 8 9*

+ push stack2
4 push to stack1
/ push stack 2
6 push to stack1
check if end of string, result = 46+/



Thanks for your help

-------UPDATE--------
Question: In building a displayStack() method within the stack class, I want to find out if the generic type in the node is = '(', and if it is I want to break from the method.
How can I avoid this error and still accomplish same thing? I seem to keep running into this same type of issue. I thought that it being a generic, it was flexible to different data types?

error: "Incompatible operand types T and char"

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!