测试环境:vs2013
stack.h
#pragma once
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char DataType;
typedef struct Stack
{
DataType *arr;
int capacity;
int size;
}Stack,* PStack;
void StackInit(PStack ps,
int capacity);
void StackPush(PStack ps, DataType data);
void StackPop(PStack ps);
DataType StackTop(PStack ps);
int StackSize(PStack ps);
int StackEmpty(PStack ps);
void PrintStack(PStack ps);
int IsBrackets(
char *ptr);
void IsMach(
char *a, PStack ps);
void Calculate(
char *a, PStack ps);
stack.c
#include "stack.h"
void StackInit(PStack ps,
int capacity)
{
if (NULL == ps)
return;
ps->arr = (DataType *)
malloc(
sizeof(DataType)*capacity);
if (NULL == ps->arr)
{
printf(
"申请空间失败!!!\n");
return;
}
ps->capacity = capacity;
ps->size =
0;
}
void AddCapacity(PStack ps)
{
if (NULL == ps)
return;
ps->arr = (DataType *)realloc(ps->arr,
sizeof(DataType)*(ps->capacity) *
2);
if (NULL == ps->arr)
{
printf(
"空间扩增失败!!!\n");
return;
}
ps->capacity =
2 * (ps->capacity);
}
void PrintStack(PStack ps)
{
int i =
0;
if (NULL == ps)
return;
for (; i < ps->size; i++)
{
printf(
"%d ", ps->arr[i]);
}
printf(
"\n");
}
void StackPush(PStack ps, DataType data)
{
if (NULL == ps)
return;
if (ps->size == ps->capacity)
AddCapacity(ps);
ps->arr[ps->size] = data;
ps->size++;
}
void StackPop(PStack ps)
{
if (NULL == ps)
return;
if (ps->size ==
0)
{
printf(
"栈已空,操作失败!!!\n");
return;
}
ps->size--;
}
DataType StackTop(PStack ps)
{
if (NULL == ps)
{
printf(
"栈不存在!!!\n");
return 0;
}
if (ps->size ==
0)
{
printf(
"栈已空!!!\n");
return 0;
}
return ps->arr[ps->size -
1];
}
int StackSize(PStack ps)
{
if (NULL == ps)
{
printf(
"栈不存在!!!\n");
return 0;
}
return ps->size;
}
int StackEmpty(PStack ps)
{
if (NULL == ps)
{
printf(
"栈不存在!!!\n");
return 0;
}
if (ps->size)
return 0;
return 1;
}
int IsBrackets(
char *ptr)
{
if (NULL == ptr)
return 0;
if (*ptr ==
'(' || *ptr ==
')' ||
*ptr ==
'[' || *ptr ==
']' ||
*ptr ==
'{' || *ptr ==
'}')
return 1;
else
return 0;
}
void IsMach(
char *a, PStack ps)
{
int i =
0;
int len =
strlen(a);
if (NULL == ps)
return;
if (NULL == a)
return;
for (; i < len; ++i)
{
if (IsBrackets(a + i))
{
if (a[i] ==
'(' || a[i] ==
'[' || a[i] ==
'{')
{
StackPush(ps, a[i]);
continue;
}
else
{
if (StackEmpty(ps))
{
printf(
"右括号比左多!!!\n");
return;
}
else
{
char top = StackTop(ps);
if ((top ==
'(' && a[i] ==
')') ||
(top ==
'[' && a[i] ==
']') ||
(top ==
'{' && a[i] ==
'}'))
StackPop(ps);
else
{
printf(
"括号次序不正确!!!\n");
return;
}
}
}
}
}
if (StackEmpty(ps))
{
printf(
"括号匹配!!!\n");
return;
}
else
printf(
"左比右多!!!\n");
}
void Calculate(
char *a, PStack ps)
{
int i =
0;
int len =
0;
int num =
0;
int flag =
0;
if (NULL == a)
return;
if (NULL == ps)
return;
len =
strlen(a);
for (; i < len; ++i)
{
if (a[i] >=
'0' && a[i] <=
'9')
{
num = num *
10 + a[i] -
'0';
flag =
1;
}
else if (a[i] ==
' ' && flag ==
1)
{
StackPush(ps, num);
num =
0;
flag =
0;
}
else
{
int left =
0;
int right =
0;
if (a[i] ==
' ')
continue;
right = StackTop(ps);
StackPop(ps);
left = StackTop(ps);
StackPop(ps);
switch (a[i])
{
case '+':
StackPush(ps, left + right);
break;
case '-':
StackPush(ps, left - right);
break;
case '*':
StackPush(ps, left * right);
break;
case '/':
StackPush(ps, left / right);
break;
default:
break;
}
}
}
}
#test.c
include “stack.h”
include