今天让Chat AI写一个程序:用VB编写一个算24点的程序。
不到一分钟就Chat AI就写出来了,而且这程序写得很整洁,有注释,有说明,太厉害了。
感觉一般的码农随时可以被替代了,码农们都要加油了。
不过,同样的要求,文心一言装得也挺像的,也是一会就写出来,不过写出来的程序不知道在做什么,但格式、说明也挺像一回事的,说明文心一言还有很长的路要走,加油!
程序清单1(Chat AI):
以下是用VB编写算24点程序的代码:
Public Class Form1
Dim a As Integer '第1个随机数
Dim b As Integer '第2个随机数
Dim c As Integer '第3个随机数
Dim d As Integer '第4个随机数
'随机生成4个数,并显示在窗体上
Private Sub GenerateNumbers()
Randomize()
a = Int(Rnd() * 9) 1
b = Int(Rnd() * 9) 1
c = Int(Rnd() * 9) 1
d = Int(Rnd() * 9) 1
Label1.Text = a
Label2.Text = b
Label3.Text = c
Label4.Text = d
End Sub
'计算给定的4个数是否能组成24
Private Function Calculate24(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer, ByVal d As Integer) As Boolean
Dim result As Boolean = False
Dim nums() As Integer = {a, b, c, d}
'枚举4个数的所有运算组合,共64种
For i = 0 To 3
For j = 0 To 3
If j i Then
For k = 0 To 3
If k i And k j Then
For l = 0 To 3
If l i And l j And l k Then
'算式1:(a b)*(c d)
result = result Or (nums(i) nums(j)) * (nums(k) nums(l)) = 24
'算式2:(a b-c)*d
result = result Or (nums(i) nums(j) - nums(k)) * nums(l) = 24
'算式3:a/(b-c/d)
result = result Or nums(i) / (nums(j) - nums(k) / nums(l)) = 24
'算式4:a-b-c-d
result = result Or nums(i) - nums(j) - nums(k) - nums(l) = 24
'算式5:(a-b)*(c-d)
result = result Or (nums(i) - nums(j)) * (nums(k) - nums(l)) = 24
End If
Next
End If
Next
End If
Next
Next
Return result
End Function
'生成新的4个数
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GenerateNumbers()
End Sub
'验证给定的4个数是否能组成24
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim result As Boolean = Calculate24(a, b, c, d)
If result Then
MsgBox("可以组成24")
Else
MsgBox("无法组成24")
End If
End Sub
End Class
程序清单2(文言一心):
Private Sub btnCalculate_Click()
'获取四个数字
Dim num1 As Double
Dim num2 As Double
Dim num3 As Double
Dim num4 As Double
Dim ans As Double
num1 = CDbl(txtNumber1.Text)
num2 = CDbl(txtNumber2.Text)
num3 = CDbl(txtNumber3.Text)
num4 = CDbl(txtNumber4.Text)
'判断是否可以进行计算
If num1 > num2 Then
MsgBox "无法进行计算,数字1大于数字2。"
Exit Sub
End If
If num1 > num3 Then
MsgBox "无法进行计算,数字1大于数字3。"
Exit Sub
End If
If num1 > num4 Then
MsgBox "无法进行计算,数字1大于数字4。"
Exit Sub
End If
If num2 > num3 Then
If num1 <= num3 Then
ans = num1 num2 - num3
Else
ans = num1 - num2 num3
End If
Else
If num1 <= num4 Then
ans = num1 num2 - num4
Else
ans = num1 - num2 num4
End If
End If
'显示结果
MsgBox "计算结果为:" & ans
End Sub
相关文章
猜你喜欢