AlgoAnim auflösen der rekursion
This commit is contained in:
parent
aa86db5d19
commit
3fb4b983cd
@ -66,9 +66,22 @@ public class RussischeBauernmultiplikation {
|
||||
|
||||
|
||||
/**
|
||||
* The table
|
||||
* The tables
|
||||
*/
|
||||
StringMatrix tb1 = null;
|
||||
StringMatrix tb2 = null;
|
||||
|
||||
|
||||
/**
|
||||
* int-array with the values of b (only for animation)
|
||||
*/
|
||||
public int[] bArray = null;
|
||||
|
||||
|
||||
/**
|
||||
* recursionArray
|
||||
*/
|
||||
public int[] recursionArray = null;
|
||||
|
||||
|
||||
private static final String DESCRIPTION =
|
||||
@ -195,6 +208,9 @@ public class RussischeBauernmultiplikation {
|
||||
aTemp = aTemp / 2;
|
||||
lines++;
|
||||
}
|
||||
//initialize the bArray und the recursionArray
|
||||
this.bArray = new int[lines-1];
|
||||
this.recursionArray = new int[lines-1];
|
||||
return lines;
|
||||
}
|
||||
|
||||
@ -258,6 +274,74 @@ public class RussischeBauernmultiplikation {
|
||||
this.statement.addCodeLine("= 2214", null, 4, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Draw the table tb2
|
||||
* @param bArrayLength
|
||||
*/
|
||||
public void drawTb2(int bArrayLength){
|
||||
|
||||
//create String[][] data array for tb2
|
||||
String[][] tb2Data = new String[2][1];
|
||||
tb2Data[0][0] = "Rekursion aufloesen:";
|
||||
|
||||
String tmp = "";
|
||||
for(int i = 1; i < bArrayLength; i++){
|
||||
if(this.bArray[i] != 0){
|
||||
tmp = tmp+this.bArray[i];
|
||||
if(i+1 != bArrayLength){
|
||||
tmp = tmp+"+";
|
||||
}
|
||||
}
|
||||
}
|
||||
tb2Data[1][0] = tmp;
|
||||
|
||||
|
||||
//create table tb2
|
||||
if(this.tb2 != null){
|
||||
this.tb2.hide(); //hide old version of tb2
|
||||
}
|
||||
this.tb2 = lang.newStringMatrix(new Offset(0, 10, this.tb1, AnimalScript.DIRECTION_SW), tb2Data, "tb2", null);
|
||||
this.tb2.changeColor(AnimationPropertiesKeys.FILL_PROPERTY, Color.WHITE, null, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates the animation of the recursion flow
|
||||
*/
|
||||
public void generateRecursion(){
|
||||
|
||||
//draw tb2
|
||||
drawTb2(this.bArray.length);
|
||||
|
||||
//STEP
|
||||
lang.nextStep();
|
||||
|
||||
int newLineNumber = 0;
|
||||
int oldLineNumber = 0;
|
||||
sc.unhighlight(2); //"return b"
|
||||
for(int i = this.bArray.length-1; i > 1; i--){
|
||||
oldLineNumber = newLineNumber;
|
||||
if(this.recursionArray[i-1] == 0){
|
||||
newLineNumber = 5;
|
||||
}else{
|
||||
newLineNumber = 7;
|
||||
}
|
||||
sc.unhighlight(oldLineNumber);
|
||||
//STEP
|
||||
lang.nextStep();
|
||||
|
||||
sc.highlight(newLineNumber);
|
||||
//update tb2
|
||||
this.bArray[i-1] = this.bArray[i-1] + this.bArray[i];
|
||||
drawTb2(i);
|
||||
//STEP
|
||||
lang.nextStep();
|
||||
}
|
||||
sc.unhighlight(oldLineNumber);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* logical method: calls the needed methods for generating the animation
|
||||
* @param a
|
||||
@ -286,13 +370,17 @@ public class RussischeBauernmultiplikation {
|
||||
//higlight line 0
|
||||
this.sc.highlight(0);
|
||||
|
||||
int result = russe(a, b, sc, tb1, 1, "");
|
||||
this.tb1.put(lines - 1, 2, "Ergebnis", null, null);
|
||||
this.tb1.put(lines - 1, 3, result+"", null, null);
|
||||
russe(a, b, sc, tb1, 1, "");
|
||||
// this.tb1.put(lines - 1, 2, "Ergebnis", null, null);
|
||||
//this.tb1.put(lines - 1, 3, result+"", null, null);
|
||||
|
||||
//generate recursion animation
|
||||
generateRecursion();
|
||||
|
||||
//STEP
|
||||
lang.nextStep();
|
||||
this.tb1.hide();
|
||||
this.tb2.hide();
|
||||
this.sc.hide();
|
||||
|
||||
//generate statement
|
||||
@ -310,7 +398,9 @@ public class RussischeBauernmultiplikation {
|
||||
* @param sum
|
||||
* @return the product of a and b (a*b)
|
||||
*/
|
||||
private int russe(int a, int b, SourceCode code, StringMatrix tb1, int line, String sum){
|
||||
public int russe(int a, int b, SourceCode code, StringMatrix tb1, int line, String sum){
|
||||
|
||||
|
||||
|
||||
int aTb = a;
|
||||
int bTb = b;
|
||||
@ -342,6 +432,7 @@ public class RussischeBauernmultiplikation {
|
||||
//update table
|
||||
tb1.put(line, 2, "Ja", null, null); //update column 2 (addieren)
|
||||
tb1.put(line, 3, sum, null, null); //update column 3 (Summe)
|
||||
this.bArray[line] = b;
|
||||
return b;
|
||||
}
|
||||
if(line != 1){ // first line
|
||||
@ -374,6 +465,9 @@ public class RussischeBauernmultiplikation {
|
||||
lang.nextStep();
|
||||
code.unhighlight(5);
|
||||
code.highlight(0);
|
||||
//safe actual value of b in bArray
|
||||
this.bArray[line] = b;
|
||||
this.recursionArray[line] = 0;
|
||||
return b + russe(a/2, b*2, code, tb1, line+1, sum);
|
||||
}
|
||||
else{
|
||||
@ -386,6 +480,9 @@ public class RussischeBauernmultiplikation {
|
||||
lang.nextStep();
|
||||
code.highlight(0);
|
||||
code.unhighlight(7);
|
||||
//safe 0 in bArray
|
||||
this.bArray[line] = 0;
|
||||
this.recursionArray[line] = 1;
|
||||
return russe(a/2, b*2, code, tb1, line+1, sum);
|
||||
}
|
||||
}
|
||||
|
||||
@ -161,8 +161,58 @@ author "Michael Scholz, Ulf Gebhardt"
|
||||
highlightCode on "sourceCode" line 2 row 0
|
||||
setGridValue "tb1[5][2]" "Ja" refresh
|
||||
setGridValue "tb1[5][3]" "82+164+656+1312" refresh
|
||||
setGridValue "tb1[6][2]" "Ergebnis" refresh
|
||||
setGridValue "tb1[6][3]" "2214" refresh
|
||||
grid "tb2" offset (0, 10) from "tb1" SW lines 2 columns 1 color (0, 0, 0) elementColor (0, 0, 0) fillColor (0, 0, 0) highlightTextColor (0, 0, 0) highlightBackColor (0, 0, 0) depth 1
|
||||
setGridValue "tb2[0][0]" "Rekursion aufloesen:"
|
||||
setGridValue "tb2[1][0]" "82+164+656+1312" refresh
|
||||
color "tb2" type "fillColor" (255, 255, 255)
|
||||
}
|
||||
{
|
||||
unhighlightCode on "sourceCode" line 2 row 0
|
||||
unhighlightCode on "sourceCode" line 0 row 0
|
||||
}
|
||||
{
|
||||
highlightCode on "sourceCode" line 5 row 0
|
||||
grid "StringMatrix1" offset (0, 10) from "tb1" SW lines 2 columns 1 color (0, 0, 0) elementColor (0, 0, 0) fillColor (0, 0, 0) highlightTextColor (0, 0, 0) highlightBackColor (0, 0, 0) depth 1
|
||||
setGridValue "StringMatrix1[0][0]" "Rekursion aufloesen:"
|
||||
setGridValue "StringMatrix1[1][0]" "82+164+1968" refresh
|
||||
color "StringMatrix1" type "fillColor" (255, 255, 255)
|
||||
hide "tb2"
|
||||
}
|
||||
{
|
||||
unhighlightCode on "sourceCode" line 5 row 0
|
||||
}
|
||||
{
|
||||
highlightCode on "sourceCode" line 7 row 0
|
||||
grid "StringMatrix2" offset (0, 10) from "tb1" SW lines 2 columns 1 color (0, 0, 0) elementColor (0, 0, 0) fillColor (0, 0, 0) highlightTextColor (0, 0, 0) highlightBackColor (0, 0, 0) depth 1
|
||||
setGridValue "StringMatrix2[0][0]" "Rekursion aufloesen:"
|
||||
setGridValue "StringMatrix2[1][0]" "82+164+1968" refresh
|
||||
color "StringMatrix2" type "fillColor" (255, 255, 255)
|
||||
hide "StringMatrix1"
|
||||
}
|
||||
{
|
||||
unhighlightCode on "sourceCode" line 7 row 0
|
||||
}
|
||||
{
|
||||
highlightCode on "sourceCode" line 5 row 0
|
||||
grid "StringMatrix3" offset (0, 10) from "tb1" SW lines 2 columns 1 color (0, 0, 0) elementColor (0, 0, 0) fillColor (0, 0, 0) highlightTextColor (0, 0, 0) highlightBackColor (0, 0, 0) depth 1
|
||||
setGridValue "StringMatrix3[0][0]" "Rekursion aufloesen:"
|
||||
setGridValue "StringMatrix3[1][0]" "82+2132" refresh
|
||||
color "StringMatrix3" type "fillColor" (255, 255, 255)
|
||||
hide "StringMatrix2"
|
||||
}
|
||||
{
|
||||
unhighlightCode on "sourceCode" line 5 row 0
|
||||
}
|
||||
{
|
||||
highlightCode on "sourceCode" line 5 row 0
|
||||
grid "StringMatrix4" offset (0, 10) from "tb1" SW lines 2 columns 1 color (0, 0, 0) elementColor (0, 0, 0) fillColor (0, 0, 0) highlightTextColor (0, 0, 0) highlightBackColor (0, 0, 0) depth 1
|
||||
setGridValue "StringMatrix4[0][0]" "Rekursion aufloesen:"
|
||||
setGridValue "StringMatrix4[1][0]" "2214" refresh
|
||||
color "StringMatrix4" type "fillColor" (255, 255, 255)
|
||||
hide "StringMatrix3"
|
||||
}
|
||||
{
|
||||
unhighlightCode on "sourceCode" line 5 row 0
|
||||
}
|
||||
{
|
||||
codegroup "statement" at (10, 75) color (0, 0, 0) highlightColor (0, 0, 0) contextColor (0, 0, 0) font SansSerif size 16 italic depth 1
|
||||
@ -170,7 +220,7 @@ author "Michael Scholz, Ulf Gebhardt"
|
||||
addCodeLine "Die Idee des Verfahrens kann man mit Hilfe des Dualsystems verdeutlichen." to "statement"
|
||||
addCodeLine "Hierbei wird eine Zahl in ihre Zweierpotenzen zerlegt." to "statement"
|
||||
addCodeLine "" to "statement"
|
||||
hide "tb1" "sourceCode"
|
||||
hide "tb1" "StringMatrix4" "sourceCode"
|
||||
}
|
||||
{
|
||||
addCodeLine "82 * 27 = 82 * (2^0 + 2^1 + 0 * 2^2 + 2^3 + 2^4 )" to "statement"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user