I have begun working on another program. I’m still doing console programs because I don’t know what I would even make with a proper GUI library such as QT. I don’t know, it just seems like it would add another level of mess on top of the mess I have been working with for the last three hours plus the eight-or-so hours I worked yesterday.
So, what have I been working on? Well, it’s another multi-purpose tool. Are you surprised? Basically, it’s supposed to have all manners of functionality, such as being able to calculate a string input by the user:
That’s basically what I spent the last 12 hours I have been on the computer doing: getting this absolute mess to work. Essentially, I have made a function which takes a string input by the user such as the one above. It then splits the terms apart, which would be the expressions segregated by the plus and minus signs, and calculates each term individually. So, it basically works like this:
Terms: 6/3*2, 5*7, 3*3
Simplified terms: 4, 35, 9
Sum: 4+35-9 = 30
In principle, it’s a very simple concept. However, actually getting it to work was a pain in the ass. First of all,
I had to separate I separated the terms using if-else-statements to find where the terms began and ended. Then, I stored the locations of the plus and minus operators in a vector so that I later could store each term in its own substring, derived from the input expression, in a separate array. In addition, the integrity of the operators had to be preserved, so I also made a reference vector to store information about whether the terms were positive or negative such that I could add them up properly later. This also had to be done with the terms themselves; I had each of the factors stored separated in an array and set up reference and coordinate vectors to keep track of where in the expression they were and which operator to use when simplifying the terms. Finally, when the all the terms had been simplified, I would add them together properly by using the operator reference vector created earlier, followed by actually returning the calculated sum. Holy fucking shit. As for converting the substrings to numerical values: I converted them from strings to numbers using std::stringstream.
I realize that this is probably one of the worst ways I could have gone about doing this, but when you consider I haven’t touched programming properly for months, I think it’s safe to say it’s surprisingly incredible how I managed to make it work. It doesn’t have support for parentheses yet, though, and I don’t think I will add that functionality in the near future, seeing how just getting the terms themselves separated properly was such a massive pain in the buttballs. I mean, shit, now you want me to consider a whole new level of expression complexity? No. This is not what I signed up for. You can simplify those parentheses yourself, because I’m not doing that for you. I can see how I could go about doing that, though. I could use recursion and split the input up into separate expressions then finally add them up, so it would go something like this:
Split terms: 5*2, 3-5*6+(5-3/2), 12/2
Check for subterms and split if found: 5*2, 3, 5*6, 5-3/2, 12/2
Continue checking until all subterms are found: 5*2, 3, 5*6, 5, 3/2, 12/2
Add together: 5*2+3-5*6+5-3/2+12/2 = 10+3-30+5-1.5+6 = -7.5
Any subterms basically just go through a recursive call of the function which handles the calculation of all the terms such that all subterms could be summed up and the correct answer be output. I won’t do that now because it was painful enough to get this far, and I’m afraid I might screw something up if I do that, so I’ll get on with programming a different tool. Exactly what I’ll do next is yet to be decided. My greatest fear is that I have to go through yet another mess with vectors. Another layer of complexity would require more reference and coordinate vectors, not to mention actually properly listing the terms in the correct order so that it all adds up correctly.
Aside from what has been mentioned, nothing else has really happened programmingwise. I suppose this is all there is to say. Not that it’s not enough. Well, there’s always the fact that I’ve got math and physics homework to do. Fuck, I should be doing that instead of fiddling around with this mess, shouldn’t I? Fuck it!
You know, I actually thought of a better method of doing this. Maybe I should just check for the operator preceding the parenthesis. If it’s addition, just remove the parentheses surrounding the term. If it’s subtraction, just flip the operators inside the parenthesis and then remove the parentheses. It still sounds awfully complicated, though… Flipping the signs in a negative term… Or not… Maybe? Well, I’m just thinking out loud at this point, so I think I’ll leave it there. I’m never going to get my homework done, am I?