SA-MP Forums Archive
[Include] Advanced Calculator (From string return calculated integer) - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Includes (https://sampforum.blast.hk/forumdisplay.php?fid=83)
+---- Thread: [Include] Advanced Calculator (From string return calculated integer) (/showthread.php?tid=621169)



Advanced Calculator (From string return calculated integer) - Ivan_Ino - 07.11.2016

Hey!

so far I've only seen calculators script which can handle only 2 numbers!
But now you can calculate with multiple numbers.

Function in this include takes string and returning result as integer..
(soon as Float)

For now it works only with:
Код:
Addition 		(+)
Subtraction 		(−)
Multiplication 		(Ч)
Division 		(ч)
Function
Код:
TryCalculate(compute[], &result, &bool:fail, failstring[], const len = sizeof(failstring));
compute[] - String which will be calculated
&result - Return result in this variable
&bool:fail - Return 'true' if is there any errors
failstring[] - Store error string
const len = sizeof(failstring)


From example:

will return




But this:

Script will remove empty letters and return:





return error:





=





=

(real calculator)




Example
Код:
CMD:calculate(playerid, arg[])
{
	new 
		compute[64], 
		result, 
		bool:fail, 
		error[24],
		str[64]
	;
	if (!sscanf(arg, "s[64]", compute))
	{
		TryCalculate(compute, result, fail, error, sizeof(error));
		if(!fail)
		{
			format (str, sizeof(str), "Calculated: %d", result);
			SendClientMessage(playerid, -1, str);
		}
		else
		{
			format (str, sizeof(str), "Calculate Error: %s", error);
			SendClientMessage(playerid, -1, str);
		}
	}
	else SendClientMessage(playerid, -1, "Usage: /calculate [anything]");
	return 1;
}
Download:
https://github.com/Ino42O/PawnCalculator - 1.0.0


Re: Advanced Calculator (From string return calculated integer) - justice96 - 07.11.2016

Useful for the laziest scripter!


Re: Advanced Calculator (From string return calculated integer) - Spmn - 07.11.2016

Quote:
Originally Posted by Ivan_Ino
Посмотреть сообщение

=
That's not quite right.


Re: Advanced Calculator (From string return calculated integer) - justice96 - 07.11.2016

Quote:
Originally Posted by Spmn
Посмотреть сообщение
That's not quite right.
5*5=25-1=24/2=12.


Re: Advanced Calculator (From string return calculated integer) - Ivan_Ino - 07.11.2016

Quote:
Originally Posted by justice96
Посмотреть сообщение
5*5=25-1=24/2=12.
Yep
(from real calculator)


Re: Advanced Calculator (From string return calculated integer) - Spmn - 07.11.2016

You guys should rely more on your math skills, not a piece of software.

I'll just leave this link here:
https://www.mathsisfun.com/operation-order-pemdas.html

// and this: https://www.******.com/search?q=5*5-1/2


Re: Advanced Calculator (From string return calculated integer) - Max_Andolini - 07.11.2016

5*5-1/2=24,5
25-0,5=24,5

Operation sequence.


Re: Advanced Calculator (From string return calculated integer) - Ivan_Ino - 07.11.2016

Quote:
Originally Posted by Spmn
Посмотреть сообщение
You guys should rely more on your math skills, not a piece of software.

I'll just leave this link here:
https://www.mathsisfun.com/operation-order-pemdas.html

// and this: https://www.******.com/search?q=5*5-1/2
Yea true, but i didn't made () support yet, and logical calculate, but it work exactly like pc calculator


Re: Advanced Calculator (From string return calculated integer) - justice96 - 07.11.2016

Quote:
Originally Posted by eco1999
Посмотреть сообщение
5*5-1/2=24,5
25-0,5=24,5

Operation sequence.
It's accurate only for PC calculator.


Re: Advanced Calculator (From string return calculated integer) - Spmn - 07.11.2016

Quote:
Originally Posted by Ivan_Ino
Посмотреть сообщение
Yea true, but i didn't made () support yet, and logical calculate, but it work exactly like pc calculator
Still not correct.
MS calculator does the math at the time you press a sign button (+, -, *, /), but when you write down an entire expression (as in your screenshots), it must be calculated according to the operations order.


Re: Advanced Calculator (From string return calculated integer) - Stinged - 07.11.2016

Quote:
Originally Posted by Ivan_Ino
Посмотреть сообщение
but it work exactly like pc calculator
There are multiple modes. The default one is a really shitty version.
Click on "View" and check the other modes.

Operation order:
  1. ()
  2. * and /
  3. + and -



Re: Advanced Calculator (From string return calculated integer) - Ivan_Ino - 07.11.2016

still working on that


Re: Advanced Calculator (From string return calculated integer) - SyS - 07.11.2016

Quote:
Originally Posted by Stinged
Посмотреть сообщение
There are multiple modes. The default one is a really shitty version.
Click on "View" and check the other modes.

Operation order:
  1. ()
  2. * and /
  3. + and -
exponents and roots have higher priority than * and / i think.

A suggestion you can add sin,cos,tan and logarithm operations too and if you can add integral and differential operations that would be pretty awesome too .