Reading a math equation from a text file, and evaluating it?


So I was thinking of some fun projects I could do in C++, and I thought it'd be cool if I could load a text file filled with math equations(on separate lines) and then print the answers in console.

I'm somewhat inexperienced… I've never really did anything more than a proportion calculator haha. I want to step that up to here.

How could I go about doing this? I don't actually know how to do ALL of this in code. The array, splitting the equations apart, and solving them is what I'm confused on. Anyway below is my logic.

1. Create an array with the equations loaded from the text file(directory inputed in console)
2. Evaluate each math problem
3. Output the array

What exactly do you want to be able to evaluate? Just * / + - or more? And if more, what?
Just those for now. I will probably implement proportions once I get my head around the code.
hmm well if I was doing this I would import the equation into a string. I would then check for invalid characters and if it passed that check, continue, otherwise, complain and break from the program.

Example sum: 5 * 3 + 9 - 9 - 8 / 3 * 9 * 2

Then I would work through the array doing multiplication and division:

(5 * 3) + 9 - 9 - (8 / 3 * 9 * 2)

15 + 9 - 9 - 48

Then evaluate +/-


Make sure you store the results in floats or doubles, because division can result in non-integer answers. It would be nice to support brackets () and they would just be evaluated before multiplication and division.

Topic archived. No new replies allowed.