データ構造---表現の求め方(スタック)
2328 ワード
#include
#include
#include
#include
typedef int ElemType;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct{
ElemType *base;//null
ElemType *top;
int stacksize;
}sqStack;
void InitStack(sqStack *s)
{
s->base=(ElemType*)malloc(STACK_INIT_SIZE* sizeof(ElemType));
if(!s->base)
{
exit(1);
}
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
}
void Push(sqStack *s,ElemType e)// e top e
{
if(s->top-s->base>=s->stacksize) //
{
s->base=(ElemType*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(ElemType));
if(!s->base)
{
exit(1);
}
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*(s->top)=e;
s->top++;
//*s->top++=e *((s->op)++)=e
}
void Pop(sqStack *s,char *e)// e ,e
{
if(s->top==s->base)
exit(1);
*e=*(--(s->top));//
}
char GetTop(sqStack *s)
{
if(s->top==s->base)
{
return -1;
}
return *(s->top-1);
}
int In(char e)//
{
if(e=='+'||e=='-'||e=='*'||e=='/'||e=='('||e==')'||e=='#')
return 1;//
else
return 0; //
}
char Precede(char a,char b)//
{
char f;
if(a=='+'||a=='-')
{
if(b=='+'||b=='-'||b==')'||b=='#')
f='>';
else if(b=='*'||b=='/'||b=='(')
f='';
else if(b=='(')
f='';
}
else if(a=='#')
{
if(b=='+'||b=='-'||b=='*'||b=='/'||b=='(')
f='':
Pop(&OPTR,&theta);
Pop(&OPND,&b);
Pop(&OPND,&a);
Push(&OPND,Operate(a,theta,b));
break;
case '=':
Pop(&OPTR,&x);
ch=getchar();
break;
}
}
}
return GetTop(&OPND)-'0';
}
void main()
{
printf(" , #
");
printf(" \t 1*(2-1)# \t
");
printf(" : %d
",EvaluateExpression());
}