|
楼主 |
发表于 2015-4-27 13:02
|
显示全部楼层
vba我手里有一个宏,能够生成随机的5题,前三个是加,后2个是加减混合。你看看能不能有借鉴。
Sub test2()
Dim a, b As Integer
Dim width As Integer
Dim length As Integer
Dim sum1, sum2, sum3, sum4, sum5 As Long
Dim cou, cou2 As Integer
Dim neg As Integer
sum = 0
cou2 = 0
Range("a2:e16") = ""
Range("a18:e18") = ""
length = Range("i3").Value
neg = Int(length * 0.4 - 0.01) + 1
width = Range("g3").Value
Range("a2").Value = ""
Range("a2").Select
For i = 1 To length
b = Int(Rnd() * 9 * 10 ^ (width - 1)) + 1 * 10 ^ (width - 1)
Selection.Value = b
sum = sum + b
Selection.Offset(1, 0).Select
Next
sum1 = sum
Range("g101").Value = sum1
sum = 0
Range("b2").Select
For i = 1 To length
b = Int(Rnd() * 9 * 10 ^ (width - 1)) + 1 * 10 ^ (width - 1)
Selection.Value = b
sum = sum + b
Selection.Offset(1, 0).Select
Next
sum2 = sum
Range("g102").Value = sum2
sum = 0
Range("c2").Select
For i = 1 To length
b = Int(Rnd() * 9 * 10 ^ (width - 1)) + 1 * 10 ^ (width - 1)
Selection.Value = b
sum = sum + b
Selection.Offset(1, 0).Select
Next
sum3 = sum
Range("g103").Value = sum3
sum = 0
Range("d2").Select
cou2 = 0
For i = 1 To length
b = Int(Rnd() * 9 * 10 ^ (width - 1)) + 1 * 10 ^ (width - 1)
If (sum - b > 0) And (cou2 < neg) Then
cou = 0
For k = 1 To 1000
ran = Rnd()
If ran < 0.4 Then cou = cou + 1
Next k
If cou < 400 Then
b = -b
cou2 = cou2 + 1
End If
End If
Selection.Value = b
sum = sum + b
Selection.Offset(1, 0).Select
Next
If cou2 < (neg) Then
For m = 1 To length - 1
Selection.Offset(-1, 0).Select
err = Selection.Value
If (err > 0) And (cou2 < (neg)) And ((sum - 2 * err) > 0) Then
Selection.Value = -err
sum = sum - 2 * err
cou2 = cou2 + 1
End If
Next m
End If
sum4 = sum
Range("g104").Value = sum4
sum = 0
Range("e2").Select
cou2 = 0
For i = 1 To length
b = Int(Rnd() * 9 * 10 ^ (width - 1)) + 1 * 10 ^ (width - 1)
If (sum - b > 0) And (cou2 < neg) Then
cou = 0
For k = 1 To 1000
ran = Rnd()
If ran < 0.4 Then cou = cou + 1
Next k
If cou < 400 Then
b = -b
cou2 = cou2 + 1
End If
End If
Selection.Value = b
sum = sum + b
Selection.Offset(1, 0).Select
Next
If cou2 < (neg) Then
For m = 1 To length - 1
Selection.Offset(-1, 0).Select
err = Selection.Value
If (err > 0) And (cou2 < (neg)) And ((sum - 2 * err) > 0) Then
Selection.Value = -err
sum = sum - 2 * err
cou2 = cou2 + 1
End If
Next m
End If
sum5 = sum
Range("g105").Value = sum5
End Sub |
|