How to convert "+" to +

M

mastmartelli

Guest
I have a system setup to generate random math problems, for example "3 + 5" or "10 / 5", I was wondering if there's anyway to convert them from strings into an actual operation? Say the variable is
Code:
mathQuestion = "1 + 4 - 2";
and I want to do
Code:
// i know this does not work
mathResult = real(mathQuestion);
where mathResult equals 3. Is that possible?
 

curato

Member
I would set up a list with both the numbers and operator separate and then the answer. It is a lot easier to take the separate information and mash it together than to parse the string to get the information out.
 

samspade

Member
There might be a better way, but assuming you can control the format of the string version of the question, you can simply loop through the string, group and convert the string numbers to real numbers and then use a switch statement or if/else if structure do mathematical operations based upon the operators.

Another option, assuming you control the format, would be to save the math question as an array (or something equivalent) and then use a switch statement or if/else if structure to do the correct mathematical operations instead. For example:

GML:
enum m {
    add,
    subtract
}

mathQuestion = [1, m.add, 4, m.subtract, 2];

var result = 0;
for (var i = 0; i < array_length(mathQuestion); i += 2) {
    if (i == 0) result = mathQuestion[i];
    switch (mathQuestion[i + 1]) {
        case m.add:
            result += mathQuestion[i + 2];
            break;
        case m.subtract:
            result -= mathQuestion[i + 2];
            break;
    }
}
That code isn't perfect, it should probably be a script and my loop might be wrong, I didn't test it, but that would be the basic idea.
 

woods

Member

something like this?
draw_text(100, 100, "Score: " + string(score) + " / Health: " + string(health));
set the variable for score and set the variable for health.. then draw the numbers




var first_num = 2
var second_num = 3

draw_text(x, y, string(first_num) + " + " string(second_num ));
 

chamaeleon

Member
Search the marketplace for an extension that allows you to perform evaluations of strings. One example is N String Parser 2. I have no idea how well it will work for you, consider it a starting point for your explorations.
 
M

mastmartelli

Guest
I would set up a list with both the numbers and operator separate and then the answer. It is a lot easier to take the separate information and mash it together than to parse the string to get the information out.
I have that, my script is:
Code:
var mathList = ds_list_create();
var symbolList = ds_list_create();

mathQuestion = "";
for (var i = 0; i < sectionMax; i++) {
    mathList[| i] = irandom_range(-numberClamp, numberClamp);
    mathQuestion += string(mathList[| i]);
    
    if (i < sectionMax - 1) {
        symbolList[| i] = choose("*", "/", "+", "-");
        mathQuestion += " " + symbolList[| i] + " ";
    }
}

mathX = RESOLUTION_W/2 - string_width(string(mathQuestion))/2;

ds_list_destroy(mathList);
ds_list_destroy(symbolList);
sectionMax++;
But I still don't know how to continue it cus the +, -, /, and * I can only get in a string
 

Yal

šŸ§ *penguin noises*
GMC Elder
Rather than starting with a string and converting them to numbers later, start with the operation and convert it to a string when needed instead.

You could define macros op_PLUS, op_MINUS, op_MULT and op_DIV and then define maths questions as arrays with a number,operation,number[,operation,number]* order; convert numbers to strings with standard string() and write your own function to return the appropriate symbol for the operations (e.g. return "+" for op_PLUS - a switch statement would be perfect for this).
 

chamaeleon

Member
But I still don't know how to continue it cus the +, -, /, and * I can only get in a string
There is no built-in functionality for it. Write code that evaluates expressions using the available functions (primarily cutting up strings and turning numbers into floating point numbers, coupled with a proper operator precedence evaluation function), or find code that some else wrote, like in the marketplace. Or don't cut up strings into components if you already have the pieces, but you'll still need to implement the evaluation function, and odds are you will not find existing code that will seamlessly work with your data represenation without modification.
 

Rob

Member
Rather than have preset questions, why not just have difficulty levels, and base the numbers on that?

Eg beginner

Answer = ( irandom(9) + 1 );
First Number = answer -irandom(answer)
Second Number = answer - First Number

You don't even need to decide on whether you want to ask an add or subtract question, as you can just decide on the order in which the 3 variables is asked and use the correct symbol accordingly. I think!?
 
Top