<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
	<id>http://digida.mgpu.ru/index.php?action=history&amp;feed=atom&amp;title=How_to_Evaluate_an_Expression</id>
	<title>How to Evaluate an Expression - История изменений</title>
	<link rel="self" type="application/atom+xml" href="http://digida.mgpu.ru/index.php?action=history&amp;feed=atom&amp;title=How_to_Evaluate_an_Expression"/>
	<link rel="alternate" type="text/html" href="http://digida.mgpu.ru/index.php?title=How_to_Evaluate_an_Expression&amp;action=history"/>
	<updated>2026-05-19T04:17:27Z</updated>
	<subtitle>История изменений этой страницы в вики</subtitle>
	<generator>MediaWiki 1.44.0</generator>
	<entry>
		<id>http://digida.mgpu.ru/index.php?title=How_to_Evaluate_an_Expression&amp;diff=1796&amp;oldid=prev</id>
		<title>Patarakin: 1 версия импортирована</title>
		<link rel="alternate" type="text/html" href="http://digida.mgpu.ru/index.php?title=How_to_Evaluate_an_Expression&amp;diff=1796&amp;oldid=prev"/>
		<updated>2022-07-21T08:33:16Z</updated>

		<summary type="html">&lt;p&gt;1 версия импортирована&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Предыдущая версия&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Версия от 11:33, 21 июля 2022&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Patarakin</name></author>
	</entry>
	<entry>
		<id>http://digida.mgpu.ru/index.php?title=How_to_Evaluate_an_Expression&amp;diff=1795&amp;oldid=prev</id>
		<title>scratch&gt;Jvvg в 21:52, 6 июня 2022</title>
		<link rel="alternate" type="text/html" href="http://digida.mgpu.ru/index.php?title=How_to_Evaluate_an_Expression&amp;diff=1795&amp;oldid=prev"/>
		<updated>2022-06-06T21:52:07Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;This tutorial teaches &amp;#039;&amp;#039;&amp;#039;how to evaluate an expression&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Making the Custom Block ==&lt;br /&gt;
&lt;br /&gt;
This implementation does not include support for functions or unary negation. This means if you want to calculate &amp;lt;code&amp;gt;-5 * 10&amp;lt;/code&amp;gt; you will have to rephrase it as &amp;lt;code&amp;gt;(0-5) * 10&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Variables needed ===&lt;br /&gt;
&lt;br /&gt;
You will need to create these [[Variables]]:&lt;br /&gt;
* &amp;lt;scratchblocks&amp;gt;(i)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
* &amp;lt;scratchblocks&amp;gt;(last token)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
* &amp;lt;scratchblocks&amp;gt;(token)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
* &amp;lt;scratchblocks&amp;gt;(letter)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
* &amp;lt;scratchblocks&amp;gt;(result)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will also need to create these [[List]]s:&lt;br /&gt;
* &amp;lt;scratchblocks&amp;gt;(queue::list)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
* &amp;lt;scratchblocks&amp;gt;(stack::list)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
* &amp;lt;scratchblocks&amp;gt;(tokens::list)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
{{note|Make sure these operators are in the exact same order in the list.}}&lt;br /&gt;
And a list called operators with these items:&lt;br /&gt;
# -&lt;br /&gt;
# +&lt;br /&gt;
# /&lt;br /&gt;
# *&lt;br /&gt;
&lt;br /&gt;
=== Full block ===&lt;br /&gt;
&lt;br /&gt;
This [[projects:470377439|Project]] has the script for easy backpacking.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
define evaluate (expression)&lt;br /&gt;
set [i v] to (1) // Tokenize the expression&lt;br /&gt;
delete all of [tokens v]&lt;br /&gt;
repeat (length of (expression))&lt;br /&gt;
set [letter v] to (letter (i) of (expression))&lt;br /&gt;
set [last token v] to (item (length of [tokens v]) of [tokens v])&lt;br /&gt;
if &amp;lt;(123456.7890) contains (letter)&amp;gt; then&lt;br /&gt;
if &amp;lt;&amp;lt;((last token) / (1)) = (last token)&amp;gt; or &amp;lt;(last token) = [.]&amp;gt;&amp;gt; then&lt;br /&gt;
replace item (length of [tokens v]) of [tokens v] with (join (last token) (letter))&lt;br /&gt;
else&lt;br /&gt;
add (letter) to [tokens v]&lt;br /&gt;
end&lt;br /&gt;
else&lt;br /&gt;
if &amp;lt;[()+-/*] contains (letter)&amp;gt; then&lt;br /&gt;
add (letter) to [tokens v]&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
delete all of [stack v] // Shunting yard algorithm&lt;br /&gt;
delete all of [queue v]&lt;br /&gt;
set [i v] to (1)&lt;br /&gt;
repeat (length of [tokens v])&lt;br /&gt;
set [token v] to (item (i) of [tokens v])&lt;br /&gt;
if &amp;lt;((token) / (1)) = (token)&amp;gt; then&lt;br /&gt;
add (token) to [queue v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;[operators v] contains (token)&amp;gt; then&lt;br /&gt;
repeat until &amp;lt;&amp;lt;&amp;lt;(item (1) of [stack v]) = [(]&amp;gt; or &amp;lt;(length of [stack v]) = [0]&amp;gt;&amp;gt; or &amp;lt;(item # of (item (1) of [stack v]) in [operators v]) &amp;lt; (item # of (token) in [operators v])&amp;gt;&amp;gt;&lt;br /&gt;
add (item (1) of [stack v]) to [queue v]&lt;br /&gt;
delete (1) of [stack v]&lt;br /&gt;
end&lt;br /&gt;
insert (token) at (1) of [stack v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(token) = [(]&amp;gt; then&lt;br /&gt;
insert (token) at (1) of [stack v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(token) = [)]&amp;gt; then&lt;br /&gt;
repeat until &amp;lt;&amp;lt;(item (1) of [stack v]) = [(]&amp;gt; or &amp;lt;(length of [stack v]) = [0]&amp;gt;&amp;gt;&lt;br /&gt;
add (item (1) of [stack v]) to [queue v]&lt;br /&gt;
delete (1) of [stack v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(item (1) of [stack v]) = [(]&amp;gt; then&lt;br /&gt;
delete (1) of [stack v]&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
change [i v] by (1)&lt;br /&gt;
end&lt;br /&gt;
repeat until &amp;lt;(length of [stack v]) = (0)&amp;gt;&lt;br /&gt;
add (item (1) of [stack v]) to [queue v]&lt;br /&gt;
delete (1) of [stack v]&lt;br /&gt;
end&lt;br /&gt;
repeat until &amp;lt;(length of [queue v]) = [0]&amp;gt; // Stack machine&lt;br /&gt;
set [token v] to (item (1) of [queue v])&lt;br /&gt;
delete [1] of [queue v]&lt;br /&gt;
if &amp;lt;[operators v] contains (token)&amp;gt; then&lt;br /&gt;
if &amp;lt;(token) = [+]&amp;gt; then&lt;br /&gt;
insert ((item [2] of [stack v]) + (item [1] of [stack v])) at [1] of [stack v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(token) = [-]&amp;gt; then&lt;br /&gt;
insert ((item [2] of [stack v]) - (item [1] of [stack v])) at [1] of [stack v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(token) = [/]&amp;gt; then&lt;br /&gt;
insert ((item [2] of [stack v]) / (item [1] of [stack v])) at [1] of [stack v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(token) = [*]&amp;gt; then&lt;br /&gt;
insert ((item [2] of [stack v]) * (item [1] of [stack v])) at [1] of [stack v]&lt;br /&gt;
end&lt;br /&gt;
delete [2] of [stack v]&lt;br /&gt;
delete [2] of [stack v]&lt;br /&gt;
else&lt;br /&gt;
insert (token) at (1) of [stack v]&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
set [result v] to (item [1] of [stack v]) // The answer will be stored in the result variable&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Explanation ==&lt;br /&gt;
&lt;br /&gt;
The problem of evaluating an expression can be broken down into 3 steps:&lt;br /&gt;
# Tokenizing the expression (turning the input into a list of symbols and numbers)&lt;br /&gt;
# Converting the tokens to postfix format (where the operator goes after the 2 operands)&lt;br /&gt;
# Evaluating the postfix expression with a stack machine (calculating the answer)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Tokenizing the expression ====&lt;br /&gt;
The expression must first be converted into a list. Every number must have all its digits in the same list element.&lt;br /&gt;
&lt;br /&gt;
This script will tokenize an expression:&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
set [i v] to (1)&lt;br /&gt;
delete all of [tokens v]&lt;br /&gt;
repeat (length of (expression :: custom))&lt;br /&gt;
set [letter v] to (letter (i) of (expression :: custom))&lt;br /&gt;
set [last token v] to (item (length of [tokens v]) of [tokens v])&lt;br /&gt;
if &amp;lt;(123456.7890) contains (letter)&amp;gt; then&lt;br /&gt;
if &amp;lt;&amp;lt;((last token) / (1)) = (last token)&amp;gt; or &amp;lt;(last token) = [.]&amp;gt;&amp;gt; then&lt;br /&gt;
replace item (length of [tokens v]) of [tokens v] with (join (last token) (letter))&lt;br /&gt;
else&lt;br /&gt;
add (letter) to [tokens v]&lt;br /&gt;
end&lt;br /&gt;
else&lt;br /&gt;
if &amp;lt;[()+-/*] contains (letter)&amp;gt; then&lt;br /&gt;
add (letter) to [tokens v]&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Converting the tokens to postfix format ====&lt;br /&gt;
Next the [[wikipedia:Shunting yard algorithm|Shunting yard algorithm]] is used to convert the tokens from infix (A op B) to postfix (A B op). This is done because it&amp;#039;s a lot less code to calculate a postfix expression than an infix one.&lt;br /&gt;
&lt;br /&gt;
This script will run the shunting yard algorithm on the tokens list:&lt;br /&gt;
&lt;br /&gt;
{{note||Make sure the operators list is in the right order! (-, +, /, *)}}&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
delete all of [stack v]&lt;br /&gt;
delete all of [queue v]&lt;br /&gt;
set [i v] to (1)&lt;br /&gt;
repeat (length of [tokens v])&lt;br /&gt;
set [token v] to (item (i) of [tokens v])&lt;br /&gt;
if &amp;lt;((token) / (1)) = (token)&amp;gt; then&lt;br /&gt;
add (token) to [queue v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;[operators v] contains (token)&amp;gt; then&lt;br /&gt;
repeat until &amp;lt;&amp;lt;&amp;lt;(item (1) of [stack v]) = [(]&amp;gt; or &amp;lt;(length of [stack v]) = [0]&amp;gt;&amp;gt; or &amp;lt;(item # of (item (1) of [stack v]) in [operators v]) &amp;lt; (item # of (token) in [operators v])&amp;gt;&amp;gt;&lt;br /&gt;
add (item (1) of [stack v]) to [queue v]&lt;br /&gt;
delete (1) of [stack v]&lt;br /&gt;
end&lt;br /&gt;
insert (token) at (1) of [stack v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(token) = [(]&amp;gt; then&lt;br /&gt;
insert (token) at (1) of [stack v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(token) = [)]&amp;gt; then&lt;br /&gt;
repeat until &amp;lt;&amp;lt;(item (1) of [stack v]) = [(]&amp;gt; or &amp;lt;(length of [stack v]) = [0]&amp;gt;&amp;gt;&lt;br /&gt;
add (item (1) of [stack v]) to [queue v]&lt;br /&gt;
delete (1) of [stack v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(item (1) of [stack v]) = [(]&amp;gt; then&lt;br /&gt;
delete (1) of [stack v]&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
change [i v] by (1)&lt;br /&gt;
end&lt;br /&gt;
repeat until &amp;lt;(length of [stack v]) = (0)&amp;gt;&lt;br /&gt;
add (item (1) of [stack v]) to [queue v]&lt;br /&gt;
delete (1) of [stack v]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Calculating the result with a stack machine ====&lt;br /&gt;
&lt;br /&gt;
This script will calculate the result of the postfix expression:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
repeat until &amp;lt;(length of [queue v]) = [0]&amp;gt;&lt;br /&gt;
set [token v] to (item (1) of [queue v])&lt;br /&gt;
delete [1] of [queue v]&lt;br /&gt;
if &amp;lt;[operators v] contains (token)&amp;gt; then&lt;br /&gt;
if &amp;lt;(token) = [+]&amp;gt; then&lt;br /&gt;
insert ((item [2] of [stack v]) + (item [1] of [stack v])) at [1] of [stack v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(token) = [-]&amp;gt; then&lt;br /&gt;
insert ((item [2] of [stack v]) - (item [1] of [stack v])) at [1] of [stack v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(token) = [/]&amp;gt; then&lt;br /&gt;
insert ((item [2] of [stack v]) / (item [1] of [stack v])) at [1] of [stack v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(token) = [*]&amp;gt; then&lt;br /&gt;
insert ((item [2] of [stack v]) * (item [1] of [stack v])) at [1] of [stack v]&lt;br /&gt;
end&lt;br /&gt;
delete [2] of [stack v]&lt;br /&gt;
delete [2] of [stack v]&lt;br /&gt;
else&lt;br /&gt;
insert (token) at (1) of [stack v]&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
set [result v] to (item [1] of [stack v])&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using the Custom Block ==&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
when green flag clicked&lt;br /&gt;
evaluate [5 + 5] :: custom&lt;br /&gt;
say (return)&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If it worked, your sprite should say 10.&lt;br /&gt;
&lt;br /&gt;
[[Category:Math Tutorials]] [[Category:Scripting Tutorials]]&lt;/div&gt;</summary>
		<author><name>scratch&gt;Jvvg</name></author>
	</entry>
</feed>