Cross-Multiply function simplifier.


  • I didn't bother doing this for all the sides, and my code includes tons of if statements which is rather bad, but let me show you how it sorta works:

    Examples:

    print(CrossMultiply("5x-4/6 = 20/5")) -- 25x-20 = 120
    
    print(CrossMultiply("20x/60 = 2/3")) -- 60x = 120
    
    print(CrossMultiply("30x-5/60 = 2/3")) -- 90x-15 = 120
    

    Currently there are a lot of things I stopped at. And, you can only have the variable in the top left or bottom right as of now.

    Just a neat thing that I started and probably won't finish. Here's the code:

    local function CrossMultiply(s)
    	local e1, e2 = s:match("(.+)%=(.+)")
    	local e1Top, e2Top = e1:match("(.+)%/"), e2:match("(.+)%/")
    	local e1Bottom, e2Bottom = e1:match("%/(.+)"), e2:match("%/(.+)")
    	local right
    	local left
    	if(tonumber(e1Top)and(tonumber(e2Bottom)))then
    		right = e1Top*e2Bottom
    	elseif(not tonumber(e1Top))and(tonumber(e2Bottom))then
    		if(e1Top:find("%d"))then
    			if(not (e1Top:find("+") or e1Top:find("-")))then
    				local num, letter = e1Top:match("(%d+)(%l+)")
    				right = (e2Bottom*num)..letter
    			else
    				if(e1Top:find("+"))then
    					local beforePlus, afterPlus = e1Top:match("(.+)%+(.+)")
    					if(beforePlus:find("%d") and beforePlus:find("%l"))then
    						local num, letter = beforePlus:match("(%d+)(%l+)")
    						right = (e2Bottom*num)..letter.."+"..afterPlus*e2Bottom
    					elseif(afterPlus:find("%d") and afterPlus:find("%l"))then
    						local num, letter = afterPlus:match("(%d+)(%l+)")
    						right = beforePlus*e2Bottom.."+"..(e2Bottom*num)..letter
    					else
    						if(tonumber(beforePlus) and not tonumber(afterPlus))then
    							right = beforePlus*e2Bottom.."+"..(e2Bottom..afterPlus)
    						elseif(tonumber(afterPlus) and not tonumber(beforePlus))then
    							right = e2Bottom..beforePlus.."+"..(e2Bottom*afterPlus)
    						end
    					end
    				else
    					local beforeMinus, afterMinus = e1Top:match("(.+)%-(.+)")
    					if(beforeMinus:find("%d") and beforeMinus:find("%l"))then
    						local num, letter = beforeMinus:match("(%d+)(%l+)")
    						right = (e2Bottom*num)..letter.."-"..afterMinus*e2Bottom
    					elseif(afterMinus:find("%d") and afterMinus:find("%l"))then
    						local num, letter = afterMinus:match("(%d+)(%l+)")
    						right = beforeMinus*e2Bottom.."-"..(e2Bottom*num)..letter
    					else
    						if(tonumber(beforeMinus) and not tonumber(afterMinus))then
    							right = beforeMinus*e2Bottom.."+"..(e2Bottom..afterMinus)
    						elseif(tonumber(afterMinus) and not tonumber(beforeMinus))then
    							right = e2Bottom..beforeMinus.."+"..(e2Bottom*afterMinus)
    						end
    					end
    				end
    			end
    		else
    			right = e2Bottom..e1Top
    		end
    	elseif(tonumber(e1Top))and(not tonumber(e2Bottom))then
    		if(e2Bottom:find("%d"))then
    			if(not (e2Bottom:find("+") or e2Bottom:find("-")))then
    				local num, letter = e2Bottom:match("(%d+)(%l+)")
    				right = (e1Top*num)..letter
    			else
    				if(e2Bottom:find("+"))then
    					local beforePlus, afterPlus = e2Bottom:match("(.+)%+(.+)")
    					if(beforePlus:find("%d") and beforePlus:find("%l"))then
    						local num, letter = beforePlus:match("(%d+)(%l+)")
    						right = (e1Top*num)..letter.."+"..afterPlus*e1Top
    					elseif(afterPlus:find("%d") and afterPlus:find("%l"))then
    						local num, letter = afterPlus:match("(%d+)(%l+)")
    						right = beforePlus*e1Top.."+"..(e1Top*num)..letter
    					else
    						if(tonumber(beforePlus) and not tonumber(afterPlus))then
    							right = beforePlus*e1Top.."+"..(e1Top..afterPlus)
    						elseif(tonumber(afterPlus) and not tonumber(beforePlus))then
    							right = e1Top..beforePlus.."+"..(e1Top*afterPlus)
    						end
    					end
    				else
    					local beforeMinus, afterMinus = e2Bottom:match("(.+)%-(.+)")
    					if(beforeMinus:find("%d") and beforeMinus:find("%l"))then
    						local num, letter = beforeMinus:match("(%d+)(%l+)")
    						right = (e1Top*num)..letter.."-"..afterMinus*e1Top
    					elseif(afterMinus:find("%d") and afterMinus:find("%l"))then
    						local num, letter = afterMinus:match("(%d+)(%l+)")
    						right = beforeMinus*e1Top.."-"..(e1Top*num)..letter
    					else
    						if(tonumber(beforeMinus) and not tonumber(afterMinus))then
    							right = beforeMinus*e1Top.."+"..(e1Top..afterMinus)
    						elseif(tonumber(afterMinus) and not tonumber(beforeMinus))then
    							right = e1Top..beforeMinus.."+"..(e1Top*afterMinus)
    						end
    					end
    				end
    			end
    		else
    			right = e1Top..e2Bottom
    		end
    	end
    	if(tonumber(e2Top)and(tonumber(e1Bottom)))then
    		left = e2Top*e1Bottom
    	end
    	return right.." = "..left
    end
    

  • What math class did you take to learn all of this?


  • @FiredDusk This is just algebra.

    This code is only sorta neat because it actually follows the steps a normal human would to complete and simplify the equation, buuut a computer can just plug in for x, which is what I did here. Because of floating point errors it's not perfect but it works better than this.


  • @OldPalHappy Anythin' you can do for time? I mean like subtracting two time periods to get how much time has past or to how to calculate it into days?

    4/ 22/ 2017 | 12 AM - 4/ 25/ 2017 | 7 PM

    2:30 - 3:40

    etc



Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.