I'm trying to write a program that can do fractional arithmetic. I need to be able to input complex functions to do this. My goal is to be able to input something like this....
Input:
(1/2 + 2/3) - (2/4 * 1/4)
What I want is to try and store each part in its own seperate variable, then calculate each individual fraction, then take those seperate variables and do the arithmetic on the fractions.
For instance, declare 5 variables, one for the numerator, one for the denominator, one for the double value of the fraction, one for storing the operator (i.e +, -, /, *), and then one for storing the end result of the arithmetic.
Example:
int fracTop, fracBot;
double fraction;
char fracOperator;
double total;
I hope this is sufficient information to help figure this out.
wasn't this solved yesterday? bc i distinctly remember pissin with a program like this? You can't let everyone do the work for you. you have to do a lil yourself
I've been working on it this whole time. With my gaming habits though and college football, its hard to finish it. I can store the whole problem into a string but I'd like to take the string, seperate it into parts, and solve each piece. Then store that result into a single variable.
I can't seem to find in my notes or my textbook about parsing a string. Its all heiroglyphs to me. Something about tokens and other nonsense. I just want to know how to take a string and read parts of it and store those parts into seperate variables. The rest I'll code. By no means do I expect anyone to write my code for me. I just need some help with this small step. As for someone else answering this, it didn't work with the knowledge of C++ I have right now. I'm solely trying to do this based off the level of coding I know right now.
use the substr() function, just make sure you don't go out of bounds when you do. When I say go out of bounds I mean don't try to get more characters than your string holds.
What if I didn't know where in the string a certain character was? Or what if I wanted it to be able to take the string and seperate it in into seperate chars and store it into an array? And then read that array until there was a space or an operator? Any of this making sense?
Well the string is basically an array, so you can perform that functionality without conversion. However if you wanted to you could use a vector and just pass in iterators to create it. I'd recommend against it.
If you want to search the string you can use the std::find() function, or you can iterate through it yourself with loops. It all depends on your goals.
Theres 2 chapters in bjarne stroustrups book "Programming principles and practice using c++" where he goes into great detail writing an almost fully fledged calculator. He introduces concepts like Tokenizing and Token streams and stuff, if you want i can link to the code.