I want help creating a calculator that calculates a value like this 23*4+56

type or paste code here

#include<Keypad.h>
#include<LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
char keys[4][4] = {
{'1', '2', '3', '+'},
{'4', '5', '6', 'x'},
{'7', '8', '9', '/'},
{' ', '0', '-', '='}
};
byte a = A0, b = A1, counter = 0, counter1 = 0, counter2 = 0, counter3 = 0, counter4 = 0;
byte ROWS[4] = {8, 9, 10, 11};
byte COLS[4] = {12, 13, a, b};
char x;
String y, o, N;
long num1, num2, num;
Keypad kp(makeKeymap(keys), (ROWS), (COLS), (4), (4));

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0, 0);

}
void eqal() {
y = y + x;
num1 = y.toInt();
} void eqil_2() {
y = y + x;
num2 = y.toInt();
}
void loop() {
// put your main code here, to run repeatedly:
lcd.clear();
x = kp.getKey();

if (x) {
o = o + x;
if (counter == 0)
{ eqal();

  if (x == 'x') {
    counter++;
    counter1++;
    y = "";
  } else  if (x == '+') {
    counter++;
    counter2++;
    y = "";
  } else  if (x == '-') {
    counter++;
    counter3++;
    y = "";
  } else  if (x == '/') {
    counter++;
    counter4++;
    y = "";
  }
} else if (counter == 1) {
  eqil_2();
  if (x == '=') {
    if (counter1 == 1) {
      num = num1 * num2;
    } else if (counter2 == 1) {
      num = num1 + num2;
    } else if (counter3 == 1) {
      num = num1 - num2;
    } else if (counter4 == 1) {
      num = num1 / num2;
    }
    N = o;
    o = "";
  }
}

} if (num != 0) {
lcd.print(N);
lcd.print(num);
} else {
lcd.print(o);
}
if (x == ' ') {
counter = 0;
counter1 = 0;
counter2 = 0;
counter3 = 0;
counter4 = 0;
num = 0;
num1 = 0;
num2 = 0;
y = "";
x="";

}
delay(200);

}

Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

What is the code actually doing? How is that different from what you want?

the title of your post does not mean anything to me... (not even speaking about the format of the post for the code you posted)

This is a lot of code.
Where is your problem?
In
String operation ( int)
You return a String that is local in the function.
It will be destroyed when the function is ended and therefore you cannot return the String like this...

I expect some more troubles, but I will wait till you have properly formatted the code..

Did you test anything of this???
I am afraid you will need to break all this to small bits and test all the small bits. If they work as expected you can add them together.

Please edit your post to add code tags.

Explain the problem, as the post title is meaningless.

@boudy3453 : how can this be a solution?

you may find chapter 8, Program Development in The Unix Programming Environment much more than enlightening. the hoc wikipedia page provides several source code listings

A calculator normally uses infix notation. That is the operator e.g. '+' is between 2 operands. You need a stack to evaluate such expressions. Here is an example: Expression Evaluation - GeeksforGeeks
It requires the tokens to be separated by a space. Good luck implementing the unary minus e.g -1×(2+3)

edit

Your thread title has an example expression which includes brackets (23×4+56), however, your keypad does not include a bracket. Implementing a four function calculator which does not allow bracketed expressions is naturally easier.

OP is MIA…. Given the solution that was flagged, it’s probably best to wait to hear from Her/Him before using more our crystal balls

There is no problem with the code I wrote, but it doesn't work except for one process, like 234-543. I want to develop it to do such an operation as 543 + 534 * 43 + 45

consider following which processes a char string

#include <stdio.h>

int
oper (
    int  res,
    char op,
    int  val )
{
    switch (op)  {
    case '+':
        res += val;
        break;
    case '-':
        res -= val;
        break;
    case '*':
        res *= val;
        break;
    case '/':
        res *= val;
        break;
    }

    return res;
}

void
process (
    char *buf )
{
    int  res = 0;
    int  val = 0;
    char op = '\0';
    char c;

    printf ("%s: %s\n", __func__, buf);

    while (c = *buf++)  {
        switch (c)  {
        case '0' ... '9':
            val = 10*val + c - '0';
            break;

        case '+':
        case '-':
        case '*':
        case '/':
            if (! op)  {
                res = val;
                val = 0;
            }
            else  {
                res = oper (res, op, val);
                val = 0;
            }

            op = c;
            break;
        default:
            printf (" invalid char - %c\n", c);
            break;
        }

        printf ("    %c %6d %c %6d\n", c, res, op ? op : '_', val);
    }

    res= oper (res, op, val);
    printf ("  %d\n", res);
}

char buf [80];

int
main ()
{
    while (gets (buf))
        process (buf);
    return 0;
}

Where can you print a calculator?

On your 3D home printer.

1 Like

:wink: :roll_eyes:

How can I print it on Serial Monitor

What does it reger to?

Print a calculator?

Or print a result?

Or print a calculation?

By the way, you still have not properly formatted your code...

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