[Include] Advanced Calculator (From string return calculated integer)
#1

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
Reply
#2

Useful for the laziest scripter!
Reply
#3

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

=
That's not quite right.
Reply
#4

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

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

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
Reply
#7

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

Operation sequence.
Reply
#8

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
Reply
#9

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.
Reply
#10

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.
Reply
#11

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 -
Reply
#12

still working on that
Reply
#13

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 .
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)