| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1522 人关注过本帖
标题:大佬们,这是怎么回事一运行电脑就卡。不能动了
只看楼主 加入收藏
堕落之地
Rank: 1
等 级:新手上路
帖 子:57
专家分:5
注 册:2019-3-11
结帖率:100%
收藏
 问题点数:0 回复次数:1 
大佬们,这是怎么回事一运行电脑就卡。不能动了
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
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;      
    }
}
 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 '-':
                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 '#':
                        return '>';
                }
            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 '#':
                        return '>';   
                }
            case '#':
                switch(Ch){
                    case '+':
                        return '<';
                    case '-':
                        return '<';
                    case '*':
                        return '<';
                    case '/':
                        return '<';
                    case '(':
                        return '<';
                    //case ')':
                    //    return '<';
                    //case ')':
                    //    return '=';//只有这种情况相等
                }   
        }
    }
      

}
int main(){
    int chr;
    sq *OPTR=InitStack(OPTR);//操作符
    sq *OPND=InitStack(OPND);//操作数
    ch *cha=(ch *)malloc(sizeof(ch));
    if(NULL==cha){
        printf("申请失败");
        exit(1);
    }
    cha->aa=getchar();//循环前面有个输入#字符
    OPTR=Push(OPTR,cha);
    //free(cha);
    chr=getchar();
     while('#'!=chr){//||/* '#'!=GetHead(OPTR).aa*/){//后面这种考虑到输入错误的情况了
    //while(NULL!=OPTR){
    //    chr=getchar();
         //ch *cha=(ch *)malloc(sizeof(ch));
        //if(NULL==cha){
        //    printf("申请失败");
        //    exit(1);
        //}
        cha->aa=chr;
        if(In(chr))
            OPND=Push(OPND,cha);
        else{
            switch(Precede(OPTR,chr)){
                case '<':
                    OPTR=Push(OPTR,cha);
                    //chr=getchar();
                    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);
                        OPND=Push(OPND,cha);
                    }
                    break;
                case '>':
                    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);
                    cha->aa=chr;
                    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);
                        OPND=Push(OPND,cha);
                    }
                    //free(cha);最后可以一起释放内存空间
                    break;
                case '=':
                //    ch *cha=(ch *)malloc(sizeof(ch));/
                //    if(NULL==ch){
                //        printf("申请失败");
                //        exit(1);
                //    }前面声明过了,不用在申请了
                    OPTR=Pop(OPTR,cha);
                    //chr=getchar();
                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);
                        OPND=Push(OPND,cha);
                    }
                    //free(cha); 可以最后一起释放内存空间
                    break;
            }
            //free(cha);
        }
        //OPND=Push(OPND,cha);
        //printf("表达式值为:%d",cha->aa);
        //free(cha);//这里释放cha动态内存   
     }
     OPND=Pop(OPND,cha);
     printf("表达式值为:%d",cha->aa);
     free(cha);//这里释放cha动态内存      
}


这是用栈 编写的一个程序   计算表达式的值           以#开始     以#结束。

这是为什么啊
搜索更多相关主题的帖子: int NULL return switch case 
2019-04-09 13:33
堕落之地
Rank: 1
等 级:新手上路
帖 子:57
专家分:5
注 册:2019-3-11
收藏
得分:0 
会了大哥,
2019-04-10 21:23
快速回复:大佬们,这是怎么回事一运行电脑就卡。不能动了
数据加载中...
 
   



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

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