![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2007
Posts: 5
Rep Power: 0
![]() |
help please calculator
Hello Everyone,
I need some help on some HW. I normally don't ask for help about hw but this assignment pretty much determines my grade. Im not asking for you guys to do it for me but I just need a little direction please. My teacher wants me to do the following: Design and build a parsing calculator. My program must accept a string of the form 12 * 3 / (123 + 4) , do the specified calculation, and display the result. The program should continue to ask for input until the user decides to quit. In addition, this program must use a stack to implement the calculation. You’ll have to accept the input as a character string, break it up into its component parts, and convert it to numbers and operators, and do the calculation.Notes: 1. Instead of the usual precedence, evaluate expressions from left to right, but make sure that operations done inside parentheses are done first. For example, the expression 22 * 3 + ( 2 * 3) , should be evaluated: 22 * 3 + ( 6) 66 + 6 72 2. Look up and use the following standard library functions to help with your design: char* strtok(char*, char*) double atof(char*) Please I need some direction ! Kumar |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: help please calculator
That's a pretty detailed set of instructions. Have you been paying attention in class?
See input functions such as fgets, which you would couple with the required strtok. It seems that atof is a requirement, so use that, but be aware that things like strtol should be used in actual practice. Once you have looked up those functions, go through your problem line by line, with pencil and paper, design your solution, code it, and ask here for help with the problems.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2007
Posts: 1
Rep Power: 0
![]() |
Re: help please calculator
I would try creating a recursive class. Send the string to the class. In the class, parse the string from left to right and push each number and operator onto the stack. When you come to a parentheses, scan the string until you find the last closing parentheses and call the class again (from within the class, this is the recursive part) with everything between the opening and closing parentheses (the return value will be a number). Then, pop each item and operator one at a time from the stack performing the operations as you go. When the stack is empty, return the number.
-Mike Last edited by mjbeam; Nov 20th, 2007 at 7:40 PM. Reason: Oops, changed reiterative to recursive |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: help please calculator
I'm thinking that you aren't ready for recursion. If you were, this post would probably not have been made.
I could be wrong. For an example of recursion, see here: recursion.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4
![]() |
Re: help please calculator
DaWei: hehe
I did this exact thing in O'Caml. http://www.programmingforums.org/thread13332.html I wouldn't expect you to understand the code (O'Caml is a wacky language), but because there is pattern-matching, it might be kind-of readable. Specifically, I think this was the latest version: http://www.programmingforums.org/att...9&d=1182313872 eval.ml, the file where I actually evaluated the tokens, might be useful. I should warn you though: I'm a beginner and I might not have done things in an optimal way. ![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help with Calculator | MiKuS | JavaScript and Client-Side Browser Scripting | 3 | Sep 23rd, 2007 6:46 PM |
| calculator | TecBrain | Java | 2 | Dec 28th, 2005 1:21 PM |
| Calculator Multiple Operations? | jl13 | C# | 5 | Oct 12th, 2005 4:32 PM |
| Javascript calculator | narroweyes | JavaScript and Client-Side Browser Scripting | 7 | Jul 30th, 2005 12:46 PM |
| simple calculator | Jessehk | Show Off Your Open Source Projects | 4 | May 25th, 2005 4:39 PM |