fork download
  1. #include <iostream>
  2. #include <iterator>
  3. #include <sstream>
  4. #include <stdexcept>
  5. #include <string>
  6. #include <vector>
  7.  
  8. int main()
  9. {
  10. bool result;
  11.  
  12. // Read the line
  13. std::string line;
  14. std::getline(std::cin, line);
  15.  
  16. // Split the line at spaces (https://stackoverflow.com/a/237280/1944004)
  17. std::istringstream iss(line);
  18. std::vector<std::string> tokens{std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>{}};
  19.  
  20. // Convert last element to bool
  21. if (tokens.back() == "true") result = true;
  22. else if (tokens.back() == "false") result = false;
  23. else throw std::invalid_argument("The last argument is not a boolean!");
  24.  
  25. // Remove the last element
  26. tokens.pop_back();
  27.  
  28. // Loop over the nots
  29. for (auto const& t : tokens)
  30. {
  31. if (t == "not") result = !result;
  32. else throw std::invalid_argument("Negation has to be indicated by 'not'!");
  33. }
  34.  
  35. // Output the result
  36. std::cout << std::boolalpha << result << '\n';
  37. }
Success #stdin #stdout 0s 16072KB
stdin
not not not not not not not false
stdout
true