Помогите сделать задание. Нужно чтоб после ввода строки из символов программа проверила правильно ли в ней поставлены скобки.
каждый символ имеет кодовое число.... тебе просто надо пробежаться циклом по строке.... и проверить ровняется ли текущий символ нужному коду, который мы ищем.
Просто оставлю здесь #include "stdafx.h" #include "iostream" #include <string> using namespace std; int main() { setlocale(LC_ALL, "Russian"); string strokaVvoda; cout << "Введи строку: "; getline(cin, strokaVvoda); int last = strokaVvoda.length()-1; if ((strokaVvoda.at(0) == '(') && (strokaVvoda.at(last) == ')')) { cout << "Все хорошо"<<endl; } else { cout << "Все плохо"<<endl; } cout << strokaVvoda<<endl; system("pause"); return 0; } Code #include "stdafx.h" #include "iostream" #include <string> using namespace std; int main() { setlocale(LC_ALL, "Russian"); string strokaVvoda; cout << "Введи строку: "; getline(cin, strokaVvoda); int last = strokaVvoda.length()-1; if ((strokaVvoda.at(0) == '(') && (strokaVvoda.at(last) == ')')) { cout << "Все хорошо"<<endl; } else { cout << "Все плохо"<<endl; } cout << strokaVvoda<<endl; system("pause"); return 0; }
Если без стеков, то так: #include <iostream> #include <string> using namespace std; int main(){ string na; cin >> na; int l=0; for(int c=0;c<na.length() && 0<=l;c++) if(na.at(c)=='(')l++; else if(na.at(c)==')')l--; if(l==0)cout << "OK!"; else cout << "BAD!"; } Code #include <iostream> #include <string> using namespace std; int main(){ string na; cin >> na; int l=0; for(int c=0;c<na.length() && 0<=l;c++) if(na.at(c)=='(')l++; else if(na.at(c)==')')l--; if(l==0)cout << "OK!"; else cout << "BAD!"; }
Со стеком: #include <iostream> #include <string> #include <queue> using namespace std; int main(){ string sa; cin >> sa; queue<char> la; for(int c=0;c<sa.length();c++) if(sa.at(c)=='(')la.push('('); else if(sa.at(c)==')') if(la.size())la.pop(); else{ la.push(')'); break; } cout << (la.size()?"BAD!":"OK!"); } Code #include <iostream> #include <string> #include <queue> using namespace std; int main(){ string sa; cin >> sa; queue<char> la; for(int c=0;c<sa.length();c++) if(sa.at(c)=='(')la.push('('); else if(sa.at(c)==')') if(la.size())la.pop(); else{ la.push(')'); break; } cout << (la.size()?"BAD!":"OK!"); }
ESPPE, тогда я скину реализацию через стек) #include "stdafx.h" #include "iostream" #include <string> #include <stack> using namespace std; int main() { setlocale(LC_ALL, "Russian"); string strokaVvoda; cout << "Введи строку: "; getline(cin, strokaVvoda); int last = strokaVvoda.length()-1; stack <char> steck; for each (char c in strokaVvoda) { steck.push(c); } cout << "Поместили в стек"<<endl; while (!steck.empty()) { if ((steck.top() == ')') || (steck.top() == '(')) { cout << "Скобка \"" << steck.top() << "\" присутствует"<< endl; } steck.pop(); } cout << "Стек: "; while (!steck.empty()) { cout << steck.top(); steck.pop(); } cout << strokaVvoda<<endl; system("pause"); return 0; } Code #include "stdafx.h" #include "iostream" #include <string> #include <stack> using namespace std; int main() { setlocale(LC_ALL, "Russian"); string strokaVvoda; cout << "Введи строку: "; getline(cin, strokaVvoda); int last = strokaVvoda.length()-1; stack <char> steck; for each (char c in strokaVvoda) { steck.push(c); } cout << "Поместили в стек"<<endl; while (!steck.empty()) { if ((steck.top() == ')') || (steck.top() == '(')) { cout << "Скобка \"" << steck.top() << "\" присутствует"<< endl; } steck.pop(); } cout << "Стек: "; while (!steck.empty()) { cout << steck.top(); steck.pop(); } cout << strokaVvoda<<endl; system("pause"); return 0; }