Exemple #1
0
bool Calculator::Execute (Input const & input)
{
    int token = input.Token();
    bool status = false;
    if (token == tokError) {
        std::cout << "Unknown token\n";
    } 
    else if (token == tokNumber) {
        if (_stack.IsFull()) {
            std::cout << "Stack is full \n";
        } 
        else {
            _stack.Push(input.Number());
            status = true;
        }
    } 
    else { //operator
        assert(token == '+' || token == '-' || token == '*' || token == '/');
        if(_stack.IsEmpty()) {
            std::cout << "Stack is empty\n";
        } 
        else {
            int num2 = _stack.Pop();
            int num1;
            if(_stack.IsEmpty()) //2 operator will be same once there is only 1 data left in stack
                num1 = num2;
            else
                num1 = _stack.Pop();
            _stack.Push(Calculate(num1, num2, token));
            status = true;
        }
         
    }
    return status;
}
Exemple #2
0
bool Calculator::Execute( Input& input )
{
	int token = input.Token();
	bool status = false;

	if(token == Input::tokError ){
		cout <<"Unknown token " << endl;
	}
	else if(token == Input::tokNumber){
		if(_stack.IsFull()){
			cout <<"Stack is full " << endl;
		}
		else{
			_stack.Push(input.Number());
			status = true;
		}
	}
	else if (token == Input::tokBool){
		if (_stack.IsFull()){
			cout << "Stack is full " << endl;
		}
		else{
			_stack.Push(input.Bool());
			status = true;
		}
	}
	else if (token == 'x'){
		status = true;
	}
	else if (token == 'r'){
		_stack.Reset();
		status = true;
	}
	else if(token == 'q'){
		status = false;
	}
	else{
		assert(token == '+' ||token == '-' ||token == '*' ||token == '/' || token == '&' || token =='|' || token == '^'
			|| token == '!');
		if(_stack.IsEmpty()){
			cout <<"Stack is empty " << endl;
		}
		else{
			int num2 = _stack.Pop();
			int num1 =0;
			if(_stack.IsEmpty())
				num1 = num2;
			else 
				num1 = _stack.Pop();

			if (_type == NUMBER_CALC)
				_stack.Push(Calculate(num1, num2, token));
			else
				_stack.Push(Calculate((bool)num1, (bool)num2, token));
			status = true;
		}
	}

	return status;
}