Need help with abstraction and RegEx in c# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Need help with abstraction and RegEx in c#

I am trying to make a program that allows the user to enter an equation: and then it returns the value Sample Input- 4 + 5 - 4 + 3 * 12 Sample Output- 41

31st Oct 2020, 6:16 PM
Gizmo-Lang
Gizmo-Lang - avatar
15 Answers
+ 4
ELITECODE9221 I just revisited your follow up comments and realized you implied some additional details about the user input: <!-- "... I got addition and subbtraction working with regex because I’m checking to see if what the user typed in matches: int\s*(.+)\s*=\s*(.+)" --/> The above quote appears to reflect something different from the details in your Q&A post description. You also stated: <!-- "... and in the second grouping, I call a function to get the end value of the equation they used, and assign the value of it to the name of the int" --/> It sounds like you shifted the original context of your question: from a) ... asking about using Regex to parse an arithmetic string expression as input; apply order of operations to branching logic; and return the appropriate calculation: ex: eval("4 + 5 - 4 + 3 * 12") to b)... asking about name binding a value assignment based on the lexical parsing of a string expression: ex: eval("result = 6") My code focuses only on the first example.
3rd Nov 2020, 9:20 PM
David Carroll
David Carroll - avatar
+ 3
ELITECODE9221 Thanks for clarifying. General Q&A Tip: It helps to provide feedback like this via comments as you just did, or upvote indicating you saw the code, or use the accepted answer option to really close the question. Otherwise, people posting answers will not know if their efforts were helpful or that you even saw the submitted answer. In anycase, your project, as it were, reminded me of a few other similar projects, which you might be interested in checking out at some point. ---- https://code.sololearn.com/ckY1k6jMY1Q8/?ref=app ---- https://code.sololearn.com/c9D1ZBkUyAMx/?ref=app https://www.sololearn.com/post/90398/?ref=app NOTE: You should be able to use the example at the bottom of the Birb Language code as input to run that code. ---- https://code.sololearn.com/ccbp388tJI5Z/?ref=app ---- Good luck with your project.
3rd Nov 2020, 9:43 PM
David Carroll
David Carroll - avatar
+ 2
ELITECODE9221 I'd be happy to guide you. But, tell me do you have an attempt already? Also, what is the reason for the specific requirements?
1st Nov 2020, 9:02 PM
David Carroll
David Carroll - avatar
+ 2
ELITECODE9221 Okay... I see... RegEx would be a solid fit. But I wanted to understand the objective more clearly. JaScript I wouldn't say that using switch is necessarily easier or safer. It's just different. Actually, RegEx could be more efficient than using switch as you could identify all branching groups based on certain matches up front for each order of operation. I'll try to draft up a simple example to review when / if I can find a moment from my work related tasks.
2nd Nov 2020, 4:24 PM
David Carroll
David Carroll - avatar
+ 2
ELITECODE9221 Here's a simple bit of code I slung together to give you some ideas for parsing a simple mathematical string expression of integers. It's not fully tested for every scenario. It's just a simple prototype to answer your question. https://code.sololearn.com/cZob7yBH1Hbr/?ref=app
3rd Nov 2020, 8:08 AM
David Carroll
David Carroll - avatar
+ 1
I think regex will be here not so good idea. Instead try catch exeptions can be used for testing user input. You can see and analyse this code for a first idea how can be seen a solution: https://code.sololearn.com/cEv41IqV7eYc/?ref=app
31st Oct 2020, 9:04 PM
JaScript
JaScript - avatar
+ 1
ELITECODE9221 Hmmm... can you provide more explanation about the use of RegEx for this?
2nd Nov 2020, 12:55 PM
David Carroll
David Carroll - avatar
+ 1
Instead o regex can be used more easier and safer solution with switch statement.
2nd Nov 2020, 3:33 PM
JaScript
JaScript - avatar
+ 1
I am satisfied with what your code was, I quess I was kind of skipping around a bit, but you answered my question and I know what to do now
3rd Nov 2020, 9:23 PM
Gizmo-Lang
Gizmo-Lang - avatar
+ 1
I had already implemented the regex parsing for the declaration statments, I just need help with the order of operations
3rd Nov 2020, 9:24 PM
Gizmo-Lang
Gizmo-Lang - avatar
0
I’m making a programming language, it’s going really well so far, but we are having trouble with implementing the order of operations
2nd Nov 2020, 12:03 PM
Gizmo-Lang
Gizmo-Lang - avatar
0
I don’t really need to use regex, but it might be helpful, I got addition and subbtraction working with regex because I’m checking to see if what the user typed in matches “int\s*(.+)\s=(.+)” and in the second grouping, I call a function to get the end value of the equation they used, and assign the value of it to the name of the int
2nd Nov 2020, 1:22 PM
Gizmo-Lang
Gizmo-Lang - avatar
0
well the regex is actually “int\s*(.+)\s*=\s*(.+)”
2nd Nov 2020, 1:23 PM
Gizmo-Lang
Gizmo-Lang - avatar
0
oh, I forgot
2nd Nov 2020, 3:49 PM
Gizmo-Lang
Gizmo-Lang - avatar
0
needed*
3rd Nov 2020, 9:24 PM
Gizmo-Lang
Gizmo-Lang - avatar