Results 1 to 10 of 10

Thread: Calculator

  1. #1
    Guest

    Post

    Ok, im making a calculator, i have the whole interface set up. My problem is i have no idea what code to put behind the operator buttons.

  2. #2
    Lively Member
    Join Date
    Jan 1999
    Location
    Rochester NY, USA
    Posts
    93
    I'm not going to give you the code because half of doing something like this is learning... but here's the directoin you're going to want to go in.

    You can't do any mathimatical operation without having both numbers, correct? So...

    2+2.. you're adding 2 and 2.

    So what you're going to have to do is store what operator was pressed.

    Example..

    User presses 2... do nothing incase the user presses more numbers...

    User presses +... store the number entered.. store that the user wants to add..

    User presses 2... wait for more numbers..

    User presses any operator.. check to see if there's 2 stored numbers and an operator.. if so.. perform the math, store the result in the first var that saves numbers. Store what operator they pressed.


    Now you have to decide if you want it to do the following..

    What if they press -

    2
    +
    2
    +
    +
    +
    +

    do you want it to keep adding 2 to the total? Just some things to think about. Basically the operators are going to check what the last operator was .. if there was one.. and trigger an event that will perform that math.. and then store the new operator which will use the total as the first number.. except the =.. which will just perform the math. Hope this wasn't to confusing...

  3. #3
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    I've made a calc program before. What rchiav said is right
    but I must add: DON'T ADD A BRACKETS FUNCTION

    It just makes it way too complex.

    I tried to implement them, but I had to use a lot of code
    just to get 1 bracket level eg (14 + 23).

    Two bracket levels ((7*2) + 23) were too hard.


    Courgettes.

  4. #4
    Lively Member
    Join Date
    Jan 1999
    Location
    Rochester NY, USA
    Posts
    93
    Brackets can be a little difficult, but it can be done for an infinite amount of brackets if you do it right. What you need to do is count how deep the brackets get.. ex -

    ((5+2) * (7-2)) + (4 +10)

    Well you don't need all those brackets but this is only for explanation .. ok..

    create a var to count brackets.. I'll call it BracketCount.

    now as you go through the string.. each time you encounter a "(" , add one to BracketCount.. Each time you encounter a ")", subtract one from BracketCount. If BracketCount is ever less than 0, then there's an error in the brackets. There's also an error if it's not equal to 0 at the end. Now as you're going through , use another var to keep track of the highest bracket count. This will tell you how deep the brackets get.

    Now go through the statement again and until your BracketCount = the highest bracket count. Take the expression from that point till the next bracket and you do that first. Go through the rest of the string to see if there's any more statements that are nested that deep. Then do it for MAX_BRACKET_COUNT -1..

    The more difficult part is going to be keeping track of the whole expression.. what you can do is use Mid$() to pull off the characters in the expression until you find a "(" that is nested MAX_BRACKET_COUNT deep. Then parse out the expressoin in the brackets, append the value to the string and keep on using Mid$() to pull off characters. This is probably pretty confiusing.. and I bet it's a lot more text than it is code

  5. #5
    Lively Member
    Join Date
    Apr 2000
    Location
    Hell
    Posts
    89

    Lightbulb

    I'll give y'all a hint, somthing I remember from my AP Computer Programming class in high school. I don't remember any of the details because it was about 3 or 4 years ago, but it remember the concept quite clearly: you use reverse-polish notation and a stack. Maybe you can look around for this info.
    - Steve

    Real programmers use COPY CON PROGRAM.EXE

  6. #6
    Lively Member
    Join Date
    Jan 2000
    Posts
    123
    Hehe... parenthesis... I have a sub that can do them... its about 1 page long... if you want it I'll be more than glad to e-mail it to you...

    Ex... Ok so it cann't do everything the first time around... it takes out one of the (2+2) statements and the solving is up to you... for multiple layers just run it through several times solving for every step... it might need some work for the exact task you want it to do,...

    And now on a more "WOW" note... I cann't believe something that took me 2 hours to write and 8 hours to debug might yet be useful to someone...

  7. #7
    Member
    Join Date
    Feb 2000
    Location
    Toronto, Canada
    Posts
    44

    Use command button Array

    Here is a Tip.

    Make all your number command buttons as an array. Make sure the Index number matches the command buttom value. Example;

    Using cmdKeyPad

    '0' ---> cmdkeyPad(0)
    '1' ---> cmdkeyPad(1)
    .
    .
    .
    '9' ---> cmdkeyPad(9)

    Then use the following code for the button click event.

    Code:
    Private Sub cmdKeyPad_Click()
    
        Dim NumPressed As Integer
        
        NumPressed = Index
        txtDisplay.SelStart = Len(txtDisplay)
        txtDisplay.SelText = str(NumPressed)
    
    End Sub
    This will take care of all your number buttons. This is much better than writing the same code event for each different command button number.


    But you should use different command buttons for the operators, equals sign, and others. It is easy to decifer the command button names and function. Examples,

    cmdPlus
    cmdMinus
    cmdDivide
    cmdMultiply
    cmdEquals
    cmdClear
    cmdMemory



    Sacred_Knight
    Electronic Technologist
    E-Mail: [email protected]

    He who laughs last, laughs later.

  8. #8
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Make a stack calculator

    I like the previous Post about using an Array of Command Buttons for the Digits 0-9. This has to be the right start.

    For more than 20 years, I have used HP calculators which work with a LIFO Stack. You should try implementing Stack Operations instead of requiring Algebraic Notation as done by most hand calculators. No parentheses parsing is required.

    The idea is to have an Enter Key (It puts current value on top of the stack) and no Equals Key.

    All Two-Operand Operations work with the last two Values entered in the Stack, removing them and putting the result on top of the stack. If you implement any Single-Operand Operations (Square Root, Trig functions, et cetera), they replace the top value with the result.

    If you display the top 4-10 values to the user, he can keep track of what he is doing. Ideally, he should be able to scroll to see everything in the Stack. Everybody who has used my calculator likes it better than the classic type with Parentheses & an Equals Key. It takes about two minutes of practice to get used to it.

    One example of the convenience of a Stack calculator is the evaluation of Polynomials. To do this, you put the Variable into the Stack many times, using the Enter Key repeatedly. Next, Key in the Highest Order Coefficient and Multiply; Then the Next Coefficient followed by Add & Multiply. Keep Keying Coefficients with Add & Multiply until the Constant term (He gets added but not multiplied). The process is equivalent to viewing the Polynomial Evaluation as the following. This is the way you would code a computer to evaluate a Polynomial: It loops nicely and has numerical precision advantages over other methods.

    {[(A*X+B)*X+C]*X+D}*X + E

    E-Mail me if you want to discuss Stack-oriented Calculators in more detail.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  9. #9
    Lively Member
    Join Date
    Apr 2000
    Location
    Hell
    Posts
    89
    (A) Why Do You like to capitalize Random words in Your Sentences?

    (B) I had an HP all through high school. Anyone who forces their users to learn RPN should be shot.

    (C) When I made my previous post, what I was referring to is an algorithm that CONVERTS an algebraeic expression to a reverse-polish notation expression, such as

    "(1 + 2) * 6"

    was translated to

    1 2 + 6 *

    and "(1 + 5 ^ 6 * 2 * 3 - 1 ^ (0.5 * 4)) * (6 - 5)"

    to
    1 5 6 ^ 2 3 * * + + 1 0.5 4 * ^ - 6 5 - *

    (Now you know why I feel that anyone who forces people to learn RPN should be shot.)

    I don't remember the specifics of the algorithm, but it involves comparing the relative precedence of the various operators (i.e. "^" is evaluated before "*" and "/", which get evaluated before "+" and "-", and parentheses always go first) as per deciding whether to evaluate the operand or put it on the stack.
    - Steve

    Real programmers use COPY CON PROGRAM.EXE

  10. #10
    Guest

    :)

    this is your project so, I wont give you the entire source, but for my calc program I did...
    Code:
    Private Sub cmd1_Click()
    If txtBox = "0" Then
    txtBox = 1
    Else
    txtBox.Text = txtBox.Text & 1
    End If
    End Sub
    now for the operand
    Code:
    Private Sub cmdDivide_Click()
    Divide = txtBox
    txtBox = ""
    End Sub
    and for equals
    Code:
    Private Sub cmdEquals_Click()
    If Minus <> "" Then
    txtBox = Val(Minus) - Val(txtBox)
    ElseIf Plus <> "" Then
    txtBox = Val(Plus) + Val(txtBox)
    ElseIf Times <> "" Then
    txtBox = Val(Times) * Val(txtBox)
    ElseIf Divide <> "" Then
    txtBox = Val(Divide) / Val(txtBox)
    Else
    MsgBox "please push an operand(+,-,*, or /)", vbCritical, "Error"
    txtBox = ""
    End If
    End Sub
    txtBox is where you enter the numbers and stuff
    Plus
    Minus
    Divide
    and
    Times

    are the vairables to store the first value of the operator....
    after you click something like times, the textbox clears...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width