逆波蘭運算式的C實現

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   io   資料   

複習下資料結構,用棧簡單實現逆波蘭運算式,參考文檔:

http://www.nowamagic.net/librarys/veda/detail/2307http://www.nowamagic.net/librarys/veda/detail/2306

 

直接上代碼:

/***code by lichmama from cnblogs.com*@逆波蘭運算式的C實現*算術支援的運算模式:*   四則運算;*   運算元不得大於9;*   中間數(即運算當中的臨時變數)不得超過127**/#include <stdio.h>#include <stdlib.h>typedef struct __STACK__ {    char op;    struct __STACK__ *next;}STACK, *PSTACK;void init(PSTACK *);void push(PSTACK *, char);void pop(PSTACK *, char *);void clear(PSTACK *);void destroy(PSTACK *);char getoplevel(char op){    if(op==‘+‘ || op==‘-‘)        return 1;    if(op==‘*‘ || op==‘/‘)        return 2;    return 0;}char calc(char *rpn){    char *p=rpn;    char e;    int x, y;    PSTACK pStack;    init(&pStack);    while(*p){        if(0<=*p && *p<=9){            push(&pStack, *p);        }else {            pop(&pStack, &e);x=e;            pop(&pStack, &e);y=e;            switch(*p){                case ‘+‘:push(&pStack, y+x);break;                case ‘-‘:push(&pStack, y-x);break;                case ‘*‘:push(&pStack, y*x);break;                case ‘/‘:push(&pStack, y/x);break;            }        }        p++;    }    pop(&pStack, &e);    free(pStack);pStack=NULL;    return e;}int main(void){    char e;    char old_exp[]="(3-1)*3+8/2+(9*3/(2+1)+3*4/6)-2=";    char rpn_exp[256]="";    char *p=old_exp;    char *r=rpn_exp;    PSTACK gStack;    init(&gStack);//    while(*p!=‘\0‘ && *p!=‘=‘){        if(‘0‘<=*p && *p<=‘9‘){            *r++=(*p-‘0‘);        }else if(*p==‘(‘){            push(&gStack, ‘(‘);        }else if(*p==‘)‘){            for(;;){                pop(&gStack, &e);                if(e==‘(‘)break;                *r++=e;            }        }else if(*p==‘+‘ || *p==‘-‘ || *p==‘*‘ || *p==‘/‘){            if(getoplevel(gStack->op)<getoplevel(*p)){                push(&gStack, *p);            }else{                for(;;){                    pop(&gStack, &e);                    if(getoplevel(e)<getoplevel(*p)){                        if(e!=‘#‘)push(&gStack, e);                        push(&gStack, *p);break;                    }                    *r++=e;                }            }        }        p++;    }    for(;;){        pop(&gStack, &e);        if(e==‘#‘)break;        *r++=e;    }    //printf("%s\n", rpn_exp);    printf("%d\n", calc(rpn_exp));//    clear(&gStack);    destroy(&gStack);    return 0;}void init(PSTACK *s){    *s=(PSTACK)malloc(sizeof(STACK));    (*s)->op=‘#‘;    (*s)->next=NULL;}void push(PSTACK *s, char e){    PSTACK p=(PSTACK)malloc(sizeof(STACK));    p->op=e;    p->next=*s;    *s=p;}void pop(PSTACK *s, char *e){    if((*s)->next){        PSTACK p=(*s);        *e=(*s)->op;        *s=(*s)->next;        free(p);        p=NULL;    }else *e=‘#‘;}void clear(PSTACK *s){    PSTACK p;    while((*s)->next){        p=*s;        *s=(*s)->next;        free(p);        p=NULL;    }}void destroy(PSTACK *s){    free(*s);    *s=NULL;}

 

 註:改善運算元的限制可使用整形(或浮點型)數組存放中間變數。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.