Advanced formula's

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

Lego_freak1999

Well-Known Member
Jan 26, 2017
658
45
28
25
HTTP-410
Category: calculation

Suggested name: formula

What I want:
Hey there i have made a advanced formula skript but i think it could be done more efficiently.

What it needs to do is calculating multiple binary values to 1 binary result.

Calculation posibilties:

* = and
+ = Or
`=not
()=bracketing
0=binary 0
1=binary 1

Let me explain what these posibilities can do.

if you look at the formula 1*1+1 this will result in a 1 Why? a*(and)b+(or)c = ? so a and b both need to be 1 to go trough OR only c needs to be 1. so 1*0+0=0, 1*1+0=1, 0*1+0=0, 0*1+1=1, 0*0+1=1.

Another value is the not value. The not value just inverts the value so 1`*1=0 because of the not it is actualy 0*1=0.

These formula's are still very basic and can get quite big like this one. a*b+c*d*e`*f+g.

But now i want to add bracketing. Bracketing is the same as math. Brackets go first. so things like this: a*(b*(c*d+e*f)*g*(h+i*j)).

Hope someone understands and is in for a challenge!

Uhm if you need a direct here you have a piece of code that does all the features except bracketing.

Code:
command /formula <text>:
    trigger:
        set {_val} to arg 1
        replace every "0`" with "1" in {_val}
        replace every "1`" with "0" in {_val}
        set {_l::*} to {_val} split at "+"
        loop {_l::*}:
            if loop-value doesn't contain "0":
                message "1"
                stop
        message "0"

Ideas for commands:

command /fomula <text>:

Ideas for permissions:

<none>

When I'd like it by: A reasonable time

 
This is what I came up with but I haven't tested it a lot, so there could be bugs:
Code:
command /formula <string>:
    trigger:
        set {_f} to formula(arg-1, sender)
        broadcast "f: %{_f}%"

function formula(str: string, exec: object=console) :: integer:
    loop 10 times:
        set {_l::*} to split {_str} at ""
        set {_depth} to 0
        set {_maxdepth} to 0
        loop {_l::*}:
            set {_l} to loop-value-2
            if {_l} is "(":
                add 1 to {_depth}
                {_depth} > {_maxdepth}
                set {_maxdepth} to {_depth}
            else if {_l} is ")":
                remove 1 from {_depth}
        if {_depth} != 0:
            send "Incorrect formula %{_depth}%, %{_str}%" to {_exec}
            stop
        if {_maxdepth} is 0:
            return formulaSolve({_str}) parsed as a integer
        set {_str} to formulaSolveDepth({_str}, {_maxdepth})
        # broadcast "%loop-number%: %{_str}%"
    
function formulaSolveDepth(str: string, depth: integer) :: string:
    set {_l::*} to split {_str} at ""
    set {_cd} to 0
    set {_i} to 0
    loop {_l::*}:
        set {_l} to loop-value
        set {_li} to loop-index parsed as a integer
        if {_l} is "(":
            add 1 to {_cd}
            {_cd} is {_depth}
            add 1 to {_i}
            set {_l2::%{_i}%} to ""
            set {_l2::%{_i}%::start} to {_li}
        if {_cd} is {_depth}:
            set {_l2::%{_i}%} to "%{_l2::%{_i}%}%%{_l}%"
        if {_l} is ")":
            remove 1 from {_cd}
            {_l2::%{_i}%::end} is not set
            set {_l2::%{_i}%::end} to {_li}
    loop {_l2::*}:
        set {_s} to formulaSolve(loop-value)
        set {_len} to {_l2::%loop-index%::end} - {_l2::%loop-index%::start} - length of {_s}
        loop {_len}-1 times:
            set {_s} to "%{_s}%-"
        # broadcast "%loop-value%: %{_s}% (%{_l2::%loop-index%::start}%, %{_l2::%loop-index%::end}%)"
        set {_str2} to {_str}
        replace every (subtext of {_str} from {_l2::%loop-index%::start} to {_l2::%loop-index%::end}) in {_str} with {_s}
        # broadcast "new str: %{_str}%, %{_str2}%, %{_depth}%"
    replace every "-" in {_str} with ""
    return {_str}
        
function formulaSolve(str: string) :: string:
    replace every "0`" with "1" in {_str}
    replace every "1`" with "0" in {_str}
    set {_l::*} to {_str} split at "+"
    loop {_l::*}:
        loop-value doesn't contain "0"
        return "1"
    return "0"
 
Last edited: