C
C#2y ago
MaGiiKx

✅ Mathematical Expressions

Hi, for my university assignment, I am required to add complex expressions alongside my variables. The complex expression is just mathematical stuff. an example is - var size 10 (this creates a variable with the value of 10.) size = size * 3 (this should overwrite the value of size to 30.) i need some help to do this. when i remove the line - size = size * 3, it draws the shape. https://hastebin.com/pivuwabelo.csharp
Hastebin: Send and Save Text or Code Snippets for Free | Toptal®
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
31 Replies
Thinker
Thinker2y ago
To be clear, is this like a language you have to implement?
MaGiiKx
MaGiiKxOP2y ago
basically yes. its to draw shapes this is basically like a little environment for kids to draw shapes using commands
Angius
Angius2y ago
Making your own version of Logo, basically?
MaGiiKx
MaGiiKxOP2y ago
i think so
Thinker
Thinker2y ago
What is it you actually need help with? Drawing the circle? Parsing?
MaGiiKx
MaGiiKxOP2y ago
no, so my variable already works when i type in
var size 10
circle size
var size 10
circle size
it will draw the circle with the variable value
Angius
Angius2y ago
So it's assigning new values to variables that doesn't work
MaGiiKx
MaGiiKxOP2y ago
what i want to do now is,
c
when users type in
var size 10
size = size * 3
circle size
c
when users type in
var size 10
size = size * 3
circle size
I want it to overwrite the current value of size (10) and update it to (30) and then draw the circle with the radius of 30
Thinker
Thinker2y ago
So... what kind of system do you have? I assume you're using a dictionary for variables?
MaGiiKx
MaGiiKxOP2y ago
im not using a dictionary ill show you
private void DeclareVariables(string name1, string value1, string name2, string value2)
{
// Check if the variables are valid and store them if they are.
variableCheck(name1, value1, name2, value2);
}










private int GetVariableValue(string name)
{
// Iterate through the variableName array.
int i = 0;
while (i < variableName.Length)
{
// If the current element of the variableName array is equal to the name parameter,
// return the corresponding element of the vParams array.
if (variableName[i] == name)
{
return vParams[i];
}
i++;
}
// If the name parameter is not found in the variableName array, return -1.
return -1;
}
private void DeclareVariables(string name1, string value1, string name2, string value2)
{
// Check if the variables are valid and store them if they are.
variableCheck(name1, value1, name2, value2);
}










private int GetVariableValue(string name)
{
// Iterate through the variableName array.
int i = 0;
while (i < variableName.Length)
{
// If the current element of the variableName array is equal to the name parameter,
// return the corresponding element of the vParams array.
if (variableName[i] == name)
{
return vParams[i];
}
i++;
}
// If the name parameter is not found in the variableName array, return -1.
return -1;
}
Thinker
Thinker2y ago
What is variableName and vParams?
Akseli
Akseli2y ago
use a dictionary instead of array to store variables
MaGiiKx
MaGiiKxOP2y ago
on the command line, it will split whenever it detects a space. parameter[0] will be the variable name (in this case it would be size) vparams is just the values of the variables
Thinker
Thinker2y ago
Assuming you don't have scopes, a simple dictionary would work fine
Angius
Angius2y ago
So you store names and values in separate arrays...? Why? Use a dictionary Or a list of classes or something
MaGiiKx
MaGiiKxOP2y ago
i was following how my lecturer did it in his examples, this looked like the easiest way
Angius
Angius2y ago
The only time when I see people store related values inside of unrelated arrays is when they're clueless about OOP So you either misunderstood or misapplied what the lecturer did, or they have no business teaching
MaGiiKx
MaGiiKxOP2y ago
lmaoo i have no idea so i should change the code to dictionary?
Thinker
Thinker2y ago
If you were to use a dictionary, you could literally just do
variables[variableName] = variableValue;
variables[variableName] = variableValue;
MaGiiKx
MaGiiKxOP2y ago
okay so suppose i change it to a dictionary, how would i do the complex expressions?
Thinker
Thinker2y ago
Are you receiving the expressions as a plain string?
MaGiiKx
MaGiiKxOP2y ago
so when user types it in the command line it will be a string, it is then split and parsed and stored in their correct parameters
Thinker
Thinker2y ago
have you already parsed the expression into something like a syntax tree?
MaGiiKx
MaGiiKxOP2y ago
no, so i need to add the expressions like + - * / into my code all it parses right now is just the numbers theres no mathematical side to it
Thinker
Thinker2y ago
I don't know what your lecturer wants, but if you wanna parse expressions with precedence and whatnot there's a couple ways. The first would be to write a simple parser, the second would be one of the several math expression algorithms out there. What's the lecture/course about?
MaGiiKx
MaGiiKxOP2y ago
the module is advanced software engineering this is just one of the module its just developing a mini ide to draw shapes using commands
Angius
Angius2y ago
If there's "advanced" in the name, chances are it does expect you to parse it into some AST and process that A parser + an interpreter
Thinker
Thinker2y ago
In which case, check out one of these two https://en.wikipedia.org/wiki/Shunting_yard_algorithm https://craftinginterpreters.com/parsing-expressions.html The second link is about implementing an abstract syntax tree (AST) and an expression parser.
MaGiiKx
MaGiiKxOP2y ago
ive heard of shunting yard algorithm hi again, will you guys be able to help me with something else?
MaGiiKx
MaGiiKxOP2y ago
will you be able to help me with this
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server