Is there a way to convert strings (given by the user via the console) to integers (more specifically- strings like these: 5 * 4 + 3 or 3 + 6). What is the simplest way to do so? (I am a beginner, so the least conplicated way would help a lot).
Can you post a picture of your code? Also what have you tried so far?
Int.TryParse if memory serves. Do it on each input to the console. If someone enter a letter rather than a number, it shouldn’t fail
Ok, just how do i use this? Just put the string in parentheses?
If you don’t have restrictions on what the user can enter, you’ll need to parse the string looking for values and operators. Then process it. The process can get complex quickly if you allow anything to be inputted and expect a correct answer.
If you restrict it to just a small subset of operands, then you can just iterate over the characters looking for the operands allowed and convert what’s between them to doubles with the double.TryParse (with any failure causing an error message to be printed to the screen).
There is a restriction, it will always be similar to what i used as example in the post, but not always with spaces between the digits
Post your code.
What you are trying to do is solve a mathematical expression, not try to convert a string based integer into an actual integer.
First and foremost, I want to make it clear that this is a complex topic because it makes heavy use of compiler theory, so let’s begin.
First, you are going to need to build a lexer, which is easy to do. The lexical analyzers job is to take human text and turn it into something a computer can better understand and work with. Each integer and operator in your string is a lexeme, which must be extracted. This process is called tokenization, which a parser will make use of. Having a good knowledge on state machines will give you an advantage here but is not always required.
Next, you must be able to parse the tokens and perform error handling if you haven’t already in your lexer. Your parser is going to iterate through every token or process them one by one if the parser requests them from the lexer which is often the most used design choice. The parser will then build an expression tree based on the tokens and their operators. This is where you want to make sure your parser supports order of operations, PEMDAS.
You now have an expression tree, using this tree you can begin traversing it and solving the expression using a simple algorithm.
Sorry if this seems like r/RestOfTheFuckingOwl, but I am here to throw you bones so you know where to begin and what to Google, I build compilers from scratch when I’m bored and can tell you this is one of its many annoyances. I suggest you go to r/ComputerScience for questions like these. If you want more help on Reddit, they, and r/Compilers will help you. I also want to make it known that Microsoft did open source their calculator app that is bundled with Windows 10, it will have most of what you’re looking for. You can find it on GitHub.
So, an algebra expression parser. That’s what you will be googling for