paranteze intermediar 2
This commit is contained in:
+71
-37
@@ -3,52 +3,86 @@
|
|||||||
//implementare proprie sau std::stack. Exemplu: şirul [()()] este corect, şirul
|
//implementare proprie sau std::stack. Exemplu: şirul [()()] este corect, şirul
|
||||||
//([]) nu este corect, şirul ()]( nu este corect.
|
//([]) nu este corect, şirul ()]( nu este corect.
|
||||||
//
|
//
|
||||||
#include <iostream>
|
#include<iostream>
|
||||||
#include <stack>
|
#include<stack>
|
||||||
|
|
||||||
std::string paranteze;
|
int main(){
|
||||||
std::stack<char> stiva;
|
std::string paranteze;
|
||||||
int ok=1;
|
std::stack<char> stiva;
|
||||||
|
|
||||||
void rotundeInchise(std::string paranteze);
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
std::cout <<"sir de paranteze = ";
|
std::cout <<"sir de paranteze = ";
|
||||||
std::cin >> paranteze;
|
std::cin >> paranteze;
|
||||||
|
bool ok = true;
|
||||||
|
int n = paranteze.size();
|
||||||
|
|
||||||
bool acolada, patrata;
|
for (int i = 0; i < n; i++) {
|
||||||
|
// {[(
|
||||||
for (int i = 0; i < paranteze.size(); i++) {
|
if(i + 2 < n && paranteze[i] == '{' && paranteze[i+1] == '[' && paranteze[i+2] == '('){
|
||||||
if (paranteze[i] == '{') {
|
stiva.push('{');
|
||||||
stiva.push('{');
|
stiva.push('[');
|
||||||
}
|
stiva.push('(');
|
||||||
else if (paranteze[i] == '[') {
|
i += 2;
|
||||||
stiva.push('[');
|
}
|
||||||
}
|
// [(
|
||||||
else if (paranteze[i] == '(')
|
else if(i + 1 < n && paranteze[i] == '[' && paranteze[i+1] == '(') {
|
||||||
stiva.push('(');
|
stiva.push('[');
|
||||||
|
stiva.push('(');
|
||||||
else {
|
i += 1;
|
||||||
if (paranteze[i] == ')' || paranteze[i] == ']' || paranteze[i] == '}') {
|
}
|
||||||
if(!stiva.empty()){
|
// (
|
||||||
stiva.pop();
|
else if (paranteze[i] == '('){
|
||||||
}
|
stiva.push('(');
|
||||||
else {
|
}
|
||||||
std::cout<<"prea multe paranteze deschise \n";
|
// )]}
|
||||||
ok=0;
|
else if(i + 2 < n && paranteze[i] == ')' && paranteze[i+1] == ']' && paranteze[i+2] == '}'){
|
||||||
break;
|
if(stiva.size() >= 3){
|
||||||
}
|
stiva.pop();
|
||||||
|
stiva.pop();
|
||||||
|
stiva.pop();
|
||||||
|
i += 2;
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
std::cout << "nu sunt închise )]} \n";
|
||||||
|
ok = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// )]
|
||||||
|
else if(i + 1 < n && paranteze[i] == ')' && paranteze[i+1] == ']') {
|
||||||
|
if(stiva.size() >= 2){
|
||||||
|
stiva.pop();
|
||||||
|
stiva.pop();
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
std::cout << "nu sunt închise )] \n";
|
||||||
|
ok = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// )
|
||||||
|
else if(paranteze[i] == ')'){
|
||||||
|
if(!stiva.empty()){
|
||||||
|
stiva.pop();
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
std::cout << "nu sunt închise ) \n";
|
||||||
|
ok = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
std::cout << "Caracter invalid sau ordine gresita\n";
|
||||||
|
ok = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ok && stiva.empty()) {
|
if(ok && stiva.empty()){
|
||||||
std::cout << "toate sunt închise";
|
std::cout << "e bun sirul";
|
||||||
}
|
}
|
||||||
else {
|
else if(ok){
|
||||||
std::cout << "nu sunt închise";
|
std::cout << "paranteze neînchise";
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user