0

私は 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 に適切にフォーマットされているようですが、何か問題が発生しています。

ありがとうございました。

4

2 に答える 2

3

変化する

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

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

メッセージの原因を確認できます。

于 2011-10-25T18:36:57.317 に答える
0

出力は、getch() が行末に改行 ("\n", 0x0A) を返していることを示しています。また、宿題のために asciiToFloat() を記述する必要がない限り、標準 C ライブラリの atof() または strtod() (どちらも「stdlib.h」で宣言されている) を使用する必要があります。それらは(通常?)変換中の精度と精度の損失を避けるために実装されています。繰り返し 10 を掛けると、同じものが失われます。そうでなければ、見栄えの良いコードです!:)

于 2011-10-25T20:26:27.710 に答える