Yambam
Member
GM Version: Any
Target Platform: All
Download: http://gamemaker.mooo.com/games/229637-calculator-and-grapher-script
Links: N/A
Summary:
This is a script that evaluates simple mathematical expressions like 10+(1+3)*3*4^2 and either returns the result from the stack (202 in this case), or you can choose to return a postfix expression from the infix input (in which case it becomes 10 1 3 + 3 * 4 * + here).
Features:
Make a new script called infix_evaluate:
Examples of usage:
Target Platform: All
Download: http://gamemaker.mooo.com/games/229637-calculator-and-grapher-script
Links: N/A
Summary:
This is a script that evaluates simple mathematical expressions like 10+(1+3)*3*4^2 and either returns the result from the stack (202 in this case), or you can choose to return a postfix expression from the infix input (in which case it becomes 10 1 3 + 3 * 4 * + here).
Features:
- Evaluate mathematical expressions.
- Convert expressions from infix (4*(3+5)) to postfix (4 3 5 + *).
- Constants like pi, e and phi.
- Functions like sin(x), cos(x), point_direction(x1,y1,x2,y2), etc.
Make a new script called infix_evaluate:
PHP:
/// infix_evaluate(infix,ret_fromstack,constants/scripts)
//
// Evalutes infix expression. It returns either a postfix
// expression (string), or it returns the answer from the
// main stack depending on the ret_fromstack argument.
//
// infix expression (infix, e.g. 5*8), string
// ret_fromstack return postfix, string; or value from stack, boolean
// constants map containing constants like pi and e, ds_map
//
/// GMLscripts.com/license
{
var str,ret_fromstack,destroy,i,j,k,l,arg,d,len,newstr,newstr2,op,push,do_push,value;
str=argument0;
push=ds_queue_create();
do_push=0;
if argument3=0
{
pcount=1+string_count('^',str)+2*(string_count('*',str)+string_count('/',str))+3*(string_count('+',str)+string_count('-',str));
str=string_repeat('(',pcount)+string_replace_all(string_replace_all(string_replace_all(string_replace_all(string_replace_all(string_replace_all(string_replace_all(str,'(','((((('),')',')))))'),'^',')^('),'*','))*(('),'/','))/(('),'+',')))+((('),'-',')))-(((')+string_repeat(')',pcount);
argument3=50;
argument4=ds_stack_create();
argument5=ds_queue_create();
global.ret_stack=argument4;
global.ret_stack_exec=argument5;
ret_fromstack=argument1;
destroy=1;
}
else
{
ret_fromstack=0;
destroy=0;
}
if string_char_at(str,1)!="("
{
if ds_map_exists(argument2,str)
ds_stack_push(argument4,ds_map_find_value(argument2,str));
else
{
if string_length(str)=string_length(string_digits(str))+string_count('-',str)+string_count('.',str)
ds_stack_push(argument4,real(str));
else
ds_stack_push(argument4,0);
}
return str;
}
if argument3=1
return "";
newstr="";
newstr2="";
i=1;
j=1;
for(k=0;k<50;k+=1)
{
if string_char_at(str,i)="("
{
i+=1;
j=i;
d=1;
len=string_length(str);
while(d&&i<=len)
{
char=string_char_at(str,i);
if char="("
d+=1;
if char=")"
d-=1;
i+=1;
}
}
op[k]=string_char_at(str,i);
newstr+=infix_evaluate(string_copy(str,j,i-1-j),argument1,argument2,argument3-1,argument4,argument5)+" ";
if string_pos(op[k],'^*/+-([]')
{
newstr2+=" "+op[k];
do_push=1;
i+=1;
}
else
break;
}
k_max=k-1;
for(k=k_max;k>=0;k-=1)
{
if do_push
{
b=ds_stack_pop(argument4);
a=ds_stack_pop(argument4);
if op[k]='^'
ds_queue_enqueue(push,power(a,b));
else if op[k]='*'
ds_queue_enqueue(push,a*b);
else if op[k]='/'
{
if b=0
ds_queue_enqueue(push,a);
else
ds_queue_enqueue(push,a/b);
}
else if op[k]='+'
ds_queue_enqueue(push,a+b);
else if op[k]='-'
ds_queue_enqueue(push,a-b);
else if op[k]=','
{
ds_queue_enqueue(argument5,b);
do_push=0;
}
if do_push while(!ds_queue_empty(push))
ds_stack_push(argument4,ds_queue_dequeue(push));
}
}
ds_queue_destroy(push);
value=ds_stack_pop(argument4);
if destroy
{
ds_stack_destroy(argument4);
ds_stack_destroy(argument5);
}
if ret_fromstack
return value;
while(string_count(' ',newstr))
newstr=string_replace_all(newstr,' ',' ');
return newstr+newstr2;
}
Code:
map=ds_map_create()
ds_map_add('pi',pi)
- Initialization
Code:
show_message(infix_evaluate("10*10+5*4",1,map))
- Result: 154
Code:
show_message(infix_evaluate("3*10^2",1,map))
- Result: 300
Code:
show_message(infix_evaluate("3*(1+2)",1,map))
- Result: 9
Code:
show_message(infix_evaluate("2*pi",1,map))
- Result: 6.28
Code:
show_message(infix_evaluate("10*10+5*4",0,map))
- Result: 10 10 * 5 4 * +
Code:
show_message(infix_evaluate("3*(1+2)",0,map))
- Result: 3 1 2 + *
Code:
ds_stack_destroy(global.ret_stack)
ds_stack_destroy(global.ret_stack_exec)
- Destroy returned stacks.
Last edited: