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

**URL:** <https://forum.arduino.cc/t/arithmetic-calculator-that-can-process-multiple-operands-and-operators-at-the-same-time/980317>\
**Category:** General Guidance\
**Created:** [April 12, 2022, 10:07pm UTC](https://forum.arduino.cc/t/arithmetic-calculator-that-can-process-multiple-operands-and-operators-at-the-same-time/980317 "2022-04-12T22:07:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexthefool](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@alexthefool](https://forum.arduino.cc/u/alexthefool)\
**Post date:** [April 12, 2022, 10:07pm UTC](https://forum.arduino.cc/t/arithmetic-calculator-that-can-process-multiple-operands-and-operators-at-the-same-time/980317/1 "2022-04-12T22:07:49Z")

</div>

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 = ' ';  
}  
}

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [April 12, 2022, 10:16pm UTC](https://forum.arduino.cc/t/arithmetic-calculator-that-can-process-multiple-operands-and-operators-at-the-same-time/980317/2 "2022-04-12T22:16:24Z")

</div>

> [@alexthefool](#):
>
> like calculator apps on phones

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

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [April 12, 2022, 10:28pm UTC](https://forum.arduino.cc/t/arithmetic-calculator-that-can-process-multiple-operands-and-operators-at-the-same-time/980317/3 "2022-04-12T22:28:53Z")

</div>

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

> [@alexthefool](#):
>
> complex arithmetic operations

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 + \*.

---

<div class="post-metadata">

**Author:** ![alexthefool](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@alexthefool](https://forum.arduino.cc/u/alexthefool)\
**Post date:** [April 13, 2022, 12:21am UTC](https://forum.arduino.cc/t/arithmetic-calculator-that-can-process-multiple-operands-and-operators-at-the-same-time/980317/4 "2022-04-13T00:21:51Z")

</div>

yeah im planning on implementing those rules as well

---

<div class="post-metadata">

**Author:** ![mrburnette](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mrburnette/32/702556_2.png) [@mrburnette](https://forum.arduino.cc/u/mrburnette)\
**Post date:** [April 13, 2022, 12:27am UTC](https://forum.arduino.cc/t/arithmetic-calculator-that-can-process-multiple-operands-and-operators-at-the-same-time/980317/5 "2022-04-13T00:27:39Z")

</div>

From the early history of Arduino ...

> [@Don't Cross The Streams (FP scientific calculator serial co-processor)](https://forum.arduino.cc/t/dont-cross-the-streams-fp-scientific-calculator-serial-co-processor/144054):
>
> OK, I'm sorry about the title, but I'll blame it on too much coffee, the real, non-decaf stuff. astonished Most probably think of a co-processor as one of those math chips like the 8087 that were plugged into the IBM PC and clones back in the dark ages. They essentially allowed coprocessor-aware software such as Lotus 1-2-3 to calculate faster because software-only calculations were soooo slow. But, essentially a co-processor can be any processor that is programmed for a somewhat dedicated fu…

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

---

<div class="post-metadata">

**Author:** ![alexthefool](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@alexthefool](https://forum.arduino.cc/u/alexthefool)\
**Post date:** [April 13, 2022, 12:32am UTC](https://forum.arduino.cc/t/arithmetic-calculator-that-can-process-multiple-operands-and-operators-at-the-same-time/980317/6 "2022-04-13T00:32:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [April 13, 2022, 1:13am UTC](https://forum.arduino.cc/t/arithmetic-calculator-that-can-process-multiple-operands-and-operators-at-the-same-time/980317/7 "2022-04-13T01:13:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mrburnette](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mrburnette/32/702556_2.png) [@mrburnette](https://forum.arduino.cc/u/mrburnette)\
**Post date:** [April 13, 2022, 1:29am UTC](https://forum.arduino.cc/t/arithmetic-calculator-that-can-process-multiple-operands-and-operators-at-the-same-time/980317/8 "2022-04-13T01:29:54Z")

</div>

> [@er\_name\_not\_found](#):
>
> infix to postfix

Is there a cure? 😀

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [April 13, 2022, 1:34am UTC](https://forum.arduino.cc/t/arithmetic-calculator-that-can-process-multiple-operands-and-operators-at-the-same-time/980317/9 "2022-04-13T01:34:16Z")

</div>

> [@mrburnette](#):
>
> a cure

There's a patch

---

<div class="post-metadata">

**Author:** ![alexthefool](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@alexthefool](https://forum.arduino.cc/u/alexthefool)\
**Post date:** [April 13, 2022, 4:36am UTC](https://forum.arduino.cc/t/arithmetic-calculator-that-can-process-multiple-operands-and-operators-at-the-same-time/980317/10 "2022-04-13T04:36:48Z")

</div>

i'll search that to thank you.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 10, 2022, 4:37am UTC](https://forum.arduino.cc/t/arithmetic-calculator-that-can-process-multiple-operands-and-operators-at-the-same-time/980317/11 "2022-10-10T04:37:37Z")

</div>

Closed on October 10, 2022, 4:37am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
