applejack wrote:
Bump?
I don't know whom we're waiting for (scimonster's bug fixes?)
.
PS: It doesn't matter if we're off the front page for a couple of days...
Offline
applejack wrote:
wrote:
wrote:
wrote:
wrote:
wrote:
wrote:
wrote:
wrote:
wrote:
wrote:
wrote:
wrote:
?
How's the debugging going, scimonster?
Offline
applejack wrote:
It's kind of like a fractal.
And kind of like spam.
Offline
applejack wrote:
Sci?
What?
Offline
Well, I could help with a screenshot of the code.
Offline
Guys, I always have to log in when I go into the forums. Can you help? The website logs me out when I go here, but if I log in here and go to the other part of the scratch website, it doesn't log me out. also, it doesn't log me back in when I go back if I don't log in here.
Offline
Okay, we need to get back on track.
I'm going to try to write out exactly what we want our program to do.
So far, I've got what we've decided to do for tokenizing the input and matching parentheses, but not evaluation. Let's write out all the code in pseudocode first and then just port it to Scratch.
Note that this code is not yet tested. I'm just putting it out there.
The following code is written in something like C++/C# syntax.
Input parsing:
// Prompt the user for an expression
Ask("Please input expression");
string Expression = Ask.Response;
// Parallel lists of values and tokens
list Values;
list Tokens;
// j represents position in lists of values/tokens.
// Note that Values[0] is equivalent to "item (1) of (Values)"
int j = 0;
for(int i = 0; i < Expression.Length; ++i)
{
if(Expression[i].isDigit)
{
Tokens[j] = "num";
// Important: +=, combine digits into one spot in list
Values[j] += Expression[i];
// Only increment j if finished reading a number Token
if(!(Expression[i + 1].isDigit))
++j;
}
else if(Expression[i].isOperator)
{
// No consecutive operators
if(Tokens[j] != "")
{
Error("Consecutive operators");
Stop;
}
Tokens[j] = "oper";
Values[j] = Expression[i];
++j;
}
else if(Expression[i].isParenthesis)
{
Tokens[j] = "par";
Values[j] = Expression[i];
}
}Parentheses:
// Consecutive elements represent indexes of paired parentheses in list Values
// Example: parPairs = {3,10,5,7}. Values[3] and Values[10] are paired
list parPairs;
// Match sets of parentheses
for(int i = 0; i < Tokens.Length; ++i)
{
if(Values[i] == "(")
{
// Will equal 0 when two parentheses match
int parCount = 1;
int j = i;
while(parCount != 0)
{
++j
if(Values[j] == ")")
--parCount;
if(Values[j] == "(")
++parCount;
if(parCount == 0)
{
parPairs.Add(i);
parPairs.Add(j);
}
}
}
}Offline
Would you like to develop a Xenon app for my collab OS, Quantum?
Offline
applejack wrote:
Please explain the "Blah[1].Bleh," or whatever. I THIRST FOR KNOWLEDGE!
In C-style languages, you can create an array of a specified data type. When I was using "list" in the code above, I was referring to the equivalent of a Scratch list (a dynamic array of a string/number type). You access a member of an array in the format arrayName[index], where arrayName is the name of the array and index is an integer.
IMPORTANT: The code I posted above is in C# syntax, where Array[0] refers to the first element of the array. That means that the last element of Array is Array[Array.Length - 1].
As for "Blah[1].Bleh," that's shorthand for: "Bleh of the 2nd element of Blah."
In other words, Expression[i] means "item (i + 1) of Expression." Expression is a string, so it's more like "letter (i + 1) of Expression."
I used Expression[i].isDigit as a way of saying "Is the letter (i + 1) of Expression a digit?"
Last edited by amcerbu (2011-10-23 22:34:29)
Offline
So like this?
// Prompt the user for an expression
Ask("Please input expression");
set answer to expression
// Parallel lists of values and tokens
list Values; (huh?)
list Tokens;
// j represents position in lists of values/tokens.
for(int i = 0; i < length of expression; ++i) (I don't understand the ++i)
{
if letter i+1 of expression is a digit
{
set item j+1 of Tokens to "num";
// Important: +=, combine digits into one spot in list (I don't understand the +=)
Values[j] += Expression[i];
// Only increment j if finished reading a number Token
if(!(Expression[i + 1].isDigit))
++j; (I don't understand this either)
}
else if letter i+1 of Expression is an operator
{
// No consecutive operators
if(Tokens[j] != "")(what is the "!"?)
{
Error("Consecutive operators");
Stop;
}
set item j+1 of Tokens to "oper";
set item j+1 of Values to letter i+1 of Expression; (huh?)
++j; (what does this mean?)
}
else if item i+1 of Expression is a parenthesis
{
set item j+1 ofTokens to "par";
set item j+1 of Values to letter i+1 of Expression; (huh?)
}
}Offline
applejack wrote:
So like this?
Code:
// Prompt the user for an expression Ask("Please input expression"); set answer to expression // Parallel lists of values and tokens list Values; (huh?) list Tokens; // j represents position in lists of values/tokens. for(int i = 0; i < length of expression; ++i) (I don't understand the ++i) { if letter i+1 of expression is a digit { set item j+1 of Tokens to "num"; // Important: +=, combine digits into one spot in list (I don't understand the +=) Values[j] += Expression[i]; // Only increment j if finished reading a number Token if(!(Expression[i + 1].isDigit)) ++j; (I don't understand this either) } else if letter i+1 of Expression is an operator { // No consecutive operators if(Tokens[j] != "")(what is the "!"?) { Error("Consecutive operators"); Stop; } set item j+1 of Tokens to "oper"; set item j+1 of Values to letter i+1 of Expression; (huh?) ++j; (what does this mean?) } else if item i+1 of Expression is a parenthesis { set item j+1 ofTokens to "par"; set item j+1 of Values to letter i+1 of Expression; (huh?) } }
First of all, "!" is the "not" of C syntax. !(condition) means not(condition). != is not(equals).
+= is shorthand for "add by value" a += b literally means "set a to (a + b)." We need this operation because if the program reads "3" from Expression and then reads "4" from Expression, we want it to know that it should combine them into one space (34). Basically, the program continues reading numbers into the same slot in the Values list until it sees that the next token is not going to be a number.
Likewise, ++ is shorthand for "increment." a++ or ++a is the same as a += 1 is the same as "set a to (a + 1)." That's why it's called "C++"
As for the "list Values;" you have to declare a variable before you can use it in C languages. In Scratch, the user defines the variables outside of the code, and they are all created at the beginning of the program. Those lines were meant to indicate that we would need those two lists.
As a side note, you should know that in C languages, variables only exist as long as they are "in scope." That means that in the following code:
if(condition)
{
int i = 0;
}
if(other condition)
{
print(i);
}
The variable i would not exist by the time you exited the first "if" clause. That's why in the for(int i = 0; i < Length; ++i;) statement, I declare i every time, because it disappears after each "for" loop ends.
Offline
I made the code! I was just about to tell everyone but my computer froze
So I'm borrowing my sister's now!
HAHA! Teh mighty applejack is not just a graphic designer! I had to make a few minor changes to the code above, but the program works!
Offline
amcerbu wrote:
Where is the rest of the team?
I'm still here...reading along...but not having any time between school and whatnot to post anything...
Offline