braketmatch

This commit is contained in:
e2hang
2025-07-22 13:55:12 +08:00
parent f14c43fe3a
commit 6ef3407506
2 changed files with 35 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
#include <iostream>
#include <stdexcept>
#include <string>
#include <stack>
using namespace std;
void isMatch(const string& s){
stack<int> t;
//bool error = false;
for(int i = 0; i < s.length(); i++){
if(s[i] == '('){
t.push(i);
}
if(s[i] == ')'){
try{
if(t.empty()) throw std::runtime_error("Error: Braket \" ) \" No Match");
t.pop();
}
catch(const runtime_error& e){
cout << e.what() << endl;
//error = true;
return;
}
}
}
if(t.empty()) cout << "Match" << endl;
else cout << "Error: Braket \" ( \" No Match" << endl;
}
int main(){
string s;
cin >> s;
isMatch(s);
return 0;
}

Binary file not shown.