★数据输入读入一个只包含 +, -, *, / , (, ) 的非负整数计算表达式,该表达式的后缀表达式。每个输入为一行字符串, 字符串长度不超过 100000,整数和运算符之间用一个空格分隔。没有非法表达式。
★数据输出对于每个输入, 输出 2 行, 第一行即该表达式的后缀表达式, 数与数之间、 符号与数之间、符号与符号之间都应有一个空格隔开(注意行末无空格),第二行是该算式的值, 精确到小数点后 2 位。
输入示例 输出示例
681 + 62 - 83 681 62 + 83-
660.00
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>
#define STACK_INIT_SIZE 100000
#define STACKINCREMENT 10
#define MAXBUFFER 20
typedef char BaseType,BaseType1;
typedef double ElemType;
typedef struct
{
BaseType *base;
BaseType *top;
int stackSize;
}aStack;
typedef struct
{
BaseType1 *base;
BaseType1 *top;
int stackSize;
}aStack1;
typedef struct
{
ElemType *base;
ElemType *top;
int stackSize;
}sqStack;
void StackInit(aStack *s);
void Push(aStack *s,BaseType b);
void Pop(aStack *s,BaseType *b);
int StackLen(aStack s);
void StackInit1(aStack1 *s);
void Push2(aStack1 *s,BaseType1 b);
void Pop2(aStack1 *s,BaseType1 *b);
int StackLen2(aStack1 s);
void InitStack(sqStack *s);
void Push1(sqStack *s,ElemType b);
void Pop1(sqStack *s,ElemType *b);
int StackLen1(sqStack s);
int main()
{
aStack s;
sqStack q;
aStack1 r;
char c,e;
char str[MAXBUFFER];
double d,f;
int i=0;
StackInit(&s);
InitStack(&q);
StackInit1(&r);
scanf("%c",&c);
while(c!='\n')
{
while(c>='0'&&c<='9')
{
Push2(&r,c);
printf("%c",c);
scanf("%c",&c);
if(c<'0'||c>'9')
{
Push2(&r,' ');
printf(" ");
}
}
if(c==')')
{
Pop(&s,&e);
while(e!='(')
{
Push2(&r,e);
Push2(&r,' ');
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
{
Push2(&r,e);
Push2(&r,' ');
printf("%c ",e);
}
}while(StackLen(s)&&e!='(');
Push(&s,c);
}
}
else if(c=='(')
{
Push(&s,c);
}
else if(c=='*'||c=='/')
{
if(!StackLen(s))
{
Push(&s,c);
}
else
{
Pop(&s,&e);
if(e=='*'||e=='/')
{
Push2(&r,e);
Push2(&r,' ');
printf("%c ",e);
}
else
{
Push(&s,e);
}
Push(&s,c);
}
}
else if(c=='\n')
break;
scanf("%c",&c);
}
while(StackLen(s))
{
Pop(&s,&e);
Push2(&r,e);
Push2(&r,' ');
printf("%c ",e);
}
e='#';
Push2(&r,e);
while(StackLen2(r))
{
Pop2(&r,&c);
Push(&s,c);
}
Pop(&s,&c);
while(c!='#')
{
while(isdigit(c))
{
str[i++]=c;
str[i]='\0';
Pop(&s,&c);
if(c==' ')
{
d=atof(str);
Push1(&q,d);
i=0;
break;
}
}
switch(c)
{
case'+':
Pop1(&q,&f);
Pop1(&q,&d);
Push1(&q,d+f);
break;
case'-':
Pop1(&q,&f);
Pop1(&q,&d);
Push1(&q,d-f);
break;
case'*':
Pop1(&q,&f);
Pop1(&q,&d);
Push1(&q,d*f);
break;
case'/':
Pop1(&q,&f);
Pop1(&q,&d);
Push1(&q,d/f);
break;
}
Pop(&s,&c);
}
Pop1(&q,&d);
printf("\n%.2f",d);
return 0;
}
void StackInit(aStack *s)
{
s->base=(BaseType *)malloc(STACK_INIT_SIZE*sizeof(BaseType));
if(!s->base)
exit(0);
s->top=s->base;
s->stackSize=STACK_INIT_SIZE;
}
void Push(aStack *s,BaseType b)
{
if(s->top-s->base>=s->stackSize)
{
s->base=(BaseType *)realloc(s->base,(s->stackSize+STACKINCREMENT)*sizeof(BaseType));
if(!s->base)
exit(0);
}
*(s->top)=b;
s->top++;
}
void Pop(aStack *s,BaseType *b)
{
if(s->top==s->base)
return;
*b=*--(s->top);
}
int StackLen(aStack s)
{
return(s.top-s.base);
}
void StackInit1(aStack1 *s)
{
s->base=(BaseType1 *)malloc(STACK_INIT_SIZE*sizeof(BaseType1));
if(!s->base)
exit(0);
s->top=s->base;
s->stackSize=STACK_INIT_SIZE;
}
void Push2(aStack1 *s,BaseType1 b)
{
if(s->top-s->base>=s->stackSize)
{
s->base=(BaseType1 *)realloc(s->base,(s->stackSize+STACKINCREMENT)*sizeof(BaseType1));
if(!s->base)
exit(0);
}
*(s->top)=b;
s->top++;
}
void Pop2(aStack1 *s,BaseType1 *b)
{
if(s->top==s->base)
return;
*b=*--(s->top);
}
int StackLen2(aStack1 s)
{
return(s.top-s.base);
}
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 Push1(sqStack *s,ElemType b)
{
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)=b;
s->top++;
}
void Pop1(sqStack *s,ElemType *b)
{
if(s->top==s->base)
return;
*b=*--(s->top);
}
int StackLen1(sqStack s)
{
return(s.top-s.base);
}