我试图通过找到公因子来分解方程式,例如:
4x+2y = 2(2x+y)
所以基本上在
之间If n1 <> n2 Then
和
End If
问题出在哪里,因为我不知道要输入什么。
我尝试这样做:
if n1/n2 = whole number
n3 = n1/n2
MsgBox(n3 & "(" & l1 & "+" & l2 & ")")
但是我无法弄清楚整个数字代码,当我把它弄成数字时,我意识到它也不会除以一个数字,所以如果问题是:
4x+2y
答案将是:
2(2x+y)
但是计算机给了我
2(x+y)
到目前为止,我有:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim n1 As Integer
Dim n2 As Integer
Dim l1 As String
Dim l2 As String
n1 = InputBox("what is the first number ")
l1 = InputBox("what is the first letter ")
n2 = InputBox("what is the second number ")
l2 = InputBox("what is the second letter ")
If n1 = n2 Then
MsgBox(n1 & "(" & l1 & "+" & l2 & ")")
End If
If n1 <> n2 Then
End If
End Sub
End Class
我希望它可以分解,这样才对。
如果问题是:
4x+12y
我希望它是:
4(x+3y)
而不是我得到的是:
4(x+y)
答案 0 :(得分:1)
您要查找的代码是:
If n1 <> n2 Then
If n1 Mod n2 = 0 Then
Dim n3 = n1 / n2
MsgBox(n3 & "(" & (n1 / n3) & l1 & "+" & (n2 / n3) & l2 & ")")
End If
End If
取模运算符Mod
给出整数除法的余数。因此,如果n1 Mod n2
为零,则n2
是n1
的除数。然后,在最终结果中,您需要取消n3
。
但是,这并非您要执行的操作。因为这也不会更改输入6x + 4y
的任何内容。您想要做的是排除最大公约数。可以使用Euclidean algorithm计算得出。这是完整的代码:
Private Function GreatestCommonDivisor(a As Integer, b As Integer) As Integer
If a = 0 Then Return b
Return GreatestCommonDivisor(b Mod a, a)
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim n1 As Integer
Dim n2 As Integer
Dim l1 As String
Dim l2 As String
n1 = InputBox("what is the first number ")
l1 = InputBox("what is the first letter ")
n2 = InputBox("what is the second number ")
l2 = InputBox("what is the second letter ")
Dim n3 = GreatestCommonDivisor(n1, n2)
MsgBox(n3 & "(" & (n1 / n3) & l1 & "+" & (n2 / n3) & l2 & ")")
End Sub
这将为2(2x + 1y)
打印4x + 2y
,为2(3x + 2y)
打印6x + 4y
。