Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- bool isExpressionValid(string expression);
- bool wyrazenie(string expression);
- char operators[5]={'+','-','*','/','^'};
- int main()
- {
- string expression;
- cout << "Enter the expression: " << endl;
- getline(cin,expression);
- if(isExpressionValid(expression))
- cout << "The expression is valid" << endl;
- else
- cout << "The expression is faulty!" << endl;
- return 0;
- }
- bool isExpressionValid(string expression)
- {
- int bracketsCounter = 0;
- int charactersCounter=0;
- int operatorsCounter=0;
- bool lastOperator=false;
- bool lastCharacter=false;
- for(int i = 0; i < expression.size(); i++){
- if(expression[i]=='('){
- lastOperator=false;
- lastCharacter=false;
- bracketsCounter++;
- }
- if(expression[i]==')'){
- bracketsCounter--;
- lastOperator=false;
- lastCharacter=false;
- if(lastOperator) return false;
- if(bracketsCounter < 0) return false;
- }
- if((expression[i]>='A' && expression[i] <= 'Z') || (expression[i]>='a' && expression[i] <= 'z')){
- if(!lastCharacter){
- charactersCounter++;
- lastCharacter=true;
- lastOperator=false;
- }
- }
- for(int j = 0; j < 5; j++){
- if(expression[i]==operators[j]){
- operatorsCounter++;
- if(lastOperator){
- return false;
- }
- lastCharacter=false;
- lastOperator=true;
- }
- }
- }
- cout << "Liczba operatorow: " << operatorsCounter << endl;
- cout << "Liczba zmiennych: " << charactersCounter << endl;
- if(bracketsCounter!=0) return false;
- if(lastOperator) return false;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement