Advertisement
krstoilo

Mathematical Expression

Oct 6th, 2019
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5. #include <stack>
  6.  
  7. bool areBracketsCorrect (std::vector <char> vec1){
  8.  
  9.     std::stack <char> st;
  10.     bool isEmpty;
  11.  
  12.     for(size_t i = 0; i < vec1.size(); i++){
  13.  
  14.         if(vec1[i] == '('){
  15.  
  16.             st.push(vec1[i]);
  17.         }
  18.     }
  19.  
  20.     for(size_t j = 0; j < vec1.size(); j++){
  21.  
  22.         if(vec1[j] == ')'){
  23.             st.pop();
  24.         }
  25.     }
  26.  
  27.     if(st.empty() == 1){
  28.  
  29.         isEmpty = 1;
  30.     } else {
  31.         isEmpty = 0;
  32.     }
  33.  
  34.     return isEmpty;
  35. }
  36.  
  37. int main (){
  38.  
  39.     std::string expression;
  40.     std::cin >> expression;
  41.  
  42.     std::istringstream exprStream(expression);
  43.  
  44.     std::vector <char> vecOfChar;
  45.     char currentChar;
  46.  
  47.     while(exprStream >> currentChar){
  48.  
  49.         vecOfChar.push_back(currentChar);
  50.     }
  51.  
  52.     if(areBracketsCorrect(vecOfChar) == 1){
  53.  
  54.         std::cout << "correct" << std::endl;
  55.  
  56.     } else {
  57.  
  58.         std::cout << "incorrect" << std::endl;
  59.     }
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement