中缀表达式转化为后缀表达式,计算中缀表达式,计算后缀表达式(有注释)(逆波兰表达式)

来源:互联网 发布:mac客户端网游 编辑:程序博客网 时间:2024/06/05 03:03

中缀表达式的计算:

#include <stdio.h>#include <stdlib.h>#include <string.h>#define maxsize 100//栈操作的应用//author:Hacker Li//time:2015.5.21//转载请注明出处:http://blog.csdn.net/xdz78int hash[50];char p[100];char b[100];typedef struct stack {    int data[100];    int top;}Stack,PStack;Stack *init(){    Stack *s;    s=(Stack *)malloc(sizeof(Stack));    if(s!=NULL){        s->top=-1;    }    return s;}int push(Stack *s,int x){    if(s->top==maxsize-1){        printf("stack full");        return 0;    }    s->top++;    s->data[s->top]=x;    return 1;}int pop(Stack *s){    if(s->top==-1){        printf("stack empty");        return 0;    }    return s->data[s->top--];}int isempty(Stack *s){    if(s->top==-1){        return 1;    }    else return 0;}int topdata(Stack *s){//查看栈顶元素    return s->data[s->top];}void inithash(){    memset(hash,0,sizeof(hash));    hash['+']=1;    hash['-']=1;    hash['*']=2;    hash['/']=2;    hash['(']=1;}int houzhui(char p[]){//计算后缀表达式的值    Stack *s;    s=init();    int a,b,i=0,j=0;    char str[10];    while(p[i]!='\0'){        while(p[i]>='0'&&p[i]<='9'){            str[j++]=p[i];            str[j]='\0';            i++;            if(p[i]==' '){                push(s,atoi(str));                j=0;            }        }        switch(p[i]){        case '+':            a=pop(s);            b=pop(s);            push(s,a+b);            break;        case '-':            a=pop(s);            b=pop(s);            push(s,b-a);            break;        case '*':            a=pop(s);            b=pop(s);            push(s,a*b);            break;        case '/':            a=pop(s);            b=pop(s);            push(s,b/a);            break;        }        i++;    }    return pop(s);}void zhongzhui(char a[])//将中缀表达式转化为后缀表达式{                       //把后缀表达式存放在数组b中    Stack *s1;    s1=init();    inithash();    memset(b,0,sizeof(b));    int i=0,j=0;    while(a[i]!='\0'){        if(a[i]>='0'&&a[i]<='9'){            b[j++]=a[i++];            if(a[i]<'0'||a[i]>'9'){                b[j++]=' ';            }        }        else if(a[i]=='('){            push(s1,a[i++]);        }        else if(a[i]==')'){            while(topdata(s1)!='('){//q是从栈中取出的符号                b[j++]=pop(s1);                b[j++]=' ';            }            i++;            pop(s1);        }        else{//a[i]为+ - * /            if(!isempty(s1)&&hash[(int)a[i]]<hash[topdata(s1)]){                while(topdata(s1)!='('&&!isempty(s1)){                    b[j++]=pop(s1);                    b[j++]=' ';                }                push(s1,a[i++]);            }            else{                push(s1,a[i++]);            }        }    }    while(s1->top!=-1){        b[j++]=pop(s1);        b[j++]=' ';    }    b[--j]='\0';    //puts(b);//输出b中存放的后缀表达式}int main(){    int result;    gets(p);//p存放的是中缀表达式,b存放的是后缀表达式    zhongzhui(p);//把后缀表达式存放在数组b中    result=houzhui(b);    printf("%d\n",result);    return 0;}



0 0