标题表达式char a[] = "3+5*2-1+8/2"可以用栈来实现
创建2个栈
1 操作数栈(3, 5, 2, 1, 8, 2)
2 运算符栈('+' '*' '-' '+' '*')
5 * 2 == 10
3 + 10 == 13
13 - 1 == 12
8 / 2 = 4
12 + 4 = 16
分析:
1,遍历字符数组判断类型,如果是数字则将其转为整型数字放到整型栈中,运算符放到字符栈。
2、如果入栈的运算符优先级小于等于字符栈栈顶字符,则该运算符先不入栈,整型栈出栈、字符栈出栈,然后整型栈再出栈,判断字符是哪个运算符。完成整型栈出栈的两个数的运算。并将运算结果放到整型栈中,运算符入栈。
3、注意减法和除法以及最后一个运算符
4、如果最后运算符优先级大,则在完成以上步骤后需再出栈计算,然后再入栈
## 主函数
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
//判断运算符优先级
int cmp(char str1, char str2)//str1 上个符号 str2 下一个
{
if(str1=='+'||str1=='-')
{
if(str2=='+'||str2=='-')
return 0; //两个优先级相同
else
return 1; //str2>str1
}else if(str1=='*'||str1=='/')
{
if(str2=='*'||str2=='/')
return 0;
else
return 0; //str2<str1
}
}
//完成运算功能
int calculate (seqstack_t *a,seqstack_tc *b)
{
int num1,num2,temp;
char c1;
num1=outSeqStack(a);
c1=outSeqStack_ch(b);
num2=outSeqStack(a);
if(c1=='*')
temp=num1*num2;
else if(c1=='/')
temp=num2/num1;
else if(c1=='+')
temp=num1+num2;
else if(c1=='-')
temp=num2-num1;
return temp;
}
int main(int argc, const char *argv[])
{
seqstack_t *a=creatEmptySeqStack(10);
seqstack_tc *b=creatEmptySeqStack_ch(10);
char c[11]={'3','+','5','*','2','-','1','+','8','/','2'};
int i;
int num,temp1;
for(i=0;i<11;i++)
{
if(c[i]>='0'&&c[i]<='9')
{
num=c[i]-48;
insertSeqStackData(a,num);
}
else
{
if(cmp(getSeqStackTopData_ch(b),c[i])==0)//栈顶运算符和该运算符比较
{
while(a->top!=0)
{
temp1=calculate(a,b);
insertSeqStackData(a,temp1);
}
}
insertSeqStackData_ch(b,c[i]);
}
}
while(a->top!=0)//最后一个运算符优先级大需在进行一次出栈入栈操作
{
temp1=calculate(a,b);
insertSeqStackData(a,temp1);
}
while(a->top!=-1)
{
printf("计算结果为-->%d<-- ",outSeqStack(a));
}
printf("\n");
return 0;
}
相应入栈出栈函数
顺序栈入栈出栈
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
seqstack_t *creatEmptySeqStack(int len)
{
seqstack_t *p=(seqstack_t *)malloc(sizeof(seqstack_t));
if(p==NULL)
{
printf("creatEmptySeqStack faild\n");
return NULL;
}
p->data=(datatype *)malloc(sizeof(datatype)*len);
if(p->data==NULL)
{
printf("malloc faild\n");
return NULL;
}
p->maxlen=len;
p->top=-1;
return p;
}
seqstack_tc* creatEmptySeqStack_ch(int len)
{
seqstack_tc *p=(seqstack_tc *)malloc(sizeof(seqstack_tc));
if(p==NULL)
{
printf("creatEmptySeqStack faild\n");
return NULL;
}
p->data=(datatype_char *)malloc(sizeof(datatype_char)*len);
if(p->data==NULL)
{
printf("malloc faild\n");
return NULL;
}
p->maxlen=len;
p->top=-1;
}
//2入栈
int insertSeqStackData(seqstack_t *p,datatype data)
{
if(isFullSeqStack(p))
{
printf("insertSeqStackData faild\n");
return -1;
}
p->top++;
p->data[p->top]=data;
return 0;
}
int insertSeqStackData_ch(seqstack_tc *p,datatype_char data)
{
p->top++;
p->data[p->top]=data;
return 0;
}
//3判断栈是否满
int isFullSeqStack(seqstack_t *p)
{
return p->top+1==p->maxlen;
}
//4出栈
datatype outSeqStack(seqstack_t *p)
{
datatype temp;
if(isEmptySeqStack(p))
{
printf("outSeqStack faild\n");
return -1;
}
temp=p->data[p->top];
p->top--;
return temp;
}
datatype_char outSeqStack_ch(seqstack_tc *p)
{
datatype_char temp;
temp=p->data[p->top];
p->top--;
return temp;
}
//5判断栈是否空
int isEmptySeqStack(seqstack_t *p)
{
return p->top==-1;
}
//6获取栈顶的数据
datatype getSeqStackTopData(seqstack_t *p)
{
if(isEmptySeqStack(p))
{
printf("getSeqStackTopData faild\n");
return -1;
}
return p->data[p->top];
}
datatype_char getSeqStackTopData_ch(seqstack_tc*p)
{
return p->data[p->top];
}
//7求栈的长度
int getSeqStackLength(seqstack_t *p)
{
return p->top+1;
}
//8清空栈
void clearSeqStak(seqstack_t *p)
{
p->top=-1;
}
//9销毁栈
void killSeqStack(seqstack_t **p)
{
free((*p)->data);
(*p)->data=NULL;
free(*p);
*p=NULL;
}