VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



A combination of functions to solve math equations.

by Muhmmad Shafi (1 Submission)
Category: Math/Dates
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sat 2nd October 2004
Date Added: Mon 8th February 2021
Rating: (1 Votes)

A combination of functions to solve math equations.

Rate A combination of functions to solve math equations.




'Code by Muhmmad Shafi for Mr Amar Ali
'this code find square or cube of mathematical equation containing numerics
'elements (Itegers only).
'place a textbox named Text1 and a command button named Command1 in the Form1
'write equation in this format ie '  (24+25)^2   ' to find square of 24+25
'-----------------------------------------------------------------------
'try to create functions for (a+b)^2 format.
'-----------------------------------------------------------------------



Dim Dummy1() As String, Dummy2() As String  'split the equation in text1
Dim Pt1 As Integer, Pt2 As Integer  '1st and 2nd part of equation
Dim Symbol As String    'symbol + or - used in the equation

Private Sub Command1_Click()
Cls
Call Find_Formulea(Text1.Text)
End Sub


Private Sub Text1_Change()
'Final equation after clearing all spaces.
'run this app and press space bar to see what happen
'or copy some text with spaces from some where else, paste it in the text box and see what happens.
Text1.Text = Get_Eq(Text1.Text)
End Sub

Private Function Get_Eq(Eq As String) As String
'this function filters spaces from the equation.
Dim Final_Eq As String
Dim x As Integer
For x = 1 To Len(Text1.Text)
If Mid$(Text1.Text, x, 1) <> " " Then
Final_Eq = Final_Eq & Mid$(Text1.Text, x, 1)
Else
End If
Next x
Get_Eq = Final_Eq
End Function

Private Function Find_Formulea(Eq As String) As String  'this function will verify the numercis or
'alpha numercis elements in the equation.
' this time only numercis are used

Call Square_Cube_Int(Pt1, Pt2) 'a call to numercis equation solving function.
    
End Function

Private Function Square_Cube_Int(Num1 As Integer, Num2 As Integer)
Call Split_Eq(Text1.Text)
Print "This formulea will be used to solve this equation."
Dim Equation As String
Dim x As Boolean
Equation = Get_Eq(Text1.Text)
If Mid$(Equation, Len(Equation) - 1, 2) = "^2" Then 'find square

    If Symbol = "+" Then '  a+b
    
    Print "' (a + b)^2 = a^2 + b^2 + 2ab '"
    'step by step solution of the equation
    Print "  (" & Num1 & " + " & Num2 & ")^2"
    Print "=(" & Num1 & ")^2 + (" & Num2 & ")^2 + 2(" & Num1 & "x" & Num2 & ")"
    Print "=" & Num1 ^ 2 & " + " & Num2 ^ 2 & " + 2 (" & Num1 * Num2 & ")"
    Print "=" & Num1 ^ 2 + Num2 ^ 2 & " + " & 2 * (Num1 * Num2)
    Print "=" & Num1 ^ 2 + Num2 ^ 2 + 2 * (Num1 * Num2)
    
    Else    'a-b
    
    
    Print "' (a - b)^2 = a^2 + b^2 - 2ab '"
    'step by step solution of the equation
    Print "  (" & Num1 & " - " & Num2 & ")^2"
    Print "=(" & Num1 & ")^2 + (" & Num2 & ")^2 - 2(" & Num1 & "x" & Num2 & ")"
    Print "=" & Num1 ^ 2 & " + " & Num2 ^ 2 & " - 2 (" & Num1 * Num2 & ")"
    Print "=" & Num1 ^ 2 + Num2 ^ 2 & " - " & 2 * (Num1 * Num2)
    Print "=" & Num1 ^ 2 + Num2 ^ 2 - 2 * (Num1 * Num2)
    End If
    
ElseIf Mid$(Equation, Len(Equation) - 1, 2) = "^3" Then 'find cube

    If Symbol = "+" Then '  a+b
    
    Print "  (" & Num1 & " + " & Num2 & ")^3"
    Print "write down step by step solution here."
    Else    'a-b
    
    
    Print "  (" & Num1 & " - " & Num2 & ")^3"
    Print "write down step by step solution here."
    End If
    
Else

Print "Sorry it is for you."

End If

End Function

Private Function Split_Eq(Eq As String)
'this function aparts the equation into two parts ie Pt1 and Pt2
'and set the value for Symbol variable in gen section
Dim Symb As Boolean
Symb = InStr(1, Text1.Text, "+", vbTextCompare) 'for a+b
If Symb = True Then
Dummy1 = Split(Text1.Text, "+")
Dummy1(0) = Replace$(Dummy1(0), "(", "")
Dummy1(1) = Replace$(Dummy1(1), ")", "")
Dummy2 = Split(Dummy1(1), "^")
Pt1 = Int(Dummy1(0))
Pt2 = Int(Dummy2(0))
Symbol = "+"
Else    'for a-b
Dummy1 = Split(Text1.Text, "-")
Dummy1(0) = Replace$(Dummy1(0), "(", "")
Dummy1(1) = Replace$(Dummy1(1), ")", "")
Dummy2 = Split(Dummy1(1), "^")
Pt1 = Int(Dummy1(0))
Pt2 = Int(Dummy2(0))
Symbol = "-"
End If
End Function



Download this snippet    Add to My Saved Code

A combination of functions to solve math equations. Comments

No comments have been posted about A combination of functions to solve math equations.. Why not be the first to post a comment about A combination of functions to solve math equations..

Post your comment

Subject:
Message:
0/1000 characters