Results 1 to 26 of 26

Thread: Calculate string

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    The Netherlands
    Posts
    403

    Question Calculate string

    Does anyone now a function that can calculate a string like this

    Ans = Calc("3*2+6^1-6/69.2*(6+4^(-1))")

    I can write it myself I think, but It'll be a whole lot of work so if anyone nows of such a function, has it or even wrote it hisself, can you please give it to me.
    There are 10 types of people, those who understand binary and those who don't

    http://merlijn.beyonix.net

  2. #2
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    This code uses reverse polish notation (ie 2 2 + instead of 2 + 2) but you can write a converter that converts 2+2 to 2 2 + pretty easy.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  3. #3
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Add a reference to Microsoft Script Control object library and do something like this:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim objSC As New MSScriptControl.ScriptControl
    3.    
    4.     objSC.Language = "javascript"
    5.    
    6.     MsgBox objSC.Eval("3*2+6^1-6/69.2*(6+4^(-1))")
    7. End Sub

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    Do a search on this.

    I believe you can use the MS Scripting control and proccess it properly as a string.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by Serge
    Add a reference to Microsoft Script Control object library and do something like this:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim objSC As New MSScriptControl.ScriptControl
    3.    
    4.     objSC.Language = "javascript"
    5.    
    6.     MsgBox objSC.Eval("3*2+6^1-6/69.2*(6+4^(-1))")
    7. End Sub
    Wow!! That's why I call a solution. Thanks for that tip!
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    The Netherlands
    Posts
    403
    Yes guys, thanks
    There are 10 types of people, those who understand binary and those who don't

    http://merlijn.beyonix.net

  7. #7
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Er...

    Indeed it is a great solution... But why does it tell me the User Type is not defined?

    What am I doing wrong?
    If it works, will it be able to calcullate Logarithm, Sine, Cosine, etc.?

    Please help me!
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  8. #8
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Er...

    Originally posted by Tec-Nico
    Indeed it is a great solution... But why does it tell me the User Type is not defined?

    What am I doing wrong?
    If it works, will it be able to calcullate Logarithm, Sine, Cosine, etc.?

    Please help me!
    What's the code you're using?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  9. #9
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    I am using the same code Serge posted... Do I need something else?
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  10. #10
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Add a reference to Microsoft Script Control object library and do something like this
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  11. #11
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Talking I guess...

    I supposed that was the problem... But how can I add that reference?
    Which is the code to do it?

    Please help me, my brain is just not working in this moment...
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  12. #12
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615

    Re: Er...

    Originally posted by Tec-Nico
    Indeed it is a great solution... But why does it tell me the User Type is not defined?

    What am I doing wrong?
    If it works, will it be able to calcullate Logarithm, Sine, Cosine, etc.?

    Please help me!
    Add a reference to Microsoft Script Control 1.0 object library

  13. #13
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Click on Project -> References.... look for the one that was said and select it. Click on Ok to finish.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  14. #14
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    Guys... Are you meaning I have to go to "References" in the proyect of VB and add it as if it was just like a control?

    Please be patient with me since my computer has not installed VB now due to a problem, so I am going back and forth from my computer to one that has VB in the computer center.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  15. #15
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Exactly... the difference is that you're not adding a control but a "reference" to the library. You're telling VB: "Hey, use this function as well!".
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  16. #16
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Talking Thanks!

    Oops.. Thank you guys, now it is working...
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  17. #17
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    You're welcome.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  18. #18
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    But...

    Just another doubt... Does it support sine, cosine, tangent and all that kind of mathematical functions?
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  19. #19
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I don't know.... if you read the code you can realize that it's Java. If those functions are available in this language, you should write it that way.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  20. #20
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Thanks!

    Thank you again... I am going to check it when I get home.

    I will install VB and check using the java format for these functions... If I get struck I will be back here...

    Heh, heh, heh... Thanks once again and see you later!
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  21. #21
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Ok... but I don't know "much" Java.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  22. #22
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    PSSST!!!!

    The script control also supports VBScript.....
    might be easier to work with.....
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  23. #23
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I thought so... but never tried it, though.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  24. #24
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918
    I just tried the code using objSC.Language = "vbscript" and for the example equation:
    "3*2+6^1-6/69.2*(6+4^(-1))"
    I get the answer as 11.4580924855491

    However if I use objSC.Language = "javascript" I get 13 as the answer.

    Also if I type the equation into VB's immediate window I get 11.4580924855491

    I'm too lazy (and brain dead at the moment) to try and figure it out on the old calculator. Which one's correct?

  25. #25
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    11.4580924855491
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  26. #26
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    Thanks for the tip, techgnome... Now it is working perfectly.

    Look guys, I am programming a Numeric Method evaluator. This means the user can write down a complex mathematical function and then let the program get the results by using methods as Newton Rapshon. Now I can evaluate the function, but I need help to know when the user has typed a function that cannot be evaluated by the MSScript....

    How can I tell if the string given can be evaluated or not?
    Anysuggestions?


    P.S. Use VBScript and not JavaScript, it is more effective and has less margin of error. Yet if you need Pi you must declare it in a constant in VBScript while JavaScript can automatically give it to you.
    Last edited by Tec-Nico; Oct 12th, 2002 at 04:39 PM.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

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