| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1884 人关注过本帖
标题:求助啊大佬 ,用栈求表达式求值
只看楼主 加入收藏
堕落之地
Rank: 1
等 级:新手上路
帖 子:57
专家分:5
注 册:2019-3-11
结帖率:100%
收藏
 问题点数:0 回复次数:0 
求助啊大佬 ,用栈求表达式求值

#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define FLAG 1
typedef struct {
    int  aa;
}ch;
typedef struct sq{
    struct sq *next;
    ch ch1;
}sq;
sq *InitStack(sq *le){
    le=NULL;
    return le;
}
sq *Push(sq *le,ch *cha){
    sq *le1;
    le1=(sq *)malloc(sizeof(sq));
    if(le1==NULL){
        printf("增加节点失败了");
        exit(1);
    }
    //*le1->ch1=*cha;
    le1->next=le;
    le1->ch1=*cha;
    le=le1;
    return le;
}
sq *Pop(sq *le,ch *cha){
    if(NULL==le){
        printf("站空了1,不能出站");
        exit(1);
    }
    sq *le1;
    le1=le;
    *cha=le->ch1;
    le=le->next;
    free(le1);
    return le;
}
ch GetHead(sq *le){
    if(NULL==le){
        printf("站为空,不能取站定元素");
        exit(1);
    }
    return le->ch1;
}
int StackEmpty(sq *le){
    if(NULL==le)
        return TRUE;
    else
        return FALSE;
}
int In(int ch){      //
    switch(ch){
        case '+':
        case '-':
        case '*':
        case '/':
        case '(':
        case ')':
        //case '#':
            return     TRUE;
    }
    return FALSE;
}
int Operate(int a,int b,int chr){
    //ch *cha=(ch *)malloc(sizeof(ch));
    //if(NULL==cha){
    //    printf("申请失败");
    //    exit(1);
    //}
    //le=Pop(le,cha);
    //int a=cha->aa;
    //le=Pop(le,cha);
    //int b=cha->aa;
    //free(cha);
    switch(chr){
        case '+':
            return a+b;
        case '-':
            return a-b;
        case '*':
            return a*b;
        case '/':
            return a/b;     
    }
    printf("这里有错");
    exit(1);
}
 int Precede(sq *le,int Ch){
    if(NULL==le){
        printf("站为空,不能使用");
        exit(1);
    }
    if('#'==le->ch1.aa){
        ch *cha=(ch *)malloc(sizeof(ch));
        if(NULL==cha){
            printf("申请失败");
            exit(1);
        }
        cha->aa=Ch;
        le=Push(le,cha);
        free(cha);
        return '<';  
    }else{
        switch(le->ch1.aa){
            case '+':
                switch(Ch){
                    case '+':
                        return '>';
                    case '-':
                        return '>';
                    case '*':
                        return '<';
                    case '/':
                        return '<';
                    case '(':
                        return '<';
                    case ')':
                        return '>';
                    case '#':
                        return '>';
                }
            case '-':
                switch(Ch){
                    case '+':
                        return '>';
                    case '-':
                        return '>';
                    case '*':
                        return '<';
                    case '/':
                        return '<';
                    case '(':
                        return '<';
                    case ')':
                        return '>';
                }
            case '*':
                switch(Ch){
                    case '+':
                        return '>';
                    case '-':
                        return '>';
                    case '*':
                        return '>';
                    case '/':
                        return '>';
                    case '(':
                        return '<';
                    case ')':
                        return '>';
                }
            case '/':
                switch(Ch){
                    case '+':
                        return '>';
                    case '-':
                        return '>';
                    case '*':
                        return '>';
                    case '/':
                        return '>';
                    case '(':
                        return '<';
                    case ')':
                        return '>';  
                }
            case '(':
                switch(Ch){
                    case '+':
                        return '<';
                    case '-':
                        return '<';
                    case '*':
                        return '<';
                    case '/':
                        return '<';
                    case '(':
                        return '<';
                    case ')':
                        return '=';//只有这种情况相等
                }
            case ')':
                switch(Ch){
                    case '+':
                        return '>';
                    case '-':
                        return '>';
                    case '*':
                        return '>';
                    case '/':
                        return '>';
                    //case '('://不可能有这种情况
                    //    return '<';不可能有这种情况
                    case ')':
                        return '>';
                }
            case '#':
                switch(Ch){
                    case '+':
                        return '<';
                    case '-':
                        return '<';
                    case '*':
                        return '<';
                    case '/':
                        return '<';
                    case '(':
                        return '<';
                    //case ')':
                    //    return '<';
                    //case ')':
                    //    return '=';//只有这种情况相等
                }  
        }
    }
     

}
int main(){
    static int chr;
    sq *OPTR=InitStack(OPTR);//操作符
    sq *OPND=InitStack(OPND);//操作数
    ch *cha=(ch *)malloc(sizeof(ch));
    if(NULL==cha){
        printf("申请失败");
        exit(1);
    }
    chr=getchar();//循环前面有个输入#字符
    cha->aa=chr;
    OPTR=Push(OPTR,cha);
    free(cha);
    chr=getchar();
     while('#'!=chr){//||/* '#'!=GetHead(OPTR).aa*/){//后面这种考虑到输入错误的情况了
         ch *cha=(ch *)malloc(sizeof(ch));
        if(NULL==cha){
            printf("申请失败");
            exit(1);
        }
        cha->aa=chr;
        if(!In(chr)){
            cha->aa=chr-'0';//将字符转换成整数
            OPND=Push(OPND,cha);
            chr=getchar();
            if('#'==chr){
                OPND=Pop(OPND,cha);
                int a=cha->aa;
                OPND=Pop(OPND,cha);
                int b=cha->aa;
                OPTR=Pop(OPTR,cha);
                int c=cha->aa;
                cha->aa=Operate(b,a,c);
                cha->aa=cha->aa;
                OPND=Push(OPND,cha);
                break;
            }
        }      
        else{
            switch(Precede(OPTR,chr)){
                case '<':
                    OPTR=Push(OPTR,cha);
                    if('#'==(chr=getchar())){
                        OPND=Pop(OPND,cha);
                        int a=cha->aa;
                        OPND=Pop(OPND,cha);
                        int b=cha->aa;
                        OPTR=Pop(OPTR,cha);
                        int c=cha->aa;
                        cha->aa=Operate(b,a,c);
                        cha->aa=cha->aa;
                        OPND=Push(OPND,cha);
                    }
                    break;
                case '>':
                    OPND=Pop(OPND,cha);//#1+(1+2+3)#
                    int a=cha->aa;
                    OPND=Pop(OPND,cha);
                    int b=cha->aa;
                    OPTR=Pop(OPTR,cha);
                    int c=cha->aa;
                    cha->aa=Operate(b,a,c);
                    OPND=Push(OPND,cha);
                    printf("%d\n",cha->aa);
                    cha->aa=chr;
                    if('('==OPTR->ch1.aa){
                        OPTR=Pop(OPTR,cha);
                     
                    }else
                        OPTR=Push(OPTR,cha);
                    if('#'==(chr=getchar())){
                        OPND=Pop(OPND,cha);
                        int a=cha->aa;
                        OPND=Pop(OPND,cha);
                        int b=cha->aa;
                        printf("b为%d\n",b);
                        OPTR=Pop(OPTR,cha);
                        int c=cha->aa;
                        cha->aa=Operate(b,a,c);
                        OPND=Push(OPND,cha);
                    }
                    break;
                case '=':
                            OPTR=Pop(OPTR,cha);
                            chr=getchar();
                            if('#'==chr){
                                OPND=Pop(OPND,cha);
                                int a=cha->aa;
                                OPND=Pop(OPND,cha);
                                int b=cha->aa;
                                OPTR=Pop(OPTR,cha);
                                int c=cha->aa;
                                cha->aa=Operate(b,a,c);
                                OPND=Push(OPND,cha);
                            }
                            break;         
                    break;
            }
            free(cha);
        }
     }
    OPND=Pop(OPND,cha);
    printf("表达式值为:%d",cha->aa);
    free(cha);//这里释放cha动态内存     
}
表达式#1+(1+2)#可以求值   表达式#1+(1+2+3)#不能求值 表达式以#开始以#结束

搜索更多相关主题的帖子: int return Push printf case 
2019-04-11 15:03
快速回复:求助啊大佬 ,用栈求表达式求值
数据加载中...
 
   



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

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