Click here to Skip to main content
14,736,834 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
After loading a file of math equations into my program I have to somehow parse the equation and calculate the correct answer.

Here's the file of math problems:

2 + 2
10 + 12
9 + 7
0 + 8
5 - 3
19 - 8
9 * 4
6 * 7
11 * 12
16 / 4
100 / 5
36 / 9

What I have tried:

using System;
using System.I0;

class program
{
static void Main(string[]args)
Console.WriteLine($"Welcome to the math quiz game!");

string[] fileInput = File.ReadAllLines(@"C:\\math_quiz.txt");
Posted
Updated 1-Apr-20 17:17pm
Comments
Maciej Los 31-Mar-20 2:31am
   
How many times you want to ask the same question?
This is the 1. one!
Member questions & answers[^]

First of all choose a language, because C++ and C# are bot the same.
Quote:
How do I parse a math equation using string manipulation?

Both languages are documented a little Google will give you plenty of links to documentation and tutorials. You will learn much more this way and you will find it very useful in future.

If your formulas are not more complex, simply detecting spaces and splitting to 3 strings is enough, in second string you will get the operator.
If formulas can be more complex (2 + 3 * 4 and more), you will need a parser and take into account priority of operators.

By the way, your snipset of code is not C++ neither c#
   
Comments
CPallini 31-Mar-20 2:12am
   
5.
Patrice T 31-Mar-20 5:49am
   
Thank you
Yo've got 2 answers to your latest question here: How to convert file content to numbers in visual studio[^]
   
Comments
CPallini 2-Apr-20 2:12am
   
(On behalf of Member 14789845):
we have the same HW and I am still looking for the solution. lol

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900