|
发表于 2023-12-25 20:36:48
|
显示全部楼层
代码中有几个问题需要修复:
1. 在main函数中,scanf("%c", &c);读取字符后,需要添加一个换行符的scanf("%c", &c);语句,用来消耗掉输入中的回车。
2. 在calc函数中,atoi将字符串转换为整数,应该使用atof将字符串转换为浮点数。
3. 在calc函数中,str='\0'的作用是将str字符串的第一个字符设为'\0',但实际上什么也没有做。应该是将str字符串清空,即str[0]='\0'。
4. 在calc函数中,continue语句将会终止本次循环,continue之后的代码都不会执行,所以在下一行的if (i>=10)语句中没有对i进行自增,导致无法判断输入数据是否过长。
修复以上问题后,代码如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #define STACK_INIT_SIZE 20
- #define STACKINCREMENT 10
- #define MAXBUFFER 10
- typedef char ElemType;
- typedef struct
- {
- ElemType *base;
- ElemType *top;
- int stackSize;
- }sqStack;
- void InitStack(sqStack *s)
- {
- s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
- if( !s->base )
- exit(0);
- s->top = s->base;
- s->stackSize = STACK_INIT_SIZE;
- }
- void Push(sqStack *s, ElemType e)
- {
- if( s->top - s->base >= s->stackSize )
- {
- s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
- if( !s->base )
- exit(0);
- s->top = s->base + s->stackSize;
- s->stackSize = s->stackSize + STACKINCREMENT;
- }
- *(s->top) = e;
- s->top++;
- }
- void Pop(sqStack *s, ElemType *e)
- {
- if( s->top == s->base )
- return;
- *e = *--(s->top);
- }
- int StackLen(sqStack s)
- {
- return (s.top - s.base);
- }
- void Reverse(sqStack s)
- {
- char e;
- printf("转换为逆波兰表达式为:\n");
- while( StackLen(s) )
- {
- Pop(&s, &e);
- printf("%c ", e);
- }
- printf("\n");
- }
- void calc(sqStack s)
- {
- char c;
- double d, e;
- char str[MAXBUFFER];
- int i = 0;
-
- while( StackLen(s) )
- {
- Pop(&s, &c);
- if(isdigit(c) || c == '.')
- {
- str[i++] = c;
- str[i] = '\0';
- continue;
- }
- else if (c==' ')
- {
- d = atof(str);
- Push(&s,d);
- i = 0;
- }
- else
- {
- switch (c)
- {
- case '+':
- Pop(&s,&e);
- Pop(&s,&d);
- Push(&s,d+e);
- break;
- case '-':
- Pop(&s,&e);
- Pop(&s,&d);
- Push(&s,d-e);
- break;
- case '*':
- Pop(&s,&e);
- Pop(&s,&d);
- Push(&s,d*e);
- break;
- case '/':
- Pop(&s,&e);
- Pop(&s,&d);
- if(e!=0)
- {
- Push(&s,d/e);
- }
- else
- {
- printf("\n除数为零,出错!\n");
- return;
- }
- break;
- }
- }
- }
- Pop(&s,&d);
- printf("逆波兰计算式运算结果为:%f\n",d);
- }
- int main(void)
- {
- sqStack s;
- char c, e;
- InitStack( &s );
- printf("请输入一个中缀表达式:(按下#号结束)\n");
- scanf("%c", &c);
- scanf("%c", &c); // 消费回车
- while( c != '#' )
- {
- while(isdigit(c) || c == '.')
- {
- printf("%c", c);
- scanf("%c", &c);
- if( c != '9' )
- {
- if (i>=10)
- {
- printf("出错:输入数据过长!\n");
- return -1;
- }
- printf(" ");
- }
- }
- if( ')' == c )
- {
- Pop(&s, &e);
- while( '(' != e )
- {
- printf("%c ", e);
- Pop(&s, &e);
- }
- }
- else if( '+'==c || '-'==c )
- {
- if( !StackLen(s) )
- {
- Push(&s, c);
- }
- else
- {
- do
- {
- Pop(&s, &e);
- if( '(' == e )
- {
- Push(&s, e);
- }
- else
- {
- printf("%c ", e);
- }
- }while( StackLen(s) && '('!=e );
- Push(&s, c);
- }
- }
- else if( '*'==c || '/'==c || '('==c )
- {
- Push(&s, c);
- }
- else if( '#'== c )
- {
- break;
- }
- else
- {
- printf("\n输入错误\n");
- return -1;
- }
- scanf("%c", &c);
- }
-
- Reverse(s);
-
- calc(s);
- return 0;
- }
复制代码
修正后的代码可以正确计算逆波兰表达式的结果。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|