| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 715 人关注过本帖
标题:表达式求值,代码错在哪?
只看楼主 加入收藏
zzxwill
Rank: 1
等 级:新手上路
帖 子:398
专家分:0
注 册:2007-8-15
收藏
 问题点数:0 回复次数:1 
表达式求值,代码错在哪?
#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;
    }
}
搜索更多相关主题的帖子: 求值 代码 表达 
2007-12-04 21:01
pinglideyu
Rank: 3Rank: 3
来 自:武汉工程大学
等 级:论坛游侠
威 望:1
帖 子:735
专家分:140
注 册:2007-1-7
收藏
得分:0 
我把你的代码改了一下:
#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);
        }
}

~~我的明天我知道~~
2007-12-04 22:52
快速回复:表达式求值,代码错在哪?
数据加载中...
 
   



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

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