Hello everyone!
I have a bit of a tricky problem to solve. I have a string with some data in it that I want to calculate, but wrapping my head around the required code is proving understandably difficult. The following is my current code:
String calculation = "\"int myResult = 7+6/5+2*(4-3)+1\"";
void setup() {
Serial.begin(115200);
Serial.println("STARTING COMMAND: " + calculation);
Serial.println();
Serial.println("OUTPUT WE WANT FROM DEFINING BRACKETS FUNCTION: (((7+(6/5))+(2*(4-3)))+1)");
Serial.println("OUTPUT WE GET FROM DEFINING BRACKETS FUNCTION: " + calculate(calculation));
Serial.println("CALCULATION COMPLETE");
}
void loop() { }
String calculate(String string) {
String result = removeStartingVariableDefinition(string);
result = addDefiningBracketsAroundCalculation(result);
return result;
}
String removeStartingVariableDefinition(String string) {
string.replace(" ",""); string.replace("\n",""); string.replace(";",""); //Remove invalid characters
return string.substring(string.indexOf('=') + 1,string.length()); //return everything after the equals sign
}
String addDefiningBracketsAroundCalculation(String in) {
//Iterate over every symbol and ask if it's a number or operation
for(int i = 0; i < in.length(); i++) {
}
}
First, I remove anything that is not directly part of the calculation (Like line ends, spaces, variables declaration, etc). Then, I wanna put brackets around each individual part of it. For example, I wanna put turn this:
7+6/5+2*(4-3)+1
Into this:
(((7+(6/5))+(2*(4-3)))+1)
That way, the next part of code can individually interpret the code and calculate each individual section. However, wrapping the brackets around the code using BIMDAS and also avoiding segmenting bracketed sections of the calculation is proving difficult. As you can see, addDefiningBracketsAroundCalculation() is currently empty (save for the iteration over all characters in the calculation).
I am aware that alot of people will question why I need this in the first place, and the answer to that is "I am building some software designed to use an SD card as the arduino's variable memory." I am aware that there are ways to change the bootloader to do this already, but I wanna do it just using a standard, unmodified Mega 2560 (Arduino Uno if I can) with the default bootloader. I have most of the other code madeup already, but I am developing this function in it's own file for now to test if I can get it to work. I am also aware that this task is better suited for a RPi, but I still want to use the arduino despite this.
The reason I wanna do this is because with using files as variables, you can dynamically define variables, check if a variable exists, and have lists like python and C# do with variable array lengths that you can append to later if wanted. I'm effectively rewriting the whole system from scratch, simply because I can.
But back to the current issue at hand, I was wondering if someone could help me with getting the brackets in their correct positions for the next section of code to work? I am fully expecting it to be a function that iterates over itself multiple times until it's complete. Any help is appreciated, and if you want to know more about the code I'm writing (ASDOS, Arduino SD Operating System) I'd be happy to elaborate.
Thanking you in advance,
Andrey