Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstring>
- #include <vector>
- void Citire(std::vector<char>& sir)
- {
- std::ifstream fin("parantezare.in");
- char p;
- while (fin >> p)
- {
- if (p == ' ')
- continue;
- else
- sir.push_back(p);
- }
- fin.close();
- }
- void Afisare(std::vector<char> sir)
- {
- for (auto c : sir)
- std::cout << c;
- }
- int Ordin(char c)
- {
- if (c == '(' or c == ')')
- return 0;
- if (c == '[' or c == ']')
- return 1;
- return 2;
- }
- bool Parantezare(std::vector<char> paranteze)
- {
- std::vector<char> deschise;
- for (auto& crt : paranteze)
- {
- auto urm = *(&crt + 1);
- if (crt == '{' && urm == '}' || crt == '[' && urm == ']')
- return 0;
- if (strchr("([{", crt))
- deschise.push_back(crt);
- else
- {
- if (!deschise.empty())
- {
- for (auto des = deschise.begin(); des != deschise.end() - 1; ++des)
- {
- if (Ordin(crt) >= Ordin(*des))
- return 0;
- }
- if (crt == ')' && deschise.back() == '(' || crt == ']' && deschise.back() == '[' || crt == '}' && deschise.back() == '{')
- deschise.pop_back();
- else
- return 0;
- }
- else
- return 0;
- }
- }
- return deschise.empty();
- }
- int main()
- {
- std::vector<char> paranteze;
- Citire(paranteze);
- if (!paranteze.empty())
- {
- std::cout << "Sirul ";
- Afisare(paranteze);
- if (Parantezare(paranteze))
- std::cout << " este corect. ";
- else
- std::cout << " nu este corect. ";
- }
- else
- std::cout << "Sirul este vid. ";
- paranteze.clear();
- paranteze.shrink_to_fit();
- return 0;
- }
Add Comment
Please, Sign In to add comment