Evaluating an equation

I have a character array:

char myArray[5] = { '1', '2', '3', '+', '7' };

Now I would like to perform a function where 123 is taken as a number and 7 is taken as a separate number so that they can be added and returned in a variable.

Can anyone help me and is it possible to do so? Thanks!

TheUNOGuy:
I have a character array:

char myArray[5] = { '1', '2', '3', '+', '7' };

Now I would like to perform a function where 123 is taken as a number and 7 is taken as a separate number so that they can be added and returned in a variable.

Can anyone help me and is it possible to do so? Thanks!

Probably not possible in the way you think.

Where did the char array come from?
More importantly how do you know it is characters long?
Why not char myArray = "123+7"; so you at least have a terminator?
What maths operations do you wish to support?
Why do you want to do this? I suspect probable X/Y problem.

Now I would like to perform a function where 123 is taken as a number and 7 is taken as a separate number so that they can be added and returned in a variable.

this doesn't make so much sense so you better describe the whole project.

however you can do following

char myArray[5] = { '1', '2', '3', '+', '7' };

int calc (char in[])
{
  int result = (in[0] - 48) * 100 + (in[1] - 48) * 10 + (in[2] - 48) + (in[4] - 48);
  return (result);
}

void setup() {
  Serial.begin(115200);
  Serial.println(calc(&myArray[0]));

}

void loop() {
  // put your main code here, to run repeatedly:
}

and yes, the addition is fixed, but that's your description. However, you can react also on in[3] and do what needed...

My guess is the OP thinks there is some function like “eval” in interpreted languages that will do the heavy lifting for them...

pcbbc:
My guess is the OP thinks there is some function like “eval” in interpreted languages that will do the heavy lifting for them…

Python’s good with that…

Ok I’ll desribe my whole project as well…

I basically want the user to enter an equation like 5+2-3 and then Arduino solves the problem and gives the output. Kinda like eval() in python. By the way input and output in serial.

pcbbc:
Probably not possible in the way you think.

Where did the char array come from?
More importantly how do you know it is characters long?
Why not char myArray = "123+7"; so you at least have a terminator?
What maths operations do you wish to support?
Why do you want to do this? I suspect probable X/Y problem.

  1. User entering it through serial monitor.
    2 & 3. I feel I should take it as a string and convert to char array by using mystring.toCharAray()
  2. +, -, *, /
  3. Any equation can be 5/3+2-1 or 3-2+1, etc.

I have explained in detail ( sorry for not doing it in Reply #0 ), can you please help me? Or first of all is it possible??

I wanted to this before and I had written a code but just can't figure out how to do it now ( I forgot )

Oh ok, then it is possible right? Or...

You have explained the “what” in more detail, but not the “why”, so the suspicion of an X-Y problem remains.

Yes, it is possible, but as already mentioned, there is no quick and easy function for this in C as there is in python. You will need to write quite a lot of code.

Some things to consider:

Can the numbers be more than one digit?

Should operators be applied in strict left-right order, or should some, like × ÷, be automatically given precedence over others, like + - (“BODMAS”)?

Do you want to allow use of brackets/braces to override operator precedence?

Are the numbers whole, or can they have decimal places?

Can negative numbers be used, like “3×-7”?

How will you know when the end of the expression is reached, and the calculation should be performed?

Sounds like homework and you are looking for calculator code for arduino, that should not be hard to find if you try.

Danois90:
Sounds like homework and you are looking for calculator code for arduino, that should not be hard to find if you try.

Ok thanks!

I found a website which gives the answer to my question! -