1 //设置一个栈,每读入一个括号,若是左括号,则作为一个新的更急迫的期待压入栈中; 2 //若是右括号,并且与当前栈顶的左括号相匹配,则将当前栈顶的左括号退出,继续读下一个括号, 3 //如果读入的右括号与当前栈顶的左括号不匹配,则属于不合法的情况。 4 //在初始和结束时,栈应该是空的。 5 6 #include<iostream> 7 #include<cstdio> 8 using namespace std; 9 10 #define MAX 100 11 12 //定义数据类型 13 typedef char DataType; 14 15 typedef struct 16 { 17 DataType data[MAX]; 18 int top; 19 }Seqstack; 20 21 //栈的初始化 22 void InitStack(Seqstack* s) 23 { 24 s->top = -1; 25 } 26 27 //判断栈是否为空 28 int EmptyStack(Seqstack* s) 29 { 30 if (s->top == -1) 31 return 1; 32 else 33 return 0; 34 } 35 36 //判断栈是否已满 37 int FullStack(Seqstack* s) 38 { 39 if (s->top == MAX - 1) 40 return 1; 41 else 42 return 0; 43 } 44 45 //入栈 46 int Push(Seqstack* s, DataType x) 47 { 48 if (FullStack(s)){ 49 printf("栈满,不可入栈!"); 50 return 0; 51 } 52 else{ 53 s->top++; 54 s->data[s->top] = x; 55 return 1; 56 } 57 } 58 59 //出栈 60 int Pop(Seqstack* s) 61 { 62 if (EmptyStack(s)) 63 return 0; 64 else 65 { 66 s->top--; 67 return 1; 68 } 69 } 70 71 DataType Top(Seqstack* s) 72 { 73 return s->data[s->top]; 74 } 75 76 bool isValid(string ss,int n,Seqstack*S) { 77 for (int i = 0; i < n; i++) 78 { 79 if (ss[i] == '(' || ss[i] == '[') { 80 Push(S, ss[i]); 81 } 82 else if (ss[i] == ')' || ss[i] == ']') 83 { 84 if (EmptyStack(S)) { 85 return false; 86 } 87 char top = Top(S); 88 if ( (ss[i] == ']' && top == '[')||(ss[i] == ')' && top == '(')) 89 { 90 Pop(S); 91 } 92 else { 93 return false; 94 } 95 } 96 } 97 return EmptyStack(S); 98 } 99 100 int main() 101 { 102 Seqstack S; 103 DataType e; 104 string input; 105 InitStack(&S);//初始化 106 int n; 107 cout << "输入括号数目:"; 108 cin >> n; 109 cout << "输入括号:"; 110 cin >> input; 111 if (isValid(input, n, &S)) { 112 cout << "匹配" << endl; 113 } 114 else { 115 cout << "此串括号匹配不合法" << endl; 116 } 117 return 0; 118 }
//设置一个栈,每读入一个括号,若是左括号,则作为一个新的更急迫的期待压入栈中;//若是右括号,并且与当前栈顶的左括号相匹配,则将当前栈顶的左括号退出,继续读下一个括号,//如果读入的右括号与当前栈顶的左括号不匹配,则属于不合法的情况。//在初始和结束时,栈应该是空的。
#include<iostream>#include<cstdio>using namespace std;
#define MAX 100
//定义数据类型typedef char DataType;
typedef struct{DataType data[MAX]; int top; }Seqstack;
//栈的初始化void InitStack(Seqstack* s){s->top = -1; }
//判断栈是否为空int EmptyStack(Seqstack* s){if (s->top == -1)return 1; elsereturn 0;}
//判断栈是否已满int FullStack(Seqstack* s){if (s->top == MAX - 1)return 1; elsereturn 0;}
//入栈int Push(Seqstack* s, DataType x){if (FullStack(s)){ printf("栈满,不可入栈!");return 0;}else{s->top++; s->data[s->top] = x;return 1;}}
//出栈int Pop(Seqstack* s){if (EmptyStack(s))return 0;else{s->top--;return 1;}}
DataType Top(Seqstack* s){return s->data[s->top];}
bool isValid(string ss,int n,Seqstack*S) {for (int i = 0; i < n; i++){if (ss[i] == '(' || ss[i] == '[') {Push(S, ss[i]);}else if (ss[i] == ')' || ss[i] == ']'){if (EmptyStack(S)) {return false;}char top = Top(S);if ( (ss[i] == ']' && top == '[')||(ss[i] == ')' && top == '(')){Pop(S);}else {return false;}}}return EmptyStack(S);}
int main(){Seqstack S;DataType e;string input;InitStack(&S);//初始化int n;cout << "输入括号数目:";cin >> n;cout << "输入括号:";cin >> input;if (isValid(input, n, &S)) {cout << "匹配" << endl;}else {cout << "此串括号匹配不合法" << endl;}return 0;}