Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a simple calculator that i just started making, I have the 0-9 buttons along with add, substract, multiply and divide.

 

For each of those buttons, when clicked i have it add whatever it does to a textbox.. so i end up with a textbox that would look something like this:

 

100+25*2

 

 

But then for my equals button, i dunno how to have the Textbox1.Text calculated and then placed back in the textbox..

  • Leaders
Posted

Depending on what you want to implement, this could eventually turn into an extensively large project (I'm still working on it)... if you are doing something that could be evaluated by VB, then you can look at the MS Scripting Control... not sure how this works in .NET.

:)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
i just want it to take whatever is in my textbox (like in my example, "100+25*2") and then calculate whats in it by clearing the textbox and putting what it equals
  • Leaders
Posted

Are you expecting a simpler answer?

Look at the MS Scripting Control. I don't really know how it works, but I know that some people use it for evaluating expressions such as 100+25*2 in a string:

 

msscriptcontrl.Eval("100+25*2") something... :).

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • Leaders
Posted

If txtDisplay.Text equals the text "100+25*2" then you just assign the string to the variable and then put the same thing back into the textbox.

 

Dim CurrentDisplay As String

CurrentDisplay = txtDisplay.Text 'CurrentDisplay now contains the string. "100+25*2"

txtDisplay.Text = CurrentDisplay 'The textbox holds the string "100+25*2"

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • Leaders
Posted
No, you cannot do that. Strings are not executable code. Look at the MS Script Control.

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
i was searching through the forums and found something about being able to convert strings to numbers.. so couldn't i just convert the string to something useable?
  • Leaders
Posted
You need to understand the difference between a number (i.e. 1.423412) and an expression (5+20*100). Yes, you can convert numbers to strings and vice-versa. You can not convert expressions in the form of strings into numbers with any sort of casting. You will find yourself dealing with an invalid-cast exception if you try this. The .Net framework does not have a calculator class built into it (although this would be an exceptionally useful class). You can write your own (not easy for someone new to vb) or search google for a solutions. (Or persue the msscript control solution suggested. A google search will be sure to turn up the code and explination. Search for ["MS Script Control" evaluate expression]).
[sIGPIC]e[/sIGPIC]
Posted

Is this for a school project or something?

 

Anyway, instead of using the informaiton in the textbox you could store the data in a stack (or move it to a stack when they pess = ). There are certian rules that define the process you would use to preserve order of operations using a stack.

 

You would have to do some research to make sure that was feasible...I seem to recall that method working for reverse polish notaition, but I don't recall whether it works for normal math notation.

 

Putting something like this in place I think would be crucial for any calcutator program. You may even be able to find an implimentation for this out there already.

 

This script control also sounds pretty neat. I'll have to check that out a little more in depth. I personally would take the raw computer science route only becuase I am more familiar with it.

  • Leaders
Posted

I did this in VB6 once. The algorithm went like this...

 




Function EvaluateExpression() As Double
Result = EvaluateNumber()
If next character = "+" then
Result += EvaluateTerm()
If next character = "-" then
Result -= EvaluateTerm()
End Function

Function EvaluateTerm() As Double
Result = EvaluateExponent()
If next character = "*" then
Result *= EvaluateExponent()
If next character = "/" then
Result /= EvaluateExponent()
End Function
Function EvaluateExponent() As Double
Result = ParseDouble()
If next character = "^" then
Result ^= ParseDouble()
End Function

Function ParseDouble() As Double
Seek through the string, extract the next number, and Integer.Parse it
Return what we parsed
OR if we run into an opening parenthesis "(" then we jump back in to EvaluateExpression
Voila! Parentheses resolved through recursion
End Function
[/Code]

 

[Edit]This doesn't involve a stack and the order of operations will happen like magic. Plus it will handle parentheses. You just need to keep a "pointer" somewhere in the class that keeps track of what character you are looking at as you seek through the string.[/Edit]

 

btw, something I am working on now... a class that accepts an algebraic expression (containing variables) and instead of evaluating the expression immediately it breaks it down into a list of simple tokenized commands (think p-code... vb6) only with real simple commands like byte code (think msil)... as in "put this number on the stack" and "add the top two numbers on the stack and store the result on the stack". This can process batches of numbers more quickly for the sake of graphing or processing tables of numbers for statistical or scientific purposes.

[sIGPIC]e[/sIGPIC]
Posted
would it be alot easier if i just made it like the windows calculator? where you put in a number, click if you want to +, -, *, or / then enter the next number..?
  • Leaders
Posted
yes.. provided that the situation for which he is making the program allows him to do it that way. what if he wants to enter an expression and apply the order of operations. If you open the windows calculator and enter [3] [+] [4] [*] [2] you get 14. If you ask ms script what "3+4*2" equals you will get 24. Granted you can sort the rules of operation out for yourself with a simple calculator but its much easier for the user if they can type the expression in as-is and use their brain as little as possible.
[sIGPIC]e[/sIGPIC]
Posted

notice that when you enter numbers into the standard calculator it evaluates the 3+4 once you press the *. In the scientific one, 3+4 is not calculated until after the 4*3 is evaluated. the 4 stays on the screen.

 

Standard --> 3+4* = 7*

Scientific --> 3+4*2 = 3+8 = 11

 

I guess it depends on what Lanc's requiremenst are as to wether he should use a simple variable behind the scenes to calcuate as you go or wether something a little heavier (be it an ms script class or a stack or something else) is needed to preserve order of operations.

 

So which is it Lanc? Order of operations or no?

  • *Experts*
Posted

I think Lanc is just trying to create a simple game with some complex logic and needs some help. He doesn't have any requirements except what he's making up himself. He does seem to want/need to evaluate the expression, including operator precedence since his first sample was "100+25*2" which should be 150 (if you multiply then add), not 250 (if you add then multiply).

 

@Lanc: Personally, I admire you for trying something rather difficult for a project. If you want some advice, I would create a few more small test apps to test out code you're writing, such as a function that takes a string and returns the decimal return value. Once you have that working in a test project, move that code to your real game code.

 

As for how to solve this particular issue... it's probably time you picked up an algorithms book and maybe a good .NET book.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...