栈处理四则运算
#ifndef STRACK
#define STRACK
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int ElemType;
#define INCREASEMENT_SIZE 10
typedef struct STRACKNODE{
ElemType* top;
ElemType* base;
int count;
}Strack, *StrackPtr;
StrackPtr InitStrack();//初始化栈
void PushStrack(StrackPtr s, ElemType e);//把元素压入栈
bool StrackEmpty();//判断栈是否为空
void StrackClear(StrackPtr s);//清空栈
void StrackFree(StrackPtr s);//释放栈
ElemType GetTop(StrackPtr s);//得到栈顶元
int StrackLength(StrackPtr s);//栈的长度
void Pop(StrackPtr s, ElemType* e);//删除栈顶元素,并将其赋值给e
void Print(StrackPtr s); //打印元素
bool Number(char c);
void Precede(char a, char b, char* ch);
ElemType Resault(ElemType a,char c, ElemType b);
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "strack.h"
int INITSIZE = 100;
StrackPtr InitStrack()
{
StrackPtr s = (StrackPtr)malloc(sizeof(Strack));
s->top = s->base = (ElemType*)malloc(sizeof(ElemType)*INITSIZE);
s->count = 0;
return s;
}
void PushStrack(StrackPtr s, ElemType e)
{
if (s->top - s->base >= INITSIZE)
{
s->base = (ElemType*)realloc(s->base, (INITSIZE + INCREASEMENT_SIZE)*sizeof(ElemType));
s->top = s->base + INITSIZE;
INITSIZE += INCREASEMENT_SIZE;
}
*(s->top)++ = e;
s->count++;
}
bool StrackEmpty(StrackPtr s)
{
if (s->base == s->top)
{
return true;
}
else
{
return false;
}
}
void StrackClear(StrackPtr s)
{
if (s->base == s->top)
{
return;
}
s->count = 0;
s->top = s->base;
}
void StrackFree(StrackPtr s)
{
if (s->base == NULL)
{
return;
}
free(s->base);
}
ElemType GetTop(StrackPtr s)
{
if (s->top == s->base)
{
return -1;
}
return *(s->top - 1);
}
int StrackLength(StrackPtr s)
{
return s->count;
}
void Pop(StrackPtr s, ElemType* e)
{
if (s->top == s->base)
{
return;
}
*e = *(s->top - 1);
s->top--;
s->count--;
}
bool Number(char c)
{
if (c >= '0' && c <= '9')
{
return true;
}
else
{
return false;
}
}
void Precede(char a, char b, char* ch)
{
switch (a)
{
case '+':
if (b == '*' || b == '/' ||b == '(')
{
*ch = '<';
}
else
{
*ch = '>';
}
break;
case '-':
if (b == '*' || b == '/' || b == '(')
{
*ch = '<';
}
else
{
*ch = '>';
}
break;
case '*':
if (b == '(')
{
*ch = '<';
}
else
{
*ch = '>';
}
break;
case '/':
if (b == '(')
{
*ch = '<';
}
else
{
*ch = '>';
}
break;
case '(':
if (b == ')')
{
*ch = '=';
}
else
{
*ch = '<';
}
break;
case '#':
*ch = '<';
break;
default :
break;
}
//return *ch;
}
ElemType Resault(ElemType a, char c, ElemType b)
{
int s;
switch (c)
{
case '+':
s = a + b;
break;
case '-':
s = a - b;
break;
case '*':
s = a * b;
break;
case '/':
s = a / b;
break;
default :
break;
}
return s;
}
/*void Print(StrackPtr s)
{
for (int i = 1; i <= s->count; i++)
{
printf("%c ", *(s->top - i));
}
}*/
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "strack.h"
int main()
{
int a1;
int b1;
int a2;
int b2;
int temp;
char ch = '1';
StrackPtr OPND = InitStrack(); //储存运算数的栈
StrackPtr OPTR = InitStrack();//储存运算符的栈
PushStrack(OPTR, (ElemType) '#');
puts("请输入运算表达式,以#号键结束!");
char c = getchar();
while (c != '#' || (char)GetTop(OPTR) != '#')
{
if (Number(c))
{
PushStrack(OPND, c - '0');
c = getchar();
}
else
{
Precede((char)GetTop(OPTR), c, &ch);
/*if (c == '#')
{
ch = '=';
}*/
switch (ch)
{
case '<':
PushStrack(OPTR, (ElemType)c);;
c = getchar();
break;
case '=':
Pop(OPTR, &temp);
c = getchar();
break;
case '>':
Pop(OPND, &a2);
Pop(OPND, &b2);
PushStrack(OPND, Resault(a2,(char)GetTop(OPTR),b2));
Pop(OPTR, &temp);
break;
}
}
}
printf("%d ", GetTop(OPND));
system("pause");
return 0;
}