首页 > 编程知识 正文

数据结构括号匹配顺序栈(数据结构括号匹配问题)

时间:2023-05-03 08:24:46 阅读:77167 作者:2029

检查括号是否匹配的方法是依次检查给定的字符串。 如果是左括号,则进入堆栈。 右对齐时

号码,堆叠左括号判断是否匹配; 其他文字,不检查。 检查到字符串的末尾,然后

检查堆栈是否为空。 如果堆栈不为空,则整个字符串不匹配。

括号()、()和() ) )是否匹配

请输入带括号()、[]和{} )的表达式

{[(5-2) ) (7-3)2) *4) *6}

主题分析(1)程序提示用户,手动输入字符串

)2)对输入的字符串,逐一判断

括号() )时,包含大括号、大括号、小括号

如果是左括号,请推送到堆栈,然后单击、

右括号时,从堆栈中pop。

看是否一致,如果不一致就报告错误。

(3) ) )。

“gets”和“fgets”函数常用于输入字符串,但两者都有缺点。

gets函数输入的字符数没有限制,容易越界

缺省情况下,fgets函数会在字符串末尾添加“n”,以影响数据的准确性

封装fgets函数并删除末尾的换行符“n”

3359 blog.csdn.net/QQ _ 43968080/article/details/84862198

首先,写入基本程序/*对于输入的字符串中的任意一个,括号为*//* c1.h (程序名称()/# include string.h # include ctype.h # include malloc.) 检查是否与includelimits ()配对的eof () ^z或F6 ), null )/#includestdlib.h/*atoi ) */#include sys/io.h /* eof ) )/#includemath.h/*ff abs () */# include define false0# defineo k1 # define error0# define define OVERFLOW-2在math.h中定义的overflow的值为3,因此该行*/typedef int Status; /* Status是函数类型,其值为OK等函数结果状态代码*/typedef int Boolean;/*布尔型,其值为TRUE或FALSE */typedef char ElemType; /*定义堆栈元素类型为整数*//* c3-1.h堆栈的顺序存储是*/#define STACK_INIT_SIZE 10 /*存储空间初始分配量*/# define stastar int i; ElemType ch; ElemType tmp_ch; ElemType contain[160]; int contain_index=0; printf (() ) ) 34 )5)6) ) ) )3)2) ) ) ) ) )带括号的公式:eg: memset(Array,0,sizeof ) Array ); gets (阵列); printf ('输入内容为:%s(n )、array ); Array_CNT=Strlen(Array ); printf (输入长度为:%dn )、array_cnt ); for(I=0; i array_cnt; I () { ch=array[i]; 开关(ch ) case ) (:case ) ) :contain(contain_index )=ch; contain_index; 布雷克; case ' ) ' : case ' ] ' : case ' } ' : if (contain _ index0) tmp _ ch=contain [-- contain _ index ]; if(tmp_ch==() ) ch==() ) ) ) }{

printf("小括号匹配n"); } else if(tmp_ch == '[' && ch == ']'){ printf("中括号匹配n"); } else if(tmp_ch == '{' && ch == '}'){ printf("大括号匹配n"); }else{ printf("匹配错误n"); return ERROR ; } } break ; } } return 0;} 遇到问题

warning: the `gets’ function is dangerous and should not be used
解决办法是使用 fgets
fgets()函数的基本用法为

fgets(char * s,int size,FILE * stream);//eg:可以用fgets(tempstr,10,stdin)//tempstr 为char[]变量,10为要输入的字符串长度,stdin为从标准终端输入。

示例程序

/* 代码实现 */#include <stdio.h>int main ( ) { char name[20]; printf("n 输入任意字符 : "); fgets(name, 20, stdin);//stdin 意思是键盘输入 fputs(name, stdout); //stdout 输出 return 0;} 换成用栈的方式 /* 对于输入的任意一个字符串,检验括号是否配对 *//* c1.h (程序名) */#include<string.h>#include<ctype.h>#include<malloc.h> /* malloc()等 */#include<limits.h> /* INT_MAX等 */#include<stdio.h> /* EOF(=^Z或F6),NULL */#include<stdlib.h> /* atoi() */#include <sys/io.h> /* eof() */#include<math.h> /* floor(),ceil(),abs() */#include<pthread.h> /* exit() *//* 函数结果状态代码 */#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1/* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */typedef char ElemType; /* 定义栈元素类型为整型 *//* c3-1.h 栈的顺序存储表示 */#define STACK_INIT_SIZE 60 /* 存储空间初始分配量 */#define STACKINCREMENT 2 /* 存储空间分配增量 */typedef struct sqStack{ ElemType *base ; ElemType *top ; int stack_size ;}sqStack;Status init_stack(sqStack * S){ S->base = (ElemType *)malloc(sizeof(ElemType)*STACK_INIT_SIZE); S->top = S->base ; S->stack_size = STACK_INIT_SIZE ; return OK;}Status push_stack(sqStack *S, ElemType e){ if(S->top - S->base < S->stack_size){ *(S->top++) = e ; return OK ; } return ERROR;}Boolean empty_stack(sqStack *S){ if(S->top == S->base){ return TRUE; } return FALSE ;}Status pop_stack(sqStack *S, ElemType *e){ if(!empty_stack(S)){ *e = *(--(S->top)); return OK; } return ERROR;}int main(){ ElemType array[160]; int array_cnt = 0 ; int i ; ElemType ch ; ElemType tmp_ch ; sqStack S ; printf("{1+()+(3+4+(5+6+6)+4+((((3+2)+3+(2+1)+((2+1+((+4+3)_+(22))))))))}n"); printf("请输入 带括号的表达式:eg: {122 + (10/5)*(2x6)}n"); memset(array,0,sizeof(array)); init_stack(&S); fgets(array,sizeof(array), stdin); char *find = strchr(array, 'n'); //找出data中的"n" if(find) *find = ''; //替换 printf("输入内容为:%sn",array); array_cnt = strlen(array); printf("输入长度为:%dn",array_cnt); for(i=0;i< array_cnt;i++){ ch = array[i]; switch(ch){ case '(': case '[': case '{': push_stack(&S,ch); break; case ')': case ']': case '}': if(pop_stack(&S,&tmp_ch) == OK){ if(tmp_ch == '(' && ch == ')'){ printf("小括号匹配n"); } else if(tmp_ch == '[' && ch == ']'){ printf("中括号匹配n"); } else if(tmp_ch == '{' && ch == '}'){ printf("大括号匹配n"); }else{ printf("匹配错误n"); return ERROR ; } } else{ printf("栈为空,匹配错误n"); return ERROR ; } break ; } } return 0;}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。