Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $${
- // Calc v 1.3
- // Bind to onSendChatMessage
- // Made by Mel
- //lol idk what my code does
- //comments look pretty
- STRIP(&chat,%CHAT%)
- IFMATCHES(%&chat%,"^(/?)/calc")
- IFMATCHES(%&chat%,"^(/?)/calc$")
- SET(&calc,"help")
- ELSE
- MATCH(%&chat%,"calc (.*)$",{&calc})
- REPLACE(&calc," ")
- ENDIF
- IFMATCHES(%&calc%,"^-?[0-9]+(\.[0-9]+)*([*/+-]-?[0-9]+(\.[0-9]+)*)*$")
- //infinite loops are bad
- DO(100)
- //Order of Operations
- IFMATCHES(%&calc%,"(-?[0-9]+(?:\.[0-9]+)*)([*/])(-?[0-9]+(?:\.[0-9]+)*)")
- MATCH(%&calc%,"(-?[0-9]+(?:\.[0-9]+)*)([*/])(-?[0-9]+(?:\.[0-9]+)*)",{&num1,&operator,&num2})
- ELSE
- IFMATCHES(%&calc%,"(-?[0-9]+(?:\.[0-9]+)*)([+-])(-?[0-9]+(?:\.[0-9]+)*)")
- MATCH(%&calc%,"(-?[0-9]+(?:\.[0-9]+)*)([+-])(-?[0-9]+(?:\.[0-9]+)*)",{&num1,&operator,&num2})
- ENDIF
- ENDIF
- IFMATCHES(%&operator%,"\*|/)
- //Are there decimals?
- IFCONTAINS(%&num1%,".")
- SET(#num1hasdec,1)
- MATCH(%&num1%00,"(-?[0-9]+)\.?([0-9]{2})",{#num1whole,&num1dec})
- SET(#num1,%#num1whole%%&num1dec%)
- ELSE
- SET(#num1hasdec,0)
- SET(#num1,%&num1%)
- ENDIF
- IFCONTAINS(%&num2%,".")
- SET(#num2hasdec,1)
- MATCH(%&num2%00,"(-?[0-9]+)\.?([0-9]{2})",{#num2whole,&num2dec})
- SET(#num2,%#num2whole%%&num2dec%)
- ELSE
- SET(#num2hasdec,0)
- SET(#num2,%&num2%)
- ENDIF
- #decimals = #num1hasdec + #num2hasdec
- //Division
- //rip me
- IF(%&operator%=="/")
- #num1a = #num1 * 1000
- #answer = #num1a / #num2
- SET(&answerwhole,%#answer%)
- IF(%#decimals%==1)
- IF(%#num1hasdec%==1)
- MATCH(0000%#answer%,(-?[0-9]+)([0-9]{2})(.{3})$,{#answerwhole,&answerdec})
- ELSE
- MATCH(0000%#answer%,(-?[0-9]+)([0-9])$,{#answerwhole,&answerdec})
- ENDIF
- ELSE
- MATCH(0000%#answer%,(-?[0-9]+)([0-9]{2})(.)$,{#answerwhole,&answerdec})
- ENDIF
- //Multiplication
- ELSEIF(%&operator%=="*")
- #answer = #num1 * #num2
- IF(%#decimals%==2)
- MATCH(%#answer%,(-?[0-9]+)([0-9]{2})(.{2})$,{#answerwhole,&answerdec})
- ELSEIF(%#decimals%==1)
- MATCH(%#answer%,(-?[0-9]+)([0-9]{2})$,{#answerwhole,&answerdec})
- ELSE
- MATCH(%#answer%00,(-?[0-9]+)([0-9]{2})$,{#answerwhole,&answerdec})
- ENDIF
- ENDIF
- ELSE
- //deal with potential decimals
- MATCH(%&num1%00,"(-?[0-9]+)\.?([0-9]{2})",{#num1whole,&num1dec})
- MATCH(%&num2%00,"(-?[0-9]+)\.?([0-9]{2})",{#num2whole,&num2dec})
- SET(#num1,%#num1whole%%&num1dec%)
- SET(#num2,%#num2whole%%&num2dec%)
- //Addition
- IF(%&operator%=="+")
- #answer = #num1 + #num2
- //Subtraction
- ELSEIF(%&operator%=="-")
- #answer = #num1 - #num2
- ENDIF
- MATCH(%#answer%,(-?[0-9]+)([0-9]{2})$,{#answerwhole,&answerdec})
- ENDIF
- SET(#answerdec,%&answerdec%)
- IF(%#answerdec%>0)
- SET(&answer,"%#answerwhole%.%&answerdec%")
- ELSE
- SET(&answer,"%#answerwhole%)
- ENDIF
- //Sub the answer back in
- REPLACE(&calc,"%&num1%%&operator%%&num2%","%&answer%")
- //Repeat if needed
- IFMATCHES(%&calc%,"([*/+-])")
- SET(loop)
- ELSE
- UNSET(loop)
- ENDIF
- WHILE(%loop%)
- //teh answer
- LOG("&6[&4CALC&6]&a %&calc%")
- ELSE
- LOG("&6[&4CALC&6] &aBasic Calculator that doesn't work well")
- LOG("&6[&4CALC&6] &aThe decimals probably aren't that accurate because the calculator is dumb")
- LOG("&6[&4CALC&6] &aRounding is for nubs")
- LOG("&6[&4CALC&6] &av 1.3")
- ENDIF
- FILTER
- ENDIF
- }$$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement