栈的应用--括号匹配

一、实验目的
本实验是关于栈的应用,栈在各种高级语言编译系统中应用十分广泛,利用栈的“先进后出”的特点,分析C语言源程序代码中的的括号是否配对正确。
通过本实验的学习,可以理解栈的基本操作的实现。
二、实验内容
本实验要求设计一个算法,检验C源程序代码中的括号是否正确配对。对本算法中的栈的存储实现,我们采用的是顺序存储结构。要求能够在某个C源程序上文件上对所设计的算法进行验证。
三、程序分析
(1)int initStack(sqstack **s) 初始化一个栈
(2)int push(sqstack *s,char x) 入栈,栈满时返回FALSE
(3)char pop(sqstack *s) 出栈,栈空时返回NULL
(4)int Empty(sqstack *s) 判断栈是否为空,为空时返回TRUE
(5)int match(FILE *f) 对文件指针f对指的文件进行比较括号配对检验,从文件中每读入一个字符ch=fgetc(f),采用多路分支switch(ch)进行比较:
①若读入的是“[”、“{”或“(”,则压入栈中,
②若读入的是“]”,则:若栈非空,则出栈,判断出栈符号是否等于“[”,不相等,则返回FALSE。
③若读入的是“}”,则:若栈非空,则出栈,判断出栈符号是否等于“{”,不相等,则返回FALSE。
④若读入的是“)”,则:若栈非空,则出栈,判断出栈符号是否等于“(”,不相等,则返回FALSE。
⑤若是其它字符,则跳过。
文件处理到结束时,如果栈为空,则配对检验正确,返回TRUE。
(6)主程序main()中定义了一个文件指针,输入一个已经存在的C源程序文件。
四、程序源代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 10

typedef struct{
 char sta[SIZE];
 int top;
}stack;

//初始化栈
int initstack(stack *s) {
 memset(s->sta,0,sizeof(s->sta));
s->top=-1;
 return 0;

}
//压栈
int push(stack *s,char x){
if((s->top)==SIZE-1)  {
 printf("the stack is full\n");
 return 1;
 }
 else{
 (s->top)++;
 (s->sta)[s->top]=x;
  return 0;
 }
}
//出栈
char pop(stack *s){
 char tmp;
 tmp=(s->sta)[s->top];
 (s->top)--;
 return tmp;

}
//判空
int empty(stack *s){
 if(s->top<0) {
printf("the stack is empty\n");
 return 1;}
 else return 0;

}
//调用
int main(){
 stack s;
 int i;
char m;
 char tmp;
int flag=0;
initstack(&s);

 for(i=0;i<SIZE;i++){
 scanf("%c",&m);
 if(m=='(' || m=='[' || m=='{'){
 push(&s,m);
 }
 else if(m==')' || m==']' || m=='}'){ 
if(m==')' ) {
if(empty(&s)) printf("error\n");
 else {tmp=pop(&s); if(tmp=='(' )  flag=1; else flag=0;}
 }
 if(m==']' ) {
 if(empty(&s)) printf("error\n");
 else {tmp=pop(&s); if(tmp=='[' ) flag=1; else flag=0 ;}
 }
 if(m=='}' ) {
if(empty(&s)) printf("error\n");
 else {tmp=pop(&s); if(tmp=='{' ) flag=1; else flag=0;}
 }

 if(flag==1) printf("the no.%d brackets match successfully\n",(s.top)+2);
else {printf("the no.%d brackets match unsuccessfully\n",(s.top)+2);break;}
 }
 else {i--;continue;}
 }


return 0; 
}
# define MAXNUM 200
# define FALSE 0
# define TRUE 1
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct {
char stack[MAXNUM];
int top; } sqstack; /*定义栈结构*/
      int initStack(sqstack **s)
{/*初始化栈*/
if ((*s=(sqstack*)malloc(sizeof(sqstack)))==NULL) return FALSE;
(*s)->top=-1;
return TRUE;
}
int push(sqstack *s,char x)
{/*将元素x插入到栈s中,作为s的新栈顶*/
if(s->top>=MAXNUM-1) return FALSE; 	/*栈满*/
s->top++;
s->stack[s->top]=x;
return TRUE;
}
char pop(sqstack *s)
{/*若栈s不为空,则删除栈顶元素*/
char  x;
if(s->top<0) return NULL; 			/*栈空*/
x=s->stack[s->top];
s->top--;
return x;
}
int Empty(sqstack *s)
{ /*栈空返回TRUE,否则返回FALSE*/
if(s->top<0) return TRUE;
return FALSE;}

 int match(FILE *f)
{ char ch,ch1; sqstack *S;
initStack(&S);
while(!feof(f))
{ch=fgetc(f);
switch(ch)
{case '(':
case '[':
case '{':push(S,ch);printf("%c",ch);break;
case ')': if (Empty(S)!=TRUE)
{ch1=pop(S); printf("%c",ch);
if (ch1=='(')
break;
else
return FALSE; }
else
return FALSE;
case ']': if (Empty(S)!=TRUE)
{ch1=pop(S); printf("%c",ch);
if (ch1=='[')
break;
else
return FALSE; }
else
return FALSE;
case '}':if (Empty(S)!=TRUE)
{ch1=pop(S); printf("%c",ch);
if (ch1=='{')
break;
else
return FALSE; }
else
return FALSE;
default:break;
}
}
if (Empty(S)!=TRUE)
return FALSE; 
return TRUE; }

int main()
{FILE *fp;char fn[20];
printf("请输入文件名:");
scanf("%s",fn);
if ((fp=fopen(fn,"r"))==NULL)
  {printf("不能打开文件\n");
  exit(0);}
else if (match(fp)==TRUE)
      printf("括号正确\n");
  else
     printf("括号不正确\n");
  fclose(fp);return 0;
}

五、实验结果
最后程序运行结果如下所示:
请输入文件名: F:\exam.c<回车>
( ){ }括号正确

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值