Advertisement
Jakubowiczish

Untitled

Mar 13th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. bool isExpressionValid(string expression);
  6. bool wyrazenie(string expression);
  7. char operators[5]={'+','-','*','/','^'};
  8.  
  9. int main()
  10. {
  11. string expression;
  12. cout << "Enter the expression: " << endl;
  13. getline(cin,expression);
  14. if(isExpressionValid(expression))
  15. cout << "The expression is valid" << endl;
  16. else
  17. cout << "The expression is faulty!" << endl;
  18. return 0;
  19. }
  20.  
  21.  
  22. bool isExpressionValid(string expression)
  23. {
  24. int bracketsCounter = 0;
  25. int charactersCounter=0;
  26. int operatorsCounter=0;
  27. bool lastOperator=false;
  28. bool lastCharacter=false;
  29. for(int i = 0; i < expression.size(); i++){
  30. if(expression[i]=='('){
  31. lastOperator=false;
  32. lastCharacter=false;
  33. bracketsCounter++;
  34. }
  35. if(expression[i]==')'){
  36. bracketsCounter--;
  37. lastOperator=false;
  38. lastCharacter=false;
  39. if(lastOperator) return false;
  40. if(bracketsCounter < 0) return false;
  41. }
  42. if((expression[i]>='A' && expression[i] <= 'Z') || (expression[i]>='a' && expression[i] <= 'z')){
  43. if(!lastCharacter){
  44. charactersCounter++;
  45. lastCharacter=true;
  46. lastOperator=false;
  47. }
  48. }
  49. for(int j = 0; j < 5; j++){
  50. if(expression[i]==operators[j]){
  51. operatorsCounter++;
  52. if(lastOperator){
  53. return false;
  54. }
  55. lastCharacter=false;
  56. lastOperator=true;
  57. }
  58. }
  59. }
  60. cout << "Liczba operatorow: " << operatorsCounter << endl;
  61. cout << "Liczba zmiennych: " << charactersCounter << endl;
  62. if(bracketsCounter!=0) return false;
  63. if(lastOperator) return false;
  64. return true;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement