#ifndef _Stack_h
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;
typedef float ElementType;
int IsEmpty(Stack s);
Stack CreateStack(void);
void DisposeStack(Stack s);
void MakeEmpty(Stack s);
void Push(ElementType x,Stack s);
ElementType Top(Stack s);
void Pop(Stack s);
#endif
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Node{
ElementType Element;
PtrToNode Next;
};
//判空
int IsEmpty(Stack s){
return s->Next==NULL;
}
//创建空栈
Stack CreateStack(){
Stack s=(Stack)malloc(sizeof(struct Node));
if(s==NULL){
printf("Out of space!!!");
}
s->Next=NULL;
MakeEmpty(s);
return s;
}
//清空
void MakeEmpty(Stack s){
if(s==NULL){
printf("Must use CreateStack first");
}else{
while(!IsEmpty(s)){
Pop(s);
}
}
}
//弹出
void Pop(Stack s){
PtrToNode FirstCell;
if(IsEmpty(s)){
printf("Empty stack");
}else{
FirstCell=s->Next;
s->Next=s->Next->Next;
free(FirstCell);
}
}
//弹出并返回top
ElementType PopAndTop(Stack s){
PtrToNode FirstCell;
if(IsEmpty(s)){
printf("Empty stack");
return '\0';
}else{
ElementType ch=Top(s);
FirstCell=s->Next;
s->Next=s->Next->Next;
free(FirstCell);
return ch;
}
}
//压入
void Push(ElementType x,Stack s){
PtrToNode TmpCell;
TmpCell=(PtrToNode)malloc(sizeof(struct Node));
if(TmpCell==NULL){
printf("Out of space");
}else{
TmpCell->Element=x;
TmpCell->Next=s->Next;
s->Next=TmpCell;
}
}
ElementType Top(Stack s){
if(!IsEmpty(s)){
return s->Next->Element;
}else{
// printf("Empty stack");
return '\0';
}
}
//栈应用三、中缀到后缀的转换
void convert(char *ch){
int i=0;
Stack s=CreateStack();
while(*(ch+i)!='\0'){
char c=*(ch+i);
char op=Top(s);
//printf("op:%c \n",op);
switch(c){
case '+':
case '-':
while(op=='-'||op=='+'||op=='*'||op=='/'){
printf("%c ",op);
Pop(s);
op=Top(s);
}
Push(c,s);
break;
case '*':
case '/':
while(op=='*'||op=='/'){
printf("%c ",op);
Pop(s);
op=Top(s);
}
Push(c,s);
break;
case '(':
Push(c,s);
break;
case ')':
while(op!='('){
printf("%c ",op);
Pop(s);
op=Top(s);
}
Pop(s);
break;
default:
printf("%c ",c);
break;
}
i++;
}
while(!IsEmpty(s)){
printf("%c ",(char)PopAndTop(s));
}
}
//栈应用二、后缀表达式的计算
float getResult(const char **ch,int len){
int i=0;
Stack s=CreateStack();
// printf("%c",*ch[9]);
while(i<len){
const char *c=ch[i];
float num1,num2;
float result=0.0f;
switch(*c){
case '+':
case '-':
case '*':
case '/':
num1=PopAndTop(s);
num2=PopAndTop(s);
result=0;
if(*c=='+'){
result=num2+num1;
}else if(*c=='-'){
result=num2-num1;
}else if(*c=='*'){
result=num2*num1;
}else if(*c=='/'){
result=num2/num1;
}
printf("num1:%f,num2:%f,result:%f ",num1,num2,result);
Push(result,s);
break;
default:
//先转为浮点数
float num=atof(c);
printf("%f\n",num);
Push(num,s);
break;
}
i++;
}
return Top(s);
}
//栈应用一、实现括号匹配
bool check(char *ch,int len){
int i=0;
Stack s=CreateStack();
int num2=0;
while(*(ch+i)!='\0'){
//用栈来检测
char c=*(ch+i);
switch(c){
char result;
case '{':
case '[':
case '(':
Push(c,s);
num2=0;
break;
case '}':
result=PopAndTop(s);
printf("%c %c 第一对该括号出现在%d 和%d\n",result,c,i-num2,i+1);
if(result!='{'){
return false;
}
break;
case ']':
result=PopAndTop(s);
printf("%c %c 第一对该括号出现在%d 和%d\n",result,c,i-num2,i+1);
if(result!='['){
return false;
}
break;
case ')':
result=PopAndTop(s);
printf("%c %c 第一对该括号出现在%d 和%d\n",result,c,i-num2,i+1);
if(result!='('){
return false;
}
break;
default:num2++;break;
}
i++;
}
//对栈中剩余进行判断
char top=Top(s);
if(top!='\0'){
//匹配失败
return false;
}else{
return true;
}
}
int main(){
// char a[]={"(1+2)+3+4+(1+2)()"};
// bool b=check(a,sizeof(a));
// if(b){
// printf("匹配成功");
// }else{
// printf("匹配失败");
// }
//测试后缀表达式
// const char *a[]={"4.99","1.06","*","5.99","+","6.99","1.06","*","+"};
// float result=getResult(a,9);
// printf("\n最后的结果:%f",result);
//测试中缀表达式转成后比表达式
char a[]={"a+b*c+(d*e+f)*g"};
convert(a);
}
栈的应用(括号匹配、后缀表式计算、中缀转后缀)
最新推荐文章于 2024-04-20 21:53:23 发布