#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define MAX 1024
typedef struct __SSTACK
{
void*data[MAX];
int m_Size;
}SeqStack;
typedef void* SStack;
SStack init_SeqStack()
{
SeqStack*p = malloc(sizeof(SeqStack));
if (p == NULL)
{
return NULL;
}
memset(p, 0, sizeof(SeqStack));
return p;
}
void push_SeqStack(SStack handle, void*data)
{
if (handle == NULL || data == NULL)
{
return;
}
SeqStack*p = handle;
if (p->m_Size == MAX)
{
return;
}
p->data[p->m_Size] = data;
p->m_Size++;
}
void pop_SeqStack(SStack handle)
{
if (handle == NULL)
{
return;
}
SeqStack*p = handle;
p->m_Size--;
p->data[p->m_Size] = NULL;
}
void* top_SeqStack(SStack handle)
{
if (handle == NULL)
{
return;
}
SeqStack*p = handle;
return p->data[--p->m_Size];
}
int size_SeqStack(SStack handle)
{
if (handle == NULL)
{
return -1;
}
SeqStack*p = handle;
return p->m_Size;
}
int isEmpty_SeqStack(SStack handle)
{
if (handle == NULL)
{
return -1;
}
SeqStack*p = handle;
if (p->m_Size == 0)
{
return 1;
}
return 0;
}
void destory_SeqStack(SStack handle)
{
if (handle == NULL)
{
return -1;
}
SeqStack*p = handle;
free(p);
}
void print_Error(const char*error,const char*p,const char*cur_pos)
{
printf("%s\n", error);
printf("%s\n", p);
int num = cur_pos - p;
for (int i = 0; i < num;i++)
{
printf(" ");
}
printf("A\n");
}
void test()
{
char*str = "(5+10)*10-(+(5+1*90(";
char*p = str;
SStack handle = init_SeqStack();
while (*p != '\0')
{
if (*p=='(')
{
push_SeqStack(handle, p);
}
if (*p==')')
{
if (isEmpty_SeqStack(handle))
{
print_Error("右括号匹配失败", str, p);
break;
}
pop_SeqStack(handle);
}
p++;
}
while (!isEmpty_SeqStack(handle))
{
char*p = top_SeqStack(handle);
print_Error("左括号匹配失败", str, p);
}
destory_SeqStack(handle);
}
int main()
{
test();
system("pause");
return 0;
}