任意给定一个数学表达式如{(12-5)*3-[22+(3*4-19)/3]}+(34-21)*2,试设计一个算法判断表达式的括号是否匹配。

任意给定一个数学表达式如{(12-5)*3-[22+(3*4-19)/3]}+(34-21)*2,试设计一个算法判断表达式的括号是否匹配。包括程序设计思路和主要代码及注释,用c++实现



#include<iostream>
#include<string.h>
#include<stack>//调用标准库
using namespace std;
int main()
{
    char s[50];
    stack<char>st;//建立一个字符型栈
    while(cin>>s)
    {
        while(!st.empty())
        {st.pop();}
        int len=strlen(s),flag=1;
        for(int i=0;i<len;i++)
        {
            if(s[i]=='('||s[i]=='{'||s[i]=='[')
                st.push(s[i]);//将左括号压入栈中
            else 
            {
                if(st.empty())
                {
                    flag=0;
                    break;  
                }
                char t=st.top();
                st.pop();
                if(s[i]==')'&&(t=='['||t=='{'))
                {
                    flag=0;
                    break;
                }
                else if(s[i]=='}'&&(t=='('||t=='['))
                {
                    flag=0;
                    break;
                }
                else if(s[i]==']'&&(t=='{'||t=='('))
                {
                    flag=0;
                    break;
                }
            }
        }
        if(!st.empty())
        flag=0;
        if(flag==1)
        cout<<"true!"<<endl;
        else
        cout<<"false!"<<endl;
    }
    return 0;
} 

#include<iostream>
#include<string>
using namespace std;


//定义栈 
#define max_size 200//栈的最大容量 
typedef char datatype;
typedef struct{
	datatype zhan[max_size];
	int top;//栈顶 
}stack;






//栈的初始化
void initial(stack &st)
{
	st.top = 0;
}






//类型为datatype的x入栈 
void push(stack &st, datatype x)
{
	//当栈顶和max_size相等时,栈满 
	if (st.top == max_size){
		// cout<<"This stack has already full!";
		cout << "no";
		exit(0);
	}
	else{
		st.zhan[st.top] = x;
		st.top++;
	}
}






//出栈 
char pop(stack &st){
	if (st.top == 0){
		// cout<<"This stack is empty!";
		cout << "no";
		exit(0);
	}
	else{
		st.top--;
		return st.zhan[st.top];
	}
}






int main(){


	stack s;
	initial(s);


	/*输入字符串,并将字符串放到字符数组中,
	实现能够逐个扫描字符串中的字符,并且不跳过空格符
	*/
	string str;
	getline(cin, str);
	char ch[200] = { '\0' };
	strcpy(ch, str.c_str());






	//flag标志状态 1为括号匹配,0为不匹配 
	int flag = 1;
	int i;
	for (i = 0; ch[i] != '\0'; i++){
		//元素若为{,(,[则入栈
		if ((ch[i] == '{') || (ch[i] == '[') || (ch[i] == '(')){
			push(s, ch[i]);
		}//元素若为},),]则出栈 赋值给a 
		else if ((ch[i] == '}') || (ch[i] == ']') || (ch[i] == ')')){
			char a;
			a = pop(s);
			//若a与ch[i]匹配,进行下一个字符扫描 
			if ((a == '{' && ch[i] == '}') || (a == '(' && ch[i] == ')') || (a == '[' && ch[i] == ']')){
				continue;
			}
			else flag = 0;
		}
	}


	if (s.top != 0){    //当左括号多出没有与右括号匹配的时候(如:" {() ")
		flag = 0;
	}


	if (flag == 0){
		cout << "no";
	}
	else cout << "yes";
	return 0;
}
// 这个是对的 上面的那个有问题