✅ 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
To be clear, is this like a language you have to implement?
basically yes. its to draw shapes
this is basically like a little environment for kids to draw shapes using commands
Making your own version of Logo, basically?
i think so
What is it you actually need help with? Drawing the circle? Parsing?
no, so my variable already works
when i type in
it will draw the circle with the variable value
So it's assigning new values to variables that doesn't work
what i want to do now is,
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
So... what kind of system do you have? I assume you're using a dictionary for variables?
im not using a dictionary
ill show you
What is
variableName
and vParams
?use a dictionary instead of array to store variables
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
Assuming you don't have scopes, a simple dictionary would work fine
So you store names and values in separate arrays...?
Why?
Use a dictionary
Or a list of classes or something
i was following how my lecturer did it in his examples, this looked like the easiest way
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
lmaoo i have no idea
so i should change the code to dictionary?
If you were to use a dictionary, you could literally just do
okay so suppose i change it to a dictionary, how would i do the complex expressions?
Are you receiving the expressions as a plain string?
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
have you already parsed the expression into something like a syntax tree?
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
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?
the module is advanced software engineering
this is just one of the module
its just developing a mini ide to draw shapes using commands
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
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.
ive heard of shunting yard algorithm
hi again, will you guys be able to help me with something else?
will you be able to help me with this
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.