- 12
- 0
- 约4.4千字
- 约 6页
- 2018-06-13 发布于河南
- 举报
堆栈四则运算
#include stdio.h
#include string.h
#include stdlib.h
#include conio.h
#define TRUE 1
#define FALSE 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 20
/************************************************************************/
typedef struct
{
int *pbase;
int *ptop;
int stacksize;
}stack;
/************************************************************************/
char operator[7]=+-*/()!;
char optr;
int opnd=-1;
int result;
char prioritytable[7][7]=
{
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,=,o},
{,,,,o,,},
{,,,,,o,=},
};
/************************************************************************///构造一个空栈
stack initstack()
{
stack s;
s.pbase=(int*)malloc(STACK_INIT_SIZE*sizeof(int));
if (!s.pbase)
{
printf(分配失败);
exit(-1);
}
else
{
s.ptop=s.pbase;
s.stacksize=STACK_INIT_SIZE;
}
return s;
}
/************************************************************************///销毁一个栈
void destorystack(stack *s)
{
if (s-pbase)
{
free(s-pbase);
s-ptop=s-pbase=NULL;
}
}
/************************************************************************///栈不空,则返回栈顶
int gettop(stack s)
{
return *(s.ptop-1);
}
/************************************************************************///压栈
int pushstack(stack *s,int e)
{
if (s-ptop-s-pbase==STACK_INIT_SIZE)
{
s-pbase=(int*)realloc(s-pbase,(s-stacksize+STACKINCREMENT)*sizeof(int));
if (!s-pbase)
{
return 0;
}
s-ptop=s-pbase+s-stacksize;
s-stacksize=s-stacksize+STACKINCREMENT;
}
*(s-ptop++)=e;
return 1;
}
/************************************************************************/
int pop(stack *s,int*e)
{
if (s-ptop==s-pbase)
{
return 0;
}
*e=*--(s-ptop);
return 1;
}
/************************************************************************///比较两个字符的优先级
char checkchar(char operator_1,char operator_2)
{
int i,j;
i=strchr(operator,operator_1)-operator;
j=strchr(operator,operator_2)-operator;
return prioritytable[i][j];
}
/************************************************************
原创力文档

文档评论(0)