#include <stdio.h>
#include <stdlib.h>
#define LENc sizeof(char)
#define LENi sizeof(int)
#define INIT_SIZE 100
#define INCREMENT 10
int i = 0;
typedef struct
{
char *base;
char *top;
int stacksize;
}CharStack;
typedef struct
{
int *base;
int *top;
int stacksize;
}IntStack;
void Init_CharStack( CharStack &s )
{
s.base = (char *) malloc (INIT_SIZE*LENc);
if( !s.base )
exit(0);
s.top = s.base;
s.stacksize = INIT_SIZE;
}
void Push_CharStack( CharStack &s, char e )
{
if( s.top-s.base >= s.stacksize )
{
s.base = (char*) realloc ( s.base, (s.stacksize + INCREMENT)*LENc );
s.top = s.base + s.stacksize;
s.stacksize += INCREMENT;
}
*s.top++ = e;
}
void Pop_CharStack( CharStack &s, char &e )
{
if( s.top == s.base )
exit(0);
e = *--s.top;
}
char Getop_CharStack( CharStack &s )
{
if( s.top == s.base )
return '1';
else
return *(s.top-1);
}
void Init_IntStack( IntStack &s )
{
s.base = (int *) malloc (INIT_SIZE*LENi);
if( !s.base )
exit(0);
s.top = s.base;
s.stacksize = INIT_SIZE;
}
void Push_IntStack( IntStack &s, int e )
{
if( s.top-s.base >= s.stacksize )
{
s.base = (int*) realloc ( s.base, (s.stacksize + INCREMENT)*LENi );
s.top = s.base + s.stacksize;
s.stacksize += INCREMENT;
}
*s.top++ = e;
}
void Pop_IntStack( IntStack &s, int &e )
{
if( s.top == s.base )
exit(0);
e = *--s.top;
}
int Getop_IntStack( IntStack &s )
{
if( s.top == s.base )
return 1;
else
return *(s.top-1);
}
void Total(CharStack &OPND, IntStack &sum )
{
int sum1, sum2;
if( Getop_CharStack( OPND )>='0' && Getop_CharStack( OPND )<='9' )
i = i*10 + Getop_CharStack( OPND ) - 48;
else if( Getop_CharStack( OPND )==' ' )
{
Push_IntStack( sum, i);
i = 0;
}
else
switch( Getop_CharStack( OPND ) )
{
case '+':
Pop_IntStack( sum, sum1 );
Pop_IntStack( sum, sum2 );
sum1 += sum2;
Push_IntStack( sum, sum1 );
break;
case '-':
Pop_IntStack( sum, sum1 );
Pop_IntStack( sum, sum2 );
sum1 -= sum2;
Push_IntStack( sum, sum1 );
break;
case '*':
Pop_IntStack( sum, sum1 );
Pop_IntStack( sum, sum2 );
sum1 *= sum2;
Push_IntStack( sum, sum1 );
break;
case '/':
Pop_IntStack( sum, sum1 );
Pop_IntStack( sum, sum2 );
sum1 = sum2/sum1;
Push_IntStack( sum, sum1 );
break;
}
}
void Work()
{
CharStack OPND, OPTR;
char e, c;
IntStack sum;
Init_CharStack( OPND );
Init_CharStack( OPTR );
Init_IntStack( sum );
Push_CharStack( OPTR, '#' );
printf("请输入正确地格式,中间不能有空格,并以‘#’号作为\n结束符号!\n");
printf("前缀表达式: ");
while( 1 )
{
c = getchar();
if( c>='0' && c<='9' )
{
Push_CharStack( OPND, c );
Total( OPND, sum );
}
else if( c=='(' || c==')' )
{
if( c==')' )
{
while( Getop_CharStack( OPTR ) != '(' )
{
// Total( OPND, sum );
Pop_CharStack( OPTR, e );
if( Getop_CharStack( OPND )>='0' && Getop_CharStack( OPND )<='9' )
{
Push_CharStack( OPND, ' ' );
Total( OPND, sum );
}
Push_CharStack( OPND, e );
Total( OPND, sum );
}
Pop_CharStack( OPTR, e );
}
else
Push_CharStack( OPTR, c );
}
else
{
//Push_CharStack( OPND, ' ');
if( c=='+' || c=='-' )
loop:switch( Getop_CharStack( OPTR ) )
{
case '/':
case '*':
//Total( OPND, sum );
Pop_CharStack( OPTR, e );
if( Getop_CharStack( OPND )>='0' && Getop_CharStack( OPND )<='9' )
{
Push_CharStack( OPND, ' ' );
Total( OPND, sum );
}
Push_CharStack( OPND, e );
Total( OPND, sum );
goto loop;
break;
default:
// Total( OPND, sum );
if( Getop_CharStack( OPND )>='0' && Getop_CharStack( OPND )<='9' )
{
Push_CharStack( OPND, ' ' );
Total( OPND, sum );
}
Push_CharStack( OPTR, c );
break;
}
if( c=='*' || c=='/' )
{
if( Getop_CharStack( OPND )>='0' && Getop_CharStack( OPND )<='9' )
{
Push_CharStack( OPND, ' ' );
Total( OPND, sum );
}
Push_CharStack( OPTR, c);
}
if( c=='#')
{
while( Getop_CharStack( OPTR ) != '#' )
{
//Total( OPND, sum );
Pop_CharStack( OPTR, e );
if( Getop_CharStack( OPND )>='0' && Getop_CharStack( OPND )<='9' )
{
Push_CharStack( OPND, ' ' );
Total( OPND, sum );
}
Push_CharStack( OPND, e );
Total( OPND, sum );
}
Pop_CharStack( OPTR, e );
}
}
//Total( OPND, sum );
if( c=='#' )
goto lop;
}
lop:while( OPND.base != OPND.top )
{
Pop_CharStack( OPND, e );
Push_CharStack( OPTR, e );
}
printf("输出后缀表达式: ");
while( OPTR.base != OPTR.top )
{
Pop_CharStack( OPTR, e );
printf("%c",e);
}
printf("\n");
printf("输出计算结果: ");
printf("%d\n", Getop_IntStack( sum ) );
}
void main()
{
Work();
}