VB利用栈实现表达式求值

时间:2024.4.20

实验一

Dim s(100) As String, t As Integer

Private Sub Command2_Click()

  Dim x As String, n, i As Integer

    Text3.Text = " "

  For i = t To 1 Step -1

    pop x, s(), t

    Text3.Text = Text3.Text & x

  Next i

End Sub

Private Sub Command1_Click()

  Dim a, b, x As String, n, i, m As Integer

    Text2.Text = ""

    a = Text1.Text

    n = Len(a)

    m = 10

    t = 0

  For i = 1 To n

    x = Mid(a, i, 1)

    PUSH x, s(), m, t

    Text2.Text = Text2.Text & s(i)

  Next i

End Sub

Public Sub PUSH(x As String, s() As String, m As Integer, top As Integer)

  If (top = m) Then

  MsgBox "数据有误"

  End

  End If

  top = top + 1

  s(top) = x

End Sub

Public Sub pop(x, s() As String, top As Integer)

    If top = 0 Then

    MsgBox "栈空"

    End If

    x = s(top)

    top = top - 1

End Sub

Public Sub pre(x As String, p As Integer)

    Select Case x

        Case "*"

        p = 2

        Case "+"

        p = 1

        Case "-"

        p = 1

        Case ";"

        p = 0

        Case "^"

        p = 3

    End Select

End Sub

Private Sub Command3_Click()

Dim topo As Integer, topn As Integer

Dim p, f, i, x1, y1 As Integer

Dim a1 As Integer, a2 As Integer

Dim y, os(100) As String

Dim a As String, x As String

Dim z As String, w As String

Dim ns(100) As String, q As String

topo = 0

topn = 0

z = ";"

f = 0

PUSH z, os(), 10, topo

i = 0

a = Text1.Text

    Do While f <> 2

    If f = 0 Then

     i = i + 1

     w = Mid(a, i, 1)

     End If

        If Not ((w = "*") Or (w = "+") Or (w = "-") Or (w = "/") Or (w = "^") Or (w = ";")) Then

        PUSH w, ns(), 10, topn

        Else

        tp q, os(), topo

        pre w, a1

        pre q, a2

            If (a1 > a2) Then

            PUSH w, os(), 10, topo

            f = 0

            ElseIf (q = ";") And (w = ";") Then

            pop x, ns(), topn

            f = 2

            Else

            pop x, ns(), topn

            pop y, ns(), topn

            pop q, os(), topo

            x1 = Val(x)

            y1 = Val(y)

            Select Case q

                Case "*"

                x1 = y1 * x1

                Case "/"

                x1 = y1 / x1

                Case "+"

                x1 = y1 + x1

                Case "-"

                x1 = y1 - x1

                Case "'"

                x1 = y1 ^ x1

            End Select

            x = Str(x1)

            PUSH x, ns(), 10, topn

            f = 1

            End If

           End If

         Loop

         Text4.Text = Str(x1)

End Sub

Private Sub Form_Load()

    Text1.Text = ""

    Text2.Text = ""

End Sub

Public Sub tp(x, s() As String, top As Integer)

    If top = 0 Then

    MsgBox "栈空"

    End If

    x = s(top)

End Sub


第二篇:栈的表达式求值


题目:使用栈来对表达式求值,运算数假设都为一位整数(值在[‘0’,'9']间)。如:9+6*2-5

首先我们要新建两个栈。一个是用来保存数字的数字栈,一个是用来保存运算符的运算栈。 我们必须先定义一个字符数组,用来保存输入的表达式。

然后我们就可以依次读取这个字符数组。

1.当遇到的这个字符是一个数字时,把它(字符型)转化为整型,然后入栈(数字栈)。

2.当遇到的是运算符时,首先要取出栈内(运算栈)的头元素,用头元素与它进行比较。如果这个运算符的优先级比头元素的要高,则入栈;否则,就弹出一个运算符和两个数字进行计算,然后把算得的这个值入栈(数字栈);一直这样循环,直到把栈内(字符栈)的运算符都弹出完为止。最后再把这个运算符入栈(运算栈)。

在字符数组读取完之后,我们发现,运算栈只剩一个运算符,数字栈只剩两个数字。 所以,这个时候,只需弹出这个运算符,和两个数字进行计算,所得的这个值就是这个表达式的的值。

下面细说运算符优先级问题:

1.我们在初始化运算栈时,给它赋一个初值'#',它的优先级是最低的。

2.'+'或'-':规定只有'#'的优先级比它低,其它都比它高(包括它自己)。

3.'*'或'/':规定只有'#','+','-'的优先级比它低,其它都比它高(包括它自己)。

下面是几组测试数据:

栈的表达式求值

栈的表达式求值

栈的表达式求值

下面是源程序:

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#define M 30

struct Soperator

{

char c[M];

int top;

}So;

struct Snum

{

int data[M];

int top;

}Sn;

void inito(struct Soperator *p); void initn(struct Snum *p);

void pusho(struct Soperator *p,char op); void pushn(struct Snum *p,int x); char popo(struct Soperator *p); int popn(struct Snum *p);

char gettopo(struct Soperator *p); int gettopn(struct Snum *p);

int isempty(struct Soperator *p);

int compare(struct Soperator *p,char op); int calculate(int a,char op,int b); int result(char s[M]);

void main()

{

int i=0;

char ch[M];

printf("输入表达式:");

gets(ch);

puts(ch);

printf("=%d\n",result(ch)); }

int result(char s[M])

{

int i=0,a,b,sum,d,flag;

char op;

inito(&So);

initn(&Sn);

while(s[i]!='\0')

{

if(s[i]>='0'&&s[i]<='9')

{

d=s[i]-'0';

pushn(&Sn,d);

}

else

{

flag=compare(&So,s[i]); if(flag==1)

pusho(&So,s[i]); if(flag==2)

{

do

{

a=popn(&Sn); b=popn(&Sn); op=popo(&So);

sum=calculate(a,op,b); pushn(&Sn,sum); }while(!isempty(&So));

pusho(&So,s[i]);

}

}

i++;

}

a=popn(&Sn);

b=popn(&Sn);

op=popo(&So);

sum=calculate(a,op,b);

return(sum);

}

void inito(struct Soperator *p)

{

p->top=-1;

p->top++;

p->c[p->top]='#';

}

void initn(struct Snum *p)

{

p->top=0;

}

void pusho(struct Soperator *p,char op) {

if(p->top==M-1)

return;

else

{

p->top++;

p->c[p->top]=op; }

}

void pushn(struct Snum *p,int x) {

if(p->top==M-1)

return;

else

{

p->top++;

p->data[p->top]=x; }

}

char popo(struct Soperator *p) {

char ch;

if(p->top==0)

printf("空表\n");

else

{

ch=p->c[p->top]; p->top--;

return(ch);

}

}

int popn(struct Snum *p)

{

int n;

if(p->top==0)

printf("空表\n");

else

{

n=p->data[p->top]; p->top--;

return(n);

}

}

char gettop(struct Soperator *p) {

return(p->c[p->top]);

}

int compare(struct Soperator *p,char op) {

char ch;

ch=gettop(p);

switch(op)

{

case '+':

if(ch=='#')

return 1;

else

return 2;

break;

case '-':

if(ch=='#')

return 1;

else

return 2;

break;

case '*':

if(ch=='#' || ch=='+' || ch=='-') return 1;

else

return 2;

break;

case '/':

if(ch=='#' || ch=='+' || ch=='-') return 1;

else

return 2;

break;

default:

return 0;

break;

}

}

int calculate(int a,char op,int b) {

switch(op)

{

case '+':return(b+a);break; case '-':return(b-a);break; case '*':return(b*a);break; case '/':return(b/a);break; }

}

int isempty(struct Soperator *p) {

if(p->top==0)

return 1;

else

return 0;

}

int gettopn(struct Snum *p) {

if(p->top==-1)

printf("空表\n"); else

return(p->data[p->top]); }

更多相关推荐:
现实表现材料书记

XXX同志现实表现材料XXX,男,19XX年x月出生,中共党员,大学学历。20xx年x月任XXX镇党委书记至今,该同志政治、思想、工作等情况表现如下:一、讲政治,讲大局,具有较高的政治理论水平,良好的政治素养该…

个人现实表现材料

***同志现实表现材料***,男,19xx年x月原贵州省农学院毕业,同年x月参加工作,大专学历,中共党员,现任**县农业局党组成员、副局长。一年来,在县委、县政府的正确领导和县农业局党组的支持下,该同志能够坚持…

xx同志现实表现材料

xx同志现实表现材料xx,男,土家族,出生于年月,中共党员,某市某区人,本科学历,年毕业后经公务员招考在某市某区某乡政府工作,年月经公选调任某区某镇任党委委员、纪委书记,年月调任某区委办公室担任综合信息和业务指…

现实表现材料模板

××××·××同志现实表现材料(格式范例)××××〃××,男,××族,19××年××月生(××岁),新疆××人,××年××月参加工作,××年××月入党,大学学历(××××年××月××院校××专业毕业)。现任墨玉…

乡镇政府文书现实表现材料

杨正东同志现实表现材料X,男,汉族,x年x月出生,x年x月毕业于xx大学,xxx学士学位。xx年xx月分配到xx政府工作。该同志自参加工作以来,始终严格要求自已,政治上要求进步,勤于学习,钻研业务,扎实工作,圆…

乡镇领导干部同志现实表现材料

刘洋同志,男,汉族,19xx年x月出生,19xx年x月参加工作,大专文化,19xx年x月加入中国共 产 党,现任鹤北镇镇领导干部。参加工作以来,他认真履职尽责,政治立场坚定,党性原则强,宗旨意识牢固,坚持做到了勤勉…

××同志现实表现材料(科级后备干部材料)

××同志现实表现材料××,男,汉族,×年×月出生,中共党员,大专学历。×年×月参加工作,先后在县运管所、县交通局办公室、县公路管理处工作,现任县港航管理处办公室主任。近三年来,该同志在政治、思想、工作上表现如下…

现实表现材料

XX同志现实表现材料XXX男XXX年XX月生本科学历20xx年7月参加工作20xx年4月到本单位工作至今现将该同志的现实表现总结如下一关于德的方面该同志认真贯彻三个代表重要思想坚持党的基本路线基本方针在政治上思...

现实表现

个人现实表现情况XXX男XXX族XX年X月出生现年XX岁XXX县人XX年XX月参加工作XX年XX月加入中国共 产 党在职大专学历20xx年10月毕业于XX翻译专业现任XX镇党委副书记镇长县委候补委员一现实表现和主要...

现实表现材料

XXX同志现实表现材料XXX男生于19xx年12月中学高级教师省市级骨干教师XX县名师XX县政协常委现任XX一中教导主任19xx年7月XX师专毕业后被分配到XX一中任教至19xx年7月至19xx年7月在XX师大...

XXX同志现实表现材料

XXX同志现实表现材料XXX男汉族出生于XXXX年X月中共党员XX市XXX人本科学历XXXX年毕业后在XXXX工作XXXX年X月在XXXX工作至今该同志政治思想工作表现如下一讲政治讲大局具有较高的政治理论水平良...

现实表现材料

刘文刚同志现实表现材料讲党性立场坚定思想政治素质好该同志坚持学习马列主义毛泽东思想邓小平理论认真实践三个代表重要思想全面贯彻落实科学发展观在教育战线上工作九年来参与了三个代表重要思想学习教育保持共 产 党员先进性教...

现实表现(143篇)