|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Folding For PCMech
Join Date: Jun 2003
Location: San Dimas, CA
Posts: 3,136
|
Visual Basic program help
I am taking a class on computer programming that is required in the Civil Engineering department at my school. So far, we have only been learning Visual Basic, and I am working on a project to solve Quadratic Equations. The basic logic of the program is to take a quadratic equation typed in by the user in the form: ax^2 + bx + c and when the enter key is hit, to retrieve the a, b, and c values and then calculate the roots when the calculate button is pressed.
Here is a visual of the program form. I have already got this part down, but the professor is offering "bonus" points for a few things that I am having some trouble with. The first one being: an equation in which b^2 - 4ac < 0 Code:
...
dim i as double
i = math.sqrt(-1)
chk = ((val_b) ^ 2) - (4 * val_a * val_c)
...
ElseIf chk < 0 Then
x1val.Text = (-val_b / (2 * val_a)) + ((Math.Abs(chk) * (i) / (2 * val_a)))
x2val.Text = (-val_b / (2 * val_a)) - ((Math.Abs(chk) * (i) / (2 * val_a)))
...
The other is to make the program able to hand either x or X when typing in the equation. I have tried using If-Then statements and Select Case statements, but I cannot get it to work. Here is what I have for getting the a, b, c values from the entered equation: Code:
Dim funcexp As String, pos1 As Integer, pos2 As Integer, pos3 As Integer
funcexp = quadval.Text
If Asc(e.KeyChar) = 13 Then
pos1 = InStr(funcexp, "x^2")
aval.Text = Mid(funcexp, 1, (pos1) - 1)
pos2 = InStr((pos1) + 3, funcexp, "x")
bval.Text = Mid(funcexp, (pos1) + 3, (((pos2)) - ((pos1) + 3)))
pos3 = InStr((pos2) + 1, funcexp)
cval.Text = Mid(funcexp, (pos2) + 1)
End If
Thanks, Andrew |
|
|
|
|
|
#2 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,806
|
I'm not familiar with VB specifically, but in most other languages, there is a common function you need to use to raise a number by a power--the carrot symbol is not recognized as a math symbol
Usually it's pow(baseval, exponentval) In VB, it might be: math.pow(baseval, exponentval)
__________________
There are two secrets to staying young, being happy, and achieving success. You have to laugh and find humor every day, and you have to have a dream.
|
|
|
|
|
|
#3 |
|
Folding For PCMech
Join Date: Jun 2003
Location: San Dimas, CA
Posts: 3,136
|
We have been using the caret to denote exponents so far in the class, so I don't think that's the problem.
|
|
|
|
|
|
#4 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,806
|
Looks like you're right. This is yet another reason why I think VB should fade out of existance
![]() I just punched sqrt(-1) on my scientific calculator, and yes, it throws a math error. Why were you trying to get the square root of -1 anyway? I'm not seeing how that makes sense in your else statement [edit]: now that I think of it, doesn't the square root of any negative number become an imaginary number? |
|
|
|
|
|
#5 |
|
Forum Administrator
Staff
Premium Member
Join Date: May 2000
Location: Joplin MO
Posts: 41,189
|
Square root of a negative number is mathematically impossible. Perhaps the professor is leading you on a wild goose chase to make you think.
|
|
|
|
|
|
#6 |
|
Folding For PCMech
Join Date: Jun 2003
Location: San Dimas, CA
Posts: 3,136
|
I know, but when dealing with complex/imaginary numbers, i is the square root of -1 (x^2 +1 = 0).
I was seeing if it was possible for the computer to do it, though. However, I think I have figured out a solution. I will simply have the computer do the math (-b/2a +/- sqrt(abs(b^2-4ac))) and convert the results to a string and then tag "i" on to the end of it when it outputs the answer to the textbox. It will then be responsibility of "the user" to figure it out. Do you guys (or anyone else) have any suggestions as far as the x vs. X situatioin goes? I would like to be able to have both the lower case and upper case x be available for input. |
|
|
|
|
|
#7 | |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,806
|
Quote:
Very simply, you can notate if the number is imanginary by a simple if-statement (or whatever the syntax is in VB) and evaluate whatever you are trying to find the square root of: Code:
if((b^2-4ac)<0){
//the number is imaginary
}
[edit]: As for capital letters vs lower case letters, there's usually a function in most languages that allows you to take strings and convert them to upper case, lower case (plus few other capitalization functions). For example, tolowercase(mystring) or touppercase(mystring). You'll have to see what the equivalent for VB is. Last edited by Force Flow; 04-27-2009 at 02:15 PM. |
|
|
|
|
|
|
#8 |
|
Folding For PCMech
Join Date: Jun 2003
Location: San Dimas, CA
Posts: 3,136
|
I forgot to post back the other day. I was able to convert the upper case to lower case by using LCase()--in case anyone every references this thread.
Thanks for your help, guys. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|