中缀表达式转换成后缀表达式的实现(VB)

在对表达式求值过程中用到了后缀表达式:
比如: 2+3*(1.5-6*(3-2+4)/3)
其后缀表达式为:2 3 1.5 6 3 2 – 4 + * 3 / - * +

 下面直接贴出VB代码,

Option  Base  1             '  让数组的下标从 1 开始

Dim  ptr  As   String         '  遍历源字符串的指针
Dim   str   As   String         '  无空格字符串
Dim  strIndex  As   Integer   '  str迭代量
Dim  dstr()  As   String      '  存放后缀表达式各字符串
Dim  dIndex  As   Integer     '  dstr() 迭代量
Dim  Stack()  As   String     '  一个栈,在后缀表达式转换过程可以用于符号栈,在求值可以存放值
Dim  Top  As   Integer        '  指向栈顶,初始值为1
Dim  InV  As   Boolean        '  判断是否在一个超过两位数字的数字内
Dim  strlen  As   Integer     '  dstr, stack的数组维数


' 主要程序,用于遍历源字符串str1
Public   Sub mainPro(str1 As String)
    
' 迭代量初始化
    strIndex = 1
    dIndex 
= 0
    Top 
= 1
    InV 
= False
    
str = ""    '切记要将str初始化
        
    str1 
= str1 + "#"       ' 添加结束符"#"
    
    str1 
= Trim(str1)       ' 清除str1两端的空格
    ptr = Mid(str1, strIndex, 1)
    
    
' 消除str1里面的空格 ,其实这个初始化过程可以考虑另写一个函数
    Do While ptr <> "#"
        
If ptr <> " " Then
            
str = str + ptr
        
End If
        
        strIndex 
= strIndex + 1
        ptr 
= Mid(str1, strIndex, 1)
    
Loop
    
    
str = str + "#"
    
    strlen 
= Len(str+ 2       ' 取长度,然后重新定义动态数组
    ReDim dstr(strlen)
    
ReDim Stack(strlen)
    
Call push("#")
    
    strIndex 
= 1
    ptr 
= Mid(str, strIndex, 1)
    
' 判断str第一个字符是否"+" or "-"
    If ptr = "+" Or ptr = "-" Then
        push (ptr)
        dIndex 
= dIndex + 1
        dstr(dIndex) 
= "0"
        strIndex 
= strIndex + 1
    
End If
    
    
' 开始遍历str,遇到"#"停止
    Do While ptr <> "#"
        
Select Case ptr
            
Case "0" To "9""."
                
If InV Then
                    dstr(dIndex) 
= dstr(dIndex) + ptr
                
Else
                    dIndex 
= dIndex + 1
                    dstr(dIndex) 
= ptr
                    InV 
= True
                
End If
            
Case "+""-""*""/"
                InV 
= False
                dealop (ptr)
            
Case "("
                
Call push(ptr)
            
Case ")"
                
Call dealclose
              
            
Case Else
                
MsgBox ("表达式出现未知字符")
        
End Select
                
        strIndex 
= strIndex + 1
        ptr 
= Mid(str, strIndex, 1)
    
Loop
    
    
If Top > 1 Then
        
Dim tmp As String
        tmp 
= pop()
        
Do While tmp <> "#"
            dIndex 
= dIndex + 1
            dstr(dIndex) 
= tmp
            tmp 
= pop()
        
Loop
    
End If
    
    dIndex 
= dIndex + 1
    dstr(dIndex) 
= "#"
    
End Sub


Private   Sub push(ptr As String)
    
If Top > strlen Then
        
MsgBox ("符号栈上溢出 in push()")
        
End
    
End If
    
    Stack(Top) 
= ptr
    Top 
= Top + 1
End Sub


Private   Function pop() As String
    
If Top <= 1 Then
        
MsgBox ("符号栈下溢 in pop()")
        
End
    
End If
    
    Top 
= Top - 1
    pop 
= Stack(Top)
End Function


Private   Function getTop() As String
    
If Top <= 1 Then
        
MsgBox ("栈空 in getTop()")
        
End
    
End If
    
    getTop 
= Stack(Top - 1)
End Function


Private   Sub dealop(ptr As String)
    
Dim stillIn As Boolean  ' 判断是否正在处理当前操作符,即当前操作符优先级小于等于符号栈顶的
                            ' 操作符时,应该将栈顶操作符出栈,放到dstr中
    Dim tmp As String       ' 用来存放栈顶元素
    stillIn = True
    
Select Case ptr
        
Case "+"
            
If Mid(str, strIndex - 11= "(" Then
                dIndex 
= dIndex + 1
                dstr(dIndex) 
= "0"
                
Call push(ptr)
            
Else
                
Do While (stillIn)
                    tmp 
= getTop()
                    
Select Case tmp
                        
Case "*""/""-""+"
                            dIndex 
= dIndex + 1
                            dstr(dIndex) 
= pop()
                        
Case "(""#"
                            
Call push(ptr)
                            stillIn 
= False
                        
Case Else
                            
MsgBox ("运算符出错 in +")
                            
End '结束程序
                    End Select
                
Loop
            
End If
        
        
Case "-"
            
If Mid(str, strIndex - 11= "(" Then
                dIndex 
= dIndex + 1
                dstr(dIndex) 
= "0"
                push (ptr)
            
Else
                
Do While (stillIn)
                    tmp 
= getTop()
                    
Select Case tmp
                        
Case "*""/""+""-"
                            dIndex 
= dIndex + 1
                            dstr(dIndex) 
= pop()
                        
Case "(""#"
                            
Call push(ptr)
                            stillIn 
= False
                        
Case Else
                            
MsgBox ("运算符出错 in -")
                            
End
                    
End Select
                
Loop
            
End If
            
        
Case "*"
            
Do While (stillIn)
                tmp 
= getTop()
                
Select Case tmp
                    
Case "/""*"
                        dIndex 
= dIndex + 1
                        dstr(dIndex) 
= pop()
                    
Case "(""#""+""-"
                        
Call push(ptr)
                        stillIn 
= False
                    
Case Else
                        
MsgBox ("运算符出错 in *")
                        
End
                
End Select
            
Loop
            
            
            
Case "/"
                
Do While (stillIn)
                    tmp 
= getTop()
                    
Select Case tmp
                        
Case "*""/"
                            dIndex 
= dIndex + 1
                            dstr(dIndex) 
= pop()
                        
Case "(""#""+""-"
                            
Call push(ptr)
                            stillIn 
= False
                        
Case Else
                            
MsgBox ("运算符出错 in /")
                            
End
                    
End Select
                
Loop
            
            
Case Else
                
MsgBox ("Error happen in dealop() case else")
                
End
        
End Select
End Sub


Private   Sub dealclose()
    
Dim tmp As String  ' 用来存放栈顶元素
    tmp = pop()
    
Do While tmp <> "("
        dIndex 
= dIndex + 1
        dstr(dIndex) 
= tmp
        tmp 
= pop()
    
Loop
    
End Sub


Public   Function execute() As String
    
Dim v1 As String, v2 As String, v3 As String   ' 定义运算暂存的变量
    
    dIndex 
= 1
    ptr 
= dstr(dIndex)
    
Do While ptr <> "#"
        
Select Case ptr
            
Case "+"
                v1 
= pop()
                v2 
= pop()
                v3 
= Val(v1) + Val(v2) ' 这里会将val值隐式转换成string么?
                push (v3)
            
            
Case "-"
                v1 
= pop()
                v2 
= pop()
                v3 
= Val(v2) - Val(v1)
                push (v3)
            
            
Case "*"
                v1 
= pop()
                v2 
= pop()
                v3 
= Val(v1) * Val(v2)
                push (v3)
                
            
Case "/"
                v1 
= pop()
                v2 
= pop()
                
If Val(v1) = 0 Then
                    
MsgBox ("式子中除数为 0 ")
                    
End
                
End If
                v3 
= Val(v2) / Val(v1)
                push (v3)
            
            
Case Else
                push (ptr)
        
End Select
        
        dIndex 
= dIndex + 1
        ptr 
= dstr(dIndex)
    
Loop
    
    v1 
= pop()          '判断最终结果绝对值是否小于1,如果
    
    
If Abs(Val(v1)) < 1 Then
        v1 
= Format(Val(v1), "0.######")
    
End If
    execute 
= v1
End Function

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中缀表达式后缀表达式是一个很常见的问题,可以用栈来实现。具体步骤如下: 1. 初始化一个栈和一个空的输出字符串。 2. 从左到右扫描中缀表达式的每一个元素。 3. 如果遇到操作数,直接将其添加到输出字符串中。 4. 如果遇到左括号,将其压入栈中。 5. 如果遇到右括号,将栈中的元素依次弹出并添加到输出字符串中,直到遇到左括号。注意,左括号不会被添加到输出字符串中。 6. 如果遇到操作符,比较其与栈顶操作符的优先级。如果栈顶操作符优先级高,将其弹出并添加到输出字符串中。然后重复比较新的栈顶操作符与该操作符的优先级,直到栈为空或栈顶操作符优先级低于该操作符。最后将该操作符压入栈中。 7. 重复步骤2-6,直到扫描完整个中缀表达式。 8. 将栈中剩余的操作符依次弹出并添加到输出字符串中。 下面是一个用C语言实现中缀表达式后缀表达式的函数: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX_LEN 100 typedef struct { int top; char data[MAX_LEN]; } Stack; void push(Stack *s, char c) { if (s->top == MAX_LEN - 1) { printf("Stack is full!\n"); exit(1); } s->top++; s->data[s->top] = c; } char pop(Stack *s) { if (s->top == -1) { printf("Stack is empty!\n"); exit(1); } char c = s->data[s->top]; s->top--; return c; } char peek(Stack *s) { if (s->top == -1) { printf("Stack is empty!\n"); exit(1); } return s->data[s->top]; } int is_operator(char c) { if (c == '+' || c == '-' || c == '*' || c == '/') { return 1; } return 0; } int precedence(char c) { if (c == '+' || c == '-') { return 1; } else if (c == '*' || c == '/') { return 2; } return 0; } void infix_to_postfix(char *infix, char *postfix) { Stack s; s.top = -1; int len = strlen(infix); int j = 0; for (int i = 0; i < len; i++) { char c = infix[i]; if (isdigit(c)) { postfix[j++] = c; } else if (is_operator(c)) { while (s.top != -1 && is_operator(peek(&s)) && precedence(peek(&s)) >= precedence(c)) { postfix[j++] = pop(&s); } push(&s, c); } else if (c == '(') { push(&s, c); } else if (c == ')') { while (peek(&s) != '(') { postfix[j++] = pop(&s); } pop(&s); } } while (s.top != -1) { postfix[j++] = pop(&s); } postfix[j] = '\0'; } int main() { char infix[MAX_LEN]; char postfix[MAX_LEN]; printf("Enter infix expression: "); fgets(infix, MAX_LEN, stdin); infix_to_postfix(infix, postfix); printf("Postfix expression: %s\n", postfix); return 0; } ``` 输入输出示例: ``` Enter infix expression: 2+3*4 Postfix expression: 234*+ ``` ``` Enter infix expression: (2+3)*4 Postfix expression: 23+4* ``` ``` Enter infix expression: 2*(3+4)-5/6 Postfix expression: 234+*56/- ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值