C程序设计语言4-6课后题参考答案不正确

4-6如果采用官方“配套”(其实是另外的人写的)参考书的话,答案代码是不正确的,无法顺利达到目的。不过稍加修改即可:

首先,全局定义var参数(名字自己定),main代码和推荐的一致:

int var;//定义var在这里

main(){
    int type,i;
    double op1,op2,v;
    char s[MAXOP];
    void clean(void);
    double vars[26];

    for(i=0;i<26;i++)
        vars[i]=0.0;
    while((type=getop(s)) !=EOF){
        switch(type){
        case NUMBER:
            push(atof(s));
            break;
        case '+':
            push(pop()+pop());
            break;
        case FUNC:
            mathFunc(s);
            break;
        case '*':
            push(pop()*pop());
            break;
        case '-':
            op2=pop();
            push(pop()-op2);
            break;
        case '/':
            op2=pop();
            if(op2!=0.0)
                push(pop()/op2);
            else
                printf("error:zero divisor\n");
            break;
        case '%'://模除运算
            op2=pop();
            if(op2!=0.0)
                push(fmod(pop(),op2));
            else
                printf("除数不可为0\n");
            break;
        case'\n':
            printf("\t%.8g\n",pop());
            break;
        case '?':
            //打印栈顶元素
            op2=pop();
            printf("\t%.8g\n",op2);
            push(op2);
            break;
        case 'c':
            //清空栈
            clean();
            break;
        case 's':
            op1=pop();
            op2=pop();
            push(op1);
            push(op2);
            break;
        case '=':
            pop();
            if(var>='A' &&var<='Z'){
                vars[var-'A']=pop();
                }
            else
                printf("记录变量失败");
            break;
        default:
            if (type>='A' &&type<='Z')
                push(vars[type-'A']);
            else if (type=='v')
                push(v);
            else
                printf("error:unknown command %s\n",s);
            break;
        }
    }
    return 0;
}

接下来,修改getop,增加大写字母检测:

int getop(char s[]){
    int i,c;

    while((s[0]=c=getch())==' '||c=='\t')
        ;
    s[1]='\0';
    i=0;
    if(isupper(c)){//增加大写字母检测
        var=c;
        return c;
    }
    if(islower(c)){
        int p=c;
        //在这里C被改变了,所以需要用新变量暂存,否则达不到预期结果
        while(islower(s[++i]=c=getch()))
            ;
        s[i]='\0';
        if(c!=EOF){
            ungetch(c);
            }
        if(strlen(s)>1){
            return FUNC;
        }
        else{
            return p;
        }
    }
    if (!isdigit(c) && c!='.'&& c!='-')
        return c;

    //在这里检验负数
    if(c == '-'){
        if(isdigit(s[++i]=c=getch())){
            while (isdigit(s[++i]=c=getch()))
                ;
        }
        else if((s[++i]=c=getch())=='.'){
            while (isdigit(s[++i]=c=getch()))
                ;
        }
        else{
            if(c!=EOF)
                ungetch(c);
            return '-';
            }
        }
    if(isdigit(c))
        while (isdigit(s[++i]=c=getch()))
            ;
    if(c == '.')
        while (isdigit(s[++i]=c=getch()))
            ;
    s[i]='\0';
    if(c!=EOF)
        ungetch(c);
    return NUMBER;
}

OK了,其实方法不止这一个。

猜你喜欢

转载自blog.csdn.net/PlusChang/article/details/78996225
今日推荐