栈的代码 包括入栈 删除 等等
真的不能在良心了 臣妾做不到啊~~
typedef char ElemType;
#define STACK_INCRE_SIZE 10
#define MEM_ALLOC_FAIL 1
#define STACK_INIT_SIZE 100
#define STACK_NOT_EXISTS 2
#define STACK_EMPTY 3
#define MEM_Add_ALLOC_FAIL 4
#define LINE_DATA_NUMS 5
typedef struct DataNode{
ElemType *top;
ElemType *base;
int size;
}SqStack;
int InitStack(SqStack *S);
int ClearStack(SqStack *S);
int DestroyStack(SqStack *S);
int StackEmpty(SqStack S);
int StackLength(SqStack S);
int Push(SqStack *S,ElemType elem);
int Pop(SqStack *S,ElemType *elem);
int GetSTop(SqStack S,ElemType *elem);
void Output(ElemType elem);
int STraverse(SqStack S,void (*visit)(ElemType elem));
int InitStack(SqStack *S)
{
//1 申请栈空间
ElemType *p = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
//2 内存空间分配失败,则返回-2
if(p == NULL)
{
return MEM_ALLOC_FAIL;
}
//3 内存分配成功,则设置头节点指针域
S->top = p;
S->base = p;
S->size = STACK_INIT_SIZE;
return 0;
}
int Push(SqStack *S,ElemType elem)
{
//1 栈不存在
if(S->base == NULL)
{
return STACK_NOT_EXISTS;
}
//2 写入数据前,空间判断
if((S->top - S->base) == S->size)
{
ElemType *p = (ElemType *)realloc(S->base,(S->size + STACK_INCRE_SIZE)* sizeof(ElemType));
if(p == NULL)
{
return MEM_Add_ALLOC_FAIL;
}
S->base = p;
S->size += STACK_INCRE_SIZE;
}
//3 内存分配成功,写入数据
*(S->top) = elem;
S->top += 1;
return 0;
}
int Pop(SqStack *S,ElemType *elem)
{
//1 若栈不存在
if(S->base == NULL)
{
return STACK_NOT_EXISTS;
}
//2 空栈
if(S->top -S->base == 0)
{
return STACK_EMPTY;
}
//3 出栈
S->top -= 1;
*elem = *(S->top) ;
return 0;
}
int ClearStack(SqStack *S)
{
if(S->base == NULL)
{
return STACK_NOT_EXISTS; //栈不存在
}
S->top = S->base;
return 0;
}
int DestroyStack(SqStack *S)
{
if(S->base == NULL)
{
return STACK_NOT_EXISTS; //栈不存在
}
free(S->base);
S->base = NULL;
S->top = NULL;
S->size = 0;
return 0;
}
int StackEmpty(SqStack S)
{
//1 栈不存在
if(S.base == NULL)
{
return STACK_NOT_EXISTS;
}
if(S.top - S.base == 0)
{
return 1;
}
return 0;
}
int StackLength(SqStack S)
{
//1 栈不存在
if(S.base == NULL)
{
return STACK_NOT_EXISTS;
}
//2 返回栈的长度
return S.top - S.base;
}
int GetSTop(SqStack S,ElemType *elem)
{
if(S.base == NULL)
{
return STACK_NOT_EXISTS;
}
if(S.top - S.base == 0)
{
return STACK_EMPTY;
}
*elem = *(S.top - 1);
return 0;
}
int STraverse(SqStack S,void (*visit)(ElemType elem))
{
//1若栈不存在
if(S.base == NULL)
{
return STACK_NOT_EXISTS;
}
if(S.top - S.base == 0)
{
return STACK_EMPTY;
}
//2 栈存在且有元素:依次遍历元素
int cnt = 0;
ElemType *p = S.top;
while(p != S.base)
{
(*visit)(*(p-1));
printf("\t");
if( (cnt+1) % LINE_DATA_NUMS == 0)
{
printf("\n");
}
cnt += 1;
p--;
}
printf("\n");
return 0;
}
主文件
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include"公式计算.cpp"
int main()
{
char a[1000];
int num[100];
int sum[10000];
SqStack n1;//数字
SqStack n2;//运算符
SqStack n3;//整数栈
InitStack(&n1);
InitStack(&n2);
InitStack(&n3);
gets(a);
for(int i=0;a[i]!='\0';i++)
{
if(isdigit(a[i]))
{
Push(&n1,a[i]);
if(!isdigit(a[i+1]))
{
Push(&n1,'#');
}
}
else
{
if(StackEmpty(n2))
{
Push(&n2,a[i]);
}
else
{
if(a[i]=='(')
{
Push(&n2,a[i]);
}
if(a[i]==')')
{
char x;
GetSTop(n2,&x);
while(x!='(')
{
char t;
Pop(&n2,&t);
Push(&n1,t);
GetSTop(n2,&x);
}
char t;
Pop(&n2,&t);
}
if(a[i]=='*')
{
char x;//x为上一个字符
GetSTop(n2,&x);
if(x==')')
{
printf("EOOER\n");
return 0;
}
if(x=='(')
{
Push(&n2,a[i]);
}
if(x=='*')
{
while(x=='*')
{
char t;//t为踢出的字符
Pop(&n2,&t);
Push(&n1,t);
if(StackEmpty(n2))
{
break;
}
GetSTop(n2,&x);
}
Push(&n2,a[i]);
}
if(x=='+'||x=='-')
{
Push(&n2,a[i]);
}
}
if(a[i]=='+'||a[i]=='-')
{
char x;//x为上一个字符
GetSTop(n2,&x);
if(x==')')
{
printf("EOOER\n");
return 0;
}
if(x=='(')
{
Push(&n2,a[i]);
}
if(x=='*'||x=='+'||x=='-')
{
while(x=='*'||x=='+'||x=='-')
{
char t;//t为踢出的字符
Pop(&n2,&t);
Push(&n1,t);
if(StackEmpty(n2))
{
break;
}
GetSTop(n2,&x);
}
Push(&n2,a[i]);
}
}
}
}
}
while(!StackEmpty(n2))
{
char t;
Pop(&n2,&t);
Push(&n1,t);
}
int size,j=0,z=0;
size=n1.top-n1.base;
for(int i=0;i<size;i++)
{
if(isdigit(n1.base[i]))
{
num[j]=n1.base[i]-'0';
j++;
}
if(n1.base[i]=='#')
{
j=j-1;
int count=0;
int p=1;
for(int i=j;i>=0;i--)
{
count=count+num[i]*p;
p=p*10;
}
sum[z]=count;
z++;
j=0;
}
if(n1.base[i]=='+'||n1.base[i]=='-'||n1.base[i]=='*')
{
int d=sum[z-1];
int b=sum[z-2];
z=z-2;
if(n1.base[i]=='+')
{
sum[z]=d+b;
}
if(n1.base[i]=='-')
{
sum[z]=b-d;
}
if(n1.base[i]=='*')
{
sum[z]=d*b;
}
z++;
}
}
printf("%d",sum[0]);
}