急求VB程序 设计一个简单的四则表达式计算程序 急急急急急!!!!!!!! 20

二、目的与要求:1.目的:培养学生综合利用VB语言进行程序设计的能力,主要是培养学生思考分析和解决循环类问题的能力。2.基本要求:①程序能够完成基本四则表达式的计算,即加... 二、目的与要求:
1.目的:
培养学生综合利用VB语言进行程序设计的能力,主要是培养学生思考分析和解决循环类问题的能力。
2.基本要求:
①程序能够完成基本四则表达式的计算,即加减乘除,可以不考虑括号、函数等情况。
②在设计程序的过程中,要求熟练使用分支和循环结构以及字符串的操作。
③程序界面简洁,使用方便,如下图:
3.创新要求:
在基本要求达到后,进行创新设计,可以从如下几方面进行考虑:
①考虑表达式里边加入基本函数的情况
②考虑加入括号的情况
③能够判断非法表达式,例如有非法字符
④能够判断非法运算,例如被0除
三、设计方法和基本原理:
1.问题描述:
在解决数学问题的过程中,经常出现一些简单的四则运算,如果使用Windows的计算器来运算,需要手工分析运算顺序并且暂存运算结果,此程序就是为解决此问题而设计的,用户只需要输入要计算的四则表达式,程序即可给出运算结果。
2.问题的解决方案:
根据问题描述,可以将问题解决分为以下几个方面来进行:
①表达式由运算符和其两侧的运算数组成(- 号作为单一的负号时除外),当一个运算符和其两侧的运算数确定以后,就可以将这一部分用其运算结果替换掉,构成新的简化的表达式,运算的整个过程就是这个不断简化的过程。
②要确定这些单一的运算符和其配套的运算数,可以先把所有的运算符(+ - * /)的位置找出来并送入数组,每两个操作符之间的即为操作数(-号作为单一操作数除外)。
③简化的过程遵循四则运算过程,先算减加,再算乘除。
一个简单的简化例子如下:
1-2+3*4-6/3 -> 1-2+12-6/3 -> 1-2+12-2 -> -1+12-2 -> -1+10 -> -9
以上的过程,正是我们操作VB语言中数组和字符串的过程
四、主要技术问题的描述:
依据2的分析,主程序过程可以如下:
Do while 找到运算符
If 有乘号 then 运算该乘法
If 有除号 then 运算该除法
If 有减号并且不在第一个位置 then 运算该减法
If 有加号 then 运算该加法
loop
循环完毕直到找不到运算符,结果即为运算结果。
运算部分可以使用一个子过程来进行,该过程通过一个参数来确定加减乘除相应的操作,在该过程中要完成如下的操作:
Sub Process(pType As String)
获得所有的运算符的位置并送入数组(-号在第一位的除外)
根据pType要操作的类型确定要操作的运算符的位置
确定该运算符两侧的运算数
根据pType的运算类型完成运算
将该运算符及其两侧的运算数用运算结果替换,返回新的串
End sub

各位高手写出来请帮忙发到904074779@qq.com 小弟感激不尽
展开
 我来答
百度网友b003138
2010-05-23 · TA获得超过1253个赞
知道小有建树答主
回答量:403
采纳率:0%
帮助的人:247万
展开全部
以前整的,可能对你有用,看看吧
VERSION 5.00
Begin VB.Form ExpressionForm
Caption = "Expression"
ClientHeight = 2310
ClientLeft = 1380
ClientTop = 2100
ClientWidth = 6615
LinkTopic = "Form1"
PaletteMode = 1 'UseZOrder
ScaleHeight = 2310
ScaleWidth = 6615
Begin VB.TextBox ExprText
Height = 285
Left = 0
TabIndex = 0
Top = 360
Width = 3615
End
Begin VB.CommandButton CmdEvaluate
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "Evaluate"
Default = -1 'True
Height = 495
Left = 1200
TabIndex = 11
Top = 960
Width = 1215
End
Begin VB.Frame Frame1
Appearance = 0 'Flat
Caption = "Primitives"
ForeColor = &H80000008&
Height = 2295
Left = 3720
TabIndex = 12
Top = 0
Width = 2895
Begin VB.TextBox NameText
Height = 285
Index = 0
Left = 240
TabIndex = 1
Top = 480
Width = 1215
End
Begin VB.TextBox ValueText
Height = 285
Index = 0
Left = 1560
TabIndex = 2
Top = 480
Width = 1215
End
Begin VB.TextBox NameText
Height = 285
Index = 1
Left = 240
TabIndex = 3
Top = 840
Width = 1215
End
Begin VB.TextBox ValueText
Height = 285
Index = 1
Left = 1560
TabIndex = 4
Top = 840
Width = 1215
End
Begin VB.TextBox NameText
Height = 285
Index = 2
Left = 240
TabIndex = 5
Top = 1200
Width = 1215
End
Begin VB.TextBox ValueText
Height = 285
Index = 2
Left = 1560
TabIndex = 6
Top = 1200
Width = 1215
End
Begin VB.TextBox NameText
Height = 285
Index = 3
Left = 240
TabIndex = 7
Top = 1560
Width = 1215
End
Begin VB.TextBox ValueText
Height = 285
Index = 3
Left = 1560
TabIndex = 8
Top = 1560
Width = 1215
End
Begin VB.TextBox NameText
Height = 285
Index = 4
Left = 240
TabIndex = 9
Top = 1920
Width = 1215
End
Begin VB.TextBox ValueText
Height = 285
Index = 4
Left = 1560
TabIndex = 10
Top = 1920
Width = 1215
End
Begin VB.Label Label1
Appearance = 0 'Flat
Caption = "Name"
ForeColor = &H80000008&
Height = 255
Index = 0
Left = 240
TabIndex = 14
Top = 240
Width = 615
End
Begin VB.Label Label1
Appearance = 0 'Flat
Caption = "Value"
ForeColor = &H80000008&
Height = 255
Index = 1
Left = 1560
TabIndex = 13
Top = 240
Width = 615
End
End
Begin VB.Label Label2
Appearance = 0 'Flat
Caption = "Expression"
ForeColor = &H80000008&
Height = 255
Left = 0
TabIndex = 17
Top = 0
Width = 975
End
Begin VB.Label Label3
Appearance = 0 'Flat
Caption = "Result"
ForeColor = &H80000008&
Height = 255
Left = 480
TabIndex = 16
Top = 1800
Width = 615
End
Begin VB.Label ResultLabel
BorderStyle = 1 'Fixed Single
Height = 255
Left = 1200
TabIndex = 15
Top = 1800
Width = 1215
End
End
Attribute VB_Name = "ExpressionForm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Dim Primitives As Collection

' ************************************************
' Evaluate the expression.
' ************************************************
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

' ************************************************
' Evaluate the expression entered by the user.
' ************************************************
Private Sub CmdEvaluate_Click()
Dim i As Integer
Dim name As String
Dim value As String
Dim expr As String
Dim rslt As Single
Dim pos As Integer

' Store the primitives.
Set Primitives = New Collection
For i = 0 To 4
name = Trim$(NameText(i).Text)
value = Trim$(ValueText(i).Text)
If name <> "" And value <> "" Then
Primitives.Add value, name
End If
Next i

' Get the expression.
expr = ExprText.Text

' Evaluate the expression.
ResultLabel.Caption = ""
On Error GoTo EvaluateError
rslt = EvaluateExpr(expr)
ResultLabel.Caption = Format$(rslt)
Exit Sub

EvaluateError:
Beep
MsgBox Err.Description
End Sub
qiaole123
2010-05-22 · TA获得超过3413个赞
知道大有可为答主
回答量:1408
采纳率:66%
帮助的人:965万
展开全部
java的可以吧。你自己在改动下
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
知道小朋友
2010-05-25
知道答主
回答量:8
采纳率:0%
帮助的人:0
展开全部
你看下这个可以吗,我发给你!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2010-05-31
展开全部
你是河北理工的吧?? 我也要设计这个程序不会做啊
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式