Implement the basicCalculatorPlusMinus method that evaluates an expression containing plus and minus operations.

The input contains a string expression made of non-negative integers with plus and minus operators.

Your task is to calculate the value of the expression and return it as an integer.

For example, 1+2-3+4 is evaluated as 4, and 10-2+3 is evaluated as 11.

Example 1
Input:
expression (string) = 1+2-3+4
Return:
(int) 4
Example 2
Input:
expression (string) = 10-2+3
Return:
(int) 11
Example 3
Input:
expression (string) = 5
Return:
(int) 5

Scan the expression from left to right and build one number at a time.

Keep a running result and a current sign. When an operator is found, add the previous number to the result using the current sign, then update the sign based on the operator. At the end, add the last number as well.

This works because the expression contains only addition and subtraction, so there is no operator precedence to manage.

Pseudocode:

function basicCalculatorPlusMinus(expression):
    result = 0
    number = 0
    sign = 1
    for each character ch in expression:
        if ch is a digit:
            number = number * 10 + digit value of ch
        else if ch == '+':
            result = result + sign * number
            number = 0
            sign = 1
        else if ch == '-':
            result = result + sign * number
            number = 0
            sign = -1
    result = result + sign * number
    return result
Run your code to see the result.