Arithmetic Calculator that can process multiple operands and operators at the same time

Basically I'm doing a Calculator that can do complex arithmetic operations. I got the simple two inputs and one output done, but I don't know where to start on making may calculator do complex arithmetic like 4+1-3*4, and make it like calculator apps on phones that take the final Answer as it's next input when pressing operators. Im doing it on a 4x4 matrix keypad and I2C LCD.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display

#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;

char keys [ROWS] [COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'.', '0', '=', '/'}
};
byte rowPins[ROWS] = {10, 9, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

boolean presentValue = false;
boolean next = false;
boolean final = false;
String num1, num2;
int answer = 0;
char op;

void setup()
{
lcd.begin();
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("Midterm Act");
lcd.setCursor(3,1);
lcd.print("Calculator");
delay(3000);
lcd.clear();
}

void loop() {
char key = myKeypad.getKey();

if (key != NO_KEY && (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0'))
{
if (presentValue != true)
{
num1 = num1 + key;
int numLength = num1.length();
lcd.setCursor(0, 0);
lcd.print(num1);
}
else
{
num2 = num2 + key;
int numLength = num2.length();
int numLength1 = num1.length();
lcd.setCursor(1 + numLength1, 0);
lcd.print(num2);
final = true;
}
}

else if (presentValue == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+'))
{
if (presentValue == false)
{
int numLength = num1.length();
presentValue = true;
op = key;
lcd.setCursor(0 + numLength, 0);
lcd.print(op);
}
}

else if (final == true && key != NO_KEY && key == '=') {

if (op == '+') {
  answer = num1.toInt() + num2.toInt();
}
else if (op == '-') {
  answer = num1.toInt() - num2.toInt();
}
else if (op == '*') {
  answer = num1.toInt() * num2.toInt();
}
else if (op == '/') {
  answer = num1.toInt() / num2.toInt();
}
lcd.clear();
lcd.setCursor(16, 1);
lcd.autoscroll();
lcd.print(answer);
lcd.noAutoscroll();

}
else if (key != NO_KEY && key == 'C') {
lcd.clear();
presentValue = false;
final = false;
num1 = "";
num2 = "";
answer = 0;
op = ' ';
}
}

Calculator apps usually have precedence rules, like "do multiplication before addition and subtraction". Do you plan to implement those rules?

Please use Code Tags </> and proper indentation to present code.

I.e. you don't want to use the complex data type but want to process multiple operands and operators with different precedence?

A simple but unusual solution is RPN (Reverse Polish Notation) where operands are pushed onto a stack (by Enter key) and each operator combines the topmost entries of that stack. The intermediate or final result (with one item in the stack) is the stack top item. That's how some famous HP calculators worked.

Otherwise I'd store everything and delay all operations until '=' is pressed. Then parse the input into a tree of operands and operations, depending on operand precedence, and finally evaluate that tree. The actual result is stored in the accumulator and can be used as the first operand of the next calculation; you'll need a button to clear the accumulator or last entry during input.

Things become more complicated if you allow for parentheses to evaluate something like 3*(4+5). In RPN that's simply 3 4 5 + *.

yeah im planning on implementing those rules as well

From the early history of Arduino ...

May give you a few ideas about parsing the keyboard and configuring the system to be expandable.

Thank you for this. I'll look this up.

You should research switching from infix to postfix, then how to evaluate a postfix expression. Both of these require the use of stacks, so if you don't know how to use a stack then start there.

Is there a cure? :grinning:

There's a patch

i'll search that to thank you.