C中的反向波兰计算器:isdigit()问题

时间:2011-10-25 18:23:49

标签: c rpn

我正在做C中的家庭作业。我必须构建一个接收RPN的计算器,将其转换为double,将其从堆栈中添加/删除并打印堆栈中剩余的内容。 在我运行之前,一切似乎都进展顺利。在输入我的输入后,char []成功转换为double(或者我认为)并且沿途某处(可能在getop()中)程序认为我没有输入数字。这是代码和输出。

#include <stdio.h>

#define MAXOP 100    /* maximum size of the operand of operator */
#define NUMBER '0'  /* signal that a number was found */

int getOp(char []); /* takes a character string as an input */
void push(double); 
double pop(void);
double asciiToFloat(char []);

/* reverse polish calculator */
int main()
{

    int type;
    double op2;
    char s[MAXOP];

    printf("Please enter Polish Notation or ^d to quit.\n");
    while ((type = getOp(s)) != EOF) {
        switch (type)   {
        case NUMBER:
            push(asciiToFloat(s));
            break;
        case '+':
            push(pop() + pop());
            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 '\n':
            printf("\t%.2f\n", pop()); /* this will print the last item on the stack */ 
            printf("Please enter Polish Notation or ^d to quit.\n"); /* re-prompt the user for further calculation */
            break;
        default:
            printf("error: unknown command %s.\n", s);
            break;
        }
    }
    return 0;   
}

#include <ctype.h>

/* asciiToFloat: this will take ASCII input and convert it to a double */
double asciiToFloat(char s[])
{
    double val;
    double power;

    int i;
    int sign;

    for (i = 0; isspace(s[i]); i++) /* gets rid of any whitespace */
        ;
    sign = (s[i] == '-') ? -1 : 1; /* sets the sign of the input */
    if (s[i] == '+' || s[i] == '-') /* excludes operands, plus and minus */
        i++;
    for (val = 0.0; isdigit(s[i]); i++) 
         val = 10.0 * val + (s[i] - '0'); 
    if (s[i] = '.')
        i++;
    for (power = 1.0; isdigit(s[i]); i++) {
        val = 10.0 * val + (s[i] - '0');
        power *= 10.0;
    }
    return sign * val / power;
}



#define MAXVAL 100  /* maximum depth of value stack */

int sp = 0;     /* next free stack position */
double val[MAXVAL]; /* value stack */

/* push: push f onto value stack */
void push(double f)
{
    if (sp < MAXVAL) {
        val[sp++] = f; /* take the input from the user and add it to the stack */
        printf("The value of the stack position is %d\n", sp);
    }
    else
        printf("error: stack full, cant push %g\n", f);
}

/* pop: pop and return the top value from the stack */
double pop(void)
{
    if (sp > 0)
        return val[--sp];
    else {
        printf("error: stack empty\n"); 
        return 0.0; 
    }
}

#include <ctype.h>

int getch(void);
void ungetch(int);

/* getOp: get next operator or numeric operand */
int getOp(char s[])
{

    int i;
    int c;

    while ((s[0] = c = getch()) == ' ' || c == '\t')
        ;
    s[1] = '\0';
    if (!isdigit(c) && c != '.') {
        printf("Neither a digit or a decimal.\n");
        return c;   /* neither a digit nor a decimal */
    }
    i = 0;
    if (isdigit(c)) /* grab the integer */
        while (isdigit(s[++i] = c = getch()))
            ;
    if (c == '.') /* grab the fraction */
                while (isdigit(s[++i] = c = getch()))
                        ;   
    s[i] = '\0';
    if (c != EOF)
        ungetch(c);
    return NUMBER;
}

#define BUFSIZE 100

char buf[BUFSIZE];  /* buffer for ungetch */
int bufp = 0;       /* next free position in buffer */

/* getch: get a number that may or may not have been pushed back */
int getch(void)
{
    return (bufp > 0) ? buf[--bufp] : getchar(); 
}

/* ungetch: if we read past the number, we can push it back onto input buffer */
void ungetch(int c)
{
    if (bufp >= BUFSIZE)
        printf("ungetch: to many characters.\n");
    else
        buf[bufp++] = c;
}

输出:

请输入波兰表示法或^ d退出。 123 堆栈位置的值为1 无数字或小数。
    123.00 请输入波兰表示法或^ d退出。

任何有关正在发生的事情的想法都会非常有用。似乎然后数字正确传入,从char到double的格式正确,然后出错了。

谢谢。

2 个答案:

答案 0 :(得分:3)

更改

        printf("Neither a digit or a decimal.\n");

        printf("Neither a digit or a decimal: %d 0x%x.\n", c, c);

所以你可以看到导致消息的原因。

答案 1 :(得分:0)

您的输出显示getch()返回行尾的换行符(“\ n”,0x0A)。此外,除非您需要为作业分配编写asciiToFloat(),否则应使用标准C库中的atof()或strtod()(均在“stdlib.h”中声明)。 (通常是?)实施它们是为了避免在转换过程中损失精度和准确度;反复乘以10会导致相同的损失。好看的代码,否则! :)