Calculating Variables In Visual Basic

October 7, 2012 at 14:33:21
Specs: Windows XP, 2GHz/2.00 GB
I need help with making a calculator in VB.
The calculator solves problems like this:
5x +7 - 3 = 66 - 90 * -5
Instead of the usual x + y = z, I am trying to make it like a scale X = Y.
I don't know the code that will "solve" each part for itself.
This is my layout:
2 text-boxes (each text-box is 1 side of the 2-sided equation)
1 combo-box (contains: =,<,>,=>,<=,<>)
1 label (displays the answer)
1 button (enter button)
What I want it to do is to take everything in textbox1 and solve it and the same for the other text-box and then I want it to single out the variable "X" and put it on 1 side and the "answer" on the other side; so if my problem was "5x +7 - 3 = 66 - 90 * -5" then it would end up looking like this: "x = -6.6" on the label or whatever the answer of what x is equal to is. Also if they aren't equal, I would like that it says x > -6.6 or x < or <> or whatever the inequality is. Thanks!!!

Please let me know what to add into the function of the button in my case so it works!
Thank you!

See More: Calculating Variables In Visual Basic

Report •

October 7, 2012 at 20:13:47
ah, an algebraic calculator. I thought about trying to make one, when I was suffering through chemistry in college. First, you need to separate out the "5x", because there is no way to calculate a value from "5x+7-3". The separation is not an algeraic separation, but just string manipulation. Then run "eval" on whatever remains (in this case: leftside=eval("+7-3") ). Then take that result and reverse the sign. Then run eval on the right side but include the negated leftside result (-4):
rightside=eval("66 - 90 * -5" & str(leftside) which gives your "key" number: 512
Next, you reverse-engineer the multiplication by taking the "5" off the "x", reversing the multiplication to become division, and apply that to the "key" number: answer=eval(key & "/5"). This is a vastly simplified example, not addressing parenthetical heirarchy overrides and thousands of other headaches.
(The great thing about the eval function is it will work on numbers as well as strings and WILL respect parenthetic priority.)
Start VERY simple, and work up, worry about style-points AFTER you got the machinery working. (at least, that's what SHE said, Lol!)
I will leave the cosmetics up to you regarding the textboxes and such.

Report •

October 10, 2012 at 13:56:01
I was wondering about a few of the steps you told me to do...

How do I perform string manipulation in this case? I read some articles online that discuss Len, Right, Left, Mid, Trim, Ltrim, Rtrim, Ucase, Lcase, Instr, Val, Str ,Chr and Asc, but I don't know how to use it in my case. Also, the user may put the "5x" anywhere in the equation at random ((On the left side) or they can also have the same variable on the right side) and these functions require it to be in a certain line or spot. Isn't there a way that it can perform a search through the textbox to see if any # is connected to a variable or has a "x" (multiplying sign)between the 2 things? Since the user would maybe write: "5x" or 5 (x) x" or "5 * x"
(the (x) means multiplying it together).

In the second step...
How do I run an "eval"? (It isn't declared)

In the 3rd...
How do I reverse the symbols (+ -)?

And in the other steps I kinda got lost on what you meant... (4,5 and 6)

: - (

Do you think that you can explain it in a more simple way? (I'm confused)
Or maybe just write out the code that I would use in my project?
Thanks a whole lot for your effort and time.

Do you know the website of MyAlgebra?
Its at:
I want my calculator to act just like that one.
Mainly the "Enter Problem" Part, and it would be nice if I had the "Enter a Topic" part too.

Report •

October 10, 2012 at 20:17:30
Ok, first, 'eval' is a visbasic intrinsic function that returns the arithmetic result of the data submitted. f/e: x=eval(5*86+2^3) yields: x=438
yields: x=288

It will do powers, modulo, and the standard stuff. I'm not sure about square roots though.
It will observe parenthetical heirarchy, so: (8+66)*37 will be correct if you want 74*37, as opposed to: 8+66*37 which yields 8+2442.
to reverse sign, for a numeric var. "a": a=-a. If string, b: a=-(val(b))
Parsing is, imho, the most tedious, time-consuming, frustrating and error-prone operation of any project. I HATE parsing! But it is critical if you want to write "robust" / adaptable / flexible code. On that note, I've got to shut down for today.
ps: here's a vbscript using eval for a commandline calculator (calcit.vbs):

'Simple commandline calculator, but handy to have around (batch math is feeble)
'(a "front-end" to vbscript "eval".
'To use (example): cscript /nologo calcit.vbs ((172+6)*3) * 2^8/34 mod 4
'or another, demonstrating fractions: 12.7 / .22
'Also note it will do hex-to-decimal conversion and hex math, like: calcit &h70 + &hC, or &h70 + 12
'(but the results always rendered in decimal, alas.)
'Will also concatenate numbers using "&".
set con=wscript.stdin
set cout=wscript.stdout
set t=wscript.arguments
if t.count=0 then
cout.write ("enter the math: ")
do while len(aa)>0
bb=eval(bb & aa)
cout.writeline("cur.ttl: --------")
dim a(99)
for i=0 to t.count-1
aa=aa&" "&t(i)
end if

Report •

Related Solutions

Ask Question