| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 811 人关注过本帖
标题:自动生成随机自则运算出错,求高手指教,
只看楼主 加入收藏
a414715440
Rank: 2
等 级:论坛游民
帖 子:37
专家分:26
注 册:2011-8-28
结帖率:100%
收藏
 问题点数:0 回复次数:8 
自动生成随机自则运算出错,求高手指教,
Dim a, b, c, d, e, f, g, m As Integer, r, s, t As Variant  ,定义变量
-----------------------------------------------------------------------
Private Sub Command1_Click()   ,生成四则运算
x = a & r & b & s & c & t & d
Print x                        ,打印出四则运算
End Sub
--------------------------------------
Private Sub Command2_Click()   ,结果检验
m = Val(Text1.Text)      ,输入运算结果
If x = m Then            ,比较电脑运算结果和自己运算结果
MsgBox "你算对了"
ElseIf m <> x Then
MsgBox "你算错了"
End If
End Sub
------------------------------
Private Sub Form_Load()
Randomize
a = Int(50 * Rnd)   ,产生随机数a
b = Int(100 * Rnd)  ,产生随机数b
c = Int(20 * Rnd)  ,产生随机数c
d = Int(30 * Rnd)  ,产生随机数d
e = Int(3 * Rnd + 1)
f = Int(3 * Rnd + 1)
g = Int(3 * Rnd + 1)
r = Choose(e, "+", "-", "*", "/")  ,产生随机运算符
s = Choose(f, "+", "-", "*", "/")  ,产生随机运算符
t = Choose(g, "+", "-", "*", "/")  ,产生随机运算符


为什么电脑不运算X呢,,

搜索更多相关主题的帖子: 检验 
2011-09-20 15:19
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
Option Explicit

'Dim a, b, c, d, e, f, g, m As Integer, r, s, t As Variant  '定义变量
'定义格式错

Dim a As Integer, b As Integer, c As Integer
Dim d As Integer, e As Integer, f As Integer
Dim g As Integer, m As Integer
Dim r As String, s As String, t As String

Dim x As String
Dim y As Single


Dim Primitives As Collection

'-----------------------------------------------------------------------
Private Sub Command1_Click()   '生成四则运算

'每次都产生新的
a = Int(50 * Rnd)   '产生随机数a
b = Int(100 * Rnd)  '产生随机数b
c = Int(20 * Rnd)  '产生随机数c
d = Int(30 * Rnd)  '产生随机数d
e = Int(3 * Rnd + 1)
f = Int(3 * Rnd + 1)
g = Int(3 * Rnd + 1)
r = Choose(e, "+", "-", "*", "/")  '产生随机运算符
s = Choose(f, "+", "-", "*", "/")  '产生随机运算符
t = Choose(g, "+", "-", "*", "/")  '产生随机运算符


x = a & r & b & s & c & t & d   '出题
Print x                        '打印出四则运算
End Sub
'--------------------------------------
Private Sub Command2_Click()   '结果检验
m = Val(Text1.Text)      '输入运算结果
'x 要进行计算,直接是算不出的

y = EvaluateExpr(x)

If y = m Then            '比较电脑运算结果和自己运算结果
MsgBox "你算对了"
ElseIf m <> x Then
MsgBox "你算错了"
End If
End Sub
'------------------------------
Private Sub Form_Load()
Randomize

'无法每次都产生新的,换位置
'a = Int(50 * Rnd)   '产生随机数a
'b = Int(100 * Rnd)  '产生随机数b
'c = Int(20 * Rnd)  '产生随机数c
'd = Int(30 * Rnd)  '产生随机数d
'e = Int(3 * Rnd + 1)
'f = Int(3 * Rnd + 1)
'g = Int(3 * Rnd + 1)
'r = Choose(e, "+", "-", "*", "/")  '产生随机运算符
's = Choose(f, "+", "-", "*", "/")  '产生随机运算符
't = Choose(g, "+", "-", "*", "/")  '产生随机运算符

End Sub


'此函数网上抄的
Private Function EvaluateExpr(ByVal expr As String) As Single
Const PREC_NONE = 11
Const PREC_UNARY = 10 ' Not actually used.
Const PREC_POWER = 9
Const PREC_TIMES = 8
Const PREC_DIV = 7
Const PREC_INT_DIV = 6
Const PREC_MOD = 5
Const PREC_PLUS = 4

Dim is_unary As Boolean
Dim next_unary As Boolean
Dim parens As Integer
Dim pos As Integer
Dim expr_len As Integer
Dim ch As String
Dim lexpr As String
Dim rexpr As String
Dim value As String
Dim status As Long
Dim best_pos As Integer
Dim best_prec As Integer

' Remove leading and trailing blanks.
expr = Trim$(expr)
expr_len = Len(expr)
If expr_len = 0 Then Exit Function

' If we find + or - now, it is a unary operator.
is_unary = True

' So far we have nothing.
best_prec = PREC_NONE

' Find the operator with the lowest precedence.
' Look for places where there are no open
' parentheses.
For pos = 1 To expr_len
' Examine the next character.
ch = Mid$(expr, pos, 1)

' Assume we will not find an operator. In
' that case the next operator will not
' be unary.
next_unary = False

If ch = " " Then
' Just skip spaces.
next_unary = is_unary
ElseIf ch = "(" Then
' Increase the open parentheses count.
parens = parens + 1

' An operator after "(" is unary.
next_unary = True
ElseIf ch = ")" Then
' Decrease the open parentheses count.
parens = parens - 1

' An operator after ")" is unary.
next_unary = True

' If parens < 0, too many ')'s.
If parens < 0 Then
Err.Raise vbObjectError + 1001, _
"EvaluateExpr", _
"Too many )s in '" & _
expr & "'"
End If
ElseIf parens = 0 Then
' See if this is an operator.
If ch = "^" Or ch = "*" Or _
ch = "/" Or ch = "\" Or _
ch = "%" Or ch = "+" Or _
ch = "-" _
Then
' An operator after an operator
' is unary.
next_unary = True

Select Case ch
Case "^"
If best_prec >= PREC_POWER Then
best_prec = PREC_POWER
best_pos = pos
End If

Case "*", "/"
If best_prec >= PREC_TIMES Then
best_prec = PREC_TIMES
best_pos = pos
End If

Case "\"
If best_prec >= PREC_INT_DIV Then
best_prec = PREC_INT_DIV
best_pos = pos
End If

Case "%"
If best_prec >= PREC_MOD Then
best_prec = PREC_MOD
best_pos = pos
End If

Case "+", "-"
' Ignore unary operators
' for now.
If (Not is_unary) And _
best_prec >= PREC_PLUS _
Then
best_prec = PREC_PLUS
best_pos = pos
End If
End Select
End If
End If
is_unary = next_unary
Next pos

' If the parentheses count is not zero,
' there's a ')' missing.
If parens <> 0 Then
Err.Raise vbObjectError + 1002, _
"EvaluateExpr", "Missing ) in '" & _
expr & "'"
End If

' Hopefully we have the operator.
If best_prec < PREC_NONE Then
lexpr = Left$(expr, best_pos - 1)
rexpr = Right$(expr, expr_len - best_pos)
Select Case Mid$(expr, best_pos, 1)
Case "^"
EvaluateExpr = _
EvaluateExpr(lexpr) ^ _
EvaluateExpr(rexpr)
Case "*"
EvaluateExpr = _
EvaluateExpr(lexpr) * _
EvaluateExpr(rexpr)
Case "/"
EvaluateExpr = _
EvaluateExpr(lexpr) / _
EvaluateExpr(rexpr)
Case "\"
EvaluateExpr = _
EvaluateExpr(lexpr) \ _
EvaluateExpr(rexpr)
Case "%"
EvaluateExpr = _
EvaluateExpr(lexpr) Mod _
EvaluateExpr(rexpr)
Case "+"
EvaluateExpr = _
EvaluateExpr(lexpr) + _
EvaluateExpr(rexpr)
Case "-"
EvaluateExpr = _
EvaluateExpr(lexpr) - _
EvaluateExpr(rexpr)
End Select
Exit Function
End If

' If we do not yet have an operator, there
' are several possibilities:
'
' 1. expr is (expr2) for some expr2.
' 2. expr is -expr2 or +expr2 for some expr2.
' 3. expr is Fun(expr2) for a function Fun.
' 4. expr is a primitive.
' 5. It's a literal like "3.14159".

' Look for (expr2).
If Left$(expr, 1) = "(" And Right$(expr, 1) = ")" Then
' Remove the parentheses.
EvaluateExpr = EvaluateExpr(Mid$(expr, 2, expr_len - 2))
Exit Function
End If

' Look for -expr2.
If Left$(expr, 1) = "-" Then
EvaluateExpr = -EvaluateExpr( _
Right$(expr, expr_len - 1))
Exit Function
End If

' Look for +expr2.
If Left$(expr, 1) = "+" Then
EvaluateExpr = EvaluateExpr( _
Right$(expr, expr_len - 1))
Exit Function
End If

' Look for Fun(expr2).
If expr_len > 5 And Right$(expr, 1) = ")" Then
lexpr = LCase$(Left$(expr, 4))
rexpr = Mid$(expr, 5, expr_len - 5)
Select Case lexpr
Case "sin("
EvaluateExpr = Sin(EvaluateExpr(rexpr))
Exit Function
Case "cos("
EvaluateExpr = Cos(EvaluateExpr(rexpr))
Exit Function
Case "tan("
EvaluateExpr = Tan(EvaluateExpr(rexpr))
Exit Function
Case "sqr("
EvaluateExpr = Sqr(EvaluateExpr(rexpr))
Exit Function
End Select
End If

' See if it's a primitive.
On Error Resume Next
value = Primitives.Item(expr)
status = Err.Number
On Error GoTo 0
If status = 0 Then
EvaluateExpr = CSng(value)
Exit Function
End If

' It must be a literal like "2.71828".
On Error Resume Next
EvaluateExpr = CSng(expr)
status = Err.Number
On Error GoTo 0
If status <> 0 Then
Err.Raise status, _
"EvaluateExpr", _
"Error evaluating '" & expr & _
"' as a constant."
End If
End Function


授人于鱼,不如授人于渔
早已停用QQ了
2011-09-20 18:00
a414715440
Rank: 2
等 级:论坛游民
帖 子:37
专家分:26
注 册:2011-8-28
收藏
得分:0 
回复 2楼 风吹过b
y = EvaluateExpr(x)
 我调戏了下,报错,子函数或者程序未定义
2011-09-22 13:46
a414715440
Rank: 2
等 级:论坛游民
帖 子:37
专家分:26
注 册:2011-8-28
收藏
得分:0 
回复 3楼 a414715440
能帮忙把你从网上找的那段代码注释一下吗?我直接看到那么长一串,头都大了,分析不下去, 谢谢,
2011-09-22 13:47
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
你确认全部复制了,并且覆盖掉了你的代码?

我测试过。

授人于鱼,不如授人于渔
早已停用QQ了
2011-09-22 16:32
a414715440
Rank: 2
等 级:论坛游民
帖 子:37
专家分:26
注 册:2011-8-28
收藏
得分:0 
回复 5楼 风吹过b
大哥,我确认,我刚才新建了个工程,全用你的代码, 不对哟,

Private Sub Command2_Click()   '结果检验
m = Val(Text1.Text)      '输入运算结果
'x 要进行计算,直接是算不出的

y = EvaluateExpr(x)

If y = m Then            '比较电脑运算结果和自己运算结果
MsgBox "你算对了"
ElseIf m <> x Then
MsgBox "你算错了"
End If
End Sub
  这段代码还是不对,   y = EvaluateExpr(x) 调戏的时候,断点在这儿,
2011-09-22 19:13
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
严重怀疑你没复制全,要包括下面的那一大堆代码都要。

刚重新测试了一下,没问题。


If y = m Then            '比较电脑运算结果和自己运算结果
MsgBox "你算对了"
ElseIf y <> m Then
MsgBox "你算错了"
End If

授人于鱼,不如授人于渔
早已停用QQ了
2011-09-23 08:10
jiashie
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:237
专家分:999
注 册:2009-4-30
收藏
得分:0 
一种偷懒的做法
程序代码:
Private Function Eval(ByVal exp As String) As Single
On Error Resume Next
Dim obj As Object
Dim ret As Single

Set obj = CreateObject("MSScriptControl.ScriptControl")
obj.language = "vbscript"
ret = obj.Eval(exp)
Set obj = Nothing

Eval = ret
End Function

'//:测试
debug.print eval("3+2*3-1")

如果LZ学过数据结构,可以看看讲“栈”的时候可能会讲到的“后缀表达式”(逆波兰表达式)
2011-09-26 11:15
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
我知道这个偷懒的做法,怕他没装 VBA 脚本, 所以才找个复杂的方法出来。

那个函数,我以前自己写过类似的,但在支持括号时会出现错误。所在就在网上找了个。

授人于鱼,不如授人于渔
早已停用QQ了
2011-09-26 15:38
快速回复:自动生成随机自则运算出错,求高手指教,
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053118 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved