您好,欢迎访问三七文档
//¼ÙÉè±í´ïʽÖÐÔÊÐí°üº¬3ÖÐÀ¨ºÅ£ºÔ²À¨ºÅ¡¢ÖÐÀ¨ºÅºÍ´óÀ¨ºÅ¡£Éè¼ÆÒ»¸öËã·¨²ÉÓá°Ë³ÐòÕ»¡±Åжϱí´ïʽÖеÄÀ¨ºÅÊÇ·ñÕýÈ·Åä¶Ô#includeiostream#includestring#includestdlib.husingnamespacestd;classstack{private:intmax;inttop;char*str;//´æ·ÅÕ»ÔªËصÄÊý×épublic:stack(intsize){max=size;top=-1;str=newchar[max];}stack(){top=-1;}~stack(){delete[]str;}voidclear(){top=-1;}boolpush(constcharitem)//ÈëÕ»²Ù×÷{if(top==max-1){coutÕ»ÂúÒç³öendl;returnfalse;}else{str[++top]=item;returntrue;}}boolpop(char&item){if(top==-1){coutջΪ¿Õ£¬²»ÄܽøÐÐɾ³ý²Ù×÷endl;returnfalse;}else{item=str[top--];returntrue;}}boolempty(){if(top==-1)returntrue;elsereturnfalse;}};intmain(){charc;stringst;intd=1,j=1;stacka(25);while(j){coutÇëÊäÈë±í´ïʽ:;cinst;for(inti=0;st[i]!='\0'&&d==1;i++){switch(st[i]){case'(':case'[':case'{':a.push(st[i]);break;case')':a.pop(c);if(c=='(')break;else{d=0;break;}case']':a.pop(c);if(c=='[')break;else{d=0;break;}case'}':a.pop(c);if(c=='{')break;else{d=0;break;}}}if(a.empty()&&d==1)coutÅä¶ÔÕýÈ·!!endl;elsecoutÅä¶Ô´íÎó!!endl;cout*****************endl;cout*1¡¢¼ÌÐø£¡£¡*endl;cout*2¡¢Í˳ö£¡£¡*endl;cout*****************endl;coutÇëÑ¡Ôñ.....;cinj;if(j==2)j=0;if(j==1){system(cls);a.clear();}}system(cls);cout³ÌÐò½áÊø!!endl;return0;}
本文标题:假设表达式中允许包含3中括号:圆括号、中括号和大括号。设计一个算法采用“顺序栈”判断表达式中的括号是
链接地址:https://www.777doc.com/doc-5839286 .html