Page 1 of 2 12 LastLast
Results 1 to 16 of 17

Thread: Making a Calculator in Visual Basic

  1. #1
    Ah, Mrs. Peel! mike_w's Avatar
    Join Date
    Oct 2003
    Location
    Hertfordshire, England
    Posts
    3,326
    Thanks
    3
    Thanked
    9 times in 7 posts

    Making a Calculator in Visual Basic

    Right, at the moment I'm trying to make a very simple calculator. At the moment, I just have the four basic functions (+, -. *, /), a decimal point and a +/- sign. I'm trying to make it so that when you type it in, it behaves like a scientific calculator and stores the calculation until the end. For example:

    6 + 7 x 33 / 9 (just random stuff)

    Then work that out when you hit the equals button.

    I've got it saved as a private string at the moment. How do I make Visual Basic work out the solution to the calculation stored in the string?

    Thanks

    Mike
    "Well, there was your Uncle Tiberius who died wrapped in cabbage leaves but we assumed that was a freak accident."

  2. #2
    Senior Member
    Join Date
    Sep 2003
    Posts
    593
    Thanks
    0
    Thanked
    1 time in 1 post
    as far as i can remember a scientific calulator only remembers the final calcualtion and breaks the calulation down.

    i.e. it would do 6+7=15, then 15X33 = 815?(didn't use a calculator),then 815/9 = can't be bothered and cant be bothered would be you final answer. If you are doing it this way i'd have thought you would have wanted to save it as an int or a float as these can instanly have calculation put on them. Strings can be trouble some if you have non digit characters in them and then convert to digits so that it can be calculated. As far as i know you cant calculate strings, you have to convert.

    I have while typing this remembered what you are talking about though.

    What might be an option would be to hold what is inputed as a String but at the same time convert each part to a float and calculate as you go along. This would mean that you would have a string of the calculation inputed but also a float of the answer.

    Another option could be to tokenize(don't know if you can do this in visual basic), also if this is web based you can have difficulty implementing a lot of the possible answers.

    This probably wont help you in the least but i'm not the best programmer ever and most deffinately not in visual basic.

  3. #3
    Ah, Mrs. Peel! mike_w's Avatar
    Join Date
    Oct 2003
    Location
    Hertfordshire, England
    Posts
    3,326
    Thanks
    3
    Thanked
    9 times in 7 posts
    Quote Originally Posted by loki
    as far as i can remember a scientific calulator only remembers the final calcualtion and breaks the calulation down.
    I thought it followed BODMAS so it would do it like this:

    7x33=231

    231/9=25.6 recurring

    25.6+6=31.6 recurring

    Isn't that how a scientific calculator works? Or am I going crazy again?

    Mike
    "Well, there was your Uncle Tiberius who died wrapped in cabbage leaves but we assumed that was a freak accident."

  4. #4
    Rys
    Rys is offline
    Tiled
    Join Date
    Jul 2003
    Location
    Abbots Langley
    Posts
    1,479
    Thanks
    0
    Thanked
    2 times in 1 post
    You need to parse the string and evaluate it. String parsing is a pretty trivial problem to solve, to break it up into constituent parts. Then you need to evaluate those parts using operator precendence and intermediate output stores.

    Try and develop the algorithm on your own, it's good programming practice. Look at the string and see how it's built....

    Rys
    MOLLY AND POPPY!

  5. #5
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS
    does vb have any built-in string tokenization abilities?

    i'm used to the java route

  6. #6
    Rys
    Rys is offline
    Tiled
    Join Date
    Jul 2003
    Location
    Abbots Langley
    Posts
    1,479
    Thanks
    0
    Thanked
    2 times in 1 post
    Nope!

    Rys
    MOLLY AND POPPY!

  7. #7
    Ah, Mrs. Peel! mike_w's Avatar
    Join Date
    Oct 2003
    Location
    Hertfordshire, England
    Posts
    3,326
    Thanks
    3
    Thanked
    9 times in 7 posts
    Sorted it now, thanks for the help! I just cut it up so that each number/function is stored in a separate variable, and worked each little bit out bit by bit.
    "Well, there was your Uncle Tiberius who died wrapped in cabbage leaves but we assumed that was a freak accident."

  8. #8
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS
    yeah, that's what a string tokenizer does.

    in java, any string can be accessed with one, it's a handy feature.

    Code:
    String Stringles = new String("The rain in spain");
    StringTokenizer tokemon = new StringTokenizer(Stringles);
    tokemon.countTokens() will give 4, StringTokenizer.nextString() returns "The" the first time it's called, then "rain", etc

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    1
    Thanks
    0
    Thanked
    0 times in 0 posts

    Smile Re: Making a Calculator in Visual Basic

    Quote Originally Posted by mike_w View Post
    Right, at the moment I'm trying to make a very simple calculator. At the moment, I just have the four basic functions (+, -. *, /), a decimal point and a +/- sign. I'm trying to make it so that when you type it in, it behaves like a scientific calculator and stores the calculation until the end. For example:

    6 + 7 x 33 / 9 (just random stuff)

    Then work that out when you hit the equals button.

    I've got it saved as a private string at the moment. How do I make Visual Basic work out the solution to the calculation stored in the string?

    Thanks

    Mike

    Hi there Mike
    I am an Electronic Engineering college undergrad. I am doing an introductory course to visual basic. We are required to write code for a Simple Calculator, with basic functions, like + - / and *, and maybe sqrt, and sin cos and tan would be great. Would you be kind enough as to post up the code which you produced when u wrote your calculator program. I would really appreciate it, as I am new in the world of programming.

    Thanks!

  10. #10
    Member
    Join Date
    Mar 2008
    Location
    Scotland
    Posts
    172
    Thanks
    7
    Thanked
    3 times in 3 posts
    • tom18230's system
      • Motherboard:
      • Abit Dark Raider IP-35
      • CPU:
      • Intel Q6600 2.7gHz
      • Memory:
      • OCZ DDR2 4GB (2 x 2GB)
      • Storage:
      • Western Digital 500GB 7500RPM
      • Graphics card(s):
      • XFX 640mb 8800GTS
      • PSU:
      • CorsaiR HX520W
      • Case:
      • Antec 900
      • Operating System:
      • Vista Home Premium 64bit
      • Monitor(s):
      • LG 22LS4D
      • Internet:
      • Virgin Media 2mb

    Re: Making a Calculator in Visual Basic

    I did this in school a few months ago in visual basics. We spent about 5 periods trying to figure out how to store calculations like a scientific calculator does..but we couldn't. I'll try what was suggested and see if I can add it to the code =D.
    I could take on 28 five year old kids in a fight.
    I could name 55 countries in 5 minutes.
    My body makes a 58% effective human shield.
    My dead body is worth £5750.
    I have a 41% chance of surviving a zombie apocalypse.
    Im 40% geek.

  11. #11
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: Making a Calculator in Visual Basic

    if your cheating the simple assignments, you'll have no hope.

    ask the lecturer for more help if your struggling at this level.
    throw new ArgumentException (String, String, Exception)

  12. Received thanks from:

    directhex (09-04-2008)

  13. #12
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts

    Re: Making a Calculator in Visual Basic

    I agree with TA for a change.
    To err is human. To really foul things up ... you need a computer.

  14. #13
    Registered+
    Join Date
    Feb 2008
    Location
    Norwich
    Posts
    78
    Thanks
    5
    Thanked
    6 times in 6 posts
    • Stevo3000's system
      • Motherboard:
      • Asus P5K Premium Balck Pearl Edition
      • CPU:
      • Intel Core 2 Duo E8500
      • Memory:
      • 4x OCZ Platinum 1024Mb 800MHz
      • Storage:
      • WD Raptor 150Gb, WD AAKS 750Gb
      • Graphics card(s):
      • EVGA 8800 GTS KO 512Mb
      • PSU:
      • Coolermaster RealPower 620W
      • Case:
      • Coolermaster RC-1000 Cosmos
      • Monitor(s):
      • Samsung SM226CW 22"
      • Internet:
      • 2MBit Broadband

    Re: Making a Calculator in Visual Basic

    Why not design it OO?

  15. #14
    Ah, Mrs. Peel! mike_w's Avatar
    Join Date
    Oct 2003
    Location
    Hertfordshire, England
    Posts
    3,326
    Thanks
    3
    Thanked
    9 times in 7 posts

    Re: Making a Calculator in Visual Basic

    Quote Originally Posted by yuveer View Post
    Hi there Mike
    I am an Electronic Engineering college undergrad. I am doing an introductory course to visual basic. We are required to write code for a Simple Calculator, with basic functions, like + - / and *, and maybe sqrt, and sin cos and tan would be great. Would you be kind enough as to post up the code which you produced when u wrote your calculator program. I would really appreciate it, as I am new in the world of programming.
    Firstly, as others have said, I think you're probably better off playing around with this yourself - just start with the basics (+, -, /, *), and build it up from there.

    Secondly, I don't think I have the code any more anyway - I haven't touched Visual Basic in years (with no regrets!)
    "Well, there was your Uncle Tiberius who died wrapped in cabbage leaves but we assumed that was a freak accident."

  16. #15
    Member
    Join Date
    Sep 2003
    Posts
    95
    Thanks
    0
    Thanked
    1 time in 1 post

    Re: Making a Calculator in Visual Basic

    is this VB classic or VB.net if the latter Sting.Split() is your friend

  17. #16
    Technojunkie
    Join Date
    May 2004
    Location
    Up North
    Posts
    2,580
    Thanks
    239
    Thanked
    213 times in 138 posts

    Re: Making a Calculator in Visual Basic

    I you want to cheat, you could do worse than browse PlanetSourceCode.com,
    lots of good code to learn from on there.

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Making a Basic App for windows, eg: an mp3 player
    By MurphmanL in forum Software
    Replies: 6
    Last Post: 16-05-2004, 08:53 PM
  2. Visual Basic Help! Web Browser.....
    By DeludedGuy in forum Software
    Replies: 3
    Last Post: 07-05-2004, 07:32 PM
  3. Visual Basic Noob Alert!!
    By MurphmanL in forum Software
    Replies: 2
    Last Post: 04-04-2004, 05:24 PM

Posting Permissions

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