注册 登录
编程论坛 数据结构与算法

表达式求值的程序

zhuchenxi 发布于 2011-10-02 00:11, 1048 次点击
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define STACK_SIZE 100
#define STACK_INC_SIZE 10
typedef struct{
    char *top,*base;
    int length;
}CStack;

typedef struct{
    int length,*top,*base;
}MStack;

int InitCStack(CStack &S)
{
    S.base=(char *)malloc(STACK_SIZE*sizeof(char));
    if(!S.base) return 0;
    S.top=S.base;
    S.length=STACK_SIZE;
    return 1;
}

int InitMStack(MStack &S)
{
    S.base=(int *)malloc(STACK_SIZE*sizeof(int));
    if(!S.base) return 0;
    S.top=S.base;
    S.length=STACK_SIZE;
    return 1;
}

int GetTop(CStack &S,char &e)
{
    if(S.base==S.top) return 0;   
    e=*S.top;
    return 1;
}

int CPush(CStack &S,char e)
{
    if(S.top-S.base>=S.length){
        S.base=(char *)realloc(S.base,(STACK_SIZE+STACK_INC_SIZE)*sizeof(char));
        if(!S.base) return 0;
        S.top=S.base+S.length;
        S.length+=STACK_INC_SIZE;
    }
    S.top++;
    *S.top=e;
    return 1;
}

int MPush(MStack &S,int e)
{
    if(S.top-S.base>=S.length){
        S.base=(int *)realloc(S.base,(STACK_SIZE+STACK_INC_SIZE)*sizeof(int));
        if(!S.base) return 0;
        S.top=S.base+S.length;
        S.length+=STACK_INC_SIZE;
    }
    S.top++;
    *S.top=e;
    return 1;
}

int CPop(CStack &S,char &e)
{
    if(S.base==S.top) return 0;
    e=*S.top;
    S.top--;
    return 1;
}

int MPop(MStack &S,int &e)
{
    if(S.base==S.top) return 0;
    e=*S.top;
    S.top--;
    return 1;
}

int In(char c)
{
    if(c>='0'&&c<='9') return 1;
    else
        return 0;
}

int yunsuan(int a,char e,int b)
{
    int jieguo;
    if(e=='+') jieguo=a+b;
    if(e=='-') jieguo=a-b;
    if(e=='*') jieguo=a*b;
    if(e=='/') jieguo=a/b;
    return jieguo;
}

char youxian(char e,char c)
{
    char youxian;
    if(e=='+'){
        if(c=='+'||c=='-'||c==')'||c=='#') youxian='>';
        else youxian='<';
    }
    if(e=='-'){
        if(c=='+'||c=='-'||c==')'||c=='#') youxian='>';
        else youxian='<';
    }
    if(e=='*'){
        if(c=='+'||c=='-'||c=='*'||c=='/'||c==')'||c=='#') youxian='>';
        else youxian='<';
    }
    if(e=='/'){
        if(c=='+'||c=='-'||c=='*'||c=='/'||c==')'||c=='#') youxian='>';
        else youxian='<';
    }
    if(e=='('){
        if(c==')') youxian='=';
        else youxian='<';
    }
    if(e==')') youxian='>';
    if(e=='#'){
        if(c=='#') youxian='=';
        else youxian='<';
    }
    return youxian;
}

int main()
{
    CStack fuhao;
    MStack shuzi;
    char ch,str[100],x,e1,e2,e3;
    int i,k,Zshuzi,a,b,jieguo;
    InitMStack(shuzi);
    InitCStack(fuhao);
    CPush(fuhao,'#');
    scanf("%c",&ch);
    k=0;
    while(ch!='#'||GetTop(fuhao,e1)&&e1!='#'){
        Zshuzi=0;
        if(In(ch)==1){
            str[k]=ch;
            k++;
            scanf("%c",&ch);
        }
        else{
            for(i=0;i<k;i++){
                Zshuzi+=(str[i]-48)*pow(10,k-i-1);
            }
            if(k!=0) MPush(shuzi,Zshuzi);
            k=0;
            GetTop(fuhao,e2);
            switch(youxian(e2,ch)){
            case'<':
                CPush(fuhao,ch);
                scanf("%c",&ch);
                break;
            case'=':
                CPop(fuhao,x);
                scanf("%c",&ch);
                break;
            case'>':
                CPop(fuhao,e3);
                MPop(shuzi,a),MPop(shuzi,b);
                MPush(shuzi,yunsuan(b,e3,a));
                break;
            }
        }
    }
    MPop(shuzi,jieguo);
    printf("%d\n",jieguo);
    return 0;
}





[ 本帖最后由 zhuchenxi 于 2011-11-4 23:59 编辑 ]
4 回复
#2
zhuchenxi2011-10-02 22:35
怎么还没人来啊,都过节去了??
#3
zhuchenxi2011-10-03 12:21
两天了啊,哎
#4
shanshan30032011-10-03 15:38
这么长啊,咋看啊?不懂
#5
zhuchenxi2011-10-03 19:52
回复 4楼 shanshan3003
原来你比我还新手
1