Задание:Нужно чтоб после ввода строки из символов программа проверила правильно ли в ней поставлены скобки. Что нужно исправить чтоб запустился код,ошибка в 18 строке. Юзаю dev c++ Код: #include "iostream" #include <string> #include <stack> using namespace std; int main() { setlocale(LC_ALL, "Russian"); string strokaVvoda; cout << "Enter a string: "; getline(cin, strokaVvoda); int last = strokaVvoda.length()-1; stack <char> steck; for_each (char c in strokaVvoda) { steck.push(c); } cout << "Placed in a stack"<<endl; while (!steck.empty()) { if ((steck.top() == ')') || (steck.top() == '(')) if ((steck.top() == '}') || (steck.top() == '{')) if ((steck.top() == ']') || (steck.top() == '[')) { cout << "Bracket \"" << steck.top() << "\" is present"<< endl; } steck.pop(); } cout << "Stack: "; while (!steck.empty()) { cout << steck.top(); steck.pop(); } cout << strokaVvoda<<endl; system("pause"); return 0; } Код #include "iostream" #include <string> #include <stack> using namespace std; int main() { setlocale(LC_ALL, "Russian"); string strokaVvoda; cout << "Enter a string: "; getline(cin, strokaVvoda); int last = strokaVvoda.length()-1; stack <char> steck; for_each (char c in strokaVvoda) { steck.push(c); } cout << "Placed in a stack"<<endl; while (!steck.empty()) { if ((steck.top() == ')') || (steck.top() == '(')) if ((steck.top() == '}') || (steck.top() == '{')) if ((steck.top() == ']') || (steck.top() == '[')) { cout << "Bracket \"" << steck.top() << "\" is present"<< endl; } steck.pop(); } cout << "Stack: "; while (!steck.empty()) { cout << steck.top(); steck.pop(); } cout << strokaVvoda<<endl; system("pause"); return 0; }
А вы синтаксис for_each смотрели? На сколько я понял он работает с итераторами, а у вас кажется булевское значение условие возвращает, но если мы убираем символ "_" становится похоже на правду
Ок, но для сверки: я исправил #include "stdafx.h" #include <iostream> Код #include "stdafx.h" #include <iostream> и вставил for each(char c in strokaVvoda) Код for each(char c in strokaVvoda) и компилятор пропустил
Сорри не разбираюсь, проходит у меня так. гугл рекомендует удалить #include "stdafx.h" Код гугл рекомендует удалить #include "stdafx.h" Вообщем я хрен знает. В Си вообще ничего не понятно, вроде пытаешься так же как на жаве делать, но из за какой то кавычки компилятор матами кроет) уже бы сам исправил
#include "stdafx.h" #include <iostream> #include <string> #include <stack> using namespace std; int main() { // setlocale(LC_ALL, "Russian"); string strokaVvoda; cout << "Enter a string: "; getline(cin, strokaVvoda); int last = strokaVvoda.length()-1; stack <char> steck; for_each (char c in strokaVvoda); { steck.push(c); } cout << "Placed in a stack"<<endl; while (!steck.empty()) { if ((steck.top() == ')') || (steck.top() == '(')) if ((steck.top() == '}') || (steck.top() == '{')) if ((steck.top() == ']') || (steck.top() == '[')) { cout << "Bracket \"" << steck.top() << "\" is present"<< endl; } steck.pop(); } cout << "Stack: "; while (!steck.empty()) { cout << steck.top(); steck.pop(); } cout << strokaVvoda<<endl; system("pause"); return 0; } Код #include "stdafx.h" #include <iostream> #include <string> #include <stack> using namespace std; int main() { // setlocale(LC_ALL, "Russian"); string strokaVvoda; cout << "Enter a string: "; getline(cin, strokaVvoda); int last = strokaVvoda.length()-1; stack <char> steck; for_each (char c in strokaVvoda); { steck.push(c); } cout << "Placed in a stack"<<endl; while (!steck.empty()) { if ((steck.top() == ')') || (steck.top() == '(')) if ((steck.top() == '}') || (steck.top() == '{')) if ((steck.top() == ']') || (steck.top() == '[')) { cout << "Bracket \"" << steck.top() << "\" is present"<< endl; } steck.pop(); } cout << "Stack: "; while (!steck.empty()) { cout << steck.top(); steck.pop(); } cout << strokaVvoda<<endl; system("pause"); return 0; }
Нужно чтоб после ввода строки из символов программа проверила правильно ли в ней поставлены скобки нужно юзать stack
Если смотреть for_each( InputIt first, InputIt last, UnaryFunction f ); https://msdn.microsoft.com/ru-ru/library/e5sk9w9k.aspx то условие которое он вписал, не подходит по синтаксису
Смотри #include <Windows.h> и for each(char c in strokaVvoda) и прошло #include "stdafx.h" #include <iostream> #include <Windows.h> #include <string> #include <stack> using namespace std; int main() { setlocale(LC_ALL, "Russian"); string strokaVvoda; cout << "Enter a string: "; getline(cin, strokaVvoda); int last = strokaVvoda.length() - 1; stack <char> steck; for each(char c in strokaVvoda) { steck.push(c); } cout << "Placed in a stack" << endl; while (!steck.empty()) { if ((steck.top() == ')') || (steck.top() == '(')) if ((steck.top() == '}') || (steck.top() == '{')) if ((steck.top() == ']') || (steck.top() == '[')) { cout << "Bracket \"" << steck.top() << "\" is present" << endl; } steck.pop(); } cout << "Stack: "; while (!steck.empty()) { cout << steck.top(); steck.pop(); } cout << strokaVvoda << endl; system("pause"); return 0; } Код #include "stdafx.h" #include <iostream> #include <Windows.h> #include <string> #include <stack> using namespace std; int main() { setlocale(LC_ALL, "Russian"); string strokaVvoda; cout << "Enter a string: "; getline(cin, strokaVvoda); int last = strokaVvoda.length() - 1; stack <char> steck; for each(char c in strokaVvoda) { steck.push(c); } cout << "Placed in a stack" << endl; while (!steck.empty()) { if ((steck.top() == ')') || (steck.top() == '(')) if ((steck.top() == '}') || (steck.top() == '{')) if ((steck.top() == ']') || (steck.top() == '[')) { cout << "Bracket \"" << steck.top() << "\" is present" << endl; } steck.pop(); } cout << "Stack: "; while (!steck.empty()) { cout << steck.top(); steck.pop(); } cout << strokaVvoda << endl; system("pause"); return 0; }