#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdlib.h>
#define MAX 100
typedef char DataType;
typedef struct Stack
{
int top;
DataType stack[MAX];
}Stack;
void StackInit(Stack* s);
void StackPush(Stack* s, DataType data);
void StackPop(Stack* s);
DataType GetStackTop(Stack*s);
int GetStackSize(Stack*s);
int StackEmpty(Stack *s);
#include"Stack.h"
void StackInit(Stack* s)
{
s->top = 0;
}
void StackPush(Stack* s, DataType data)
{
if (s->top == MAX)
{
printf("栈已满!无法入栈元素\n");
return;
}
(s->top)++;
s->stack[s->top] = data;
}
void StackPop(Stack *s)
{
if (s->top == 0)
{
printf("栈为空,无法出栈元素\n");
return;
}
s->stack[s->top] = 0;
(s->top)--;
}
DataType GetStackTop(Stack*s)
{
if (s->top == 0)
{
printf("栈为空,无法获取栈顶\n");
return 0;
}
return s->stack[s->top ];
}
int GetStackSize(Stack*s)
{
return s->top;
}
int StackEmpty(Stack *s)
{
if (s->top == 0)
{
return 1;
}
else
{
return 0;
}
}
#逆波兰表达式
#include "Stack.h"
enum { Data, Add, Sub, Mul, Div }operate; //枚举
typedef struct Cell
{
enum operate op;
int data;
}cell;
int CalRPN(cell* RPN, int sz)//逆波兰表达式(后缀表达式)
{
int i = 0;
Stack s;
assert(RPN);
StackInit(&s);
for (; i < sz; i++)
{
int left = 0;
int right = 0;
switch (RPN[i].op)
{
case Data:
StackPush(&s, RPN[i].data);
break;
case Add:
right = GetStackTop(&s);
StackPop(&s);
left = GetStackTop(&s);
StackPop(&s);
StackPush(&s, right + left);
break;
case Sub:
right = GetStackTop(&s);
StackPop(&s);
left = GetStackTop(&s);
StackPop(&s);
StackPush(&s, left - right);
break;
case Mul:
right = GetStackTop(&s);
StackPop(&s);
left = GetStackTop(&s);
StackPop(&s);
StackPush(&s, left * right);
break;
case Div:
right = GetStackTop(&s);
StackPop(&s);
left = GetStackTop(&s);
StackPop(&s);
if (0 == right)
printf("除数非法\n");
else
StackPush(&s, left / right);
break;
}
}
return GetStackTop(&s);
}
void RPNTest()
{
cell RPN[] = { { Data,12 },{ Data,3 },{ Data,4 },{ Add,0 },{ Mul,0 },{ Data,6 },
{ Sub,0 },{ Data,8 },{ Data,2 },{ Div,0 },{ Add,0 } };
int sz = sizeof(RPN) / sizeof(RPN[0]);
int ret = CalRPN(RPN, sz);
printf("%d\n", ret);
}
int main()
{
RPNTest();
system("pause");
return 0;
}
#括号匹配
#include "Stack.h"
int MatchBrackets(const char *str)
{
int i = 0;
Stack s;
StackInit(&s);
StackPush(&s, str[i]);
int length = strlen(str);
i++;
while ( i< length)
{
if (str[i] == '{' || str[i] == '(' || str[i] == '['|| str[i] == '}' || str[i] == ')' || str[i] == ']')
{
if (str[i] == '{' || str[i] == '(' || str[i] == '[')
{
StackPush(&s, str[i]);
}
else
{
if (!StackEmpty(&s))
{
return 0;
}
if (str[i] == '}' && GetStackTop(&s) == '{')
{
StackPop(&s);
}
else if (str[i] == ']' && GetStackTop(&s) == '[')
{
StackPop(&s);
}
else if (str[i] == ')' && GetStackTop(&s) == '(')
{
StackPop(&s);
}
else
{
StackPush(&s ,str[i]);
break;
}
}
}
i++;
}
if (StackEmpty(&s))
{
return 0;
}
return 1;
}
int main()
{
char a[] = "(())abc{[(])}"; // 左右括号次序匹配
char b[] = "(()))abc{[]}"; // 右括号多于左括号
char c[] = "(()()abc{[]}"; // 左括号多于右括号
char d[] = "(())abc{[]()}";
int Flag = MatchBrackets(a);
if (Flag == 1)
{
printf("括号匹配!\n");
}
else
{
printf("括号不匹配!\n");
}
Flag = MatchBrackets(b);
if (Flag == 1)
{
printf("括号匹配!\n");
}
else
{
printf("括号不匹配!\n");
}
Flag = MatchBrackets(c);
if (Flag == 1)
{
printf("括号匹配!\n");
}
else
{
printf("括号不匹配!\n");
}
Flag = MatchBrackets(d);
if (Flag == 1)
{
printf("括号匹配!\n");
}
else
{
printf("括号不匹配!\n");
}
system("pause");
return 0;
}