-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
252 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
|
||
<link rel="apple-touch-icon" sizes="180x180" href="../icon/apple-touch-icon.png"> | ||
<link rel="icon" type="image/png" sizes="32x32" href="../icon/favicon-32x32.png"> | ||
<link rel="icon" type="image/png" sizes="16x16" href="../icon/favicon-16x16.png"> | ||
<link rel="manifest" href="../icon/manifest.json"> | ||
<link rel="mask-icon" href="../icon/safari-pinned-tab.svg" color="#5bbad5"> | ||
<link rel="shortcut icon" href="../icon/favicon.ico"> | ||
<meta name="msapplication-config" content="../icon/browserconfig.xml"> | ||
<meta charset="utf-8"> | ||
<!-- twitter --> | ||
<meta name="twitter:card" content="summary"> | ||
<meta name="twitter:site" content="@TonyStr_"> | ||
<meta name="twitter:title" content="A visual REVOLUTION"> | ||
<meta name="twitter:description" content="TonyStr - Programming from NUL to DEL"> | ||
<meta name="twitter:image" content="https://i.imgur.com/jmIL8vj.gif"> | ||
<meta name="twitter:url" content="/"> | ||
<meta name="theme-color" content="#F14165"> | ||
<meta property="og:title" content="A visual REVOLUTION"> | ||
<meta property="og:url" content="/"> | ||
<meta property="og:image" content="https://i.imgur.com/jmIL8vj.gif"> | ||
<meta property="og:description" content="TonyStr : Programming from NUL to DEL"> | ||
|
||
<meta name="viewport" content="width=device-width"> | ||
<meta name="description" content="Programming from NUL to DEL"> | ||
<meta name="keywords" content="gamedesign, programming, gml, gml programming"> | ||
<meta name="author" content="Tony Strømsnæs"> | ||
<title id="blog-titlebar">TonyStr - </title> | ||
<link rel="stylesheet" href="../css/style.css"> | ||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> | ||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> | ||
<script src="../scripts/blog.js"></script> | ||
<script src="../scripts/asyncload.js"></script> | ||
<script src="../scripts/stringMath.js"></script> | ||
<script> jQuery(document).ready(update_links($)); </script> | ||
</head> | ||
|
||
<body> | ||
<div id="wrapper-blog"> | ||
<header> | ||
<div class="ribbon"> | ||
- - - - <i class="fa fa-scissors" aria-hidden="true"></i> - - - - - - - | ||
</div> | ||
<div class="container"> | ||
<div id="branding"> | ||
<h1 class="nonlight"><span class="highlight">TonyStr</span>ømsnæs</h1> | ||
</div> | ||
<div id="icons"> | ||
<a href="https://twitter.com/TonyStroemsnaes?ref_src=twsrc%5Etfw" target="_blank" class="fa fa-twitter fa-intbut" style="font-size:26px"></a> | ||
<a href="https://www.reddit.com/user/TonyStr/" target="_blank" class="fa fa-reddit fa-intbut" style="font-size:26px"></a> | ||
<a href="https://github.com/tonystr" target="_blank" class="fa fa-github fa-intbut" style="font-size:26px"></a> | ||
<a href="mailto:[email protected]" target="_top" class="fa fa-envelope-o fa-intbut" style="font-size:26px"></a> | ||
</div> | ||
<nav> | ||
<ul> | ||
<li><a href="../">Home</a></li> | ||
<li> | ||
<div class="dropdown"> | ||
<a href="../#skills">Skills</a> <i class="fa fa-angle-down fa-static" aria-hidden="true"></i> | ||
<div class="dropdown-content blue"> | ||
<p><a href="../skills/gml" target="_top" class="link" style="font-size:13px"> | ||
GML Programming | ||
</a></p> | ||
<p><a href="../skills/pixelart" target="_top" class="link" style="font-size:13px"> | ||
Pixelart Design | ||
</a></p> | ||
<p><a href="../skills/js" target="_top" class="link" style="font-size:13px"> | ||
JS Programming | ||
</a></p> | ||
</div> | ||
</div> | ||
</li> | ||
<li> | ||
<div class="dropdown"> | ||
<a href="../#games_and_tools">Games & Tools</a> <i class="fa fa-angle-down fa-static" aria-hidden="true"></i> | ||
<div class="dropdown-content blue"> | ||
<p><a href="../games_and_tools/gml_ik_ext" target="_top" class="link" style="font-size:13px"> | ||
IK Extension (GM) | ||
</a></p> | ||
<p><a href="../games_and_tools/udda" target="_top" class="link" style="font-size:13px"> | ||
UpDownDuckAround | ||
</a></p> | ||
<p><a href="../games_and_tools/gml_ik_tut" target="_top" class="link" style="font-size:13px"> | ||
IK Tutorial (GM) | ||
</a></p> | ||
</div> | ||
</div> | ||
</li> | ||
</ul> | ||
</nav> | ||
</div> | ||
</header> | ||
|
||
<section id="main"> | ||
<div id="full-col"> | ||
<article id="blog-content"> | ||
<div id="blog-title"> | ||
<h1 class="page-title">Simple Math Parser</h1> <!--Welcome / hello & ATIRAG--> | ||
<h3 id="blogpost-date">7. Mar 2018</h3> <!--Feb 19: 2018--> | ||
<br> | ||
</div> | ||
<p> | ||
Hello! I can divide <span class="il-code">/</span>, multiply <span class="il-code">*</span>, add <span class="il-code">+</span>, subtract <span class="il-code">-</span>, power <span class="il-code">^</span> and enclose | ||
segments within paranthesises <span class="il-code">()</span>. | ||
</p> | ||
<div id="math-parser" name="calcform"> | ||
<textarea name="input" id="input" placeholder="Mathematical Expression ..." tabindex="2" onkeyup="stringMath()"></textarea> | ||
<span id="equals-pynt" onclick="stringMath()">=</span> | ||
<textarea name="output" id="output" placeholder="Parsed Result ..." tabindex="2" readonly="readonly"></textarea> | ||
</div> | ||
</article> | ||
</div> | ||
</section> | ||
</div> | ||
<div id="blog-links"> | ||
<a id="link-prev" class="link invisible"><h3 class="link-nexprev">previous post</h3></a> | ||
<a id="link-next" class="link invisible"><h3 class="link-nexprev">next post</h3></a> | ||
</div> | ||
<footer> | ||
<p>Tony Strømsnæs, Copyright © 2017</p> <!-- , Copyright © 2017 --> | ||
<p id="right">v1.0.3</p> | ||
</footer> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
|
||
/** | ||
* Parses a string for mathematical equations, and returns the answer | ||
* @param {String} str Input string containing mathematical expression | ||
*/ | ||
stringMath = function() { | ||
let string = document.getElementById('input').value; | ||
|
||
let variables = []; | ||
let callStack = []; | ||
let number = ""; | ||
let action = ""; | ||
let stage = 0; | ||
let stagemax = 1; | ||
let sign = 1; | ||
|
||
for (let i = 0; i < string.length; i++) { | ||
switch (string.charAt(i)) { | ||
|
||
// Operator | ||
case (string.charAt(i).match(`[+\\-*/^]`) || false).input: | ||
let numset = false; | ||
if (number.length > 0) { | ||
variables.push(Number(number) * sign); | ||
sign = 1; | ||
number = ""; | ||
numset = true; | ||
} | ||
|
||
let tertiary = string.charAt(i).match(`\\^`) && true; | ||
let secondary = string.charAt(i).match(`[*/]`) && true; | ||
let stg = stage*3 + Number(secondary) + Number(tertiary) * 2; | ||
let operator = string.charAt(i); | ||
|
||
if (operator == "-") { | ||
sign = -1; | ||
operator = "+"; | ||
if (!numset) operator = ""; | ||
} | ||
|
||
if (operator) { | ||
callStack.push(stg + operator); | ||
stagemax = stg > stagemax ? stg : stagemax; | ||
console.log("operator"); | ||
} | ||
break; | ||
|
||
// Digit | ||
case (string.charAt(i).match(`[0-9.]`) || false).input: | ||
number += string.charAt(i); | ||
console.log("digit"); | ||
break; | ||
|
||
// Paranthesises | ||
case "(": stage++; break; | ||
case ")": stage--; | ||
if (stage < 0) { | ||
return "Error: Non-matched closing bracket(s)"; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
if (number) variables.push(Number(number) * sign); | ||
|
||
if(variables.length > 1) { | ||
for (let stage = stagemax; stage >= 0; stage--) { | ||
if (callStack.length < 1) break; | ||
for (let i = 0; i < callStack.length; i++) { | ||
switch (callStack[i]) { | ||
|
||
case stage + "*": variables[i] = (variables[i] * Number(variables[i+1])); callStack.splice(i, 1); variables.splice(i--+1, 1); break; | ||
case stage + "/": variables[i] = (variables[i] / Number(variables[i+1])); callStack.splice(i, 1); variables.splice(i--+1, 1); break; | ||
case stage + "+": variables[i] = (variables[i] + Number(variables[i+1])); callStack.splice(i, 1); variables.splice(i--+1, 1); break; | ||
case stage + "-": variables[i] = (variables[i] - Number(variables[i+1])); callStack.splice(i, 1); variables.splice(i--+1, 1); break; | ||
case stage + "^": variables[i] = Math.pow(variables[i], Number(variables[i+1])); callStack.splice(i, 1); variables.splice(i--+1, 1); break; | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
if (isNaN(variables[0]) || variables[0] === undefined || variables[0] === null ) { | ||
if (variables.length < 1) document.getElementById('output').value = ""; | ||
|
||
} else { | ||
document.getElementById('output').value = variables[0]; | ||
} | ||
} |
Oops, something went wrong.