Advertisement
Jakubowiczish

Untitled

Mar 13th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. bool valid(string expression);
  6. bool is_operator(char op);
  7.  
  8. char operators[5]={'+','-','*','/','^'};
  9.  
  10. int main()
  11. {
  12. string expression;
  13. cout << "Enter the expression: " << endl;
  14. //getline(cin,expression);
  15. cin >> expression;
  16. cout << valid(expression);
  17. return 0;
  18. }
  19.  
  20. bool valid(string expression)
  21. {
  22. int brackets_counter = 0;
  23. int operators_counter = 0;
  24. int counter = 0;
  25.  
  26. for(int i = 0; i < expression.length(); i++) {
  27. if (expression[i] == '(')
  28. brackets_counter++;
  29. if (expression[i] == ')') {
  30. brackets_counter--;
  31. if (brackets_counter < 0)
  32. return false;
  33. }
  34. if (expression[i] >= 'a' and expression[i] <= 'z') {
  35. counter++;
  36. }
  37.  
  38. if(is_operator(expression[i])){
  39. operators_counter++;
  40. if(i == 0) return false;
  41. }
  42. if((operators_counter - counter) == -1)
  43. return true;
  44. }
  45. return false;
  46. }
  47.  
  48. bool is_operator(char op)
  49. {
  50. for(int i=0; i < 5; i++)
  51. if(op == operators[i])
  52. return true;
  53. return false;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement