Since this is not a single operation, computer would require a complex program to do this. There are two way of doing this. Typically, simple calculator programs are developed where you only resolve a single expression, "
a + b = ?
". Yours is more like pipelining the output as input to another expression.
First of the ways is to create a calculator application that resolves the expressions that are provided (it doesn't start resolving them). That method involves a stack push/pop operation to build up an expression for the calculator. A simple data structures lesson would have this covered. For an example, please see:
Calculator in C using stack - Stack Overflow[
^],
Reverse Polish notation - Wikipedia, the free encyclopedia[
^]. Common examples are,
(1 + 1) + 2 = ?
.
Then there is another way of doing this. That requires you to resolve one expression and when user tries to reuse the output (such as,
(1 + 1 = 2) + 2 = ?
) then you store the previous answer and build the next expression based on that result. That would require a condition to determine if there was a previous result,
public void operator_Click (object sender, EventArgs e) {
if(resultAvailable) {
a = result;
}
op = operatorTxt.Text;
}
public void evaluate(object sender, EventArgs e) {
}
This way, you will be able to process the expressions after resolving the previous ones. That is what is done in the modern mobile calculators.