| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1048 人关注过本帖
标题:表达式求值的程序
只看楼主 加入收藏
zhuchenxi
Rank: 1
等 级:新手上路
帖 子:61
专家分:6
注 册:2011-4-28
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
表达式求值的程序
#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 编辑 ]
搜索更多相关主题的帖子: include return 表达式 
2011-10-02 00:11
zhuchenxi
Rank: 1
等 级:新手上路
帖 子:61
专家分:6
注 册:2011-4-28
收藏
得分:0 
怎么还没人来啊,都过节去了??
2011-10-02 22:35
zhuchenxi
Rank: 1
等 级:新手上路
帖 子:61
专家分:6
注 册:2011-4-28
收藏
得分:0 
两天了啊,哎
2011-10-03 12:21
shanshan3003
Rank: 2
等 级:论坛游民
帖 子:53
专家分:66
注 册:2011-8-29
收藏
得分:20 
这么长啊,咋看啊?不懂
2011-10-03 15:38
zhuchenxi
Rank: 1
等 级:新手上路
帖 子:61
专家分:6
注 册:2011-4-28
收藏
得分:0 
回复 4楼 shanshan3003
原来你比我还新手
2011-10-03 19:52
快速回复:表达式求值的程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016819 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved