gpt4 book ai didi

c - C 中的反向波兰计算器 : isdigit() issues

转载 作者:太空宇宙 更新时间:2023-11-04 07:37:08 25 4
gpt4 key购买 nike

我正在用 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,然后出现问题。

谢谢。

摇滚

最佳答案

改变

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

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

这样您就可以看到是什么导致了该消息。

关于c - C 中的反向波兰计算器 : isdigit() issues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7893982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com