标签:linux c
栈:
栈的应用,平衡符号:
读入一个字符串表达式,其中包括有( 、)、[ 、] 、{ 、}这6个符号。写一个程序检验字符串表达式中这6个符号是否正确匹配。
程序:
//stack.h
typedef char ElementType;
#ifndef _Stack_h
struct Node;
typedef struct Node *Stack;
int IsEmpty(Stack S);
int IsFull(Stack S);
Stack CreateStack( int MaxElements );
void DisposeStack(Stack S);
void MakeEmpty(Stack S);
void Push( ElementType X, Stack S);
ElementType Top( Stack S);
void Pop(Stack S);
ElementType TopAndPop( Stack S );
#endif
#define EmptyTOS (-1)
#define MinStackSize (5)
struct Node
{
int Capacity;
int TopOfStack;
ElementType *Array;
};//stack.c
#include "stack.h"
#include<stdio.h>
#include<error.h>
#include<stdlib.h>
int IsEmpty(Stack S)
{
return S->TopOfStack==EmptyTOS;
}
int IsFull(Stack S)
{
return S->TopOfStack==(S->Capacity-1);
}
Stack CreateStack( int MaxElements )
{
Stack S;
if(MaxElements<MinStackSize)
error(1,1,"Stack size is too small");
S=malloc(sizeof(struct Node));
if(S==NULL)
error(1,1,"Out of space!!!");
S->Array=malloc(sizeof(ElementType)*MaxElements);
if(S->Array==NULL)
error(1,1,"Out of space!!!");
S->Capacity=MaxElements;
MakeEmpty(S);
return S;
}
void DisposeStack(Stack S)
{
if(S!=NULL)
{
free(S->Array);
free(S);
}
}
void MakeEmpty(Stack S)
{
S->TopOfStack=EmptyTOS;
}
void Push(ElementType X,Stack S)
{
if(IsFull(S))
error(1,1,"Full stack");
else
{
S->TopOfStack++;
S->Array[S->TopOfStack]=X;
}
}
ElementType Top( Stack S)
{
if(!IsEmpty(S))
return S->Array[S->TopOfStack];
error(1,1,"Empty stack");
return 0;
}
void Pop( Stack S)
{
if(IsEmpty(S))
error(1,1,"Empty stack");
else
S->TopOfStack=EmptyTOS;
}
ElementType TopAndPop( Stack S)
{
if(!IsEmpty(S))
{
return S->Array[ S->TopOfStack--];
}
error(1,1,"Empty stack");
return 0;
}//signal_match.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"stack.h"
#define MaxElements 100
#define SIZE 100
int convert(char ch)
{
if(ch==‘(‘)
return 1;
else if(ch==‘)‘)
return 2;
else if(ch==‘[‘)
return 3;
else if(ch==‘]‘)
return 4;
else if(ch==‘{‘)
return 5;
else if(ch==‘}‘)
return 6;
else
return 0;
}
void solution(char* str,Stack s)
{
int i;
for(i=0;i<strlen(str);++i)
{
switch(convert(str[i]))
{
case 0:
{
break;
}
case 1:
case 3:
case 5:
{
if(IsFull(s))
{
printf("Full stack!\n");
return ;
}
Push(str[i],s);
break;
}
case 2:
{
if(IsEmpty(s))
{
printf("The string is wrong,there is no ‘(‘ to ‘)‘!\n");
return ;
}
if(TopAndPop(s)!=‘(‘)
{
printf("The string is wrong,it should not be a ‘)‘!\n");
return ;
}
else
break;
}
case 4:
{
if(IsEmpty(s))
{
printf("The string is wrong,there is no ‘[‘ to ‘]‘!\n");
return ;
}
if(TopAndPop(s)!=‘[‘)
{
printf("The string is wrong,it should not be a ‘]‘!\n");
return ;
}
else
break;
}
case 6:
{
if(IsEmpty(s))
{
printf("The string is wrong,there is no ‘{‘ to ‘}‘!\n");
return ;
}
if(TopAndPop(s)!=‘{‘)
{
printf("The string is wrong,it should not be a ‘}‘!\n");
return ;
}
else
break;
}
}
}
if(!IsEmpty(s))
{
printf("There is too much ‘(‘ , ‘[‘ or ‘{‘ !\n");
return ;
}
printf("It is a good string!\n");
}
int main()
{
Stack mystack=CreateStack(MaxElements);
char *str=malloc(sizeof(char)*SIZE);
printf("please input the string : ");
scanf("%s",str);
solution(str,mystack);
return 0;
}stack.h和stack.c是栈的声明和定义,signal_match.c是栈的应用。
标签:linux c
原文地址:http://yuzwei.blog.51cto.com/10126623/1684920