当前位置: 代码迷 >> C语言 >> 表达式求值,代码错在哪?
  详细解决方案

表达式求值,代码错在哪?

热度:299   发布时间:2007-12-04 21:01:42.0
表达式求值,代码错在哪?
#include <stdio.h>
//算法2.11
//使用堆栈的顺序存储结构
char Precede(char ch1,char ch2);
int Operate(int m,char ch,int n);
main()
{
    char OPTR[100];
    char OPND[100];
    char c,cpri;
    int i,j;
    i=j=0;
    OPTR[0]='#';
    c=getchar();
    while(c!='#'||OPTR[i]!='#'){
        cpri=c;
        c=getchar();
        if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c=='#'||c==')'){
            switch(Precede(c,cpri)){
                case '<':
                    OPTR[++i]=c;
                case '=':
                    OPTR[i]='\0';
                    cpri=OPTR[i--];
//                    c=getchar();
//                    OPTR[++i]=c;
                    break;

                case '>':
                    OPND[j]=Operate(OPND[j-1],c,OPND[j]);
                    break;


            }
        }
        else
            OPND[j++]=c-'0';
    }
    printf("%d",OPND[0]);



}



char Precede(char ch1,char ch2){
    if( (ch1=='+'||ch1=='-'||ch1=='*'||ch1=='/'||ch1=='('||ch1=='#')&&ch2=='('
        ||(ch1=='+'||ch1=='-'||ch1=='('||ch1=='#')&&(ch2=='/'||ch2=='*')
        ||(ch1=='('||ch1=='#')&&(ch2=='+'||ch2=='*') ){
        return '<';
    }
    else if(ch1=='('&&ch2==')'||ch1=='#'&&ch2=='#'){
        return '=';
    }
    else
        return '>';
}


int Operate(int m,char ch,int n){
    switch(ch){
    case '+':
        return m+n;
        break;
    case '-':
        return m-n;
        break;
    case '*':
        return m*n;
        break;
    case '/':
        return m/n;
        break;
    }
}
搜索更多相关的解决方案: 求值  代码  表达  

----------------解决方案--------------------------------------------------------
我把你的代码改了一下:
#include <stdio.h>
#include <stdlib.h>
//算法2.11
//使用堆栈的顺序存储结构
char Precede(char ch1,char ch2);
int Operate(int m,char ch,int n);
int main()
{
        char OPTR[100];
        char OPND[100];
        char c,cpri;
        int i,j;
        i=j=0;
        OPTR[0]='#';
        c=getchar();
        while(c!='#'||OPTR[i]!='#'){
                cpri=c;
                c=getchar();
                if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c=='#'||c==')'){
                        switch(Precede(c,cpri)){
                                case '<':
                                        OPTR[++i]=c;
                                case '=':
                                        OPTR[i]='\0';
                                        cpri=OPTR[i--];
//                                        c=getchar();
//                                        OPTR[++i]=c;
                                        break;

                                case '>':
                                        OPND[j]=Operate(OPND[j-1],c,OPND[j]);
                                        break;


                        }
                }
                else
                        OPND[j++]=c-'0';
        }
        printf("%d",OPND[0]);

return 0;

}



char Precede(char ch1,char ch2){
        if( (ch1=='+'||ch1=='-'||ch1=='*'||ch1=='/'||ch1=='('||ch1=='#')&&ch2=='('
                ||(ch1=='+'||ch1=='-'||ch1=='('||ch1=='#')&&(ch2=='/'||ch2=='*')
                ||(ch1=='('||ch1=='#')&&(ch2=='+'||ch2=='*') ){
                return '<';
        }
        else if(ch1=='('&&ch2==')'||ch1=='#'&&ch2=='#'){
                return '=';
        }
        else
                return '>';
}


int Operate(int m,char ch,int n){
        switch(ch){
        case '+':
                return m+n;
                break;
        case '-':
                return m-n;
                break;
        case '*':
                return m*n;
                break;
        case '/':
                return m/n;
                break;
        default:exit(1);
        }
}
----------------解决方案--------------------------------------------------------
  相关解决方案